You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Notification.class.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. class Notification {
  3. private $ctrl;
  4. private $config;
  5. private $mail;
  6. private $db;
  7. private $sms;
  8. private $wa;
  9. public $fromName;
  10. public $fromAddress;
  11. public $smsAuth;
  12. private $skebby;
  13. function __construct($ctrl=null, $SMTPDebug=0) {
  14. global $db, $mailer, $config; //App/boostrap.php
  15. $this->mail = $mailer;
  16. $this->ctrl = $ctrl;
  17. $this->config = $config;
  18. $this->db = $db;
  19. $this->smsAuth = null;
  20. $this->mail->SMTPDebug = $SMTPDebug;
  21. $this->mail->isSMTP();
  22. //$mail->mail->CharSet = 'UTF-8';
  23. $this->mail->Host = $this->config['settings']['smtp']['host'];
  24. $this->mail->SMTPAuth = true;
  25. $this->mail->Username = $this->config['settings']['smtp']['username'];
  26. $this->mail->Password = $this->config['settings']['smtp']['password'];
  27. $this->mail->SMTPSecure = $this->config['settings']['smtp']['secure'];
  28. $this->mail->Port = $this->config['settings']['smtp']['port'];
  29. $this->mail->isHTML(true);
  30. $this->fromAddress = $this->config['settings']['smtp']['from-address'];
  31. $this->fromName = $this->config['settings']['smtp']['from-name'];
  32. }
  33. public function sendEmail($to='', $subject='', $body='', $mailID='', $from=[], $attachs=[]) {
  34. $fromName = $this->fromName;
  35. $fromAddress = $this->fromAddress;
  36. if (!empty($from)) {
  37. if (isset($from['fromAddress']) && isset($from['fromName'])) {
  38. $fromName = $from['fromName'];
  39. $fromAddress = $from['fromAddress'];
  40. }
  41. }
  42. $this->mail->setFrom($fromAddress, $fromName);
  43. $this->mail->addAddress($to);
  44. if (is_array($attachs) && !empty($attachs)) {
  45. foreach($attachs as $attach) {
  46. $this->mail->addAttachment($attach['path'], $attach['name']);
  47. }
  48. }
  49. $this->mail->Subject = $subject;
  50. $this->mail->Body = $body;
  51. $this->mail->AltBody = $this->setHtmlToPlainText($body);
  52. $mailResult = 0;
  53. $mailErrorInfo = '';
  54. if ($this->mail->send()) {
  55. $mailResult = 1;
  56. } else {
  57. $mailErrorInfo = $this->mail->ErrorInfo;
  58. }
  59. $this->logEmail($mailID, $fromAddress, $to, $subject, $body, $mailResult, $mailErrorInfo);
  60. $this->mail->clearAddresses();
  61. $this->mail->clearAttachments();
  62. }
  63. public function loginSMS() {
  64. $endpoint = $this->config['settings']['skebby']['endpoint'];
  65. $username = $this->config['settings']['skebby']['username'];
  66. $password = $this->config['settings']['skebby']['password'];
  67. $sender = $this->config['settings']['skebby']['sender'];
  68. $this->skebby = new Skebby($username, $password, $endpoint, $sender);
  69. $this->smsAuth = $this->skebby->login();
  70. return $this->smsAuth;
  71. }
  72. public function sendSMS($message, $recipients=[]) {
  73. $result = $this->skebby->sendSMS($message, $recipients);
  74. $this->logSMS($message, $recipients, $result);
  75. }
  76. public function receiveSMS($idSim='', $limit=100, $auth=[]) {
  77. $result = $this->skebby->receiveSMS($idSim, $limit, $auth);
  78. return $result;
  79. }
  80. public function setNotificationAsSent($queueId=0) {
  81. return $this->db->where('id', $queueId)->update('requests_messages_queue', ['sent_status'=>1, 'sent_at'=>date('Y-m-d H:i:s')]);
  82. }
  83. public function setSurveyNotificationAsSent($queueId=0) {
  84. return $this->db->where('id', $queueId)->update('survey_queue', ['msg_sent'=>1, 'msg_sent_date'=>date('Y-m-d H:i:s')]);
  85. }
  86. private function setHtmlToPlainText($html='') {
  87. $breaks = array("<br />","<br>","<br/>");
  88. $html = str_ireplace($breaks, "\r\n", $html);
  89. return strip_tags($html);
  90. }
  91. private function logSMS($msg='', $recipients=[], $result=[]) {
  92. $id = uniqid();
  93. $user = [];
  94. if (empty($recipients)) {
  95. return false;
  96. } else {
  97. $recipients = !is_array($recipients) ? [$recipients] : $recipients;
  98. foreach($recipients as $recipient) {
  99. $number = str_replace('+', '', $recipient);
  100. $user = $this->db->where('mobile_number', $number)->getOne('users', 'id');
  101. $this->db->insert('log_notifications', [
  102. 'id' => $id,
  103. 'user_id' => isset($user['id']) ? $user['id'] : -1,
  104. 'notif_type' => 'sms',
  105. 'phone_to' => $recipient,
  106. 'phone_msg' => $msg,
  107. 'request_result' => isset($result['result']) && $result['result'] == 'OK' ? 1 : 0,
  108. 'request_error_info' => isset($result['result']) && $result['result'] == 'ERR' ? json_encode($result) : null,
  109. 'created_at' => date('Y-m-d H:i:s')
  110. ]);
  111. }
  112. }
  113. return $id;
  114. }
  115. private function logEmail($mailId='', $from='', $to='', $subject='', $body='', $result=0, $errorInfo='') {
  116. $user = $this->db->where('email', trim($to))->getOne('users', 'id');
  117. $this->db->insert('log_notifications', [
  118. 'id' => $mailId,
  119. 'user_id' => isset($user['id']) ? $user['id'] : -1,
  120. 'notif_type' => 'email',
  121. 'mail_from' => $from,
  122. 'mail_to' => $to,
  123. 'mail_subject' => $subject,
  124. 'mail_body' => $body,
  125. 'request_result' => $result,
  126. 'request_error_info' => $errorInfo,
  127. 'created_at' => date('Y-m-d H:i:s')
  128. ]);
  129. return $mailId;
  130. }
  131. }