redsms.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. class redsms extends SendDogProto {
  3. /**
  4. * Loads RED-sms service config
  5. *
  6. * @return void
  7. */
  8. public function loadRedsmsConfig() {
  9. $smsgateway = zb_StorageGet('SENDDOG_REDSMS_GATEWAY');
  10. if (empty($smsgateway)) {
  11. $smsgateway = 'https://lk.redsms.ru/get/send.php';
  12. zb_StorageSet('SENDDOG_REDSMS_GATEWAY', $smsgateway);
  13. }
  14. $smsbilgateway = zb_StorageGet('SENDDOG_REDSMS_BILGATEWAY');
  15. if (empty($smsbilgateway)) {
  16. $smsbilgateway = 'https://lk.redsms.ru/get/balance.php';
  17. zb_StorageSet('SENDDOG_REDSMS_BILGATEWAY', $smsbilgateway);
  18. }
  19. $smslogin = zb_StorageGet('SENDDOG_REDSMS_LOGIN');
  20. if (empty($smslogin)) {
  21. $smslogin = 'Login';
  22. zb_StorageSet('SENDDOG_REDSMS_LOGIN', $smslogin);
  23. }
  24. $smsapikey = zb_StorageGet('SENDDOG_REDSMS_APIKEY');
  25. if (empty($smsapikey)) {
  26. $smsapikey = 'MyAPIKey';
  27. zb_StorageSet('SENDDOG_REDSMS_APIKEY', $smsapikey);
  28. }
  29. $smssign = zb_StorageGet('SENDDOG_REDSMS_SIGN');
  30. if (empty($smssign)) {
  31. $smssign = 'InfoCentr';
  32. zb_StorageSet('SENDDOG_REDSMS_SIGN', $smssign);
  33. }
  34. $this->settings['REDSMS_GATEWAY'] = $smsgateway;
  35. $this->settings['REDSMS_BILGATEWAY'] = $smsbilgateway;
  36. $this->settings['REDSMS_LOGIN'] = $smslogin;
  37. $this->settings['REDSMS_APIKEY'] = $smsapikey;
  38. $this->settings['REDSMS_SIGN'] = $smssign;
  39. }
  40. /**
  41. * Sends all sms storage via redsms.ru service
  42. *
  43. * @return void
  44. */
  45. public function redsmsPushMessages() {
  46. $result = '';
  47. $timestamp = file_get_contents('https://lk.redsms.ru/get/timestamp.php');
  48. $api_key = $this->settings['REDSMS_APIKEY'];
  49. $login = $this->settings['REDSMS_LOGIN'];
  50. $return = 'xml';
  51. $sender = $this->settings['REDSMS_SIGN'];
  52. $allSmsQueue = $this->smsQueue->getQueueData();
  53. if (!empty($allSmsQueue)) {
  54. foreach ($allSmsQueue as $io => $eachsms) {
  55. $phone = str_replace('+', '', $eachsms['number']); //numbers in international format without +
  56. $text = $eachsms['message'];
  57. $params = array(
  58. 'timestamp' => $timestamp,
  59. 'login' => $login,
  60. 'phone' => $phone,
  61. 'text' => $text,
  62. 'sender' => $sender,
  63. 'return' => $return);
  64. ksort($params);
  65. reset($params);
  66. $signature = md5(implode($params) . $api_key);
  67. $query = $this->settings['REDSMS_GATEWAY'] . "?login=" . $login . "&signature=" . $signature . "&phone=" . $phone . "&sender=" . $sender . "&return=" . $return . "&timestamp=" . $timestamp . "&text=" . urlencode($text);
  68. $curl = curl_init();
  69. curl_setopt($curl, CURLOPT_URL, $query);
  70. curl_setopt($curl, CURLOPT_ENCODING, "utf-8");
  71. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 120);
  72. curl_setopt($curl, CURLOPT_TIMEOUT, 120);
  73. curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
  74. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  75. $response = curl_exec($curl);
  76. curl_close($curl);
  77. //remove old sent message
  78. $this->smsQueue->deleteSms($eachsms['filename']);
  79. }
  80. }
  81. }
  82. /**
  83. * Renders current RED-Sms service user balance
  84. *
  85. * @return string
  86. */
  87. public function renderRedsmsBalance() {
  88. $result = '';
  89. $timestamp = file_get_contents('https://lk.redsms.ru/get/timestamp.php');
  90. $api_key = $this->settings['REDSMS_APIKEY'];
  91. $login = $this->settings['REDSMS_LOGIN'];
  92. $return = 'xml';
  93. $params = array(
  94. 'timestamp' => $timestamp,
  95. 'login' => $login,
  96. 'return' => $return
  97. );
  98. ksort($params);
  99. reset($params);
  100. $signature = md5(implode($params) . $api_key);
  101. $query = $this->settings['REDSMS_BILGATEWAY'] . "?login=" . $login . "&signature=" . $signature . "&timestamp=" . $timestamp . "&return=" . $return;
  102. $curl = curl_init();
  103. curl_setopt($curl, CURLOPT_URL, $query);
  104. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  105. $response = curl_exec($curl);
  106. curl_close($curl);
  107. $result .= wf_BackLink(self::URL_ME, '', true);
  108. $result .= $this->messages->getStyledMessage(__('Current account balance') . ': ' . $response . ' RUR', 'info');
  109. return ($result);
  110. }
  111. /**
  112. * Returns set of inputs, required for RED-Sms service configuration
  113. *
  114. * @return string
  115. */
  116. public function renderRedsmsConfigInputs() {
  117. $inputs = wf_tag('h2') . __('RED-Sms') . ' ' . wf_Link(self::URL_ME . '&showmisc=redsms', wf_img_sized('skins/icon_dollar.gif', __('Balance'), '10', '10'), true) . wf_tag('h2', true);
  118. $inputs .= wf_TextInput('editredsmsgateway', __('RED-Sms API address'), $this->settings['REDSMS_GATEWAY'], true, 30);
  119. $inputs .= wf_TextInput('editredsmsbilgateway', __('RED-Sms Balance API address'), $this->settings['REDSMS_BILGATEWAY'], true, 30);
  120. $inputs .= wf_TextInput('editredsmslogin', __('User login to access RED-Sms API'), $this->settings['REDSMS_LOGIN'], true, 20);
  121. $inputs .= wf_TextInput('editredsmsapikey', __('User API key for access RED-Sms API'), $this->settings['REDSMS_APIKEY'], true, 20);
  122. $inputs .= wf_TextInput('editredsmssign', __('RED-Sms') . ' ' . __('Sign') . ' (' . __('Alphaname') . ')', $this->settings['REDSMS_SIGN'], true, 20);
  123. $smsServiceFlag = ($this->settings['SMS_SERVICE'] == 'redsms') ? true : false;
  124. $inputs .= wf_RadioInput('defaultsmsservice', __('Use RED-Sms as default SMS service'), 'redsms', true, $smsServiceFlag);
  125. return ($inputs);
  126. }
  127. /**
  128. * Saves service settings to database
  129. *
  130. * @return void
  131. */
  132. public function saveSettings() {
  133. //RED-Sms configuration
  134. if ($_POST['editredsmsgateway'] != $this->settings['REDSMS_GATEWAY']) {
  135. zb_StorageSet('SENDDOG_REDSMS_GATEWAY', $_POST['editredsmsgateway']);
  136. log_register('SENDDOG CONFIG SET REDSMSGATEWAY `' . $_POST['editredsmsgateway'] . '`');
  137. }
  138. if ($_POST['editredsmsbilgateway'] != $this->settings['REDSMS_BILGATEWAY']) {
  139. zb_StorageSet('SENDDOG_REDSMS_BILGATEWAY', $_POST['editredsmsbilgateway']);
  140. log_register('SENDDOG CONFIG SET REDSMSBILGATEWAY `' . $_POST['editredsmsbilgateway'] . '`');
  141. }
  142. if ($_POST['editredsmslogin'] != $this->settings['REDSMS_LOGIN']) {
  143. zb_StorageSet('SENDDOG_REDSMS_LOGIN', $_POST['editredsmslogin']);
  144. log_register('SENDDOG CONFIG SET REDSMSLOGIN `' . $_POST['editredsmslogin'] . '`');
  145. }
  146. if ($_POST['editredsmsapikey'] != $this->settings['REDSMS_APIKEY']) {
  147. zb_StorageSet('SENDDOG_REDSMS_APIKEY', $_POST['editredsmsapikey']);
  148. log_register('SENDDOG CONFIG SET REDSMSAPIKEY `' . $_POST['editredsmsapikey'] . '`');
  149. }
  150. if ($_POST['editredsmssign'] != $this->settings['REDSMS_SIGN']) {
  151. zb_StorageSet('SENDDOG_REDSMS_SIGN', $_POST['editredsmssign']);
  152. log_register('SENDDOG CONFIG SET REDSMSSIGN `' . $_POST['editredsmssign'] . '`');
  153. }
  154. }
  155. }