BxDolAIProvider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 $_iId;
  12. protected $_sName;
  13. protected $_sCaption;
  14. protected $_sPrefix;
  15. protected $_aOptions;
  16. public function __construct($aProvider)
  17. {
  18. parent::__construct();
  19. if(empty($aProvider) || !is_array($aProvider) || strcmp($aProvider['name'], $this->_sName) != 0)
  20. $this->_log("Unexpected value provided for the credentials");
  21. $this->_oDb = new BxDolAIQuery();
  22. $this->_iId = (int)$aProvider['id'];
  23. $this->_sName = $aProvider['name'];
  24. $this->_sCaption = _t($aProvider['caption']);
  25. $this->_sPrefix = $aProvider['option_prefix'];
  26. $this->_aOptions = [];
  27. if(!empty($aProvider['options']) && is_array($aProvider['options']))
  28. $this->initOptions($aProvider['options']);
  29. }
  30. /**
  31. * Get provider object instance by provider ID
  32. * @param $sProvider provider name
  33. * @return object instance or false on error
  34. */
  35. public static function getObjectInstance($iId)
  36. {
  37. $sPrefix = 'BxDolAIProvider!';
  38. if(isset($GLOBALS['bxDolClasses'][$sPrefix . $iId]))
  39. return $GLOBALS['bxDolClasses'][$sPrefix . $iId];
  40. $aProvider = BxDolAIQuery::getProviderObject($iId);
  41. if (!$aProvider || !is_array($aProvider))
  42. return false;
  43. $sClass = 'BxDolAIProvider';
  44. if(!empty($aObject['class_name'])) {
  45. $sClass = $aObject['class_name'];
  46. if(!empty($aObject['class_file']))
  47. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['class_file']);
  48. }
  49. $o = new $sClass($aProvider);
  50. return ($GLOBALS['bxDolClasses'][$sPrefix . $sObject] = $o);
  51. }
  52. public function initOptions($aOptions)
  53. {
  54. $this->_aOptions = $aOptions;
  55. }
  56. public function getOption($sName)
  57. {
  58. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  59. $sName = $this->_sPrefix . $sName;
  60. return isset($this->_aOptions[$sName]) ? $this->_aOptions[$sName]['value'] : '';
  61. }
  62. /**
  63. * Internal methods.
  64. */
  65. protected function _call($sRequest, $aParams, $sMethod = 'post-json', $aHeaders = [])
  66. {}
  67. protected function _log($sMessage, $bUseLog = false)
  68. {
  69. if($bUseLog) {
  70. //TODO: Use bx_log here.
  71. }
  72. else
  73. throw new Exception($sMessage);
  74. return false;
  75. }
  76. }