api.senddog.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /**
  3. * SMS/Telegram/Email messages sending implementation
  4. */
  5. class SendDog {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains senddog config
  14. *
  15. * @var array
  16. */
  17. protected $settings = array();
  18. /**
  19. * System message helper object placeholder
  20. *
  21. * @var object
  22. */
  23. protected $messages = '';
  24. /**
  25. * System SMS queue object placeholder
  26. *
  27. * @var object
  28. */
  29. protected $smsQueue = '';
  30. /**
  31. * Placeholder for UbillingConfig object
  32. *
  33. * @var null
  34. */
  35. protected $ubConfig = null;
  36. /**
  37. * Contains array of SMS services that will be loaded as serviceId=>serviceParams
  38. *
  39. * @var array
  40. */
  41. protected $servicesEnabled = array();
  42. /**
  43. * Active services objects array
  44. *
  45. * @var array
  46. */
  47. protected $activeServices = array();
  48. /**
  49. * contains default interface module URL
  50. */
  51. const URL_ME = '?module=senddog';
  52. /**
  53. * Path to running dog flag
  54. */
  55. const PID_PATH = 'exports/senddogrunning.pid';
  56. /**
  57. * Classic-SendDog SMS services libs path
  58. */
  59. const SERVICES_LIB_PATH = 'api/vendor/senddog_classic_services/';
  60. public function __construct() {
  61. global $ubillingConfig;
  62. $this->ubConfig = $ubillingConfig;
  63. $this->loadAltCfg();
  64. $this->setOptions();
  65. $this->loadBaseConfig();
  66. $this->preloadSmsServicesLibs();
  67. $this->initSmsQueue();
  68. $this->initMessages();
  69. $this->loadServicesConfigs();
  70. $this->loadTelegramConfig();
  71. }
  72. /**
  73. * Loads system alter config into protected property for further usage
  74. *
  75. * @return void
  76. */
  77. protected function loadAltCfg() {
  78. $this->altCfg = $this->ubConfig->getAlter();
  79. }
  80. /**
  81. * Loads required options and
  82. *
  83. * @return void
  84. */
  85. protected function setOptions() {
  86. $servicesConfigPath = CONFIG_PATH . 'senddog.d/';
  87. $allServicesConf = rcms_scandir($servicesConfigPath, '*.ini');
  88. if (!empty($allServicesConf)) {
  89. foreach ($allServicesConf as $io => $eachConfig) {
  90. $serviceConfig = rcms_parse_ini_file($servicesConfigPath . $eachConfig, true);
  91. if (!empty($serviceConfig)) {
  92. $this->servicesEnabled += $serviceConfig;
  93. }
  94. }
  95. }
  96. if (isset($this->altCfg['SENDDOG_SMS_SERVICES_ENABLED'])) {
  97. if (!empty($this->altCfg['SENDDOG_SMS_SERVICES_ENABLED'])) {
  98. $servicesEnabledOnly = explode(',', $this->altCfg['SENDDOG_SMS_SERVICES_ENABLED']);
  99. if (!empty($servicesEnabledOnly)) {
  100. $servicesEnabledOnly = array_flip($servicesEnabledOnly);
  101. foreach ($this->servicesEnabled as $serviceId => $serviceParams) {
  102. if (!isset($servicesEnabledOnly[$serviceId])) {
  103. unset($this->servicesEnabled[$serviceId]);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * Preloads all enabled SMS services libs and creates separate instances of each
  112. *
  113. * @return void
  114. */
  115. protected function preloadSmsServicesLibs() {
  116. if (!empty($this->servicesEnabled)) {
  117. foreach ($this->servicesEnabled as $serviceId => $serviceParams) {
  118. require_once (self::SERVICES_LIB_PATH . $serviceId . '.php');
  119. $serviceClassName = $serviceId;
  120. $this->activeServices[$serviceId] = new $serviceClassName($this->settings);
  121. }
  122. }
  123. }
  124. /**
  125. * Loads enabled SMS services data
  126. *
  127. * @return void
  128. */
  129. protected function loadServicesConfigs() {
  130. if (!empty($this->servicesEnabled)) {
  131. foreach ($this->servicesEnabled as $serviceId => $serviceParams) {
  132. if (isset($serviceParams['CONFIG'])) {
  133. if (!empty($serviceParams['CONFIG'])) {
  134. $loadConfigMethodName = $serviceParams['CONFIG'];
  135. $this->activeServices[$serviceId]->$loadConfigMethodName();
  136. }
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * Inits system SMS queue object
  143. *
  144. * @return void
  145. */
  146. protected function initSmsQueue() {
  147. $this->smsQueue = new UbillingSMS();
  148. }
  149. /**
  150. * Inits message helper object for further usage
  151. *
  152. * @return void
  153. */
  154. protected function initMessages() {
  155. $this->messages = new UbillingMessageHelper();
  156. }
  157. /**
  158. * Loads basic send dog settings
  159. *
  160. * @return void
  161. */
  162. protected function loadBaseConfig() {
  163. $defaultSMSservice = zb_StorageGet('SENDDOG_SMS_SERVICE');
  164. if (empty($defaultSMSservice)) {
  165. $defaultSMSservice = 'tsms';
  166. zb_StorageSet('SENDDOG_SMS_SERVICE', $defaultSMSservice);
  167. }
  168. $this->settings['SMS_SERVICE'] = $defaultSMSservice;
  169. }
  170. /**
  171. * Returns base module URL
  172. *
  173. * @return string
  174. */
  175. public function getBaseUrl() {
  176. return (self::URL_ME);
  177. }
  178. /**
  179. * Dirty input data filtering
  180. *
  181. * @param $string - string to filter
  182. *
  183. * @return string
  184. */
  185. protected function safeEscapeString($string) {
  186. @$result = preg_replace("#[~@\?\%\/\;=\*\>\<\"\']#Uis", '', $string);
  187. return ($result);
  188. }
  189. /**
  190. * Loads telegram config
  191. *
  192. * @return void
  193. */
  194. protected function loadTelegramConfig() {
  195. $telegramBotToken = zb_StorageGet('SENDDOG_TELEGRAM_BOTTOKEN');
  196. if (empty($telegramBotToken)) {
  197. $telegramBotToken = 'input_token_here';
  198. zb_StorageSet('SENDDOG_TELEGRAM_BOTTOKEN', $telegramBotToken);
  199. }
  200. $this->settings['TELEGRAM_BOTTOKEN'] = $telegramBotToken;
  201. }
  202. /**
  203. * Renders current telegram bot contacts
  204. *
  205. * @return string
  206. */
  207. public function renderTelegramContacts() {
  208. $result = '';
  209. $allEmployeeChatIds = array();
  210. $telegram = new UbillingTelegram();
  211. $telegram->setToken($this->settings['TELEGRAM_BOTTOKEN']);
  212. $rawContacts = $telegram->getBotContacts();
  213. $allEmployeeData = ts_GetAllEmployeeData();
  214. if (!empty($allEmployeeData)) {
  215. foreach ($allEmployeeData as $io => $each) {
  216. if (!empty($each['telegram'])) {
  217. if (!empty($each['admlogin'])) {
  218. $empNameLabel = $each['name'] . ' (' . $each['admlogin'] . ')';
  219. } else {
  220. $empNameLabel = $each['name'];
  221. }
  222. $allEmployeeChatIds[$each['telegram']] = $empNameLabel;
  223. }
  224. }
  225. }
  226. $result .= wf_BackLink(self::URL_ME, '', true);
  227. if (!empty($rawContacts)) {
  228. $cells = wf_TableCell('');
  229. $cells .= wf_TableCell(__('Chat ID'));
  230. $cells .= wf_TableCell(__('Type'));
  231. $cells .= wf_TableCell(__('Worker'));
  232. $cells .= wf_TableCell(__('Username'));
  233. $cells .= wf_TableCell(__('Name'));
  234. $cells .= wf_TableCell(__('Message'));
  235. $rows = wf_TableRow($cells, 'row1');
  236. foreach ($rawContacts as $io => $each) {
  237. $cells = wf_TableCell($this->newContact($each['lastmessage']));
  238. $cells .= wf_TableCell($each['chatid']);
  239. $chatType=__($each['type']);
  240. $cells .= wf_TableCell($chatType);
  241. $employeeName = (isset($allEmployeeChatIds[$each['chatid']])) ? $allEmployeeChatIds[$each['chatid']] : '';
  242. $cells .= wf_TableCell($employeeName);
  243. $userNameLabel = (!empty($each['name'])) ? wf_Link('https://t.me/'.$each['name'], $each['name']) : '';
  244. $cells .= wf_TableCell($userNameLabel);
  245. $cells .= wf_TableCell($each['first_name'] . ' ' . $each['last_name']);
  246. $cells .= wf_TableCell($each['lastmessage']);
  247. $rows .= wf_TableRow($cells, 'row5');
  248. }
  249. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  250. } else {
  251. $result .= $this->messages->getStyledMessage(__('Nothing found'), 'warning');
  252. }
  253. return ($result);
  254. }
  255. /**
  256. * Returns new contact marker
  257. *
  258. * @param string $message
  259. *
  260. * @return string
  261. */
  262. protected function newContact($message) {
  263. $result = '';
  264. $markers = array('go', 'start', 'хуй', 'huy'); //default new contact markers array
  265. if (!empty($markers)) {
  266. foreach ($markers as $io => $eachMarker) {
  267. if (ispos($message, $eachMarker)) {
  268. $result = wf_img_sized('skins/icon_telegram_small.png', '', '10');
  269. }
  270. }
  271. }
  272. return($result);
  273. }
  274. /**
  275. * Returns set of inputs, required for SMS-Fly service configuration
  276. *
  277. * @return string
  278. */
  279. protected function renderTelegramConfigInputs() {
  280. $inputs = wf_tag('h2') . __('Telegram') . ' ' . wf_Link(self::URL_ME . '&showmisc=telegramcontacts', wf_img_sized('skins/icon_search_small.gif', __('Telegram bot contacts'), '10', '10'), true) . wf_tag('h2', true);
  281. $inputs .= wf_TextInput('edittelegrambottoken', __('Telegram bot token'), $this->settings['TELEGRAM_BOTTOKEN'], true, 55);
  282. return ($inputs);
  283. }
  284. /**
  285. * Renders SendDog config interface
  286. *
  287. * @return string
  288. */
  289. public function renderConfigForm() {
  290. $result = '';
  291. $inputs = '';
  292. if (!empty($this->servicesEnabled)) {
  293. foreach ($this->servicesEnabled as $serviceId => $serviceParams) {
  294. if (isset($serviceParams['INTERFACE'])) {
  295. if (!empty($serviceParams['INTERFACE'])) {
  296. $configInterfaceMethodName = $serviceParams['INTERFACE'];
  297. $inputs .= $this->activeServices[$serviceId]->$configInterfaceMethodName();
  298. }
  299. }
  300. }
  301. }
  302. $inputs .= $this->renderTelegramConfigInputs();
  303. $inputs .= wf_Submit(__('Save'));
  304. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  305. return ($result);
  306. }
  307. /**
  308. * Saves config in database
  309. *
  310. * @return void
  311. */
  312. public function saveConfig() {
  313. if (!empty($this->servicesEnabled)) {
  314. foreach ($this->servicesEnabled as $serviceId => $serviceParams) {
  315. if (isset($serviceParams['SAVE'])) {
  316. if (!empty($serviceParams['SAVE'])) {
  317. $saveSettingsMethodName = $serviceParams['SAVE'];
  318. $this->activeServices[$serviceId]->$saveSettingsMethodName();
  319. }
  320. }
  321. }
  322. }
  323. //telegram bot token configuration
  324. if (ubRouting::post('edittelegrambottoken') != $this->settings['TELEGRAM_BOTTOKEN']) {
  325. zb_StorageSet('SENDDOG_TELEGRAM_BOTTOKEN', ubRouting::post('edittelegrambottoken'));
  326. log_register('SENDDOG CONFIG SET TELEGRAMBOTTOKEN');
  327. }
  328. //default sms service
  329. if (ubRouting::post('defaultsmsservice') != $this->settings['SMS_SERVICE']) {
  330. zb_StorageSet('SENDDOG_SMS_SERVICE', ubRouting::post('defaultsmsservice'));
  331. log_register('SENDDOG CONFIG SET SMSSERVICE `' . ubRouting::post('defaultsmsservice') . '`');
  332. }
  333. }
  334. /**
  335. * Loads and sends all email messages from system queue
  336. *
  337. * @return int
  338. */
  339. public function emailProcessing() {
  340. $email = new UbillingMail();
  341. $messagesCount = $email->getQueueCount();
  342. if ($messagesCount > 0) {
  343. $allMessagesData = $email->getQueueData();
  344. if (!empty($allMessagesData)) {
  345. foreach ($allMessagesData as $io => $eachmessage) {
  346. $email->directPushEmail($eachmessage['email'], $eachmessage['subj'], $eachmessage['message']);
  347. $email->deleteEmail($eachmessage['filename']);
  348. }
  349. }
  350. }
  351. return ($messagesCount);
  352. }
  353. /**
  354. * Loads and sends all stored SMS from system queue
  355. *
  356. * @return int
  357. */
  358. public function smsProcessing() {
  359. $smsCount = $this->smsQueue->getQueueCount();
  360. if ($smsCount > 0) {
  361. $smsServiceId = $this->settings['SMS_SERVICE'];
  362. if (isset($this->servicesEnabled[$smsServiceId])) {
  363. $messagePushMethodName = $this->servicesEnabled[$smsServiceId]['PUSH'];
  364. $this->activeServices[$smsServiceId]->$messagePushMethodName();
  365. }
  366. }
  367. return ($smsCount);
  368. }
  369. /**
  370. * Goes through sms_history table and checks statuses for messages
  371. *
  372. * @return void
  373. */
  374. public function smsHistoryProcessing() {
  375. $defaultServiceId = $this->settings['SMS_SERVICE'];
  376. if (isset($this->servicesEnabled[$defaultServiceId])) {
  377. if (isset($this->servicesEnabled[$defaultServiceId]['HISTORY'])) {
  378. if (!empty($this->servicesEnabled[$defaultServiceId]['HISTORY'])) {
  379. $historyMethodName = $this->servicesEnabled[$defaultServiceId]['HISTORY'];
  380. $this->activeServices[$defaultServiceId]->$historyMethodName();
  381. }
  382. }
  383. }
  384. }
  385. /**
  386. * Loads and sends all stored Telegram messages from system queue
  387. *
  388. * @return int
  389. */
  390. public function telegramProcessing() {
  391. $telegram = new UbillingTelegram($this->settings['TELEGRAM_BOTTOKEN']);
  392. $messagesCount = $telegram->getQueueCount();
  393. if ($messagesCount > 0) {
  394. $allMessagesData = $telegram->getQueueData();
  395. if (!empty($allMessagesData)) {
  396. foreach ($allMessagesData as $io => $eachmessage) {
  397. $telegram->directPushMessage($eachmessage['chatid'], $eachmessage['message']);
  398. $telegram->deleteMessage($eachmessage['filename']);
  399. }
  400. }
  401. }
  402. return ($messagesCount);
  403. }
  404. /**
  405. * Cuts international codes like "+38", "+7" from phone number
  406. * This function might be supplemented with new country codes and refactored
  407. *
  408. * @param $PhoneNumber
  409. *
  410. * @return bool|mixed|string
  411. */
  412. public static function cutInternationalsFromPhoneNum($PhoneNumber) {
  413. // if we have users phones in DB like "0991234567" and some function/module
  414. // appended "+38" or "+7" to the beginning of it and if we need to remove that prefix
  415. // for MYSQL "LIKE" to search properly
  416. $PhoneNumber = str_replace(array('+7', '+38', '+'), '', $PhoneNumber);
  417. // sometimes phone number may be stored without leading "+"
  418. // and we still need to remove international codes
  419. $Prefix = '38';
  420. if (substr($PhoneNumber, 0, strlen($Prefix)) == $Prefix) {
  421. $PhoneNumber = substr($PhoneNumber, strlen($Prefix));
  422. }
  423. $Prefix = '7';
  424. if (substr($PhoneNumber, 0, strlen($Prefix)) == $Prefix) {
  425. $PhoneNumber = substr($PhoneNumber, strlen($Prefix));
  426. }
  427. return $PhoneNumber;
  428. }
  429. /**
  430. * Renders service balance or another misc info by its serviceId
  431. *
  432. * @param string $serviceId
  433. *
  434. * @return void
  435. */
  436. public function renderBalanceInfo($serviceId) {
  437. if (isset($this->servicesEnabled[$serviceId])) {
  438. if (isset($this->servicesEnabled[$serviceId]['BALANCE'])) {
  439. if (!empty($this->servicesEnabled[$serviceId]['BALANCE'])) {
  440. $balanceMethodName = $this->servicesEnabled[$serviceId]['BALANCE'];
  441. $serviceName = (isset($this->servicesEnabled[$serviceId]['NAME'])) ? $this->servicesEnabled[$serviceId]['NAME'] : $serviceId;
  442. show_window(__($serviceName) . ' ' . __('Balance'), $this->activeServices[$serviceId]->$balanceMethodName());
  443. }
  444. }
  445. }
  446. }
  447. }