skyriver.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. class skyriver extends SendDogProto {
  3. /**
  4. * Loads Skyriver service config
  5. *
  6. * @return void
  7. */
  8. public function loadSkyriverConfig() {
  9. $smsgateway = zb_StorageGet('SENDDOG_SKYSMS_GATEWAY');
  10. if (empty($smsgateway)) {
  11. $smsgateway = 'http://sms.skysms.net/api/bulk_sm';
  12. zb_StorageSet('SENDDOG_SKYSMS_GATEWAY', $smsgateway);
  13. }
  14. $smslogin = zb_StorageGet('SENDDOG_SKYSMS_LOGIN');
  15. if (empty($smslogin)) {
  16. $smslogin = 'InfoCentr';
  17. zb_StorageSet('SENDDOG_SKYSMS_LOGIN', $smslogin);
  18. }
  19. $smspassword = zb_StorageGet('SENDDOG_SKYSMS_PASSWORD');
  20. if (empty($smspassword)) {
  21. $smspassword = 'MySecretPassword';
  22. zb_StorageSet('SENDDOG_SKYSMS_PASSWORD', $smspassword);
  23. }
  24. $this->settings['SKYSMS_GATEWAY'] = $smsgateway;
  25. $this->settings['SKYSMS_LOGIN'] = $smslogin;
  26. $this->settings['SKYSMS_PASSWORD'] = $smspassword;
  27. }
  28. /**
  29. * Returns set of inputs, required for Skyriver service configuration
  30. *
  31. * @return string
  32. */
  33. public function renderSkyriverConfigInputs() {
  34. $inputs = wf_tag('h2') . 'Skyriver' . wf_tag('h2', true);
  35. $inputs .= wf_TextInput('editskysmsgateway', __('Skyriver API address'), $this->settings['SKYSMS_GATEWAY'], true, 30);
  36. $inputs .= wf_TextInput('editskysmslogin', __('User login to access Skyriver API (this is sign also)'), $this->settings['SKYSMS_LOGIN'], true, 20);
  37. $inputs .= wf_TextInput('editskysmspassword', __('User password for access Skyriver API'), $this->settings['SKYSMS_PASSWORD'], true, 20);
  38. $smsServiceFlag = ($this->settings['SMS_SERVICE'] == 'skyriver') ? true : false;
  39. $inputs .= wf_RadioInput('defaultsmsservice', __('Use Skyriver as default SMS service'), 'skyriver', true, $smsServiceFlag);
  40. return ($inputs);
  41. }
  42. /**
  43. * Saves service settings to database
  44. *
  45. * @return void
  46. */
  47. public function saveSettings() {
  48. //Skyriver configuration
  49. if ($_POST['editskysmsgateway'] != $this->settings['SKYSMS_GATEWAY']) {
  50. zb_StorageSet('SENDDOG_SKYSMS_GATEWAY', $_POST['editskysmsgateway']);
  51. log_register('SENDDOG CONFIG SET SKYSMSGATEWAY `' . $_POST['editskysmsgateway'] . '`');
  52. }
  53. if ($_POST['editskysmslogin'] != $this->settings['SKYSMS_LOGIN']) {
  54. zb_StorageSet('SENDDOG_SKYSMS_LOGIN', $_POST['editskysmslogin']);
  55. log_register('SENDDOG CONFIG SET SKYSMSLOGIN `' . $_POST['editskysmslogin'] . '`');
  56. }
  57. if ($_POST['editskysmspassword'] != $this->settings['SKYSMS_PASSWORD']) {
  58. zb_StorageSet('SENDDOG_SKYSMS_PASSWORD', $_POST['editskysmspassword']);
  59. log_register('SENDDOG CONFIG SET SKYSMSPASSWORD `' . $_POST['editskysmspassword'] . '`');
  60. }
  61. }
  62. /**
  63. * Sends all sms storage via SKYSMS service
  64. *
  65. * @return void
  66. */
  67. public function skysmsPushMessages() {
  68. $result = '';
  69. $skySmsApiUrl = $this->settings['SKYSMS_GATEWAY'];
  70. $skySmsApiLogin = $this->settings['SKYSMS_LOGIN'];
  71. $skySsmApiPassw = $this->settings['SKYSMS_PASSWORD'];
  72. $allSmsQueue = $this->smsQueue->getQueueData();
  73. if (!empty($allSmsQueue)) {
  74. $i = 0;
  75. $smsHistoryEnabled = $this->ubConfig->getAlterParam('SMS_HISTORY_ON');
  76. $smsHistoryTabFreshIds = array();
  77. $preSendStatus = __('Perparing for delivery');
  78. $telepatia = new Telepathy(false);
  79. if ($smsHistoryEnabled) {
  80. $telepatia->flushPhoneTelepathyCache();
  81. $telepatia->usePhones();
  82. }
  83. $xmlPacket = '<?xml version="1.0" encoding="utf-8"?>
  84. <packet version="1.0">
  85. <auth login="' . $skySmsApiLogin . '" password="' . $skySsmApiPassw . '"/>
  86. <command name="sendmessage">
  87. <message id="0" type="sms">
  88. <data charset="lat"></data>
  89. <recipients>
  90. ';
  91. foreach ($allSmsQueue as $io => $eachsms) {
  92. if ($smsHistoryEnabled) {
  93. $phoneToSearch = $this->cutInternationalsFromPhoneNum($eachsms['number']);
  94. $login = $telepatia->getByPhoneFast($phoneToSearch);
  95. $query = "INSERT INTO `sms_history` (`login`, `phone`, `send_status`, `msg_text`)
  96. VALUES ('" . $login . "', '" . $eachsms['number'] . "', '" . $preSendStatus . "', '" . $eachsms['message'] . "');";
  97. nr_query($query);
  98. $recId = simple_get_lastid('sms_history');
  99. $smsHistoryTabFreshIds[] = $recId;
  100. $xmlPacket .= '<recipient id="' . $recId . '" address="' . $eachsms['number'] . '">' . $eachsms['message'] . '</recipient>';
  101. } else {
  102. $xmlPacket .= '<recipient id="' . ++$i . '" address="' . $eachsms['number'] . '">' . $eachsms['message'] . '</recipient>';
  103. }
  104. $this->smsQueue->deleteSms($eachsms['filename']);
  105. }
  106. $telepatia->savePhoneTelepathyCache();
  107. $xmlPacket .= '</recipients>
  108. </message>
  109. </command>
  110. </packet>
  111. ';
  112. $curl = curl_init();
  113. curl_setopt($curl, CURLOPT_POST, true);
  114. curl_setopt($curl, CURLOPT_URL, $skySmsApiUrl);
  115. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Accept: text/xml", "Cache-Control: no-cache"));
  116. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  117. curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlPacket);
  118. $result = curl_exec($curl);
  119. curl_close($curl);
  120. $parsedResult = zb_xml2array($result);
  121. if (!empty($parsedResult)) {
  122. $serverAnswerCode = (isset($parsedResult['packet']['result_attr']['type'])) ? $parsedResult['packet']['result_attr']['type'] : '42';
  123. if ($serverAnswerCode == '00') {
  124. $smsPacketID = $parsedResult['packet']['result']['message_attr']['smsmsgid'];
  125. log_register('SENDDOG SKYSMS packet ' . $smsPacketID . ' sent successfully');
  126. if ($smsHistoryEnabled) {
  127. $recipients = $parsedResult['packet']['result']['message']['recipients']['recipient'];
  128. if (empty($recipients)) {
  129. $recipients = $parsedResult['packet']['result']['message']['recipients'];
  130. }
  131. foreach ($recipients as $each => $Recipient) {
  132. if (isset($Recipient['id'])) {
  133. $query = "UPDATE `sms_history` SET `srvmsgself_id` = '" . $Recipient['smsid'] . "',
  134. `srvmsgpack_id` = '" . $smsPacketID . "',
  135. `date_send` = '" . curdatetime() . "',
  136. `send_status` = '" . __('Message queued') . "'
  137. WHERE `id` = '" . $Recipient['id'] . "';";
  138. nr_query($query);
  139. }
  140. }
  141. }
  142. } else {
  143. $serverErrorMsg = $this->decodeSkySmsErrorMessage($serverAnswerCode);
  144. log_register('SENDDOG SKYSMS failed to sent SMS packet. Server answer: ' . $serverErrorMsg . ( ($serverAnswerCode == '42') ? $result : ''));
  145. if ($smsHistoryEnabled) {
  146. $idsAsStr = implode(',', $smsHistoryTabFreshIds);
  147. $query = "UPDATE `sms_history` SET `date_send` = '" . curdatetime() . "',
  148. `date_statuschk` = '" . curdatetime() . "',
  149. `no_statuschk` = '1',
  150. `send_status` = '" . __('Failed to send message') . ': ' . $serverErrorMsg . "'
  151. WHERE `id` IN (" . $idsAsStr . ");";
  152. nr_query($query);
  153. }
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * Checks messages status for SKYSMS service
  160. *
  161. * @return void
  162. */
  163. public function skysmsCheckMessagesStatus() {
  164. $smsCheckStatusExpireDays = $this->altCfg['SMS_CHECKSTATUS_EXPIRE_DAYS'];
  165. $query = "UPDATE `sms_history` SET `no_statuschk` = 1,
  166. `send_status` = '" . __('SMS status check period expired') . "'
  167. WHERE ABS( DATEDIFF(NOW(), `date_send`) ) > " . $smsCheckStatusExpireDays . " AND no_statuschk < 1;";
  168. nr_query($query);
  169. $query = "SELECT DISTINCT `srvmsgpack_id` FROM `sms_history` WHERE `no_statuschk` < 1 AND `delivered` < 1;";
  170. $chkMessages = simple_queryall($query);
  171. if (!empty($chkMessages)) {
  172. $skySmsApiUrl = $this->settings['SKYSMS_GATEWAY'];
  173. $skySmsApiLogin = $this->settings['SKYSMS_LOGIN'];
  174. $skySmsApiPassw = $this->settings['SKYSMS_PASSWORD'];
  175. foreach ($chkMessages as $io => $eachmessage) {
  176. $smsPacketID = $eachmessage['srvmsgpack_id'];
  177. if (empty($smsPacketID)) {
  178. continue;
  179. }
  180. $xmlPacket = '<?xml version="1.0" encoding="utf-8"?>
  181. <packet version="1.0">
  182. <auth login="' . $skySmsApiLogin . '" password="' . $skySmsApiPassw . '"/>
  183. <command name="querymessage">
  184. <message smsmsgid="' . $smsPacketID . '"/>
  185. </command>
  186. </packet>
  187. ';
  188. $curl = curl_init();
  189. curl_setopt($curl, CURLOPT_POST, true);
  190. curl_setopt($curl, CURLOPT_URL, $skySmsApiUrl);
  191. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Accept: text/xml", "Cache-Control: no-cache"));
  192. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  193. curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlPacket);
  194. $result = curl_exec($curl);
  195. curl_close($curl);
  196. $parsedResult = zb_xml2array($result);
  197. if (!empty($parsedResult)) {
  198. $serverAnswerCode = (isset($parsedResult['packet']['result_attr']['type'])) ? $parsedResult['packet']['result_attr']['type'] : '42';
  199. if ($serverAnswerCode == '00') {
  200. $recipients = $parsedResult['packet']['result']['message']['recipients']['recipient'];
  201. if (empty($recipients)) {
  202. $recipients = $parsedResult['packet']['result']['message']['recipients'];
  203. }
  204. foreach ($recipients as $each => $recipient) {
  205. if (isset($recipient['smsid'])) {
  206. $messageId = $recipient['smsid'];
  207. $messageStatus = $recipient['status'];
  208. $decodedMessageStatus = $this->decodeSkySmsStatusMessage($messageStatus);
  209. $query = "UPDATE `sms_history` SET `date_statuschk` = '" . curdatetime() . "',
  210. `delivered` = '" . $decodedMessageStatus['DeliveredStatus'] . "',
  211. `no_statuschk` = '" . $decodedMessageStatus['NoStatusCheck'] . "',
  212. `send_status` = '" . $decodedMessageStatus['StatusMsg'] . "'
  213. WHERE `srvmsgself_id` = '" . $messageId . "';";
  214. nr_query($query);
  215. }
  216. }
  217. log_register('SENDDOG SKYSMS checked SMS packet ' . $smsPacketID . ' send status');
  218. } else {
  219. $serverErrorMsg = $this->decodeSkySmsErrorMessage($serverAnswerCode);
  220. log_register('SENDDOG SKYSMS failed to get SMS packet ' . $smsPacketID . ' send status. Server answer: ' . $serverErrorMsg . ( ($serverAnswerCode == '42') ? $result : ''));
  221. }
  222. }
  223. }
  224. }
  225. }
  226. /**
  227. * Gets the error message code as a parameter and returns appropriate message string
  228. *
  229. * @param string $errorMsgCode
  230. * @return string
  231. */
  232. public function decodeSkySmsErrorMessage($errorMsgCode) {
  233. switch ($errorMsgCode) {
  234. case '01':
  235. $message = __('Incorrect parameters value or insufficient parameters count');
  236. break;
  237. case '02':
  238. $message = __('Database server connection error');
  239. break;
  240. case '03':
  241. $message = __('Database was not found');
  242. break;
  243. case '04':
  244. $message = __('Authorization procedure error');
  245. break;
  246. case '05':
  247. $message = __('Login or password is incorrect');
  248. break;
  249. case '06':
  250. $message = __('Malfunction in user\'s configuration');
  251. break;
  252. default:
  253. $message = __('Error code is unknown. Servers answer:') . ' ' . $errorMsgCode;
  254. }
  255. return $message;
  256. }
  257. /**
  258. * Gets the status message code as a parameter and returns appropriate message string
  259. *
  260. * @param string $statusMsgCode
  261. * @return array
  262. */
  263. public function decodeSkySmsStatusMessage($statusMsgCode) {
  264. $statusArray = array('StatusMsg' => '', 'DeliveredStatus' => 0, 'NoStatusCheck' => 0);
  265. switch ($statusMsgCode) {
  266. case 'DELIVERED':
  267. $statusArray['StatusMsg'] = __('Message is delivered to recipient');
  268. $statusArray['DeliveredStatus'] = 1;
  269. $statusArray['NoStatusCheck'] = 0;
  270. break;
  271. case 'TOSEND':
  272. $statusArray['StatusMsg'] = __('Message is queued for delivering');
  273. $statusArray['DeliveredStatus'] = 0;
  274. $statusArray['NoStatusCheck'] = 0;
  275. break;
  276. case 'ENROUTE':
  277. $statusArray['StatusMsg'] = __('Message is sent but not yet delivered to recipient');
  278. $statusArray['DeliveredStatus'] = 0;
  279. $statusArray['NoStatusCheck'] = 0;
  280. break;
  281. case 'PAUSED':
  282. $statusArray['StatusMsg'] = __('Message delivering is paused');
  283. $statusArray['DeliveredStatus'] = 0;
  284. $statusArray['NoStatusCheck'] = 0;
  285. break;
  286. case 'CANCELED':
  287. $statusArray['StatusMsg'] = __('Message delivering is canceled');
  288. $statusArray['DeliveredStatus'] = 0;
  289. $statusArray['NoStatusCheck'] = 1;
  290. break;
  291. case 'FAILED':
  292. $statusArray['StatusMsg'] = __('Failed to send message');
  293. $statusArray['DeliveredStatus'] = 0;
  294. $statusArray['NoStatusCheck'] = 1;
  295. break;
  296. case 'EXPIRED':
  297. $statusArray['StatusMsg'] = __('Failed to deliver message - delivery term is expired');
  298. $statusArray['DeliveredStatus'] = 0;
  299. $statusArray['NoStatusCheck'] = 1;
  300. break;
  301. case 'UNDELIVERABLE':
  302. $statusArray['StatusMsg'] = __('Message can not be delivered to recipient');
  303. $statusArray['DeliveredStatus'] = 0;
  304. $statusArray['NoStatusCheck'] = 1;
  305. break;
  306. case 'REJECTED':
  307. $statusArray['StatusMsg'] = __('Message is rejected by server');
  308. $statusArray['DeliveredStatus'] = 0;
  309. $statusArray['NoStatusCheck'] = 1;
  310. break;
  311. case 'BADCOST':
  312. $statusArray['StatusMsg'] = __('Message is not delivered to recipient - can not determine message cost');
  313. $statusArray['DeliveredStatus'] = 0;
  314. $statusArray['NoStatusCheck'] = 1;
  315. break;
  316. case 'UNKNOWN':
  317. $statusArray['StatusMsg'] = __('Message status is unknown');
  318. $statusArray['DeliveredStatus'] = 0;
  319. $statusArray['NoStatusCheck'] = 0;
  320. break;
  321. default:
  322. $statusArray['StatusMsg'] = __('Sending status code is unknown:') . ' ' . $statusMsgCode;
  323. $statusArray['DeliveredStatus'] = 0;
  324. $statusArray['NoStatusCheck'] = 1;
  325. }
  326. return $statusArray;
  327. }
  328. }