notificoresms.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Notificore API implementation
  4. * https://github.com/Notificore/notificore-php
  5. */
  6. class notificoresms extends SendDogProto {
  7. /**
  8. * Contains default external lib path
  9. */
  10. const VENDOR_LIB = 'api/vendor/notificore/Notificore.php';
  11. /**
  12. * Defines default log path
  13. */
  14. const LOG_PATH = 'exports/senddog_notificoresms.log';
  15. /**
  16. * Renders account balance
  17. *
  18. * @return string
  19. */
  20. public function showMiscInfo() {
  21. $result = '';
  22. require_once (self::VENDOR_LIB);
  23. $api = new Notificore($this->settings['NOTIFICORE_APIKEY']);
  24. $client = $api->getSmsClient();
  25. $balanceRaw = $client->getBalance();
  26. if (!empty($balanceRaw)) {
  27. if ($balanceRaw['error'] == 0) {
  28. if (isset($balanceRaw['amount'])) {
  29. $balance = $balanceRaw['amount'] . ' ' . $balanceRaw['currency'] . ', ' . __('Threshold') . ': ' . $balanceRaw['limit'];
  30. $type = 'success';
  31. if ($balance < 3000) {
  32. $type = 'warning';
  33. }
  34. if ($balance < 0) {
  35. $type = 'error';
  36. }
  37. $result .= $this->messages->getStyledMessage(__('Balance') . ': ' . $balance, $type);
  38. } else {
  39. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . implode(' ', $balanceRaw), 'error');
  40. }
  41. } else {
  42. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . implode(' ', $balanceRaw), 'error');
  43. }
  44. } else {
  45. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Empty reply received'), 'error');
  46. }
  47. $result .= wf_delimiter();
  48. $result .= wf_BackLink(self::URL_ME);
  49. return($result);
  50. }
  51. /**
  52. * Sends all messages from queue
  53. *
  54. * @return void
  55. */
  56. public function pushMessages() {
  57. $allSmsQueue = $this->smsQueue->getQueueData();
  58. if (!empty($allSmsQueue)) {
  59. require_once (self::VENDOR_LIB);
  60. $sign = $this->safeEscapeString($this->settings['NOTIFICORE_SIGN']);
  61. $api = new Notificore($this->settings['NOTIFICORE_APIKEY'], $sign);
  62. $smsClient = $api->getSmsClient();
  63. //processing messages queue
  64. foreach ($allSmsQueue as $eachSms) {
  65. if (!empty($eachSms['number']) AND ! empty($eachSms['message'])) {
  66. $recipient = $eachSms['number'];
  67. $text = $eachSms['message'];
  68. $reference = 'ubsms' . time() . str_replace('.', '', microtime(true));
  69. $sendingResult = $smsClient->sendSms($recipient, $text, $reference);
  70. if (!empty($sendingResult)) {
  71. if (isset($sendingResult['result']['error'])) {
  72. if (empty($sendingResult['result']['error'])) {
  73. //Message sent. Now we can delete it from queue.
  74. $this->smsQueue->deleteSms($eachSms['filename']);
  75. //success debug logging?
  76. $this->putLog('MESSAGE SENDING SUCCESS: ' . print_r($sendingResult, true));
  77. } else {
  78. $this->putLog('MESSAGE SENDING FAILED: ' . print_r($sendingResult, true));
  79. }
  80. } else {
  81. $this->putLog($sendingResult);
  82. }
  83. } else {
  84. //something went wrong
  85. $this->putLog('EMPTY SENDING RESULT RECEIVED');
  86. }
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * Loads config from database
  93. *
  94. * @return void
  95. */
  96. public function loadConfig() {
  97. $apikey = zb_StorageGet('SENDDOG_NOTIFICORE_APIKEY');
  98. if (empty($apikey)) {
  99. $apikey = 'yourapikey';
  100. zb_StorageSet('SENDDOG_NOTIFICORE_APIKEY', $apikey);
  101. }
  102. $smssign = zb_StorageGet('SENDDOG_NOTIFICORE_SIGN');
  103. if (empty($smssign)) {
  104. $smssign = 'Alphaname';
  105. zb_StorageSet('SENDDOG_NOTIFICORE_SIGN', $smssign);
  106. }
  107. $this->settings['NOTIFICORE_APIKEY'] = $apikey;
  108. $this->settings['NOTIFICORE_SIGN'] = $smssign;
  109. }
  110. /**
  111. * Return set of inputs, required for service configuration
  112. *
  113. * @return string
  114. */
  115. public function renderConfigInputs() {
  116. $miscInfo = wf_Link(self::URL_ME . '&showmisc=notificoresms', wf_img_sized('skins/icon_dollar.gif', __('Balance'), 10), true);
  117. $inputs = wf_tag('h2') . __('Notificore') . ' ' . $miscInfo . wf_tag('h2', true);
  118. $inputs .= wf_HiddenInput('editconfig', 'true');
  119. $inputs .= wf_TextInput('editnotificoreapikey', __('API key') . ' ' . __('Notificore'), $this->settings['NOTIFICORE_APIKEY'], true, 35);
  120. $inputs .= wf_TextInput('editnotificoresign', __('Notificore') . ' ' . __('Sign'), $this->settings['NOTIFICORE_SIGN'], true, 20);
  121. $smsServiceFlag = ($this->settings['SMS_SERVICE'] == 'notificoresms') ? true : false;
  122. $inputs .= wf_RadioInput('defaultsmsservice', __('Use') . ' ' . __('Notificore') . ' ' . __('as default SMS service'), 'notificoresms', true, $smsServiceFlag);
  123. return ($inputs);
  124. }
  125. /**
  126. * Saves service settings to database
  127. *
  128. * @return void
  129. */
  130. public function saveSettings() {
  131. if (ubRouting::post('editnotificoreapikey') != $this->settings['NOTIFICORE_APIKEY']) {
  132. zb_StorageSet('SENDDOG_NOTIFICORE_APIKEY', ubRouting::post('editnotificoreapikey'));
  133. log_register('SENDDOG CONFIG SET NOTIFICOREAPIKEY `' . ubRouting::post('editnotificoreapikey') . '`');
  134. }
  135. if (ubRouting::post('editnotificoresign') != $this->settings['NOTIFICORE_SIGN']) {
  136. zb_StorageSet('SENDDOG_NOTIFICORE_SIGN', ubRouting::post('editnotificoresign'));
  137. log_register('SENDDOG CONFIG SET NOTIFICORESIGN `' . ubRouting::post('editnotificoresign') . '`');
  138. }
  139. }
  140. /**
  141. * Writes some to log
  142. *
  143. * @param mixed $data
  144. *
  145. * @return void
  146. */
  147. protected function putLog($data) {
  148. $time = curdatetime();
  149. if (is_array($data)) {
  150. $data = print_r($data, true);
  151. }
  152. $logData = $time . ' ' . $data . PHP_EOL;
  153. file_put_contents(self::LOG_PATH, $logData, FILE_APPEND);
  154. }
  155. }