BxDolAIProvider.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolAIProvider extends BxDol
  10. {
  11. protected $_oDb;
  12. protected $_iId;
  13. protected $_sProviderName;
  14. protected $_sName;
  15. protected $_sPrefix;
  16. protected $_aOptions;
  17. public function __construct($aProvider)
  18. {
  19. parent::__construct();
  20. if(empty($aProvider) || !is_array($aProvider) || strcmp($aProvider['type']['name'], $this->_sProviderName) != 0)
  21. $this->_log("Unexpected value provided for the credentials");
  22. $this->_oDb = new BxDolAIQuery();
  23. $this->_iId = (int)$aProvider['id'];
  24. $this->_sProviderName = $aProvider['type']['name'];
  25. $this->_sName = _t($aProvider['name']);
  26. $this->_sPrefix = $aProvider['type']['option_prefix'];
  27. $this->_aOptions = [];
  28. if(!empty($aProvider['options']) && is_array($aProvider['options']))
  29. $this->initOptions($aProvider['options']);
  30. }
  31. /**
  32. * Get provider object instance by provider unique name
  33. * @param $sName provider unique name
  34. * @return object instance or false on error
  35. */
  36. public static function getObjectInstanceByName($sName)
  37. {
  38. $iId = BxDolAIQuery::getProviderIdByName($sName);
  39. if(!$iId)
  40. return false;
  41. return self::getObjectInstance($iId);
  42. }
  43. /**
  44. * Get provider object instance by provider ID
  45. * @param $iId provider ID
  46. * @return object instance or false on error
  47. */
  48. public static function getObjectInstance($iId)
  49. {
  50. $sPrefix = 'BxDolAIProvider!';
  51. if(isset($GLOBALS['bxDolClasses'][$sPrefix . $iId]))
  52. return $GLOBALS['bxDolClasses'][$sPrefix . $iId];
  53. $aProvider = BxDolAIQuery::getProviderObject($iId);
  54. if(!$aProvider || !is_array($aProvider))
  55. return false;
  56. $sClass = 'BxDolAIProvider';
  57. if(!empty($aProvider['type']['class_name'])) {
  58. $sClass = $aProvider['type']['class_name'];
  59. if(!empty($aProvider['type']['class_file']))
  60. require_once(BX_DIRECTORY_PATH_ROOT . $aProvider['type']['class_file']);
  61. }
  62. $o = new $sClass($aProvider);
  63. return ($GLOBALS['bxDolClasses'][$sPrefix . $iId] = $o);
  64. }
  65. public function initOptions($aOptions)
  66. {
  67. $this->_aOptions = $aOptions;
  68. }
  69. public function getInfo()
  70. {
  71. $aInfo = $this->_oDb->getProvidersBy(['sample' => 'id', 'id' => $this->_iId]);
  72. $aInfo['options'] = $this->getOptions();
  73. return $aInfo;
  74. }
  75. public function getOptions()
  76. {
  77. $aOptions = $this->_oDb->getProvidersBy(['sample' => 'options_by_id', 'id' => $this->_iId]);
  78. foreach($aOptions as &$aOption)
  79. $aOption['value'] = $this->getOption($aOption['name']);
  80. return $aOptions;
  81. }
  82. public function getOption($sName)
  83. {
  84. $sCustomMethod = 'getOption' . bx_gen_method_name(str_replace($this->_sPrefix, '', $sName));
  85. if(method_exists($this, $sCustomMethod))
  86. return $this->$sCustomMethod();
  87. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  88. $sName = $this->_sPrefix . $sName;
  89. return isset($this->_aOptions[$sName]) ? $this->_aOptions[$sName]['value'] : '';
  90. }
  91. public function processActionWebhook()
  92. {
  93. // Should be overwritten to process webhooks.
  94. }
  95. public function call($sRequest, $aParams, $sMethod = 'post-json', $aHeaders = [])
  96. {
  97. // Should be overwritten to process calls.
  98. }
  99. /**
  100. * Internal methods.
  101. */
  102. protected function _log($mixedError, $bUseLog = true)
  103. {
  104. if(!$bUseLog) {
  105. $sMessage = 'Error occurred';
  106. if(is_string($mixedError))
  107. $sMessage = $mixedError;
  108. else if(is_array($mixedError) && isset($mixedError['message']))
  109. $sMessage = $mixedError['message'];
  110. throw new Exception($sMessage);
  111. }
  112. else
  113. BxDolAI::getInstance()->log($mixedError, 'Providers');
  114. return false;
  115. }
  116. }