2
0

sms_hook_common.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /* $Id$ */
  3. /*
  4. call.php
  5. Copyright (C) 2008, 2009 Mark J Crane
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. 1. Redistributions of source code must retain the above copyright notice,
  10. this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  16. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  17. AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  18. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. Contributor(s):
  25. Mark J Crane <[email protected]>
  26. James Rose <[email protected]>
  27. */
  28. //includes files
  29. require_once dirname(__DIR__, 2) . "/resources/require.php";
  30. include "app/sms/sms_email.php";
  31. //luarun /var/www/html/app/sms/sms.lua TO FROM 'BODY'
  32. $debug = true;
  33. function route_and_send_sms($from, $to, $body, $media = "") {
  34. global $db, $debug, $domain_uuid, $domain_name, $mailsent;
  35. //create the event socket connection and send the event socket command
  36. $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  37. if (!$fp) {
  38. //error message
  39. echo "<div align='center'><strong>Connection to Event Socket failed.</strong></div>";
  40. }
  41. else {
  42. $mailsent = false;
  43. $to = intval(preg_replace('/(^[1])/','', $to));
  44. $from = intval($from);
  45. $body = preg_replace('([\'])', '\\\'', $body); // escape apostrophes
  46. if ($debug) {
  47. error_log("TO: " . print_r($to,true));
  48. error_log("FROM: " . print_r($from,true));
  49. error_log("BODY: " . print_r($body,true));
  50. }
  51. $mailbody = $body;
  52. if (gettype($media)=="array") {
  53. if (empty($body)) {
  54. $body = "MMS message received, see email for attachment";
  55. }
  56. else {
  57. $body .= " (MMS message received, see email for attachment)";
  58. }
  59. if ($debug) {
  60. error_log("MMS message (media array present)");
  61. }
  62. }
  63. if ($debug) {
  64. error_log("BODY: " . print_r($body,true));
  65. }
  66. $body = preg_replace('([\n])', '<br>', $body); // escape newlines
  67. if ($debug) {
  68. error_log("BODY-revised: " . print_r($body,true));
  69. }
  70. // Check for chatplan_detail in sms_destinations table
  71. $sql = "select domain_name, ";
  72. $sql .= "chatplan_detail_data, ";
  73. $sql .= "v_sms_destinations.domain_uuid as domain_uuid ";
  74. $sql .= "from v_sms_destinations, ";
  75. $sql .= "v_domains ";
  76. $sql .= "where v_sms_destinations.domain_uuid = v_domains.domain_uuid";
  77. $sql .= " and destination like :to";
  78. $sql .= " and chatplan_detail_data <> ''";
  79. if ($debug) {
  80. error_log("SQL: " . print_r($sql,true));
  81. }
  82. $prep_statement = $db->prepare(check_sql($sql));
  83. $prep_statement->bindValue(':to', "%{$to}%");
  84. $prep_statement->execute();
  85. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  86. if (count($result) > 0) {
  87. foreach ($result as &$row) {
  88. $domain_name = $row["domain_name"];
  89. preg_match('/(\d{2,7})/',$row["chatplan_detail_data"],$match);
  90. $domain_uuid = $row["domain_uuid"];
  91. break; //limit to 1 row
  92. }
  93. }
  94. else { // Fall back to destinations table for backwards compatibility
  95. $sql = "select domain_name, ";
  96. $sql .= "dialplan_detail_data, ";
  97. $sql .= "v_domains.domain_uuid as domain_uuid ";
  98. $sql .= "from v_destinations, ";
  99. $sql .= "v_dialplan_details, ";
  100. $sql .= "v_domains ";
  101. $sql .= "where v_destinations.dialplan_uuid = v_dialplan_details.dialplan_uuid ";
  102. $sql .= "and v_destinations.domain_uuid = v_domains.domain_uuid";
  103. $sql .= " and destination_number like :to and dialplan_detail_type = 'transfer'";
  104. if ($debug) {
  105. error_log("SQL: " . print_r($sql,true));
  106. }
  107. $prep_statement = $db->prepare(check_sql($sql));
  108. $prep_statement->bindValue(':to', "%{$to}%");
  109. $prep_statement->execute();
  110. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  111. if (count($result) == 0) {
  112. error_log("Cannot find a destination: " . print_r($result,true));
  113. die("Invalid Destination");
  114. }
  115. foreach ($result as &$row) {
  116. $domain_name = $row["domain_name"];
  117. preg_match('/(\d{2,7})/',$row["dialplan_detail_data"],$match);
  118. $domain_uuid = $row["domain_uuid"];
  119. break; //limit to 1 row
  120. }
  121. }
  122. unset ($prep_statement);
  123. if ($debug) {
  124. error_log("SQL: " . print_r($sql,true));
  125. error_log("MATCH: " . print_r($match[0],true));
  126. error_log("DOMAIN_NAME: " . print_r($domain_name,true));
  127. error_log("DOMAIN_UUID: " . print_r($domain_uuid,true));
  128. }
  129. //load default and domain settings
  130. $_SESSION["domain_uuid"] = $domain_uuid;
  131. require_once "resources/classes/domains.php";
  132. $domain = new domains();
  133. $domain->set();
  134. if ($debug) {
  135. error_log("Email from: ". $_SESSION['email']['smtp_from']['text']);
  136. }
  137. $mailsent = send_sms_to_email($from, $to, $mailbody, $media);
  138. //check to see if we have a ring group or single extension
  139. $sql = "select destination_number ";
  140. $sql .= "from v_ring_groups, v_ring_group_destinations ";
  141. $sql .= "where v_ring_groups.ring_group_uuid = v_ring_group_destinations.ring_group_uuid ";
  142. $sql .= "and ring_group_extension = :extension ";
  143. $sql .= "and v_ring_groups.domain_uuid = :domain_uuid";
  144. $prep_statement = $db->prepare(check_sql($sql));
  145. $prep_statement->execute(array(':extension' => $match[0], ':domain_uuid' => $domain_uuid));
  146. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  147. if ($debug) {
  148. error_log("SQL: " . print_r($sql,true));
  149. error_log("RG RESULT: " . print_r($result,true));
  150. }
  151. //send sms via Lua script
  152. if (count($result)) { //ring group
  153. foreach ($result as &$row) {
  154. $switch_cmd = "api luarun app.lua sms inbound ";
  155. $switch_cmd .= $row['destination_number'] . "@" . $domain_name;
  156. $switch_cmd .= " " . $from . " '" . $body . "' " . $mailsent;
  157. if ($debug) {
  158. error_log(print_r($switch_cmd,true));
  159. }
  160. $result2 = trim(event_socket_request($fp, $switch_cmd));
  161. if ($debug) {
  162. error_log("RESULT: " . print_r($result2,true));
  163. }
  164. }
  165. } else { //single extension
  166. $switch_cmd = "api luarun app.lua sms inbound " . $match[0] . "@" . $domain_name . " " . $from . " '" . $body . "' " . $mailsent;
  167. if ($debug) {
  168. error_log(print_r($switch_cmd,true));
  169. }
  170. $result2 = trim(event_socket_request($fp, $switch_cmd));
  171. if ($debug) {
  172. error_log("RESULT: " . print_r($result2,true));
  173. }
  174. }
  175. unset ($prep_statement);
  176. }
  177. }
  178. ?>