BxDolAIProvider.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 $_sName;
  14. protected $_sCaption;
  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->_sName) != 0)
  21. $this->_log("Unexpected value provided for the credentials");
  22. $this->_oDb = new BxDolAIQuery();
  23. $this->_iId = (int)$aProvider['id'];
  24. $this->_sName = $aProvider['type']['name'];
  25. $this->_sCaption = _t($aProvider['title']);
  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 ID
  33. * @param $sProvider provider name
  34. * @return object instance or false on error
  35. */
  36. public static function getObjectInstance($iId)
  37. {
  38. $sPrefix = 'BxDolAIProvider!';
  39. if(isset($GLOBALS['bxDolClasses'][$sPrefix . $iId]))
  40. return $GLOBALS['bxDolClasses'][$sPrefix . $iId];
  41. $aProvider = BxDolAIQuery::getProviderObject($iId);
  42. if(!$aProvider || !is_array($aProvider))
  43. return false;
  44. $sClass = 'BxDolAIProvider';
  45. if(!empty($aProvider['type']['class_name'])) {
  46. $sClass = $aProvider['type']['class_name'];
  47. if(!empty($aProvider['type']['class_file']))
  48. require_once(BX_DIRECTORY_PATH_ROOT . $aProvider['type']['class_file']);
  49. }
  50. $o = new $sClass($aProvider);
  51. return ($GLOBALS['bxDolClasses'][$sPrefix . $iId] = $o);
  52. }
  53. public function initOptions($aOptions)
  54. {
  55. $this->_aOptions = $aOptions;
  56. }
  57. public function getOption($sName)
  58. {
  59. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  60. $sName = $this->_sPrefix . $sName;
  61. return isset($this->_aOptions[$sName]) ? $this->_aOptions[$sName]['value'] : '';
  62. }
  63. public function processActionWebhook()
  64. {
  65. // Should be overwritten to process webhooks.
  66. }
  67. public function call($sRequest, $aParams, $sMethod = 'post-json', $aHeaders = [])
  68. {
  69. // Should be overwritten to process calls.
  70. }
  71. /**
  72. * Internal methods.
  73. */
  74. protected function _log($sMessage, $bUseLog = false)
  75. {
  76. if($bUseLog) {
  77. //TODO: Use bx_log here.
  78. }
  79. else
  80. throw new Exception($sMessage);
  81. return false;
  82. }
  83. }