SmsClubMobi.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. class SmsClubMobi extends SMSServiceApi {
  3. public function __construct($smsServiceId, array $smsPack = array()) {
  4. parent::__construct($smsServiceId, $smsPack);
  5. }
  6. public function getBalance() {
  7. $result = '';
  8. $url = 'https://gate.smsclub.mobi/http/getbalance.php?';
  9. $url_result = $url . 'username=' . $this->serviceLogin . '&password=' . $this->servicePassword;
  10. $curl = curl_init();
  11. curl_setopt($curl, CURLOPT_URL, $url_result);
  12. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  13. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  14. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  15. $response = curl_exec($curl);
  16. curl_close($curl);
  17. $result.= $this->instanceSendDog->getUBMsgHelperInstance()->getStyledMessage(__('Current account balance') . ': ' . $response, 'info');
  18. //return ($result);
  19. die(wf_modalAutoForm(__('Balance'), $result, $_POST['modalWindowId'], '', true, 'false', '700'));
  20. }
  21. public function getSMSQueue() {
  22. $this->showErrorFeatureIsNotSupported();
  23. }
  24. public function pushMessages() {
  25. if (!empty($this->smsMessagePack)) {
  26. global $ubillingConfig;
  27. $i = 0;
  28. $smsHistoryEnabled = $ubillingConfig->getAlterParam('SMS_HISTORY_ON');
  29. $smsAdvancedEnabled = $ubillingConfig->getAlterParam('SMS_SERVICES_ADVANCED_ENABLED');
  30. $smsHistoryTabFreshIds = array();
  31. $smsHistoryTabPhonesIds = array();
  32. $preSendStatus = __('Perparing for delivery');
  33. $telepatia = new Telepathy(false);
  34. if ($smsHistoryEnabled) {
  35. $telepatia->flushPhoneTelepathyCache();
  36. $telepatia->usePhones();
  37. }
  38. $xmlPacket = '<?xml version="1.0" encoding="utf-8"?><request_sendsms><username><![CDATA[' . $this->serviceLogin . ']]></username>';
  39. $xmlPacket .= '<password><![CDATA[' . $this->servicePassword . ']]></password><from><![CDATA[' . $this->serviceAlphaName . ']]></from>';
  40. foreach ($this->smsMessagePack as $io => $eachsms) {
  41. $formattedPhone = $this->makePhoneFormat($eachsms['number']);
  42. if ($smsHistoryEnabled) {
  43. //$PhoneToSearch = $this->sendDog->cutInternationalsFromPhoneNum($eachsms['number']);
  44. $login = $telepatia->getByPhoneFast($eachsms['number']);
  45. if ($smsAdvancedEnabled) {
  46. $query = "INSERT INTO `sms_history` (`smssrvid`, `login`, `phone`, `send_status`, `msg_text`)
  47. VALUES (" . $this->serviceId . ", '" . $login . "', '" . $eachsms['number'] . "', '" . $preSendStatus . "', '" . $eachsms['message'] . "');";
  48. } else {
  49. $query = "INSERT INTO `sms_history` (`login`, `phone`, `send_status`, `msg_text`)
  50. VALUES ('" . $login . "', '" . $eachsms['number'] . "', '" . $preSendStatus . "', '" . $eachsms['message'] . "');";
  51. }
  52. nr_query($query);
  53. $recId = simple_get_lastid('sms_history');
  54. $smsHistoryTabFreshIds[] = $recId;
  55. $smsHistoryTabPhonesIds[$formattedPhone] = $recId;
  56. }
  57. $xmlPacket .= '<to><![CDATA[' . $formattedPhone . ']]></to><text><![CDATA[' . $eachsms['message'] . ']]></text>';
  58. $this->instanceSendDog->getSmsQueueInstance()->deleteSms($eachsms['filename']);
  59. }
  60. $telepatia->savePhoneTelepathyCache();
  61. $xmlPacket .= '</request_sendsms>';
  62. $curl = curl_init();
  63. curl_setopt($curl, CURLOPT_POST, true);
  64. curl_setopt($curl, CURLOPT_CRLF, true);
  65. curl_setopt($curl, CURLOPT_URL, $this->serviceGatewayAddr . 'individual.php');
  66. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Accept: text/xml", "Cache-Control: no-cache"));
  67. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  68. curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlPacket);
  69. $result = curl_exec($curl);
  70. curl_close($curl);
  71. $parsedResult = zb_xml2array($result);
  72. if ( !empty($parsedResult) ) {
  73. $sendStatus = $parsedResult['response']['status'];
  74. $serverAnswer = $parsedResult['response']['text'];
  75. if (strtolower($sendStatus) == 'ok') {
  76. $smsPacketId = strtoupper(md5(uniqid(rand(), true)));
  77. log_register('SENDDOG SmsClubMobi packet ' . $smsPacketId . ' sent successfully');
  78. if ($smsHistoryEnabled) {
  79. $messagePhone = '';
  80. $phonesServiceIds = array();
  81. if ( isset($parsedResult['response']['ids']['mess_attr']['tel']) ) {
  82. $phonesServiceIds[$parsedResult['response']['ids']['mess_attr']['tel']] = $parsedResult['response']['ids']['mess'];
  83. } else {
  84. $messagesIds = $parsedResult['response']['ids']['mess'];
  85. foreach ($messagesIds as $key => $val) {
  86. if (stristr($key, 'attr') !== FALSE) {
  87. continue;
  88. }
  89. $phoneKeyIndex = $key . '_attr';
  90. $messageId = $val;
  91. if ($messagePhone !== $messagesIds[$phoneKeyIndex]['tel']) {
  92. $messagePhone = $messagesIds[$phoneKeyIndex]['tel'];
  93. } else {
  94. continue;
  95. }
  96. $phonesServiceIds[$messagePhone] = $messageId;
  97. }
  98. }
  99. if ( !empty($smsHistoryTabPhonesIds) and !empty($phonesServiceIds) ) {
  100. foreach ($smsHistoryTabPhonesIds as $phone => $recId) {
  101. $ServiceMessageID = $phonesServiceIds[$phone];
  102. $query = "UPDATE `sms_history` SET `srvmsgself_id` = '" . $ServiceMessageID . "',
  103. `srvmsgpack_id` = '" . $smsPacketId . "',
  104. `date_send` = '" . curdatetime() . "',
  105. `send_status` = '" . __('Message queued') . "'
  106. WHERE `id` = '" . $recId . "';";
  107. nr_query($query);
  108. }
  109. }
  110. }
  111. } else {
  112. log_register('SENDDOG SmsClubMobi failed to send SMS packet. Server answer: ' . $serverAnswer);
  113. if ($smsHistoryEnabled) {
  114. $idsAsStr = implode(',', $smsHistoryTabFreshIds);
  115. $query = "UPDATE `sms_history` SET `date_send` = '" . curdatetime() . "',
  116. `date_statuschk` = '" . curdatetime() . "',
  117. `no_statuschk` = '1',
  118. `send_status` = '" . __('Failed to send message') . ': ' . $serverAnswer ."'
  119. WHERE `id` IN (" . $idsAsStr . ");";
  120. nr_query($query);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. public function checkMessagesStatuses() {
  127. if ( empty($this->smsMessagePack) ) {
  128. if ($this->isDefaultService) {
  129. $query = "SELECT DISTINCT `srvmsgself_id` FROM `sms_history` WHERE `no_statuschk` < 1 AND `delivered` < 1 AND (`smssrvid` = " . $this->serviceId . " OR `smssrvid` = 0);";
  130. } else {
  131. $query = "SELECT DISTINCT `srvmsgself_id` FROM `sms_history` WHERE `no_statuschk` < 1 AND `delivered` < 1 AND `smssrvid` = " . $this->serviceId . ";";
  132. }
  133. $checkMessages = simple_queryall($query);
  134. } else { $checkMessages = $this->smsMessagePack; }
  135. if ( !empty($checkMessages) ) {
  136. $messagesIDs = array();
  137. foreach ($checkMessages as $io => $eachmessage) {
  138. if (!empty($eachmessage['srvmsgself_id'])) {
  139. $messagesIDs[] = $eachmessage['srvmsgself_id'];
  140. }
  141. }
  142. $messagesIdsList = implode(';', $messagesIDs);
  143. $xmlPacket = '<?xml version="1.0" encoding="utf-8"?><request_getstate><username><![CDATA[' . $this->serviceLogin . ']]></username>';
  144. $xmlPacket .= '<password><![CDATA[' . $this->servicePassword . ']]></password><smscid><![CDATA[' . $messagesIdsList . ']]></smscid>';
  145. $xmlPacket .= '</request_getstate>';
  146. $curl = curl_init();
  147. curl_setopt($curl, CURLOPT_POST, true);
  148. curl_setopt($curl, CURLOPT_CRLF, true);
  149. curl_setopt($curl, CURLOPT_URL, $this->serviceGatewayAddr . 'state.php');
  150. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Accept: text/xml", "Cache-Control: no-cache"));
  151. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  152. curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlPacket);
  153. $result = curl_exec($curl);
  154. curl_close($curl);
  155. $parsedResult = zb_xml2array($result);
  156. if ( !empty($parsedResult) ) {
  157. $sendStatus = $parsedResult['response']['status'];
  158. $serverAnswer = $parsedResult['response']['text'];
  159. if (strtolower($sendStatus) == 'ok') {
  160. $statusEntries = $parsedResult['response']['entries']['entry'];
  161. if ( isset($statusEntries['smscid']) and isset($statusEntries['state'])) {
  162. $messageId = $statusEntries['smscid'];
  163. $messageStatus = $statusEntries['state'];
  164. $decodedMessageStatus = $this->decodeMobiSmsStatusMsg($messageStatus);
  165. $query = "UPDATE `sms_history` SET `date_statuschk` = '" . curdatetime() . "',
  166. `delivered` = '" . $decodedMessageStatus['DeliveredStatus'] . "',
  167. `no_statuschk` = '" . $decodedMessageStatus['NoStatusCheck'] . "',
  168. `send_status` = '" . $decodedMessageStatus['StatusMsg'] . "'
  169. WHERE `srvmsgself_id` = '" . $messageId . "';";
  170. nr_query($query);
  171. } else {
  172. foreach ($statusEntries as $io => $EachEntry) {
  173. $messageId = $EachEntry['smscid'];
  174. $messageStatus = $EachEntry['state'];
  175. $decodedMessageStatus = $this->decodeMobiSmsStatusMsg($messageStatus);
  176. $query = "UPDATE `sms_history` SET `date_statuschk` = '" . curdatetime() . "',
  177. `delivered` = '" . $decodedMessageStatus['DeliveredStatus'] . "',
  178. `no_statuschk` = '" . $decodedMessageStatus['NoStatusCheck'] . "',
  179. `send_status` = '" . $decodedMessageStatus['StatusMsg'] . "'
  180. WHERE `srvmsgself_id` = '" . $messageId . "';";
  181. nr_query($query);
  182. }
  183. }
  184. log_register('SENDDOG SmsClubMobi checked statuses for ' . count($messagesIDs) . ' messages.');
  185. } else {
  186. log_register('SENDDOG SmsClubMobi failed to check messages statuses. Server answer: ' . $serverAnswer);
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * Gets the status message code as a parameter and returns appropriate message string
  193. *
  194. * @param string $statusMsgCode
  195. * @return array
  196. */
  197. protected function decodeMobiSmsStatusMsg($statusMsgCode) {
  198. $statusArray = array('StatusMsg' => '', 'DeliveredStatus' => 0, 'NoStatusCheck' => 0);
  199. switch ($statusMsgCode) {
  200. case 'DELIVRD':
  201. $statusArray['StatusMsg'] = __('Message is delivered to recipient');
  202. $statusArray['DeliveredStatus'] = 1;
  203. $statusArray['NoStatusCheck'] = 0;
  204. break;
  205. case 'UNDELIV':
  206. $statusArray['StatusMsg'] = __('Message can not be delivered to recipient');
  207. $statusArray['DeliveredStatus'] = 0;
  208. $statusArray['NoStatusCheck'] = 1;
  209. break;
  210. case 'ENROUTE':
  211. $statusArray['StatusMsg'] = __('Message is sent but not yet delivered to recipient');
  212. $statusArray['DeliveredStatus'] = 0;
  213. $statusArray['NoStatusCheck'] = 0;
  214. break;
  215. case 'EXPIRED':
  216. $statusArray['StatusMsg'] = __('Failed to deliver message - delivery term is expired');
  217. $statusArray['DeliveredStatus'] = 0;
  218. $statusArray['NoStatusCheck'] = 1;
  219. break;
  220. case 'REJECTD':
  221. $statusArray['StatusMsg'] = __('Message is rejected by server');
  222. $statusArray['DeliveredStatus'] = 0;
  223. $statusArray['NoStatusCheck'] = 1;
  224. break;
  225. }
  226. return $statusArray;
  227. }
  228. /**
  229. * As MobiClub needs phone numbers to be only in 38 0YY XXX XX XX format
  230. * this function will try to make the phone number suitable
  231. *
  232. * @param $phoneNumber string
  233. *
  234. * @return string
  235. */
  236. protected function makePhoneFormat($phoneNumber) {
  237. $phoneNumber = str_replace('+', '', $phoneNumber);
  238. if ( strlen($phoneNumber) == 10 ) {
  239. if (substr($phoneNumber, 0, 1) == '0') {
  240. $phoneNumber = '8' . $phoneNumber;
  241. }
  242. if (substr($phoneNumber, 0, 1) == '8') {
  243. $phoneNumber = '3' . $phoneNumber;
  244. }
  245. }
  246. return $phoneNumber;
  247. }
  248. }
  249. ?>