SmsFlyAPI2.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. class SmsFlyAPI2 extends SMSServiceApi {
  3. /**
  4. * Contains country code for target country
  5. *
  6. * @var string
  7. */
  8. protected $countryCode = '38';
  9. public function __construct($smsServiceId, $smsPack = array()) {
  10. parent::__construct($smsServiceId, $smsPack);
  11. }
  12. public function getSMSQueue() {
  13. $this->showErrorFeatureIsNotSupported();
  14. }
  15. public function pushMessages() {
  16. $result = '';
  17. $smsHistoryEnabled = $this->ubConfig->getAlterParam('SMS_HISTORY_ON');
  18. $smsAdvancedEnabled = $this->instanceSendDog->getUBConfigInstance()->getAlterParam('SMS_SERVICES_ADVANCED_ENABLED');
  19. $telepatia = new Telepathy(false);
  20. if ($smsHistoryEnabled) {
  21. $telepatia->flushPhoneTelepathyCache();
  22. $telepatia->usePhones();
  23. }
  24. $allSmsQueue = $this->smsMessagePack;
  25. if (!empty($allSmsQueue)) {
  26. foreach ($allSmsQueue as $io => $eachsms) {
  27. $params = array(
  28. "action" => "SENDMESSAGE",
  29. "data" => array(
  30. "recipient" => $this->checkPhone($eachsms['number']),
  31. "channels" => array("sms"),
  32. "sms" => array(
  33. "source" => $this->serviceAlphaName,
  34. "text" => $eachsms['message']
  35. )
  36. )
  37. );
  38. // Send SMS
  39. $responce = $this->apiquery($params);
  40. //remove old sent message
  41. $this->instanceSendDog->getSmsQueueInstance()->deleteSms($eachsms['filename']);
  42. if ($smsHistoryEnabled) {
  43. if ($responce) {
  44. $messageStatusCode = $responce['data']['sms']['status'];
  45. $decodedMessageStatus = $this->decodeStatusMsg($messageStatusCode);
  46. $smsMsgId = $responce['data']['messageID'];
  47. $sessionID = strtoupper(md5(uniqid(rand(), true)));
  48. $Login = $telepatia->getByPhoneFast($eachsms['number']);
  49. if ($smsAdvancedEnabled) {
  50. $query = "INSERT INTO `sms_history` (`smssrvid`, `login`, `phone`, `srvmsgself_id`, `srvmsgpack_id`, `send_status`, `msg_text`, `date_send`)
  51. VALUES (" . $this->serviceId . ", '" . $Login . "', '" . $eachsms['number'] . "', '" . $smsMsgId . "', '" . $sessionID . "', '" . $decodedMessageStatus['DeliveredStatus'] . "', '" . $eachsms['message'] . "', '" . curdatetime() . "');";
  52. } else {
  53. $query = "INSERT INTO `sms_history` (`login`, `phone`, `srvmsgself_id`, `srvmsgpack_id`, `send_status`, `msg_text`, `date_send`)
  54. VALUES ('" . $Login . "', '" . $eachsms['number'] . "', '" . $smsMsgId . "', '" . $sessionID . "', '" . $decodedMessageStatus['DeliveredStatus'] . "', '" . $eachsms['message'] . "', '" . curdatetime() . "');";
  55. }
  56. nr_query($query);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * Renders current SMS-Fly service user balance
  64. *
  65. * @return string
  66. */
  67. public function getBalance() {
  68. $result = '';
  69. $balance = '';
  70. $params = array("action" => "GETBALANCE");
  71. $responce = $this->apiquery($params);
  72. if ($responce) {
  73. $balance = $responce['data']['balance'];
  74. }
  75. $result .= $this->instanceSendDog->getUBMsgHelperInstance()->getStyledMessage(__('Current account balance') . ': ' . $balance, 'info');
  76. die(wf_modalAutoForm(__('Balance'), $result, $_POST['modalWindowId'], '', true, 'false', '700'));
  77. }
  78. protected function apiquery(array $params) {
  79. $params['auth'] = array(
  80. 'key' => $this->serviceApiKey,
  81. );
  82. $ch = curl_init();
  83. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  84. curl_setopt($ch, CURLOPT_POST, 1);
  85. curl_setopt($ch, CURLOPT_URL, $this->serviceGatewayAddr);
  86. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json"));
  87. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  88. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params, 256));
  89. $result = curl_exec($ch);
  90. curl_close($ch);
  91. if (!empty($result)) {
  92. $response = json_decode($result, true);
  93. if ($response['success'] == 1) {
  94. return $response;
  95. } else {
  96. $decodedMessageStatus = $this->decodeStatusMsg($response['error']['code']);
  97. log_register('SENDDOG SMSFLYAPI2 ERROR failed. Server answer: ' . $decodedMessageStatus['StatusMsg']);
  98. return false;
  99. }
  100. } else {
  101. log_register('SENDDOG SMSFLYAPI2 ERROR for use API');
  102. return false;
  103. }
  104. }
  105. /**
  106. * Checks messages status for SMS-Fly service
  107. *
  108. * @return void
  109. */
  110. public function checkMessagesStatuses() {
  111. if ($this->isDefaultService) {
  112. $query = "SELECT DISTINCT `srvmsgself_id` FROM `sms_history` WHERE `no_statuschk` < 1 AND `delivered` < 1 AND (`smssrvid` = " . $this->serviceId . " OR `smssrvid` = 0);";
  113. } else {
  114. $query = "SELECT DISTINCT `srvmsgself_id` FROM `sms_history` WHERE `no_statuschk` < 1 AND `delivered` < 1 AND `smssrvid` = " . $this->serviceId . ";";
  115. }
  116. $checkMessages = simple_queryall($query);
  117. if (!empty($checkMessages)) {
  118. foreach ($checkMessages as $io => $eachmessage) {
  119. $params = array(
  120. "action" => "GETMESSAGESTATUS",
  121. "data" => array(
  122. "messageID" => $eachmessage['srvmsgself_id']
  123. )
  124. );
  125. // Check SMS Status
  126. $responce = $this->apiquery($params);
  127. if ($responce) {
  128. $messageStatusCode = $responce['data']['sms']['status'];
  129. $decodedMessageStatus = $this->decodeStatusMsg($messageStatusCode);
  130. $query = "UPDATE `sms_history` SET `date_statuschk` = '" . curdatetime() . "',
  131. `delivered` = '" . $decodedMessageStatus['DeliveredStatus'] . "',
  132. `no_statuschk` = '" . $decodedMessageStatus['NoStatusCheck'] . "',
  133. `send_status` = '" . $decodedMessageStatus['StatusMsg'] . "'
  134. WHERE `srvmsgself_id` = '" . $eachmessage['srvmsgself_id'] . "';";
  135. nr_query($query);
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * SMS-Fly messages statuses codes decoding routine
  142. *
  143. * @param $statusMsgCode
  144. *
  145. * @return array
  146. */
  147. protected function decodeStatusMsg($statusMsgCode) {
  148. $msgStatusCodes = array(
  149. 'ACCEPTD' => 'Message received by the system',
  150. 'PENDING' => 'Message in queue for sending',
  151. 'INPROGRESS' => 'Message in processing',
  152. 'SENT' => 'Message sent',
  153. 'DELIVRD' => 'Message delivered',
  154. 'VIEWED' => 'Message viewed',
  155. 'EXPIRED' => 'Message delivery time expired',
  156. 'UNDELIV' => 'Message not delivered',
  157. 'STOPED' => 'Message stopped by the system',
  158. 'ERROR' => 'Message sending error',
  159. 'INSUFFICIENTFUNDS' => 'Insufficient funds to send this message',
  160. 'MODERATION' => 'Message on moderation',
  161. 'RESERVED' => 'Message reserved by the system',
  162. 'REFUND' => 'Message prepared for refund'
  163. );
  164. $statusArray = array('StatusMsg' => '', 'DeliveredStatus' => 0, 'NoStatusCheck' => 0);
  165. $statusMsg = (isset($msgStatusCodes[$statusMsgCode])) ? __($msgStatusCodes[$statusMsgCode]) : __('Unknown status code');
  166. switch ($statusMsgCode) {
  167. case 'ACCEPTD':
  168. case 'PENDING':
  169. case 'INPROGRESS':
  170. case 'MODERATION':
  171. case 'SENT':
  172. $statusArray['StatusMsg'] = $statusMsg;
  173. $statusArray['DeliveredStatus'] = 0;
  174. $statusArray['NoStatusCheck'] = 0;
  175. break;
  176. case 'EXPIRED':
  177. case 'UNDELIV':
  178. case 'STOPED':
  179. case 'ERROR':
  180. case 'INSUFFICIENTFUNDS':
  181. case 'RESERVED':
  182. case 'REFUND':
  183. $statusArray['StatusMsg'] = $statusMsg;
  184. $statusArray['DeliveredStatus'] = 0;
  185. $statusArray['NoStatusCheck'] = 1;
  186. break;
  187. case 'VIEWED':
  188. case 'DELIVRD':
  189. $statusArray['StatusMsg'] = $statusMsg;
  190. $statusArray['DeliveredStatus'] = 1;
  191. $statusArray['NoStatusCheck'] = 1;
  192. break;
  193. default:
  194. $statusArray['StatusMsg'] = __('Sending status code is unknown:') . ' ' . $statusMsgCode;
  195. $statusArray['DeliveredStatus'] = 0;
  196. $statusArray['NoStatusCheck'] = 1;
  197. }
  198. return ($statusArray);
  199. }
  200. protected function checkPhone($number) {
  201. $valid_operators = array(
  202. "039" => "kstar",
  203. "050" => "mts",
  204. "063" => "life",
  205. "066" => "mts",
  206. "067" => "kstar",
  207. "068" => "kstar",
  208. "073" => "life",
  209. "091" => "utel",
  210. "092" => "peoplenet",
  211. "093" => "life",
  212. "094" => "intertelecom",
  213. "095" => "mts",
  214. "096" => "kstar",
  215. "097" => "kstar",
  216. "098" => "kstar",
  217. "099" => "mts",
  218. );
  219. preg_match_all("/([0-9]+)/", $number, $matches);
  220. $number = implode("", $matches[1]);
  221. $number = str_pad($number, 12, "0", STR_PAD_LEFT);
  222. $phone = substr($number, -7);
  223. $operator = substr($number, -10, 3);
  224. if(!isset($valid_operators[$operator]) || 7 != strlen($phone)) {
  225. return false;
  226. }
  227. $result = $this->countryCode . $operator . $phone;
  228. return $result;
  229. }
  230. }
  231. ?>