sms_email.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2012
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. Jonathan Black <[email protected]>
  21. */
  22. //Regex from https://emailregex.com
  23. function validateEMAIL($EMAIL) {
  24. $v = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';
  25. return (bool)preg_match($v, $EMAIL);
  26. }
  27. function send_sms_to_email($from, $to, $body, $media = null) {
  28. global $db, $debug, $domain_uuid, $domain_name, $carrier;
  29. if ($debug) {
  30. error_log('Media: ' . print_r($media, true));
  31. }
  32. //$domain_name = string.match(to,'%@+(.+)');
  33. if (preg_match('/@+(.+)/',$to,$matches)) {
  34. $domain_name = $matches[1];
  35. }
  36. //get email address from db
  37. // Check for email address in sms_destinations table
  38. $sql = "select domain_name, ";
  39. $sql .= "email, ";
  40. $sql .= "v_sms_destinations.domain_uuid as domain_uuid, ";
  41. $sql .= "carrier ";
  42. $sql .= "from v_sms_destinations, ";
  43. $sql .= "v_domains ";
  44. $sql .= "where v_sms_destinations.domain_uuid = v_domains.domain_uuid";
  45. $sql .= " and destination like :to";
  46. // $sql .= " and chatplan_detail_data <> ''"; //uncomment to disable email-only
  47. if ($debug) {
  48. error_log("SQL: " . print_r($sql,true));
  49. }
  50. $prep_statement = $db->prepare(check_sql($sql));
  51. $prep_statement->bindValue(':to', "%{$to}%");
  52. $prep_statement->execute();
  53. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  54. if (count($result) > 0) {
  55. foreach ($result as &$row) {
  56. $domain_name = $row["domain_name"];
  57. $email_to = $row["email"];
  58. $domain_uuid = $row["domain_uuid"];
  59. $carrier = $row["carrier"];
  60. break; //limit to 1 row
  61. }
  62. }
  63. //error_log('to: ' . $to);
  64. //error_log($email_to);
  65. if (empty($email_to)) {
  66. error_log("[sms] email address is empty, cannot send sms to email.");
  67. return false;
  68. }
  69. else {
  70. //set email values
  71. $email_subject = 'Text Message from: ' . $from;
  72. $semi_rand = md5(time());
  73. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  74. if (!empty($_SESSION['email']['smtp_from']['text']) and validateEMAIL($_SESSION['email']['smtp_from']['text'])) {
  75. $headers = "From: " . $_SESSION['email']['smtp_from']['text'] . "\n";
  76. }
  77. else {
  78. $headers = "From: [email protected]\n";
  79. }
  80. if ($debug) {
  81. error_log("Email Sender: " . $headers);
  82. }
  83. $headers .= "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed; " . "boundary=\"{$mime_boundary}\"";
  84. $body = urldecode($body);
  85. $body = preg_replace('([\n])', '<br>', $body); // fix newlines
  86. $email_txt = 'To: ' . $to . '<br>Msg: ' . $body;
  87. $email_message = "" . $email_txt . "";
  88. if ($carrier == "telnyx") {
  89. if (gettype($media)=="array") {
  90. $email_txt = 'To: ' . $to . '<br>Msg: ' . $body . '<br>MMS Message received, see attachment';
  91. $email_message = "" . $email_txt . "";
  92. //process MMS attachment
  93. foreach ($media as $attachment) {
  94. $url = $attachment->url;
  95. $start = strrpos($url, '/') == -1 ? strrpos($url, '//') : strrpos($url, '/')+1;
  96. $fileatt_name = substr($url, $start, strlen($url)); // Filename that will be used for the file as the attachment
  97. if (!empty($_SESSION['sms']['mms_attachment_temp_path']['text'])) {
  98. $fileatt = $_SESSION['sms']['mms_attachment_temp_path']['text'];
  99. if (substr($fileatt, -1) != '/') {
  100. $fileatt .= '/';
  101. }
  102. $fileatt .= $fileatt_name;
  103. }
  104. else {
  105. $fileatt = '/var/www/fusionpbx/app/sms/tmp/' . $fileatt_name; // Path to the file
  106. }
  107. // download attachment
  108. file_put_contents($fileatt, fopen($url, 'r'));
  109. //$fileatt_type = "application/octet-stream"; // File Type
  110. $fileatt_type = $attachment->content_type; // File Type
  111. //$start = strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
  112. $file = fopen($fileatt,'rb');
  113. $attdata = fread($file,filesize($fileatt));
  114. fclose($file);
  115. $attdata = chunk_split(base64_encode($attdata));
  116. $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" .
  117. " name = \"{$fileatt_name}\"\n" . "Content-Disposition: inline;\n" . " filename = \"{$fileatt_name}\"\n" .
  118. "Content-Transfer-Encoding:base64\n\n" . $attdata . "\n\n" . "--{$mime_boundary}--\n";
  119. error_log("email_message: " . $email_message);
  120. unlink($fileatt); // delete a file after attachment sent.
  121. }
  122. }
  123. }
  124. else {
  125. $email_message .= "";
  126. }
  127. if ($debug) {
  128. error_log("headers: " . $headers);
  129. }
  130. //send email
  131. $ok = send_email($email_to, $email_subject, $email_message);//, $headers);
  132. if ($ok) {
  133. error_log("[sms] Email Sent Successfully.");
  134. return true;
  135. } else {
  136. error_log("[sms] Email could not be sent.");
  137. return false;
  138. }
  139. }
  140. }
  141. ?>