bulksms.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * bulksms.md SMS service implementation
  4. * API documentation: http://store.nightfly.biz/bulksmsdocumentatiedeintegrare_2021.docx
  5. */
  6. class bulksms extends SendDogProto {
  7. /**
  8. * Option name that enabled record of service debug log
  9. */
  10. const DEBUG_OPTION = 'SENDDOG_BULKSMS_DEBUG';
  11. /**
  12. * Default debug log path
  13. */
  14. const DEBUGLOG_PATH = 'exports/bulksmsdebug.log';
  15. /**
  16. * Sends all messages from queue using bulksms.md service
  17. *
  18. * @return void
  19. */
  20. public function bulkSmsPushMessages() {
  21. $allSmsQueue = $this->smsQueue->getQueueData();
  22. if (!empty($allSmsQueue)) {
  23. //basic API callback URL
  24. $apiCallback = $this->settings['BULKSMS_GATEWAY'] . '/UnifunBulkSMSAPI.asmx/SendSMSSimple';
  25. //creating service API handler
  26. $bulkSmsApi = new OmaeUrl($apiCallback);
  27. //setting request wait timeout the same as inter-message
  28. if ($this->settings['BULKSMS_TIMEOUT']) {
  29. $bulkSmsApi->setTimeout($this->settings['BULKSMS_TIMEOUT']);
  30. }
  31. //processing messages queue
  32. foreach ($allSmsQueue as $eachSms) {
  33. if (!empty($eachSms['number']) AND ! empty($eachSms['message'])) {
  34. //appending credentials and SMS content data
  35. $sendToNumber = $eachSms['number']; //may be some postprocessing will be required here
  36. $sendToNumber = str_replace('+', '', $sendToNumber); // service accepts phones only without + symbol
  37. $messageTextEncoded = urlencode($eachSms['message']); //just urlencoded message
  38. $bulkSmsApi->dataGet('username', $this->settings['BULKSMS_LOGIN']);
  39. $bulkSmsApi->dataGet('password', $this->settings['BULKSMS_PASSWORD']);
  40. $bulkSmsApi->dataGet('from', $this->settings['BULKSMS_SIGN']);
  41. $bulkSmsApi->dataGet('to', $sendToNumber);
  42. $bulkSmsApi->dataGet('text', $messageTextEncoded);
  43. $sendResult = $bulkSmsApi->response(); //push request
  44. //debug log is here and optional
  45. if ($this->ubConfig->getAlterParam(self::DEBUG_OPTION)) {
  46. file_put_contents(self::DEBUGLOG_PATH, curdatetime() . PHP_EOL, FILE_APPEND);
  47. file_put_contents(self::DEBUGLOG_PATH, '===============' . PHP_EOL, FILE_APPEND);
  48. file_put_contents(self::DEBUGLOG_PATH, $sendResult . PHP_EOL, FILE_APPEND);
  49. file_put_contents(self::DEBUGLOG_PATH, 'HTTP Code: ' . print_r($bulkSmsApi->httpCode(), true) . PHP_EOL, FILE_APPEND);
  50. file_put_contents(self::DEBUGLOG_PATH, 'Errors: ' . print_r($bulkSmsApi->error(), true) . PHP_EOL, FILE_APPEND);
  51. file_put_contents(self::DEBUGLOG_PATH, 'Request info: ' . print_r($bulkSmsApi->lastRequestInfo(), true) . PHP_EOL, FILE_APPEND);
  52. file_put_contents(self::DEBUGLOG_PATH, '===============' . PHP_EOL, FILE_APPEND);
  53. }
  54. $bulkSmsApi->dataGet(); //flush get data for next request
  55. //remove already sent message
  56. $this->smsQueue->deleteSms($eachSms['filename']);
  57. //optional timeout
  58. if ($this->settings['BULKSMS_TIMEOUT']) {
  59. sleep($this->settings['BULKSMS_TIMEOUT']);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Return set of inputs, required for BulkSMS service configuration
  67. *
  68. * @return string
  69. */
  70. public function renderBulksmsConfigInputs() {
  71. $inputs = wf_tag('h2') . __('BulkSMS.md') . wf_tag('h2', true);
  72. $inputs .= wf_HiddenInput('editconfig', 'true');
  73. $inputs .= wf_TextInput('editbulksmsgateway', __('API address') . ' ' . __('BulkSMS'), $this->settings['BULKSMS_GATEWAY'], true, 30);
  74. $inputs .= wf_TextInput('editbulksmslogin', __('User login to access API') . ' ' . ('BulkSMS'), $this->settings['BULKSMS_LOGIN'], true, 20);
  75. $inputs .= wf_TextInput('editbulksmspassword', __('User password for access API') . ' ' . __('BulkSMS'), $this->settings['BULKSMS_PASSWORD'], true, 20);
  76. $inputs .= wf_TextInput('editbulksmssign', __('BulkSMS') . ' ' . __('Sign'), $this->settings['BULKSMS_SIGN'], true, 20);
  77. $inputs .= wf_TextInput('editbulksmstimeout', __('BulkSMS') . ' ' . __('Timeout') . ' (' . __('sec.') . ')', $this->settings['BULKSMS_TIMEOUT'], true, 2, 'digits');
  78. $smsServiceFlag = ($this->settings['SMS_SERVICE'] == 'bulksms') ? true : false;
  79. $inputs .= wf_RadioInput('defaultsmsservice', __('Use') . ' ' . __('BulkSMS') . ' ' . __('as default SMS service'), 'bulksms', true, $smsServiceFlag);
  80. return ($inputs);
  81. }
  82. /**
  83. * Loads BulkSMS config
  84. *
  85. * @return void
  86. */
  87. public function loadBulksmsConfig() {
  88. $smsgateway = zb_StorageGet('SENDDOG_BULKSMS_GATEWAY');
  89. if (empty($smsgateway)) {
  90. $smsgateway = 'https://api.bulksms.md:4432';
  91. zb_StorageSet('SENDDOG_BULKSMS_GATEWAY', $smsgateway);
  92. }
  93. $smslogin = zb_StorageGet('SENDDOG_BULKSMS_LOGIN');
  94. if (empty($smslogin)) {
  95. $smslogin = 'yourlogin';
  96. zb_StorageSet('SENDDOG_BULKSMS_LOGIN', $smslogin);
  97. }
  98. $smspassword = zb_StorageGet('SENDDOG_BULKSMS_PASSWORD');
  99. if (empty($smspassword)) {
  100. $smspassword = 'yourpassword';
  101. zb_StorageSet('SENDDOG_BULKSMS_PASSWORD', $smspassword);
  102. }
  103. $smssign = zb_StorageGet('SENDDOG_BULKSMS_SIGN');
  104. if (empty($smssign)) {
  105. $smssign = 'Alphaname';
  106. zb_StorageSet('SENDDOG_BULKSMS_SIGN', $smssign);
  107. }
  108. $smstimeout = zb_StorageGet('SENDDOG_BULKSMS_TIMEOUT');
  109. if ($smstimeout == '') {
  110. $smstimeout = 7;
  111. zb_StorageSet('SENDDOG_BULKSMS_TIMEOUT', $smstimeout);
  112. }
  113. $this->settings['BULKSMS_GATEWAY'] = $smsgateway;
  114. $this->settings['BULKSMS_LOGIN'] = $smslogin;
  115. $this->settings['BULKSMS_PASSWORD'] = $smspassword;
  116. $this->settings['BULKSMS_SIGN'] = $smssign;
  117. $this->settings['BULKSMS_TIMEOUT'] = $smstimeout;
  118. }
  119. /**
  120. * Saves service settings to database
  121. *
  122. * @return void
  123. */
  124. public function saveSettings() {
  125. if (ubRouting::post('editbulksmsgateway') != $this->settings['BULKSMS_GATEWAY']) {
  126. zb_StorageSet('SENDDOG_BULKSMS_GATEWAY', ubRouting::post('editbulksmsgateway'));
  127. log_register('SENDDOG CONFIG SET BULKSMSGATEWAY `' . ubRouting::post('editbulksmsgateway') . '`');
  128. }
  129. if (ubRouting::post('editbulksmslogin') != $this->settings['BULKSMS_LOGIN']) {
  130. zb_StorageSet('SENDDOG_BULKSMS_LOGIN', ubRouting::post('editbulksmslogin'));
  131. log_register('SENDDOG CONFIG SET BULKSMSLOGIN `' . ubRouting::post('editbulksmslogin') . '`');
  132. }
  133. if (ubRouting::post('editbulksmspassword') != $this->settings['BULKSMS_PASSWORD']) {
  134. zb_StorageSet('SENDDOG_BULKSMS_PASSWORD', ubRouting::post('editbulksmspassword'));
  135. log_register('SENDDOG CONFIG SET BULKSMSPASSWORD `' . ubRouting::post('editbulksmspassword') . '`');
  136. }
  137. if (ubRouting::post('editbulksmssign') != $this->settings['BULKSMS_SIGN']) {
  138. zb_StorageSet('SENDDOG_BULKSMS_SIGN', ubRouting::post('editbulksmssign'));
  139. log_register('SENDDOG CONFIG SET BULKSMSSIGN `' . ubRouting::post('editbulksmssign') . '`');
  140. }
  141. if (ubRouting::post('editbulksmstimeout') != $this->settings['BULKSMS_TIMEOUT']) {
  142. zb_StorageSet('SENDDOG_BULKSMS_TIMEOUT', ubRouting::post('editbulksmstimeout'));
  143. log_register('SENDDOG CONFIG SET BULKSMSTIMEOUT `' . ubRouting::post('editbulksmstimeout') . '`');
  144. }
  145. }
  146. }