smsflyapi2.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. class smsflyapi2 extends SendDogProto {
  3. protected $baseurl = 'https://sms-fly.ua/api/v2/api.php';
  4. protected $apikey = '';
  5. /**
  6. * Returns set of inputs, required for SMS-Fly service configuration
  7. *
  8. * @return string
  9. */
  10. public function renderSmsflyConfigInputs() {
  11. $inputs = wf_tag('h2') . __('SMS-Fly API2') . ' ' . wf_Link(self::URL_ME . '&showmisc=smsflyapi2', wf_img_sized('skins/icon_dollar.gif', __('Balance'), '10', '10'), true) . wf_tag('h2', true);
  12. $inputs .= wf_TextInput('editsmsflyapi2gateway', __('SMS-Fly API address'), $this->settings['SMSFLYAPI2_GATEWAY'], true, 30);
  13. $inputs .= wf_TextInput('editsmsflyapi2key', __('User login to access SMS-Fly API'), $this->settings['SMSFLYAPI2_KEY'], true, 20);
  14. $inputs .= wf_TextInput('editsmsflyapi2sign', __('SMS-Fly') . ' ' . __('Sign') . ' (' . __('Alphaname') . ')', $this->settings['SMSFLYAPI2_SIGN'], true, 20);
  15. $smsServiceFlag = ($this->settings['SMS_SERVICE'] == 'smsflyapi2') ? true : false;
  16. $inputs .= wf_RadioInput('defaultsmsservice', __('Use SMS-Fly as default SMS service'), 'smsflyapi2', true, $smsServiceFlag);
  17. return ($inputs);
  18. }
  19. /**
  20. * Sends all sms storage via SMS-Fly service
  21. *
  22. * @return void
  23. */
  24. public function smsflyPushMessages() {
  25. $result = '';
  26. $smsHistoryEnabled = $this->ubConfig->getAlterParam('SMS_HISTORY_ON');
  27. $telepatia = new Telepathy(false);
  28. if ($smsHistoryEnabled) {
  29. $telepatia->flushPhoneTelepathyCache();
  30. $telepatia->usePhones();
  31. }
  32. $allSmsQueue = $this->smsQueue->getQueueData();
  33. if (!empty($allSmsQueue)) {
  34. foreach ($allSmsQueue as $io => $eachsms) {
  35. $params = array(
  36. "action" => "SENDMESSAGE",
  37. "data" => array(
  38. "recipient" => $this->cutInternationalsFromPhoneNum($eachsms['number']),
  39. "channels" => array("sms"),
  40. "sms" => array(
  41. "source" => $this->source,
  42. "text" => $eachsms['message']
  43. )
  44. )
  45. );
  46. // Send SMS
  47. $responce = $this->apiquery($params);
  48. //remove old sent message
  49. $this->smsQueue->deleteSms($eachsms['filename']);
  50. if ($smsHistoryEnabled) {
  51. if ($responce) {
  52. $messageStatusCode = $responce['data']['sms']['status'];
  53. $decodedMessageStatus = $this->decodeStatusMsg($messageStatusCode);
  54. $smsMsgId = $responce['data']['messageID'];
  55. $sessionID = strtoupper(md5(uniqid(rand(), true)));
  56. $Login = $telepatia->getByPhoneFast($eachsms['number']);
  57. $query = "INSERT INTO `sms_history` (`login`, `phone`, `srvmsgself_id`, `srvmsgpack_id`, `send_status`, `msg_text`, `date_send`)
  58. VALUES ('" . $Login . "', '" . $eachsms['number'] . "', '" . $smsMsgId . "', '" . $sessionID . "', '" . $decodedMessageStatus['DeliveredStatus'] . "', '" . $eachsms['message'] . "', '" . curdatetime() . "');";
  59. nr_query($query);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Renders current SMS-Fly service user balance
  67. *
  68. * @return string
  69. */
  70. public function renderSmsflyBalance() {
  71. $result = '';
  72. $balance = '';
  73. $params = array("action" => "GETBALANCE");
  74. $responce = $this->apiquery($params);
  75. if ($responce) {
  76. $balance = $responce['data']['balance'];
  77. }
  78. $result .= wf_BackLink(self::URL_ME, '', true);
  79. $result .= $this->messages->getStyledMessage(__('Current account balance') . ': ' . $balance, 'info');
  80. return ($result);
  81. }
  82. /**
  83. * Renders current SMS-Fly service user balance
  84. *
  85. * @return array/bool
  86. */
  87. protected function apiquery(array $params) {
  88. $params['auth'] = array(
  89. 'key' => $this->apikey,
  90. );
  91. $ch = curl_init();
  92. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  93. curl_setopt($ch, CURLOPT_POST, 1);
  94. curl_setopt($ch, CURLOPT_URL, $this->baseurl);
  95. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json"));
  96. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  97. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params, 256));
  98. $result = curl_exec($ch);
  99. curl_close($ch);
  100. if (!empty($result)) {
  101. $response = json_decode($result, true);
  102. if ($response['success'] == 1) {
  103. return $response;
  104. } else {
  105. $decodedMessageStatus = $this->decodeStatusMsg($response['error']['code']);
  106. log_register('SENDDOG SMSFLYAPI2 ERROR failed. Server answer: ' . $decodedMessageStatus['StatusMsg']);
  107. return false;
  108. }
  109. } else {
  110. log_register('SENDDOG SMSFLYAPI2 ERROR for use API');
  111. return false;
  112. }
  113. }
  114. /**
  115. * Checks messages status for SMS-Fly service
  116. *
  117. * @return void
  118. */
  119. public function smsflyCheckMessagesStatus() {
  120. $query = "SELECT DISTINCT `srvmsgself_id` FROM `sms_history` WHERE `no_statuschk` < 1 AND `delivered` < 1;";
  121. $checkMessages = simple_queryall($query);
  122. if (!empty($checkMessages)) {
  123. foreach ($checkMessages as $io => $eachmessage) {
  124. $params = array(
  125. "action" => "GETMESSAGESTATUS",
  126. "data" => array(
  127. "messageID" => $eachmessage['srvmsgself_id']
  128. )
  129. );
  130. // Check SMS Status
  131. $responce = $this->apiquery($params);
  132. if ($responce) {
  133. $messageStatusCode = $responce['data']['sms']['status'];
  134. $decodedMessageStatus = $this->decodeStatusMsg($messageStatusCode);
  135. $query = "UPDATE `sms_history` SET `date_statuschk` = '" . curdatetime() . "',
  136. `delivered` = '" . $decodedMessageStatus['DeliveredStatus'] . "',
  137. `no_statuschk` = '" . $decodedMessageStatus['NoStatusCheck'] . "',
  138. `send_status` = '" . $decodedMessageStatus['StatusMsg'] . "'
  139. WHERE `srvmsgself_id` = '" . $eachmessage['srvmsgself_id'] . "';";
  140. nr_query($query);
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * SMS-Fly messages statuses codes decoding routine
  147. *
  148. * @param $statusMsgCode
  149. *
  150. * @return array
  151. */
  152. protected function decodeStatusMsg($statusMsgCode) {
  153. $msgStatusCodes = array(
  154. 'ACCEPTD' => 'Message received by the system',
  155. 'PENDING' => 'message in queue for sending',
  156. 'INPROGRESS' => 'Message in processing',
  157. 'SENT' => 'Message sent',
  158. 'DELIVRD' => 'Message delivered',
  159. 'VIEWED' => 'Message viewed',
  160. 'EXPIRED' => 'Message delivery time expired',
  161. 'UNDELIV' => 'Message not delivered',
  162. 'STOPED' => 'Message stopped by the system',
  163. 'ERROR' => 'Message sending error',
  164. 'INSUFFICIENTFUNDS' => 'Insufficient funds to send this message',
  165. 'MODERATION' => 'Message on moderation',
  166. 'RESERVED' => 'Message reserved by the system',
  167. 'REFUND' => 'Message prepared for refund'
  168. );
  169. $statusArray = array('StatusMsg' => '', 'DeliveredStatus' => 0, 'NoStatusCheck' => 0);
  170. $statusMsg = (isset($msgStatusCodes[$statusMsgCode])) ? __($msgStatusCodes[$statusMsgCode]) : __('Unknown status code');
  171. switch ($statusMsgCode) {
  172. case 'ACCEPTD':
  173. case 'PENDING':
  174. case 'INPROGRESS':
  175. case 'MODERATION':
  176. case 'SENT':
  177. $statusArray['StatusMsg'] = $statusMsg;
  178. $statusArray['DeliveredStatus'] = 0;
  179. $statusArray['NoStatusCheck'] = 0;
  180. break;
  181. case 'EXPIRED':
  182. case 'UNDELIV':
  183. case 'STOPED':
  184. case 'ERROR':
  185. case 'INSUFFICIENTFUNDS':
  186. case 'RESERVED':
  187. case 'REFUND':
  188. $statusArray['StatusMsg'] = $statusMsg;
  189. $statusArray['DeliveredStatus'] = 0;
  190. $statusArray['NoStatusCheck'] = 1;
  191. break;
  192. case 'VIEWED':
  193. case 'DELIVRD':
  194. $statusArray['StatusMsg'] = $statusMsg;
  195. $statusArray['DeliveredStatus'] = 1;
  196. $statusArray['NoStatusCheck'] = 1;
  197. break;
  198. default:
  199. $statusArray['StatusMsg'] = __('Sending status code is unknown:') . ' ' . $statusMsgCode;
  200. $statusArray['DeliveredStatus'] = 0;
  201. $statusArray['NoStatusCheck'] = 1;
  202. }
  203. return ($statusArray);
  204. }
  205. /**
  206. * Loads SMS-Fly service config
  207. *
  208. * @return void
  209. */
  210. public function loadSmsflyConfig() {
  211. $smsgateway = zb_StorageGet('SENDDOG_SMSFLYAPI2_GATEWAY');
  212. if (empty($smsgateway)) {
  213. $smsgateway = 'https://sms-fly.ua/api/v2/api.php';
  214. zb_StorageSet('SENDDOG_SMSFLYAPI2_GATEWAY', $smsgateway);
  215. }
  216. $smskey = zb_StorageGet('SENDDOG_SMSFLYAPI2_KEY');
  217. if (empty($smskey)) {
  218. $smskey = 'dtTXXXXXXXHUdZ5m2mCXXXXXXXXXX';
  219. zb_StorageSet('SENDDOG_SMSFLYAPI2_KEY', $smskey);
  220. }
  221. $smssign = zb_StorageGet('SENDDOG_SMSFLYAPI2_SIGN');
  222. if (empty($smssign)) {
  223. $smssign = 'InfoCentr';
  224. zb_StorageSet('SENDDOG_SMSFLYAPI2_SIGN', $smssign);
  225. }
  226. $this->settings['SMSFLYAPI2_GATEWAY'] = $smsgateway;
  227. $this->settings['SMSFLYAPI2_KEY'] = $smskey;
  228. $this->settings['SMSFLYAPI2_SIGN'] = $smssign;
  229. $this->baseurl = $this->settings['SMSFLYAPI2_GATEWAY'];
  230. $this->apikey = $this->settings['SMSFLYAPI2_KEY'];
  231. $this->source = $this->settings['SMSFLYAPI2_SIGN'];
  232. }
  233. /**
  234. * Saves service settings to database
  235. *
  236. * @return void
  237. */
  238. public function saveSettings() {
  239. //SMS-Fly configuration
  240. if ($_POST['editsmsflyapi2gateway'] != $this->settings['SMSFLYAPI2_GATEWAY']) {
  241. zb_StorageSet('SENDDOG_SMSFLYAPI2_GATEWAY', $_POST['editsmsflyapi2gateway']);
  242. log_register('SENDDOG CONFIG SET SMSFLYAPI2GATEWAY `' . $_POST['editsmsflyapi2gateway'] . '`');
  243. }
  244. if ($_POST['editsmsflyapi2key'] != $this->settings['SMSFLYAPI2_KEY']) {
  245. zb_StorageSet('SENDDOG_SMSFLYAPI2_KEY', $_POST['editsmsflyapi2key']);
  246. log_register('SENDDOG CONFIG SET SMSFLYAPI2KEY `' . $_POST['editsmsflyapi2key'] . '`');
  247. }
  248. if ($_POST['editsmsflyapi2sign'] != $this->settings['SMSFLYAPI2_SIGN']) {
  249. zb_StorageSet('SENDDOG_SMSFLYAPI2_SIGN', $_POST['editsmsflyapi2sign']);
  250. log_register('SENDDOG CONFIG SET SMSFLYAPI2SIGN `' . $_POST['editsmsflyapi2sign'] . '`');
  251. }
  252. }
  253. }