AlphaSms.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. class AlphaSms extends SMSServiceApi {
  3. public function __construct($smsServiceId, $smsPack = array()) {
  4. parent::__construct($smsServiceId, $smsPack);
  5. }
  6. public function getBalance() {
  7. $balance = '';
  8. $balance = $this->execute('balance');
  9. $result = $this->instanceSendDog->getUBMsgHelperInstance()->getStyledMessage(__('Current account balance') . ': ' . $balance['balance'] . ' UAN', 'info');
  10. die(wf_modalAutoForm(__('Balance'), $result, $_POST['modalWindowId'], '', true, 'false', '700'));
  11. }
  12. public function getSMSQueue() {
  13. $this->showErrorFeatureIsNotSupported();
  14. }
  15. public function checkMessagesStatuses() {
  16. log_register('Checking statuses for [' . get_class($this) . '] SMS service is not implemented');
  17. }
  18. public function pushMessages() {
  19. $apikey = $this->serviceApiKey;
  20. $sender = $this->serviceAlphaName;
  21. $allSmsQueue = $this->smsMessagePack;
  22. if (!empty($allSmsQueue)) {
  23. foreach ($allSmsQueue as $sms) {
  24. $formattedPhone = $this->makePhoneFormat($sms['number']);
  25. $data = array('from' => urlencode($sender),
  26. 'to' => urlencode($formattedPhone),
  27. 'message'=> urlencode($sms['message']));
  28. $result = $this->execute('send', $data);
  29. //remove old sent message
  30. $this->instanceSendDog->getSmsQueueInstance()->deleteSms($sms['filename']);
  31. }
  32. }
  33. }
  34. protected function execute($command, $params = array()) {
  35. $params['login'] = $this->serviceLogin;
  36. $params['password'] = $this->servicePassword;
  37. $params['key'] = $this->serviceApiKey;
  38. $params['command'] = $command;
  39. $params_url = '';
  40. foreach($params as $key => $value) {
  41. $params_url .= '&' . $key . '=' . $this->base64_url_encode($value);
  42. }
  43. //cURL HTTPS POST
  44. $ch = curl_init($this->serviceGatewayAddr);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  46. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  47. curl_setopt($ch, CURLOPT_POST, count($params));
  48. curl_setopt($ch, CURLOPT_POSTFIELDS, $params_url);
  49. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  50. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  51. $response = @curl_exec($ch);
  52. curl_close($ch);
  53. $this->response = @unserialize($this->base64_url_decode($response));
  54. return $this->response;
  55. }
  56. protected function base64_url_encode($input) {
  57. return strtr(base64_encode($input), '+/=', '-_,');
  58. }
  59. protected function base64_url_decode($input) {
  60. return base64_decode(strtr($input, '-_,', '+/='));
  61. }
  62. /**
  63. * As AlphaSMS needs phone numbers to be only in 38 0YY XXX XX XX format
  64. * this function will try to make the phone number suitable
  65. *
  66. * @param $phoneNumber string
  67. *
  68. * @return string
  69. */
  70. protected function makePhoneFormat($phoneNumber) {
  71. $phoneNumber = str_replace('+', '', $phoneNumber);
  72. if ( strlen($phoneNumber) == 10 ) {
  73. if (substr($phoneNumber, 0, 1) == '0') {
  74. $phoneNumber = '8' . $phoneNumber;
  75. }
  76. if (substr($phoneNumber, 0, 1) == '8') {
  77. $phoneNumber = '3' . $phoneNumber;
  78. }
  79. }
  80. return $phoneNumber;
  81. }
  82. }
  83. ?>