forked from FreePBX-ContributedModules/googlevoice
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.inc.php
408 lines (339 loc) · 13.4 KB
/
functions.inc.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/*
* Copyright (c) 2011 Marcus Brown <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
global $amp_conf;
// Do we run FreePBX 2.8?
if (file_exists($amp_conf['AMPWEBROOT'] . "/admin/extensions.class.php")) {
// Yes, then include these files
//I need to know if we really need these files. Questionable.
require_once("functions.inc.php");
require_once("extensions.class.php");
}
function googlevoice_hookGet_config($engine) {
global $ext;
//Hook into FreePBX config generator but check to see if we are running FreePBX 2.10
//In this release the dev team already added SendDTMF
if (!method_exists($ext, 'ext_senddtmf') && (!class_exists('ext_senddtmf'))) {
class ext_senddtmf extends extension {
var $digits;
function ext_senddtmf($digits) {
$this->digits = $digits;
}
function output() {
return 'SendDTMF(' . $this->digits . ')';
}
}
}
}
class googlevoice_conf {
function get_filename() {
$files = array(
'jabber.conf',
'gtalk.conf',
'extensions_additional.conf'
);
return $files;
}
function generateConf($file) {
global $version,$amp_conf;
if(!file_exists($amp_conf['ASTETCDIR'] . '/jabber_custom.conf')) {
touch($amp_conf['ASTETCDIR'] . '/jabber_custom.conf');
}
if(!file_exists($amp_conf['ASTETCDIR'] . '/gtalk_custom.conf')) {
touch($amp_conf['ASTETCDIR'] . '/jabber_custom.conf');
}
if(file_exists($amp_conf['ASTETCDIR'] . '/jabber.conf') && !file_exists($amp_conf['ASTETCDIR'] . '/jabber.conf.bak')) {
copy($amp_conf['ASTETCDIR'] . '/jabber.conf', $amp_conf['ASTETCDIR'] . '/jabber.conf.bak');
}
if(file_exists($amp_conf['ASTETCDIR'] . '/gtalk.conf') && !file_exists($amp_conf['ASTETCDIR'] . '/gtalk.conf.bak')) {
copy($amp_conf['ASTETCDIR'] . '/gtalk.conf', $amp_conf['ASTETCDIR'] . '/gtalk.conf.bak');
}
switch ($file) {
case 'jabber.conf':
return $this->generate_jabber_conf($version);
break;
case 'gtalk.conf':
return $this->generate_gtalk_conf($version);
break;
case 'extensions_additional.conf':
return $this->generate_extensions_conf($version);
break;
}
}
function generate_jabber_conf($ast_version) {
global $astman;
/*
$this->use_warning_banner = true;
*/
$output = "[general]\ndebug=no\nautoprune=no\nautoregister=yes\n\n";
$accounts = googlevoice_list();
foreach ($accounts as $account) {
$phonenum = $account[0];
$full_address = explode('@', $account[1]);
if (array_key_exists(1, $full_address)) {
$username = str_replace('.', '', $account[1]);
$username = str_replace('@', '', $username);
$address = $full_address[0] . '@' . $full_address[1];
} else {
$username = $account[1];
$address = $account[1] . '@gmail.com';
}
$password = $account[2];
$output .= "[" . $username . "]\ntype=client\nserverhost=talk.google.com\n";
$output .= "username=" . $address . "/Talk\nsecret=" . $password . "\n";
$output .= "port=5222\npriority=1\nusetls=yes\nusesasl=yes\nstatus=Available\n";
$output .= "statusmessage=\"No Information Available\"\n";
$output .= "timeout=100\nkeepalive=yes\n\n#include jabber_custom.conf\n\n";
}
$response = $astman->send_request('Command', array('Command' => 'module unload res_jabber.so'));
$response = $astman->send_request('Command', array('Command' => 'module load res_jabber.so'));
return $output;
}
function generate_gtalk_conf($ast_version) {
global $astman;
/*
$this->use_warning_banner = true;
*/
$output = "[general]\nallowguest=yes\ncontext=googlein\n\n";
$output .= "[guest]\ndisallow=all\nallow=ulaw\nconnection=asterisk\ncontext=googlein\n\n#include gtalk_custom.conf\n\n";
$response = $astman->send_request('Command', array('Command' => 'module unload chan_gtalk.so'));
$response = $astman->send_request('Command', array('Command' => 'module load chan_gtalk.so'));
return $output;
}
function generate_extensions_conf($ast_version) {
global $ext;
$accounts = googlevoice_list();
foreach ($accounts as $account) {
$incontext = "googlein";
$phonenum = $account[0];
$full_address = explode('@', $account[1]);
if (array_key_exists(1, $full_address)) {
$username = str_replace('.', '', $account[1]);
$username = str_replace('@', '', $username);
$address = $full_address[0] . '@' . $full_address[1];
} else {
$username = $account[1];
$address = $account[1] . '@gmail.com';
}
$outcontext = 'googlevoice-' . $username;
/* INBOUND CONTEXT */
$ext->add($incontext, $address, '', new ext_noop('Receiving GoogleVoice call'));
$ext->add($incontext, $address, '', new ext_setvar('CALLERID(name)', '${CUT(CALLERID(name),@,1)}'));
$ext->add($incontext, $address, '', new ext_gotoif('$["${CALLERID(name):0:2}" != "+1"]', 'nextstop'));
$ext->add($incontext, $address, '', new ext_setvar('CALLERID(name)', '${CALLERID(name):2}'));
$ext->add($incontext, $address, 'nextstop', new ext_gotoif('$["${CALLERID(name):0:1}" != "+"]', 'notrim'));
$ext->add($incontext, $address, '', new ext_setvar('CALLERID(name)', '${CALLERID(name):1}'));
$ext->add($incontext, $address, 'notrim', new ext_setvar('CALLERID(number)', '${CALLERID(name)}'));
if (true) {
$ext->add($incontext, $address, '', new ext_answer(''));
$ext->add($incontext, $address, '', new ext_wait('1'));
$ext->add($incontext, $address, '', new ext_senddtmf('1'));
}
$ext->add($incontext, $address, '', new ext_goto('1', $phonenum, 'from-trunk'));
if (true) {
$ext->add($incontext, 'h', '', new ext_hangup(''));
}
$ext->add('from-google-voice', $username, '', new ext_goto('1', $phonenum, 'from-trunk'));
/* OUTBOUND CONTEXT */
$ext->add($outcontext, '_X.', '', new ext_dial('Gtalk/' . $username . '/+${EXTEN}@voice.google.com'));
$ext->add($outcontext, '_X.', '', new ext_noop('GoogleVoice Call to ${EXTEN} failed'));
$ext->add($outcontext, 'h', '', new ext_hangup(''));
}
$ext->add('from-google-voice', 'h', '', new ext_hangup(''));
needreload();
return $ext->generateConf();
}
}
function googlevoice_list() {
global $db;
$sql = "SELECT phonenum, username, password FROM googlevoice ORDER BY username";
$results = $db->getAll($sql);
if (DB::IsError($results)) {
$results = array();
}
foreach ($results as $result) {
$account[] = array($result[0], $result[1], $result[2]);
}
if (isset($account)) {
return $account;
}
return array();
}
function googlevoice_add($phonenum, $username, $password, $add_trunk, $add_routes) {
if (($phonenum == "") || ($username == "") || ($password == "")) {
return array();
}
$result = googlevoice_getnum($phonenum);
if ($result) {
return false;
}
$query = "INSERT INTO googlevoice (phonenum, username, password)
VALUES ('" . $phonenum . "', '" . $username . "', '" . $password . "')";
$result = sql($query);
if (DB::IsError($result)) {
return false;
}
$full_address = explode('@', $username);
if (array_key_exists(1, $full_address)) {
$username = str_replace('.', '', $username);
$username = str_replace('@', '', $username);
} else {
$username = $username;
}
$trunknum = false;
if ($add_trunk) {
/* TRUNK */
$trunknum = core_trunks_add('custom', 'local/$OUTNUM$@googlevoice-' . $username, '', '', $phonenum, '', 'notneeded', '', '', 'off', '', 'off', 'GV_' . $phonenum, '');
dbug_write('Trunknum = ' . $trunknum . '
', '');
$patterns[] = array(
'prepend_digits' => '1',
'match_pattern_prefix' => '',
'match_pattern_pass' => 'NXXNXXXXXX',
'match_cid' => '',
);
core_trunks_update_dialrules($trunknum, $patterns);
if ($add_routes) {
/* OUTBOUND ROUTE */
dbug_write('Adding routes
', '');
$trunkroutes = core_trunks_gettrunkroutes($trunknum);
if (!empty($trunkroutes)) {
dbug_write('Trunk-routes already exist?!?!?!?!', '');
return;
}
$dialpattern[] = array(
'prepend_digits' => '1',
'match_pattern_prefix' => '',
'match_pattern_pass' => 'NXXNXXXXXX',
'match_cid' => '',
);
$result = core_routing_addbyid($username, '', '', '', '', '', 'default', '', $dialpattern, array($trunknum));
/*
// INBOUND ROUTE
$did[] = array(
'cidnum' => '',
'extension' => $phonenum,
'destination' => '',
'privacyman' => '',
'pmmaxretries' => '',
'pmminlength' => '',
'alertinfo' => '',
'ringing' => '',
'mohclass' => 'default',
'description' => $username,
'grppre' => '',
'delay_answer' => '',
'pricid' => '',
);
$result = core_did_add($did)
*/
}
}
return true;
}
function googlevoice_getuser($username) {
global $db;
if ($username == "") {
return false;
}
$sql = "SELECT phonenum, username, password FROM googlevoice
WHERE username = ?";
$params = $username;
$result = $db->getAssoc($sql, false, $params);
if (DB::IsError($result) || !is_array($result)) {
return false;
}
if (is_array($result) && ($result[$phonenum] == '')) {
return false;
}
return $result;
}
function googlevoice_getnum($phonenum) {
global $db;
if ($phonenum == "") {
return false;
}
$sql = "SELECT phonenum, username, password FROM googlevoice
WHERE phonenum = ?";
$params = $phonenum;
$result = $db->getAssoc($sql, false, $params);
if (DB::IsError($result) || !is_array($result)) {
return false;
}
return $result;
}
function googlevoice_del($phonenum) {
if (empty($phonenum)) {
return false;
}
$username = false;
$result = googlevoice_getnum($phonenum);
if ($result) {
$username = $result[$phonenum][0];
} else {
return false;
}
$query = "DELETE FROM googlevoice
WHERE phonenum = '" . $phonenum . "'";
$result = sql($query);
if (DB::IsError($result)) {
return false;
}
$trunknum = googlevoice_trunkID($phonenum);
if ($username && $trunknum) {
core_routing_trunk_delbyid($trunknum); // Not sure if this is necessary
googlevoice_del_routes($trunknum, $username);
core_trunks_del($trunknum);
core_trunks_delete_dialrules($trunknum);
}
return true;
}
function googlevoice_update($phonenum, $username, $password) {
global $db;
if (empty($phonenum)) {
return array();
}
$sql = "UPDATE googlevoice SET username = '" . $username . "', password = '" . $password . "' WHERE phonenum = '" . $phonenum . "'";
$params = array($username, $password, $phonenum);
$result = $db->query($sql);
if (DB::IsError($result) || empty($result)) {
return false;
}
return true;
}
function googlevoice_trunkID($phonenum) {
$trunknum = '';
foreach (core_trunks_list() as $trunk) {
$trunknum = ltrim($trunk[0], "OUT_");
$details = core_trunks_getDetails($trunknum);
if (($details['tech'] == 'custom') && ($details['name'] = 'GV_' . $phonenum)) {
return $trunknum;
}
}
return false;
}
function googlevoice_del_routes($trunkID, $username) {
foreach (core_routing_list() as $routeID) {
if ($routeID['name'] == $username) {
core_routing_delbyid($routeID['route_id']);
}
}
return;
}