-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
757 lines (664 loc) · 27.2 KB
/
index.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
<?php
//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";
require_once "resources/functions.php";
require_once "resources/functions/message_media_builder.php";
require_once "resources/pdo.php";
//connect to the database
$database = database::new();
//initialize database and settings
$settings = new settings(['database' => $database, $_SESSION['domain_uuid'] ?? '']);
//debug
$debug = $settings->get('message','debug', false);
//log file
$log_file = '/tmp/message.log';
//write the remote address to the log
if ($debug) {
file_put_contents($log_file, "Remote Address: ".$_SERVER['REMOTE_ADDR']."\n", FILE_APPEND);
}
//get the provider addresses
$sql = "select provider_uuid, provider_address_cidr ";
$sql .= "from v_provider_addresses ";
$sql .= "where provider_address_cidr is not null ";
$sql .= "and provider_address_enabled = true ";
$parameters = null;
$provider_addresses = $database->select($sql, $parameters, 'all');
//default authorized to false
$authorized = false;
//use the ip address to get the provider uuid and determine if request is authorized
foreach($provider_addresses as $row) {
if (check_cidr($row['provider_address_cidr'], $_SERVER['REMOTE_ADDR'])) {
$provider_uuid = $row['provider_uuid'];
$authorized = true;
break;
}
}
//authorization failed
if ($authorized) {
if ($debug) {
file_put_contents($log_file, "authorized\n", FILE_APPEND);
file_put_contents($log_file, "provider_uuid ".$provider_uuid."\n", FILE_APPEND);
}
}
else {
//log the failed auth attempt to the system, to be available for fail2ban.
if ($debug) {
file_put_contents($log_file, "unauthorized\n", FILE_APPEND);
}
openlog('FusionPBX', LOG_NDELAY, LOG_AUTH);
syslog(LOG_WARNING, '['.$_SERVER['REMOTE_ADDR']."] authentication failed for ".($_GET['key'] ?? ''));
closelog();
//send http 403
header("HTTP/1.0 403 Forbidden");
echo "Forbidden\n";
exit();
}
//check if string is url encoded
function is_urlencoded($string) {
$urlencoded = preg_match('~%[0-9A-F]{2}~i', $string);
if ($urlencoded) {
return true;
}
else {
return false;
}
}
//use the provider uuid to get the provider_settings
$sql = "select provider_setting_category, provider_setting_subcategory, \n";
$sql .= "provider_setting_name, provider_setting_value, provider_setting_order \n";
$sql .= "from v_provider_settings \n";
$sql .= "where provider_uuid = :provider_uuid \n";
$sql .= "and provider_setting_enabled = 'true' \n";
$sql .= "and provider_setting_category = 'inbound' \n";
$parameters['provider_uuid'] = $provider_uuid;
$provider_settings = $database->select($sql, $parameters, 'all');
foreach ($provider_settings as $row) {
if ($row['provider_setting_subcategory'] == 'content') {
$setting[$row['provider_setting_name']] = $row['provider_setting_value'];
}
elseif ($row['provider_setting_subcategory'] == 'format') {
$format[$row['provider_setting_name']] = $row['provider_setting_value'];
}
}
unset($parameters);
//view_array($settings, false);
//set the default content type to json
$content_type = 'json';
//get the content location for the destination number
if (isset($setting['content_type'])) {
$content_type = strtolower($setting['content_type']);
}
if ($debug) {
file_put_contents($log_file, "Server CONTENT_TYPE ".$_SERVER['CONTENT_TYPE']."\n", FILE_APPEND);
file_put_contents($log_file, "content_type: $content_type\n", FILE_APPEND);
}
//get the content
if ($content_type == 'json') {
$message_json = file_get_contents("php://input");
}
elseif ($content_type == 'get') {
$message_json = json_encode($_GET);
}
elseif ($content_type == 'post') {
$message_json = json_encode($_POST);
}
// Used for Providers that send HTTP requests with mixed http method and http variables
// For example, a Provider that sends a POST with a query string would populate $_GET, not $_POST
elseif ($content_type == 'mixed') {
if(!empty($_GET)) {
$message_json = json_encode($_GET);
$content_type = 'get';
}
elseif (!empty($_POST)) {
$message_json = json_encode($_POST);
$content_type = 'post';
}
else {
$message_json = file_get_contents("php://input");
$content_type = 'json';
}
}
//write content to the logs
if ($debug) {
if ($content_type == 'json') {
file_put_contents($log_file, $message_json, FILE_APPEND);
}
}
//save the http post to the log
if ($debug) {
if (count($_POST)) {
file_put_contents($log_file, json_encode($_POST)."\n", FILE_APPEND);
}
if (count($_GET)) {
file_put_contents($log_file, json_encode($_GET)."\n", FILE_APPEND);
}
}
//decode the json into array
if ($content_type == 'json') {
$message = json_decode($message_json, true);
}
//ignore inbound delivery receipt - used by bulkvs
if (isset($message['DeliveryReceipt']) && $message['DeliveryReceipt'] == 'true') {
exit;
}
//send print_r to the log
//if ($debug) {
// file_put_contents($log_file, print_r($message, true)."\n", FILE_APPEND);
//}
//get the content location for the destination number
$message_to = $setting['content']['message_to'] ?? null;
//debug info
if ($debug) {
file_put_contents($log_file, "--------------\n", FILE_APPEND);
file_put_contents($log_file, print_r($setting, true)."\n", FILE_APPEND);
file_put_contents($log_file, "message_to $message_to\n", FILE_APPEND);
file_put_contents($log_file, "--------------\n", FILE_APPEND);
}
/*
$setting['message_from'] => data.attributes.from
$setting['message_to'] => data.attributes.to
$setting['message_content'] => data.attributes.body
$from_array = explode('.', $setting['message_from']);
$to_array = explode('.', $setting['message_to']);
$content_array = explode('.', $setting['message_content']);
*/
//version 3
/*
function get_value($array, $key) {
$keys = explode('.', $key);
$segment = array_shift($keys);
if (isset($array[$segment]) && is_array($array[$segment])) {
$next_key = substr($key, strpos($key, '.') + 1);
$data = get_value($array[$segment], $next_key);
}
else {
$data = $array[$segment] ?? '';
}
return $data;
}
*/
//version 2
function get_value($data, $path) {
$keys = explode('.', $path);
foreach ($keys as $key) {
$data = $data[$key];
}
return $data;
}
//version 1
/*
function get_value($data, $path) {
$keys = explode('.', $path);
if (count($keys) == 1) {
return $data[$keys[0]];
}
if (count($keys) == 2) {
return $data[$keys[0]][$keys[1]];
}
if (count($keys) == 3) {
return $data[$keys[0]][$keys[1]][$keys[2]];
}
if (count($keys) == 4) {
return $data[$key_array[0]][$keys[1]][$keys[2]][$keys[3]];
}
if (count($keys) == 5) {
return $data[$keys[0]][$keys[1]][$keys[2]][$keys[3]][$keys[4]];
}
}
*/
/*
if (count($from_array) == 3) {
$message_from = $message[$from_array[0]][$from_array[1]][$from_array[2]];
}
if (count($to_array) == 3) {
$message_to = $message[$to_array[0]][$to_array[1]][$to_array[2]];
}
if (count($message_content) == 3) {
$message_content = $message[$message_content[0]][$message_content[1]][$message_content[2]];
}
*/
//get the values from the message array using the provider settings
if ($content_type == 'json') {
$message_from = get_value($message, $setting['message_from']);
$message_to = get_value($message, $setting['message_to']);
$message_content = get_value($message, $setting['message_content']);
$message_media_array = !empty($setting['message_media_array']) ? get_value($message, $setting['message_media_array']) : null;
//get the message_type options: sms, mms
if (isset($setting['message_type'])) {
$message_type = strtolower($setting['message_type']);
}
else {
$message_type = !empty($message_media_array) && is_array($message_media_array) ? 'mms' : 'sms';
}
}
elseif ($content_type == 'post') {
if (!empty($setting['message_media_array']) && isset($_POST[$setting['message_media_array']])){
$message_media_array = $_POST[$setting['message_media_array']] ;
}
else {
$message_media_array = message_media_builder($_POST, [$setting['message_media_url'], $setting['message_media_type']]);
}
//get the message_type options: sms, mms
if (isset($setting['message_type'])) {
$message_type = strtolower($setting['message_type']);
}
else {
$message_type = !empty($message_media_array) && is_array($message_media_array) ? 'mms' : 'sms';
}
$message_from = ($message_type == 'mms') ? $_POST[$setting['message_media_from']] : $_POST[$setting['message_from']];
$message_to = ($message_type == 'mms') ? $_POST[$setting['message_media_to']] : $_POST[$setting['message_to']];
$message_content = ($message_type == 'mms') ? $_POST[$setting['message_media_content']] : $_POST[$setting['message_content']];
$message_content = preg_replace('/<smil[^>]*>([\s\S]*?)<\/smil[^>]*>/', '', $message_content);
}
elseif ($content_type == 'get') {
if (!empty($setting['message_media_array']) && isset($_GET[$setting['message_media_array']])){
$message_media_array = $_GET[$setting['message_media_array']] ;
}
else {
$message_media_array = message_media_builder($_GET, [$setting['message_media_url'], $setting['message_media_type']]);
}
//get the message_type options: sms, mms
if (isset($setting['message_type'])) {
$message_type = strtolower($setting['message_type']);
}
else {
$message_type = !empty($message_media_array) && is_array($message_media_array) ? 'mms' : 'sms';
}
$message_from = ($message_type == 'mms') ? $_GET[$setting['message_media_from']] : $_GET[$setting['message_from']];
$message_to = ($message_type == 'mms') ? $_GET[$setting['message_media_to']] : $_GET[$setting['message_to']];
$message_content = ($message_type == 'mms') ? $_GET[$setting['message_media_content']] : $_GET[$setting['message_content']];
$message_content = preg_replace('/<smil[^>]*>([\s\S]*?)<\/smil[^>]*>/', '', $message_content);
}
//message to is an array get first number in the array
if (is_array($message_to)) {
$message_to = $message_to['0'];
}
//decode the content if it is encoded
if (isset($message_content)) {
if (is_urlencoded($message_content)) {
$message_content = urldecode($message_content);
}
}
//format the phone numbers
if($message_type == 'mms') {
//check if message_media formats are defined and non-empty, and if so, use those instead of default formats
if (isset($format['message_media_message_from']) && !empty($format['message_media_message_from'])) {
$message_from = format_string($format['message_media_message_from'], $message_from);
}
elseif (isset($format['message_from'])) {
$message_from = format_string($format['message_from'], $message_from);
}
if (isset($format['message_media_message_to']) && !empty($format['message_media_message_to'])) {
$message_to = format_string($format['message_media_message_to'], $message_to);
}
elseif (isset($format['message_to'])) {
$message_to = format_string($format['message_to'], $message_to);
}
}
else {
//default formats. If setting is defined but format string is left blank, the format_string function
//will return the data as is (No changes made)
if (isset($format['message_from'])) {
$message_from = format_string($format['message_from'], $message_from);
}
if (isset($format['message_to'])) {
$message_to = format_string($format['message_to'], $message_to);
}
}
//debug info
if ($debug) {
file_put_contents($log_file, "setting.message_from: ".$setting['message_from']."\n", FILE_APPEND);
file_put_contents($log_file, "setting.message_to: ".$setting['message_to']."\n", FILE_APPEND);
file_put_contents($log_file, "setting.message_content: ".$setting['message_content']."\n", FILE_APPEND);
file_put_contents($log_file, "content_type: $content_type\n", FILE_APPEND);
file_put_contents($log_file, "provider_uuid: $provider_uuid\n", FILE_APPEND);
file_put_contents($log_file, "from: ".$message_from."\n", FILE_APPEND);
file_put_contents($log_file, "to: ".$message_to."\n", FILE_APPEND);
file_put_contents($log_file, "content: ".$message_content."\n", FILE_APPEND);
file_put_contents($log_file, "message_media_array: ".print_r($message_media_array, true)."\n", FILE_APPEND);
}
/*
()
[data] => Array
(
[attributes] => Array
(
[status] => delivered
[body] => Ddd
[direction] => inbound
[amount_nanodollars] => 4000000
[message_encoding] => 0
[timestamp] => 2021-05-16T06:12:59.88Z
[to] => 12089068227
[amount_display] => $0.0040
[from] => 12088058985
[is_mms] =>
[message_callback_url] => https://voip.fusionpbx.com/app/messages/index.php
[message_type] => longcode
)
[type] => message
[id] => mdr2-c3afc962b60d11ebb748aecb682882cc
)
)
*/
//set the hostname if it wasn't provided
$hostname = gethostname();
//get the source phone number
$destination_number = preg_replace('{[\D]}', '', $message_to);
//use the phone number to get the destination details
$sql = "SELECT * FROM v_destinations ";
$sql .= "WHERE ( ";
$sql .= " destination_prefix || destination_area_code || destination_number = :destination_number ";
$sql .= " OR destination_trunk_prefix || destination_area_code || destination_number = :destination_number ";
$sql .= " OR destination_prefix || destination_number = :destination_number ";
$sql .= " OR '+' || destination_prefix || destination_number = :destination_number ";
$sql .= " OR '+' || destination_prefix || destination_area_code || destination_number = :destination_number ";
$sql .= " OR destination_area_code || destination_number = :destination_number ";
$sql .= " OR destination_number = :destination_number ";
$sql .= ") ";
$sql .= "and provider_uuid is not null ";
$sql .= "and destination_enabled = 'true'; ";
$parameters['destination_number'] = $destination_number;
if ($debug) {
file_put_contents($log_file, "sql: ".$sql."\n", FILE_APPEND);
file_put_contents($log_file, print_r($parameters, true)."\n", FILE_APPEND);
}
$row = $database->select($sql, $parameters, 'row');
$domain_uuid = $row['domain_uuid'];
$user_uuid = $row['user_uuid'];
$group_uuid = $row['group_uuid'];
if ($debug) {
file_put_contents($log_file, print_r($row, true)."\n", FILE_APPEND);
}
//check if message to email is enabled
$destination_email_enabled = $row['destination_email'];
unset($sql, $parameters, $row);
//get the contact uuid
$sql = "select c.contact_uuid ";
$sql .= "from v_contacts as c, v_contact_phones as p ";
$sql .= "where p.contact_uuid = c.contact_uuid ";
$sql .= "and p.phone_number = :phone_number ";
$sql .= "and c.domain_uuid = :domain_uuid ";
$parameters['phone_number'] = $destination_number;
$parameters['domain_uuid'] = $domain_uuid;
$contact_uuid = $database->select($sql, $parameters, 'column');
unset($sql, $parameters);
//add to the messages array
$message_uuid = uuid();
$array['messages'][0]['message_uuid'] = $message_uuid;
$array['messages'][0]['domain_uuid'] = $domain_uuid;
$array['messages'][0]['provider_uuid'] = $provider_uuid;
if (is_uuid($user_uuid)) {
$array['messages'][0]['user_uuid'] = $user_uuid;
}
if (is_uuid($group_uuid)) {
$array['messages'][0]['group_uuid'] = $group_uuid;
}
if (is_uuid($contact_uuid)) {
$array['messages'][0]['contact_uuid'] = $contact_uuid;
}
$array['messages'][0]['message_type'] = $message_type;
$array['messages'][0]['message_direction'] = 'inbound';
$array['messages'][0]['message_date'] = 'now()';
$array['messages'][0]['message_from'] = $message_from;
$array['messages'][0]['message_to'] = $message_to;
$array['messages'][0]['message_text'] = $message_content;
$array['messages'][0]['message_json'] = $message_json;
//add to message queue array
$message_queue_uuid = uuid();
$array['message_queue'][0]['message_queue_uuid'] = $message_queue_uuid;
$array['message_queue'][0]['domain_uuid'] = $domain_uuid;
if (is_uuid($user_uuid)) {
$array['message_queue'][0]['user_uuid'] = $user_uuid;
}
if (is_uuid($group_uuid)) {
$array['message_queue'][0]['group_uuid'] = $group_uuid;
}
$array['message_queue'][0]['provider_uuid'] = $provider_uuid;
$array['message_queue'][0]['hostname'] = $hostname;
if (is_uuid($contact_uuid)) {
$array['message_queue'][0]['contact_uuid'] = $contact_uuid;
}
$array['message_queue'][0]['message_type'] = $message_type;
$array['message_queue'][0]['message_direction'] = 'inbound';
$array['message_queue'][0]['message_status'] = 'waiting';
$array['message_queue'][0]['message_date'] = 'now()';
$array['message_queue'][0]['message_from'] = $message_from;
$array['message_queue'][0]['message_to'] = $message_to;
$array['message_queue'][0]['message_text'] = $message_content;
$array['message_queue'][0]['message_json'] = $message_json;
//add the required permission
$p = permissions::new();
$p->add("message_add", "temp");
$p->add("message_queue_add", "temp");
$p->add("message_media_add", "temp");
//build message media array (if necessary)
if (is_array($message_media_array)) {
foreach($message_media_array as $index => $media_row) {
//get the value out of the array using dot notation
if (isset($setting['message_media_url'])) {
$message_media_url = get_value($media_row, $setting['message_media_url']);
}
if (isset($setting['message_media_type'])) {
$message_media_type = get_value($media_row, $setting['message_media_type']);
}
//get the file extension
if (isset($message_media_type)) {
if ($message_media_type == 'image/jpg') { $message_media_type = 'jpg'; }
if ($message_media_type == 'image/jpeg') { $message_media_type = 'jpg'; }
if ($message_media_type == 'image/png') { $message_media_type = 'png'; }
if ($message_media_type == 'image/gif') { $message_media_type = 'gif'; }
}
//get the media url
if (!isset($message_media_url)) {
$message_media_url = $media_row;
}
//get the media type from the URL
if (!isset($message_media_type)) {
$message_media_type = pathinfo($message_media_url, PATHINFO_EXTENSION);
}
//get the file name from the URL
if (!isset($message_media_name)) {
$message_media_name = pathinfo($message_media_url, PATHINFO_FILENAME).'.'.$message_media_type;
}
if ($debug) {
file_put_contents($log_file, "media_row: ".print_r($media_row, true)."\n", FILE_APPEND);
file_put_contents($log_file, "message_media_url: ".$message_media_url."\n", FILE_APPEND);
file_put_contents($log_file, "message_media_name: ".$message_media_name."\n", FILE_APPEND);
file_put_contents($log_file, "message_media_type: ".$message_media_type."\n", FILE_APPEND);
}
//build the array for the media
if ($message_media_type !== 'xml' && strlen($message_media_url) > 0) {
//build the message media
$array['message_media'][$index]['message_media_uuid'] = uuid();
$array['message_media'][$index]['message_uuid'] = $message_uuid;
$array['message_media'][$index]['domain_uuid'] = $domain_uuid;
$array['message_media'][$index]['user_uuid'] = $user_uuid;
$array['message_media'][$index]['message_media_type'] = $message_media_type;
$array['message_media'][$index]['message_media_name'] = $message_media_name;
$array['message_media'][$index]['message_media_date'] = 'now()';
$array['message_media'][$index]['message_media_url'] = $message_media_url;
$array['message_media'][$index]['message_media_content'] = base64_encode(url_get_contents($message_media_url));
//get email attachments
if($destination_email_enabled == 'true'){
$email_attachments[$index]['base64'] = $array['message_media'][$index]['message_media_content'];
$email_attachments[$index]['name'] = $message_media_name;
$email_attachments[$index]['type'] = $message_media_type;
}
}
}
}
else {
//get the value out of the array using dot notation
if (isset($setting['message_media_url'])) {
$message_media_url = get_value($message, $setting['message_media_url']);
}
if (isset($setting['message_media_type'])) {
$message_media_type = get_value($message, $setting['message_media_type']);
}
//get the media type from the URL
if (!isset($message_media_type) && !empty($message_media_url)) {
$message_media_type = pathinfo($message_media_url, PATHINFO_EXTENSION);
}
//get the file extension
if (!empty($message_media_type)) {
if ($message_media_type == 'image/jpeg') { $message_media_type = 'jpg'; }
if ($message_media_type == 'image/png') { $message_media_type = 'png'; }
if ($message_media_type == 'image/gif') { $message_media_type = 'gif'; }
}
//build the array for the media
if (!empty($message_media_url) && strlen($message_media_url) > 0 && $message_media_type !== 'xml') {
//build the message media array
$index = 0;
$array['message_media'][$index]['message_media_uuid'] = uuid();
$array['message_media'][$index]['message_uuid'] = $message_uuid;
$array['message_media'][$index]['domain_uuid'] = $domain_uuid;
$array['message_media'][$index]['user_uuid'] = $user_uuid;
$array['message_media'][$index]['message_media_type'] = $message_media_type;
$array['message_media'][$index]['message_media_url'] = $message_media_url;
$array['message_media'][$index]['message_media_content'] = base64_encode(url_get_contents($message_media_url));
//prepare the email attachments array
if($destination_email_enabled == 'true'){
$email_attachments[$index]['base64'] = $array['message_media'][$index]['message_media_content'];
$email_attachments[$index]['name'] = $message_media_name;
$email_attachments[$index]['type'] = $message_media_type;
}
}
}
//if ($debug) {
// file_put_contents($log_file, print_r($array, true), FILE_APPEND);
//}
//save message to the database;
$database->app_name = 'messages';
$database->app_uuid = '4a20815d-042c-47c8-85df-085333e79b87';
$database->save($array, false);
$result = $database->message;
//if ($debug) {
// file_put_contents($log_file, print_r($result, true), FILE_APPEND);
//}
//remove the temporary permission
$p->delete("message_add", "temp");
$p->delete("message_queue_add", "temp");
$p->delete("message_media_add", "temp");
//convert the array to json
//$array_json = json_encode($array);
//get the list of extensions using the user_uuid
$sql = "select * from v_domains as d, v_extensions as e ";
$sql .= "where extension_uuid in ( ";
$sql .= " select extension_uuid ";
$sql .= " from v_extension_users ";
$sql .= " where user_uuid = :user_uuid ";
$sql .= ") ";
$sql .= "and e.domain_uuid = d.domain_uuid ";
$sql .= "and e.enabled = 'true' ";
$parameters['user_uuid'] = $user_uuid;
$extensions = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
//send the sip message
if (is_array($extensions) && @sizeof($extensions) != 0) {
//create the event socket connection
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
//loop through assigned extensions
foreach ($extensions as $row) {
//get variables from the array
$domain_name = $row['domain_name'];
$extension = $row['extension'];
$number_alias = $row['number_alias'];
//get the sip profile
$command = "sofia_contact ".$extension."@".$domain_name;
$response = event_socket_request($fp, "api ".$command);
if ($response != 'error/user_not_registered') {
$sip_profile = explode("/", $response)[1];
}
//send the sip messages
//$command = "luarun app/messages/resources/send.lua ".$message["from"]."@".$domain_name." ".$extension."@".$domain_name." '".$message["text"]."'";
//$message_from_orig = $message_from;
//original number with the domain name
$message_from = $message_from .'@'.$domain_name;
//$message_to_orig = $message_to;
//send to the assigned extension(s)
$message_to = $extension . '@'.$domain_name;
//$message_to = '[email protected]';
//add debug info to the message
//$message_content = $message_content . ' - ' .$message_to_orig;
//send the SIP message (working)
$event = "sendevent CUSTOM\n";
$event .= "Event-Subclass: SMS::SEND_MESSAGE\n";
$event .= "proto: sip\n";
$event .= "dest_proto: sip\n";
$event .= "from: ".$message_from."\n";
$event .= "from_full: sip:".$message_from."\n";
$event .= "to: ".$message_to."\n";
$event .= "subject: sip:".$message_to."\n";
//$event .= "type: text/html\n";
$event .= "type: text/plain\n";
$event .= "hint: the hint\n";
$event .= "replying: true\n";
$event .= "sip_profile: ".$sip_profile."\n";
$event .= "_body: ". $message_content;
event_socket_request($fp, $event);
}
}
if ($destination_email_enabled == 'true') {
//get the email template from the database - Category: message; Subcategory:new
$sql = "select template_subcategory, template_subject, template_body from v_email_templates ";
$sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
$sql .= "and template_language = :template_language ";
$sql .= "and template_category = :template_category ";
$sql .= "and template_subcategory = :template_subcategory ";
$sql .= "and template_enabled = 'true' ";
$parameters['domain_uuid'] = $domain_uuid;
$parameters['template_language'] = $settings->get('domain','language');
$parameters['template_category'] = 'message';
$parameters['template_subcategory'] = 'inbound';
$message_inbound_template = $database->select($sql, $parameters, 'row');
unset($sql, $parameters);
$email_subject = $message_inbound_template['template_subject'] ?? "";
$email_body = $message_inbound_template['template_body'] ?? "";
//add placeholders as needed
$email_subject = str_replace('${message_to}', $message_to, $email_subject);
$email_subject = str_replace('${message_from}', $message_from, $email_subject);
$email_body = str_replace('${message_to}', $message_to, $email_body);
$email_body = str_replace('${message_from}', $message_from, $email_body);
$email_body = str_replace('${message_text}', $message_content, $email_body);
$time_zone = $settings->get('domain','time_zone');
$dt = new DateTime("now", new DateTimeZone($time_zone));
$email_body = str_replace('${message_date}', $dt->format('m/d/Y, H:i:s').' '.$time_zone, $email_body);
$sql = "select user_email from v_users ";
$sql .= "where user_uuid = :user_uuid";
$parameters['user_uuid'] = $user_uuid;
$user_email = $database->select($sql, $parameters, 'column');
unset($sql, $parameters);
$email = new email;
$email->domain_uuid = $domain_uuid;
$email->recipients = $user_email;
$email->subject = $email_subject;
$email->body = $email_body;
$email->from_address = $settings->get('email','smtp_from');
$email->from_name = $settings->get('email','smtp_from_name');
$email->debug_level = 3;
$email->attachments = $email_attachments;
$email->send();
}
//set the file
//$file = '/tmp/sms.txt';
//save the file
//file_put_contents($file, $json);
//save the data to the file system
//file_put_contents($file, $json."\n");
//file_put_contents($file, $array_json."\nfrom: ".$message["from"]." to: ".$message["to"]." text: ".$message["text"]."\n$sql_test\njson: ".$json."\n".$saved_result."\n");
//send response to provider, if defined
foreach ($provider_settings as $row) {
if ($row['provider_setting_subcategory'] == 'response' && $row['provider_setting_name'] == 'message_content' && !empty($row['provider_setting_value'])) {
$message_content = $row['provider_setting_value'];
if ($debug) {
file_put_contents($log_file, "Response...\n".$row['provider_setting_value']."\n\n", FILE_APPEND);
}
echo $row['provider_setting_value'];
break;
}
}
?>