SkySms.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. class SkySms extends SMSServiceApi {
  3. public function __construct($smsServiceId, $smsPack = array()) {
  4. parent::__construct($smsServiceId, $smsPack);
  5. }
  6. public function getBalance() {
  7. $this->showErrorFeatureIsNotSupported();
  8. }
  9. public function getSMSQueue() {
  10. $this->showErrorFeatureIsNotSupported();
  11. }
  12. public function pushMessages() {
  13. if ( !empty($this->smsMessagePack) ) {
  14. global $ubillingConfig;
  15. $i = 0;
  16. $smsHistoryEnabled = $ubillingConfig->getAlterParam('SMS_HISTORY_ON');
  17. $smsAdvancedEnabled = $ubillingConfig->getAlterParam('SMS_SERVICES_ADVANCED_ENABLED');
  18. $smsHistoryTabFreshIds = array();
  19. $preSendStatus = __('Perparing for delivery');
  20. $telepatia = new Telepathy(false);
  21. if ($smsHistoryEnabled) {
  22. $telepatia->flushPhoneTelepathyCache();
  23. $telepatia->usePhones();
  24. }
  25. $xmlPacket = '<?xml version="1.0" encoding="utf-8"?>
  26. <packet version="1.0">
  27. <auth login="' . $this->serviceLogin . '" password="' . $this->servicePassword . '"/>
  28. <command name="sendmessage">
  29. <message id="0" type="sms">
  30. <data charset="lat"></data>
  31. <recipients>
  32. ';
  33. foreach ($this->smsMessagePack as $io => $eachsms) {
  34. if ($smsHistoryEnabled) {
  35. //$PhoneToSearch = $this->sendDog->cutInternationalsFromPhoneNum($eachsms['number']);
  36. $Login = $telepatia->getByPhoneFast($eachsms['number']);
  37. if ($smsAdvancedEnabled) {
  38. $query = "INSERT INTO `sms_history` (`smssrvid`, `login`, `phone`, `send_status`, `msg_text`)
  39. VALUES (" . $this->serviceId . ", '" . $Login . "', '" . $eachsms['number'] . "', '" . $preSendStatus . "', '" . $eachsms['message'] . "');";
  40. } else {
  41. $query = "INSERT INTO `sms_history` (`login`, `phone`, `send_status`, `msg_text`)
  42. VALUES ('" . $Login . "', '" . $eachsms['number'] . "', '" . $preSendStatus . "', '" . $eachsms['message'] . "');";
  43. }
  44. nr_query($query);
  45. $recId = simple_get_lastid('sms_history');
  46. $smsHistoryTabFreshIds[] = $recId;
  47. $xmlPacket .= '<recipient id="' . $recId . '" address="' . $eachsms['number'] . '">' . $eachsms['message'] . '</recipient>';
  48. } else {
  49. $xmlPacket .= '<recipient id="' . ++$i . '" address="' . $eachsms['number'] . '">' . $eachsms['message'] . '</recipient>';
  50. }
  51. $this->instanceSendDog->getSmsQueueInstance()->deleteSms($eachsms['filename']);
  52. }
  53. $telepatia->savePhoneTelepathyCache();
  54. $xmlPacket .= '</recipients>
  55. </message>
  56. </command>
  57. </packet>
  58. ';
  59. $curl = curl_init();
  60. curl_setopt($curl, CURLOPT_POST, true);
  61. curl_setopt($curl, CURLOPT_URL, $this->serviceGatewayAddr);
  62. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Accept: text/xml", "Cache-Control: no-cache"));
  63. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  64. curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlPacket);
  65. $result = curl_exec($curl);
  66. curl_close($curl);
  67. $parsedResult = zb_xml2array($result);
  68. if ( !empty($parsedResult) ) {
  69. $serverAnswerCode = (isset($parsedResult['packet']['result_attr']['type'])) ? $parsedResult['packet']['result_attr']['type'] : '42';
  70. if ($serverAnswerCode == '00') {
  71. $smsPacketId = $parsedResult['packet']['result']['message_attr']['smsmsgid'];
  72. log_register('SENDDOG SKYSMS packet ' . $smsPacketId . ' sent successfully');
  73. if ($smsHistoryEnabled) {
  74. $recipients = $parsedResult['packet']['result']['message']['recipients']['recipient'];
  75. if ( empty($recipients) ) { $recipients = $parsedResult['packet']['result']['message']['recipients']; }
  76. foreach ($recipients as $each => $Recipient) {
  77. if ( isset($Recipient['id']) ) {
  78. $query = "UPDATE `sms_history` SET `srvmsgself_id` = '" . $Recipient['smsid'] . "',
  79. `srvmsgpack_id` = '" . $smsPacketId . "',
  80. `date_send` = '" . curdatetime() . "',
  81. `send_status` = '" . __('Message queued') . "'
  82. WHERE `id` = '" . $Recipient['id'] . "';";
  83. nr_query($query);
  84. }
  85. }
  86. }
  87. } else {
  88. $serverErrorMsg = $this->decodeSkySMSErrorMsg($serverAnswerCode);
  89. log_register('SENDDOG SKYSMS failed to sent SMS packet. Server answer: ' . $serverErrorMsg . ( ($serverAnswerCode == '42') ? $result : '') );
  90. if ($smsHistoryEnabled) {
  91. $idsAsStr = implode(',', $smsHistoryTabFreshIds);
  92. $query = "UPDATE `sms_history` SET `date_send` = '" . curdatetime() . "',
  93. `date_statuschk` = '" . curdatetime() . "',
  94. `no_statuschk` = '1',
  95. `send_status` = '" . __('Failed to send message') . ': ' . $serverErrorMsg ."'
  96. WHERE `id` IN (" . $idsAsStr . ");";
  97. nr_query($query);
  98. }
  99. }
  100. }
  101. /*//remove old sent message
  102. foreach ($allSmsQueue as $io => $eachsms) {
  103. $this->smsQueue->deleteSms($eachsms['filename']);
  104. }*/
  105. }
  106. }
  107. /**
  108. * Checks messages status for SKYSMS service
  109. *
  110. * @return void
  111. */
  112. public function checkMessagesStatuses() {
  113. /*if ( empty($this->smsMessagePack) ) {
  114. } else { $chkMessages = $this->smsMessagePack; }*/
  115. // we better ignore the contents of $this->smsMessagePack because it will probably come not distinguished by srvmsgpack_id
  116. // which will mess up all the things and flood our events log with plenty of unneeded shit
  117. // and as long as SkySms allows to check statuses by packet ID (no need to do it for every message individually)
  118. // it will be better to requery data from DB in our manner:
  119. if ($this->isDefaultService) {
  120. $query = "SELECT DISTINCT `srvmsgpack_id` FROM `sms_history` WHERE `no_statuschk` < 1 AND `delivered` < 1 AND (`smssrvid` = " . $this->serviceId . " OR `smssrvid` = 0);";
  121. } else {
  122. $query = "SELECT DISTINCT `srvmsgpack_id` FROM `sms_history` WHERE `no_statuschk` < 1 AND `delivered` < 1 AND `smssrvid` = " . $this->serviceId . ";";
  123. }
  124. $chkMessages = simple_queryall($query);
  125. if ( !empty($chkMessages) ) {
  126. $skySmsApiUrl = $this->serviceGatewayAddr;
  127. $skySmsApiLogin = $this->serviceLogin;
  128. $skySmsApiPassw = $this->servicePassword;
  129. foreach ($chkMessages as $io => $eachmessage) {
  130. $smsPacketID = $eachmessage['srvmsgpack_id'];
  131. if ( empty($smsPacketID) ) { continue; }
  132. $xmlPacket = '<?xml version="1.0" encoding="utf-8"?>
  133. <packet version="1.0">
  134. <auth login="' . $skySmsApiLogin . '" password="' . $skySmsApiPassw . '"/>
  135. <command name="querymessage">
  136. <message smsmsgid="' . $smsPacketID . '"/>
  137. </command>
  138. </packet>
  139. ';
  140. $curl = curl_init();
  141. curl_setopt($curl, CURLOPT_POST, true);
  142. curl_setopt($curl, CURLOPT_URL, $skySmsApiUrl);
  143. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Accept: text/xml", "Cache-Control: no-cache"));
  144. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  145. curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlPacket);
  146. $result = curl_exec($curl);
  147. curl_close($curl);
  148. $parsedResult = zb_xml2array($result);
  149. if ( !empty($parsedResult) ) {
  150. $serverAnswerCode = (isset($parsedResult['packet']['result_attr']['type'])) ? $parsedResult['packet']['result_attr']['type'] : '42';
  151. if ($serverAnswerCode == '00') {
  152. $recipients = $parsedResult['packet']['result']['message']['recipients']['recipient'];
  153. if ( empty($recipients) ) { $recipients = $parsedResult['packet']['result']['message']['recipients']; }
  154. foreach ($recipients as $each => $Recipient) {
  155. if (isset($Recipient['smsid'])) {
  156. $messageId = $Recipient['smsid'];
  157. $messageStatus = $Recipient['status'];
  158. $decodedMessageStatus = $this->decodeSkySMSStatusMsg($messageStatus);
  159. $query = "UPDATE `sms_history` SET `date_statuschk` = '". curdatetime() . "',
  160. `delivered` = '" . $decodedMessageStatus['DeliveredStatus'] . "',
  161. `no_statuschk` = '" . $decodedMessageStatus['NoStatusCheck'] . "',
  162. `send_status` = '" . $decodedMessageStatus['StatusMsg'] . "'
  163. WHERE `srvmsgself_id` = '" . $messageId . "';";
  164. nr_query($query);
  165. }
  166. }
  167. //log_register('SENDDOG SKYSMS checked SMS message ' . $messageId . ' send status');
  168. log_register('SENDDOG SKYSMS checking SMS packet ' . $smsPacketID . ' send status');
  169. } else {
  170. $serverErrorMsg = $this->decodeSkySMSErrorMsg($serverAnswerCode);
  171. log_register('SENDDOG SKYSMS failed to get SMS packet ' . $smsPacketID . ' send status. Server answer: ' . $serverErrorMsg . ( ($serverAnswerCode == '42') ? $result : '') );
  172. }
  173. }
  174. }
  175. }
  176. }
  177. /**
  178. * Gets the error message code as a parameter and returns appropriate message string
  179. *
  180. * @param string $errorMsgCode
  181. * @return string
  182. */
  183. protected function decodeSkySMSErrorMsg($errorMsgCode) {
  184. switch ($errorMsgCode) {
  185. case '01':
  186. $message = __('Incorrect parameters value or insufficient parameters count');
  187. break;
  188. case '02':
  189. $message = __('Database server connection error');
  190. break;
  191. case '03':
  192. $message = __('Database was not found');
  193. break;
  194. case '04':
  195. $message = __('Authorization procedure error');
  196. break;
  197. case '05':
  198. $message = __('Login or password is incorrect');
  199. break;
  200. case '06':
  201. $message = __('Malfunction in user\'s configuration');
  202. break;
  203. default:
  204. $message = __('Error code is unknown. Servers answer:') . ' ' . $errorMsgCode;
  205. }
  206. return $message;
  207. }
  208. /**
  209. * Gets the status message code as a parameter and returns appropriate message string
  210. *
  211. * @param string $statusMsgCode
  212. * @return array
  213. */
  214. protected function decodeSkySMSStatusMsg($statusMsgCode) {
  215. $statusArray = array('StatusMsg' => '', 'DeliveredStatus' => 0, 'NoStatusCheck' => 0);
  216. switch ($statusMsgCode) {
  217. case 'DELIVERED':
  218. $statusArray['StatusMsg'] = __('Message is delivered to recipient');
  219. $statusArray['DeliveredStatus'] = 1;
  220. $statusArray['NoStatusCheck'] = 0;
  221. break;
  222. case 'TOSEND':
  223. $statusArray['StatusMsg'] = __('Message is queued for delivering');
  224. $statusArray['DeliveredStatus'] = 0;
  225. $statusArray['NoStatusCheck'] = 0;
  226. break;
  227. case 'ENROUTE':
  228. $statusArray['StatusMsg'] = __('Message is sent but not yet delivered to recipient');
  229. $statusArray['DeliveredStatus'] = 0;
  230. $statusArray['NoStatusCheck'] = 0;
  231. break;
  232. case 'PAUSED':
  233. $statusArray['StatusMsg'] = __('Message delivering is paused');
  234. $statusArray['DeliveredStatus'] = 0;
  235. $statusArray['NoStatusCheck'] = 0;
  236. break;
  237. case 'CANCELED':
  238. $statusArray['StatusMsg'] = __('Message delivering is canceled');
  239. $statusArray['DeliveredStatus'] = 0;
  240. $statusArray['NoStatusCheck'] = 1;
  241. break;
  242. case 'FAILED':
  243. $statusArray['StatusMsg'] = __('Failed to send message');
  244. $statusArray['DeliveredStatus'] = 0;
  245. $statusArray['NoStatusCheck'] = 1;
  246. break;
  247. case 'EXPIRED':
  248. $statusArray['StatusMsg'] = __('Failed to deliver message - delivery term is expired');
  249. $statusArray['DeliveredStatus'] = 0;
  250. $statusArray['NoStatusCheck'] = 1;
  251. break;
  252. case 'UNDELIVERABLE':
  253. $statusArray['StatusMsg'] = __('Message can not be delivered to recipient');
  254. $statusArray['DeliveredStatus'] = 0;
  255. $statusArray['NoStatusCheck'] = 1;
  256. break;
  257. case 'REJECTED':
  258. $statusArray['StatusMsg'] = __('Message is rejected by server');
  259. $statusArray['DeliveredStatus'] = 0;
  260. $statusArray['NoStatusCheck'] = 1;
  261. break;
  262. case 'BADCOST':
  263. $statusArray['StatusMsg'] = __('Message is not delivered to recipient - can not determine message cost');
  264. $statusArray['DeliveredStatus'] = 0;
  265. $statusArray['NoStatusCheck'] = 1;
  266. break;
  267. case 'UNKNOWN':
  268. $statusArray['StatusMsg'] = __('Message status is unknown');
  269. $statusArray['DeliveredStatus'] = 0;
  270. $statusArray['NoStatusCheck'] = 0;
  271. break;
  272. default:
  273. $statusArray['StatusMsg'] = __('Sending status code is unknown:') . ' ' . $statusMsgCode;
  274. $statusArray['DeliveredStatus'] = 0;
  275. $statusArray['NoStatusCheck'] = 1;
  276. }
  277. return $statusArray;
  278. }
  279. }
  280. ?>