BxDolAI.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. define('BX_DOL_AI_AUTOMATOR_EVENT', 'event');
  10. define('BX_DOL_AI_AUTOMATOR_SCHEDULER', 'scheduler');
  11. define('BX_DOL_AI_AUTOMATOR_WEBHOOK', 'webhook');
  12. define('BX_DOL_AI_AUTOMATOR_STATUS_AUTO', 'auto');
  13. define('BX_DOL_AI_AUTOMATOR_STATUS_MANUAL', 'manual');
  14. define('BX_DOL_AI_AUTOMATOR_STATUS_READY', 'ready');
  15. class BxDolAI extends BxDolFactory implements iBxDolSingleton
  16. {
  17. protected $_oDb;
  18. protected $_iProfileId;
  19. protected $_aExcludeAlertUnits;
  20. protected function __construct()
  21. {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. parent::__construct();
  25. $this->_oDb = new BxDolAIQuery();
  26. $this->_iProfileId = (int)getParam('sys_profile_bot');
  27. $this->_aExcludeAlertUnits = [
  28. 'system', 'module_template_method_call'
  29. ];
  30. }
  31. /**
  32. * Prevent cloning the instance
  33. */
  34. public function __clone()
  35. {
  36. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  37. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  38. }
  39. /**
  40. * Get singleton instance of the class
  41. */
  42. public static function getInstance()
  43. {
  44. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  45. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolAI();
  46. return $GLOBALS['bxDolClasses'][__CLASS__];
  47. }
  48. public function getProfileId()
  49. {
  50. return $this->_iProfileId;
  51. }
  52. public function getDefaultModel()
  53. {
  54. return (int)getParam('sys_agents_model');
  55. }
  56. public function getModels()
  57. {
  58. return $aModel = $this->_oDb->getModelsBy(['sample' => 'all_pairs']);
  59. }
  60. public function getModel($iId)
  61. {
  62. $aModel = $this->_oDb->getModelsBy(['sample' => 'id', 'id' => $iId]);
  63. if(!empty($aModel['params']))
  64. $aModel['params'] = json_decode($aModel['params'], true);
  65. return $aModel;
  66. }
  67. public function getModelObject($iId)
  68. {
  69. if(!$iId)
  70. $iId = $this->getDefaultModel();
  71. if(!$iId)
  72. return false;
  73. return BxDolAIModel::getObjectInstance($iId);
  74. }
  75. public function getProviderObject($iId)
  76. {
  77. if(!$iId)
  78. return false;
  79. return BxDolAIProvider::getObjectInstance($iId);
  80. }
  81. public function getAutomator($iId, $bFullInfo = false)
  82. {
  83. $aAutomator = $this->_oDb->getAutomatorsBy(['sample' => 'id' . ($bFullInfo ? '_full' : ''), 'id' => $iId]);
  84. if(!empty($aAutomator['params']))
  85. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  86. if($bFullInfo && !empty($aAutomator['model_params']))
  87. $aAutomator['model_params'] = json_decode($aAutomator['model_params'], true);
  88. return $aAutomator;
  89. }
  90. public function getAutomatorInstruction($sType, $aParams = [])
  91. {
  92. $mixedResult = '';
  93. switch($sType) {
  94. case 'providers':
  95. $aProviders = $this->_oDb->getProviderBy(['sample' => 'ids', 'ids' => $aParams]);
  96. if(!empty($aProviders) && is_array($aProviders)) {
  97. $mixedResult = "\n avaliable proividers list:";
  98. foreach($aProviders as $aProvider)
  99. $mixedResult .= "\n- " . $aProvider['provider_name'] . ", \$iProviderId=" . $aProvider['id'];
  100. }
  101. break;
  102. }
  103. return $mixedResult;
  104. }
  105. public function getAutomatorsEvent($sUnit, $sAction)
  106. {
  107. if(in_array($sUnit, $this->_aExcludeAlertUnits))
  108. return [];
  109. return $this->_oDb->getAutomatorsBy([
  110. 'sample' => 'events',
  111. 'alert_unit' => $sUnit,
  112. 'alert_action' => $sAction,
  113. 'active' => true
  114. ]);
  115. }
  116. public function getAutomatorsScheduler()
  117. {
  118. $aAutomators = $this->_oDb->getAutomatorsBy(['sample' => 'schedulers', 'active' => true]);
  119. foreach($aAutomators as &$aAutomator)
  120. if(!empty($aAutomator['params']))
  121. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  122. return $aAutomators;
  123. }
  124. public function getAutomatorsWebhook($iProviderId)
  125. {
  126. $aAutomators = $this->_oDb->getAutomatorsBy(['sample' => 'webhooks', 'provider_id' => $iProviderId, 'active' => true]);
  127. foreach($aAutomators as &$aAutomator)
  128. if(!empty($aAutomator['params']))
  129. $aAutomator['params'] = json_decode($aAutomator['params'], true);
  130. return $aAutomators;
  131. }
  132. public function callAutomator($sType, $aParams = [])
  133. {
  134. $sMethod = '_callAutomator' . bx_gen_method_name($sType);
  135. if(!method_exists($this, $sMethod))
  136. return false;
  137. return $this->$sMethod($aParams);
  138. }
  139. protected function _callAutomatorEvent($aParams = [])
  140. {
  141. if(!isset($aParams['automator'], $aParams['alert']) || !is_a($aParams['alert'], 'BxDolAlerts'))
  142. return false;
  143. $oAlert = &$aParams['alert'];
  144. $this->evalCode($aParams['automator'], true, ['alert' => $oAlert]);
  145. }
  146. protected function _callAutomatorScheduler($aParams = [])
  147. {
  148. if(!isset($aParams['automator']))
  149. return false;
  150. $this->evalCode($aParams['automator'], true);
  151. }
  152. protected function _callAutomatorWebhook($aParams = [])
  153. {
  154. if(!isset($aParams['automator']))
  155. return false;
  156. $this->evalCode($aParams['automator'], true);
  157. }
  158. public function evalCode($aAutomator, $isWriteLog = true, $aParams = null)
  159. {
  160. ob_start();
  161. set_error_handler("evalErrorHandler");
  162. try {
  163. $sCode = '';
  164. switch($aAutomator['type']) {
  165. case BX_DOL_AI_AUTOMATOR_EVENT:
  166. $sCode = $aAutomator['code']. '; onAlert($aParams["alert"]->iObject , $aParams["alert"]->iSender , $aParams["alert"]->aExtras);';
  167. break;
  168. case BX_DOL_AI_AUTOMATOR_SCHEDULER:
  169. $sCode = $aAutomator['code'] . '; onCron();';
  170. break;
  171. case BX_DOL_AI_AUTOMATOR_WEBHOOK:
  172. $sCode = $aAutomator['code'] . '; onHook();';
  173. break;
  174. }
  175. eval($sCode);
  176. }
  177. catch (Exception $oException) {
  178. if ($isWriteLog){
  179. $this->log($oException->getFile() . ':' . $oException->getLine() . ' ' . $oException->getMessage());
  180. }
  181. return $oException->getMessage();
  182. }
  183. finally {
  184. $sOutput = ob_get_clean();
  185. if($sOutput != '')
  186. return "Eval error: " . $sOutput;
  187. restore_error_handler();
  188. }
  189. }
  190. public function log($mixedContents, $sSection = '')
  191. {
  192. if(is_array($mixedContents))
  193. $mixedContents = var_export($mixedContents, true);
  194. else if(is_object($mixedContents))
  195. $mixedContents = json_encode($mixedContents);
  196. if(empty($sSection))
  197. $sSection = "Core";
  198. $sTitle .= "\n";
  199. bx_log('sys_agents', ":\n[" . $sSection . "] " . $mixedContents);
  200. }
  201. }
  202. class EvalException extends Exception {}
  203. function evalErrorHandler($errno, $errstr, $errfile, $errline) {
  204. if (!(error_reporting() & $errno)) {
  205. // This error code is not included in error_reporting
  206. return false;
  207. }
  208. throw new EvalException($errstr, $errno);
  209. }