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