forked from simontelephonics/smsconnector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmsconnector.class.php
985 lines (898 loc) · 26.9 KB
/
Smsconnector.class.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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
<?php
namespace FreePBX\modules;
use BMO;
use FreePBX_Helpers;
use PDO;
class Smsconnector extends FreePBX_Helpers implements BMO
{
const adapterName = 'Smsconnector';
public $providers;
public $FreePBX = null;
protected $Database = null;
protected $Userman = null;
protected $tables = array(
'relations' => 'smsconnector_relations',
);
protected $tablesSms = array(
'routing' => 'sms_routing',
'dids' => 'sms_dids',
);
public function __construct($freepbx = null)
{
if ($freepbx == null) {
throw new \Exception("Not given a FreePBX Object");
}
$this->FreePBX = $freepbx;
$this->Database = $freepbx->Database;
$this->Userman = $freepbx->Userman;
$this->loadProviders();
}
/**
* smsAdaptor loaded by SMS module hook (loadAdapter)
*
* @param string $adaptor Adaptor name
* @return Smsconnector adaptor object
*/
public function smsAdaptor($adaptor)
{
if(!class_exists('\FreePBX\modules\Sms\Adaptor\Smsconnector')) {
include __DIR__.'/adaptor/Smsconnector.class.php';
}
return new \FreePBX\modules\Sms\Adaptor\Smsconnector($adaptor);
}
/**
* Installer run on fwconsole ma install
*
* @return void
*/
public function install()
{
outn(_("Creating link to public folder..."));
$link_public = sprintf("%s/smsconn", $this->FreePBX->Config->get("AMPWEBROOT"));
$link_public_module = sprintf("%s/admin/modules/smsconnector/public", $this->FreePBX->Config->get("AMPWEBROOT"));
if (! file_exists($link_public))
{
symlink($link_public_module, $link_public);
out(_("Done"));
}
else
{
out(_('Skip: the path already exists!'));
}
// if daemon got installed in 16.0.13, remove it
if (class_exists('FreePBX\modules\Pm2') && $this->FreePBX->Pm2->getStatus("smsconnector-sipsms"))
{
$this->FreePBX->Pm2->delete("smsconnector-sipsms");
}
}
/**
* Uninstaller run on fwconsole ma uninstall
*
* @return void
*/
public function uninstall()
{
outn(_("Removing public folder link..."));
$link_public = sprintf("%s/smsconn", $this->FreePBX->Config->get("AMPWEBROOT"));
if(file_exists($link_public))
{
if(is_link($link_public))
{
unlink($link_public);
if( ! file_exists($link_public))
{
out(_("Done"));
}
else
{
out(_("Error: the path still exists!"));
}
}
else
{
out(_("Skip: the path is not a symbolic link!"));
}
}
}
/**
* Processes form submission and pre-page actions.
*
* @param string $page Display name
* @return void
*/
public function doConfigPageInit($page)
{
/** getReq provided by FreePBX_Helpers see https://wiki.freepbx.org/x/0YGUAQ */
$action = $this->getReq('action', '');
$providers = $this->getReq('providers');
switch ($action)
{
case 'setproviders':
return $this->updateProviders($providers);
break;
}
}
public function getRightNav($request)
{
switch($request['view'])
{
case 'settings':
return load_view(dirname(__FILE__).'/views/rnav.php', array());
break;
default:
//No show Nav
}
}
/**
* Adds buttons to the bottom of pages per set conditions
*
* @param array $request $_REQUEST
* @return void
*/
public function getActionBar($request)
{
if ('smsconnector' == $request['display'])
{
if (!isset($_GET['view']))
{
return [];
}
$buttons = [
'reset' => [
'name' => 'reset',
'id' => 'reset',
'value' => _("Reset")
],
'submit' => [
'name' => 'submit',
'id' => 'submit',
'value' => _("Submit")
]
];
return $buttons;
}
}
/**
* Returns bool permissions for AJAX commands
* https://wiki.freepbx.org/x/XoIzAQ
* @param string $command The ajax command
* @param array $setting ajax settings for this command typically untouched
* @return bool
*/
public function ajaxRequest($req, &$setting)
{
// ** Allow remote consultation with Postman **
// ********************************************
// $setting['authenticate'] = false;
// $setting['allowremote'] = true;
// return true;
// ********************************************
switch($req)
{
case "get_selects":
case "numbers_list":
case "numbers_get":
case "numbers_update":
case "numbers_delete":
return true;
break;
default:
return false;
}
return false;
}
/**
* Handle Ajax request
*/
public function ajaxHandler()
{
$command = $this->getReq("command", "");
$data_return = false;
switch ($command)
{
case 'get_selects':
$data['users'] = array();
$data['providers'] = array();
foreach($this->Userman->getAllUsers() as $user)
{
$data['users'][$user['id']] = empty($user['displayname']) ? $user['username'] : sprintf('%s (%s)', $user['displayname'], $user['username']);
}
foreach ($this->getAvailableProviders() as $provider => $info)
{
$data['providers'][$info['nameraw']] = $info['name'];
}
$data_return = array("status" => true, 'data' => $data);
break;
case 'numbers_list':
$data_return = $this->getList();
break;
case 'numbers_get':
$id = $this->getReq("id", null);
if (empty($id))
{
$data_return = array("status" => false, "message" => _("ID is missing!"));
}
else if (! $this->isExistDIDByID($id))
{
$data_return = array("status" => false, "message" => _("ID does not exist!"));
}
else
{
$dataId = $this->getNumber($id);
$data_return = array("status" => true, "data" => $dataId);
}
break;
case 'numbers_update':
$getdata = $this->getReq("data", array());
$id = $getdata['id'];
$did = $getdata['didNumber'];
$uids = $getdata['uidsNumber'];
$name = $getdata['providerNumber'];
$uids = explode(",", $uids);
try
{
if ($getdata['type'] == 'edit')
{
$this->updateNumber($uids, $did, $name);
$data_return = array("status" => true, "message" => _("Number updated successfully"));
}
else
{
$this->addNumber($uids, $did, $name);
$data_return = array("status" => true, "message" => _("Number created successfully"));
}
}
catch (\Exception $e)
{
$data_return = array("status" => false, "message" => $e->getMessage());
}
break;
case 'numbers_delete':
$id = $this->getReq("id", null);
if (empty($id))
{
$data_return = array("status" => false, "message" => _("ID is missing!"));
}
else if (! $this->isExistDIDByID($id))
{
$data_return = array("status" => false, "message" => _("ID does not exist!"));
}
else if ($this->deleteNumber($id))
{
$data_return = array("status" => true, "message" => _("Number delete successfully"));
}
else
{
$data_return = array("status" => false, "message" => _("Number delete failed!"));
}
break;
default:
$data_return = array("status" => false, "message" => _("Command not found!"), "command" => $command);
}
return $data_return;
}
/**
* getProviderSettings
* @return array returns an associative array
*/
public function getProviderSettings()
{
return array('providers' => $this->getAll('provider'));
}
/**
* getAvailableProviders
* @return array list of providers that are configured and available for use
*/
public function getAvailableProviders()
{
$retlist = array();
$list = $this->getProvider("");
foreach ($list as $key => $value)
{
if ($value['class']->isAvailable())
{
$retlist[$key] = $value;
}
}
return $retlist;
}
/**
* getList gets a list of numbers and their associations
* @return array
*/
public function getList()
{
$sql = sprintf('SELECT r.id, rt.didid, r.providerid AS name, GROUP_CONCAT(DISTINCT rt.uid) AS users, rt.did FROM %s AS rt ' .
'INNER JOIN %s as r ON rt.didid = r.didid ' .
'WHERE rt.adaptor = "%s" ' .
'GROUP BY r.id, rt.didid, r.providerid, rt.did', $this->tablesSms['routing'], $this->tables['relations'], self::adapterName);
$data = $this->Database->query($sql)->fetchAll(\PDO::FETCH_NAMED);
foreach ($data as $key => &$value)
{
$value['users'] = $this->getInfoUserByID($value['users'], true);
}
return $data;
}
/**
* getNumber Gets an individual item by smsconnector_relations.ID
* @param int $id Item ID
* @return array Returns an associative array with id, subject and body.
*/
public function getNumber($id)
{
$data_return = null;
if ($this->isExistDIDByID($id))
{
$sql = sprintf('SELECT r.id, rt.didid, r.providerid AS name, GROUP_CONCAT(DISTINCT rt.uid) AS users, rt.did FROM %s as rt ' .
'INNER JOIN %s as r ON rt.didid = r.didid ' .
'WHERE rt.adaptor = "%s" AND rt.didid = :id ' .
'GROUP BY r.id, rt.didid, r.providerid, rt.did', $this->tablesSms['routing'], $this->tables['relations'], self::adapterName);
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetchObject();
$data_return = [
'id' => $row->id,
'didid' => $row->didid,
'name' => $row->name,
'users' => $this->getInfoUserByID($row->users, true),
'did' => $row->did,
];
}
return $data_return;
}
/**
* getInfoUserByID We obtain the information of the users with the userman module.
* @param mixed $uid UID or array of UIDs of the users that we want to obtain information.
* @param bool $needExplode True if we need to exploit the uids, False (default) if nothing needs to be done.
* @param string $explodeChar The character the used for the explode (default ',').
* @param array $info_return The array of the information that we wish to obtain (default userman and displayname).
* @return array Array of the information the users.
*/
private function getInfoUserByID($uid, $needExplode = false, $explodeChar = ",", $info_return = null)
{
if ($needExplode && ! is_array($needExplode))
{
$uid = explode($explodeChar, $uid);
}
if (! is_array($uid))
{
$uid = array($uid);
}
$data_return = array();
if (is_null($info_return))
{
$info_return = array('username', 'displayname');
}
foreach ($uid as $userid)
{
$user_info = $this->Userman->getUserByID($userid);
$data_user = array('uid' => $userid);
foreach ($info_return as $option)
{
$data_user[$option] = $user_info[$option];
}
$data_return[$userid] = $data_user;
}
return $data_return;
}
/* Helper functions */
public function getUsersByDid($did)
{
return $this->FreePBX->Sms->getAssignedUsers($did);
}
public function getDidsByUser($uid)
{
return $this->FreePBX->Sms->getDIDs($uid);
}
public function getUidByDefaultExtension($extension)
{
return $this->Userman->getUserByDefaultExtension($extension)['id'];
}
public function getSipDefaultDidByUid($uid)
{
return $this->Userman->getModuleSettingByID($uid, 'smsconnector', 'sipsmsdefaultdid', true, false);
}
/**
* getSIPMessageDeviceByUserID
* @param int $uid UID of the users that we want to obtain information.
* @return mixed extension number or NULL
*/
public function getSIPMessageDeviceByUserID($uid)
{
if ($this->Userman->getModuleSettingByID($uid, 'smsconnector', 'sipsmsenabled', false, false))
{
$user = $this->Userman->getUserByID($uid);
$extension = $user['default_extension'];
$device = $this->FreePBX->Core->getDevice($extension);
// only select PJSIP devices that have been set with the appropriate message_context
if ($device['tech'] == 'pjsip' && $device['message_context'] == 'smsconnector-messages')
{
return $extension;
}
}
return NULL;
}
/**
* processOutboundSip Used by AGI script to send SMS via connector
* @param string $to To (destination)
* @param string $from Extension that is sending the SMS
* @param string $messageBody SMS body
* @return bool
*/
public function processOutboundSip($to, $from, $messageBody)
{
freepbx_log(FPBX_LOG_INFO, sprintf(_("Processing SIP SMS from %s to %s"), $from, $to), true);
$did = $actualTo = NULL;
$allowedToSend = false;
$retval = true;
if (preg_match('/^\+?(\d+)\+\+?(\d+)$/', $to, $matches))
{
$did = $matches[1];
$actualTo = $matches[2];
}
elseif (preg_match('/^\+?(\d+)$/', $to, $matches))
{
$actualTo = $matches[1];
}
$uid = $this->getUidByDefaultExtension($from);
if ($this->Userman->getModuleSettingByID($uid, 'smsconnector', 'sipsmsenabled', false, false))
{
if ($did)
{
if (in_array($did, $this->getDidsByUser($uid))) { $allowedToSend = true; }
}
else
{
if ($defaultDid = $this->getSipDefaultDidByUid($uid))
{
$did = $defaultDid;
$allowedToSend = true;
}
}
}
if ($allowedToSend && $actualTo)
{
$formattedTo = (preg_match('/^[2-9][0-9]{2}[2-9][0-9]{6}$/', $actualTo)) ? '1'.$actualTo : $actualTo; // format NANP
$adaptor = $this->FreePBX->Sms->getAdaptor($did);
if(is_object($adaptor)) {
$o = $adaptor->sendMessage($formattedTo, $did, '', $messageBody);
if(! $o['status']) {
freepbx_log(FPBX_LOG_INFO, sprintf(_("Outbound message failed: %s"), $o['message']), true);
$retval = false;
}
} else {
freepbx_log(FPBX_LOG_INFO, sprintf(_("No adaptor found for DID %s"), $did), true);
$retval = false;
}
}
else
{
freepbx_log(FPBX_LOG_INFO, sprintf(_("%s tried to send SMS from %s but is not allowed!"), $from, $did), true);
$retval = false;
}
return $retval;
}
/**
* inboundHookForSip Hooked by AdaptorBase getMessage on inbound SMS. Relays SMS to SIP devices if enabled.
* @param string $to To (destination)
* @param string $from caller ID
* @param string $message SMS body
*/
public function inboundHookForSip($id, $to, $from, $cnam, $message, $time, $adaptor, $emid, $threadid, $didid)
{
// lookup users for DID
$uids = $this->getUsersByDid($to);
foreach ($uids as $uid)
{
// get device that can receive SMS
$device = $this->getSIPMessageDeviceByUserID($uid);
if ($device)
{
if ($to != $this->getSipDefaultDidByUid($uid))
{
// format the caller ID to include the DID, which will allow replying via non-default DID
$sipFrom = sprintf("%s+%s", $to, $from);
}
else
{
$sipFrom = $from;
}
// get contacts
$result = $this->FreePBX->astman->send_request('Getvar', array('Variable' => "PJSIP_DIAL_CONTACTS($device)"));
$contacts = array();
if (!empty($result['Value']))
{
$contacts = explode('&', $result['Value']);
}
if (!empty($contacts))
{
foreach ($contacts as $contact) // message all registered
{
$sipTo = sprintf("pjsip:%s", substr($contact, 6)); // replace "PJSIP/" with "pjsip:"
$result = $this->FreePBX->astman->MessageSend($sipTo, $sipFrom, $message);
if ($result['Response'] == 'Error')
{
freepbx_log(FPBX_LOG_INFO, sprintf(_("Error sending message to %s: %s"), $contact, $result['Message']));
}
}
}
else // no contacts registered - send email if enabled
{
if ($this->Userman->getModuleSettingByID($uid, 'smsconnector', 'sipsmsemailoffline', false, false)) {
// if no email address defined for user, does nothing
//TODO: Generate body from template.
$body = sprintf(_("While offline, you received an SMS from %s to %s:\n\n%s"), $from, $to, $message);
//TODO: Allow setting the subject message
$subject = _('SMS received while offline');
$this->Userman->sendEmail($uid, $subject, $body);
}
}
}
}
}
/**
* addNumber Add a number
* @param int $uid userman user id
* @param string $did DID
* @param string $name name of the SMS provider
*/
public function addNumber($uid, $did, $name, $checkExists = true)
{
if (! is_array($uid))
{
$uid = array($uid);
}
if (preg_match('/^[2-9]\d{2}[2-9]\d{6}$/', $did)) // ten digit NANP, make it 11-digit
{
$did = '1'.$did;
}
if ( ($name == "") || ($did == "") || (empty($uid)))
{
throw new \Exception(_('Necessary data is missing!'));
}
else if (($checkExists == true) && ($this->isExistDID($did)))
{
throw new \Exception(_('The DID already exists!'));
}
$this->FreePBX->Sms->addDIDRouting($did, $uid, self::adapterName);
$sql = sprintf("SELECT id FROM %s WHERE did = :did", $this->tablesSms['dids']);
$sth = $this->Database->prepare($sql);
$sth->execute(array(':did' => $did));
$didid = $sth->fetchColumn();
if (! empty($didid))
{
$sql = sprintf('INSERT INTO %s (didid, providerid) VALUES (:didid, :provider) ON DUPLICATE KEY UPDATE providerid = :provider', $this->tables['relations']);
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':didid', $didid, \PDO::PARAM_INT);
$stmt->bindParam(':provider', $name, \PDO::PARAM_STR);
$stmt->execute();
return true;
}
return false;
}
/**
* updateNumber Updates the given ID
* @param int $uid userman user ID
* @param string $did DID
* @param string $name provider name
* @return bool Returns true on success or false on failure
*/
public function updateNumber($uid, $did, $name)
{
return $this->addNumber($uid, $did, $name, false);
}
/**
* deleteNumber Deletes the given number by smsconnector_relations.didid
* @param int $id ID
* @return bool Returns true on success or false on failure
*/
public function deleteNumber($id)
{
if ($this->isExistDIDByID($id))
{
$sql = sprintf('DELETE FROM %s WHERE didid = :id', $this->tables['relations']);
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
$sql = sprintf('DELETE FROM %s WHERE didid = :id', $this->tablesSms['routing']);
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
return true;
}
return false;
}
/**
* isExistDIDByID Check if the ID exists using the ID or DIDID (table relations)
* @param int $id ID
* @param bool $useddidid True used column DIDID, False used column ID (defualt DIDID).
* @return bool Returns true on existe of false if is not exist or not definded id.
*/
public function isExistDIDByID($id, $useddidid = true)
{
if (trim($id) != "")
{
$sql = sprintf('SELECT COUNT(*) FROM %s WHERE %s = :id', $this->tables['relations'], ($useddidid ? 'didid' : 'id'));
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
if ($stmt->fetchColumn() > 0)
{
return true;
}
}
return false;
}
/**
* isExistDID Check if the Did exist
* @param string $did Number DID
* @return bool Returns true on exist and False if not exist.
*/
public function isExistDID($did)
{
$data_raturn = false;
if (trim($did) != "")
{
$sql = sprintf('SELECT COUNT(*) FROM %s as r INNER JOIN %s AS d ON r.didid = d.id WHERE d.did = :did', $this->tables['relations'], $this->tablesSms['dids']);
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':did', $did, \PDO::PARAM_STR);
$stmt->execute();
if ($stmt->fetchColumn() > 0)
{
$data_raturn = true;
}
}
return $data_raturn;
}
/**
* updateProviders
* @param array hash of provider settings from form
* @return bool success or failure
*/
public function updateProviders($providers)
{
foreach ($providers as $provider => $creds)
{
$this->setProviderConfig($provider, $creds);
}
return true;
}
/**
* getUsersWithDids
* @return array of user IDs associated with SMS DIDs
*/
public function getUsersWithDids()
{
$sql = sprintf('SELECT DISTINCT uid FROM %s', $this->tablesSms['routing']);
return $this->Database->query($sql)->fetchAll(\PDO::FETCH_COLUMN, 0);
}
/**
* This returns html to the main page
*
* @return string html
*/
public function showPage($page, $params = array())
{
$request = $_REQUEST;
$data = array(
"smsconnector" => $this,
'request' => $request,
'page' => $page,
);
$data = array_merge($data, $params);
switch ($page)
{
case 'main':
$data_return = load_view(__DIR__ . '/views/page.main.php', $data);
break;
case 'grid':
$data_return = load_view(__DIR__ . '/views/view.number.grid.php', $data);
$data_return .= load_view(__DIR__ . '/views/view.number.form.php', $data);
break;
case 'settings':
foreach ($this->listProviders() as $provider)
{
$data['settings'][$provider]['info'] = $this->getProvider($provider);
// unset($data['settings'][$provider]['info']['class']);
$data['settings'][$provider]['value'] = $this->getProviderConfig($provider);
}
$data_return = load_view(__DIR__ . '/views/settings.php', $data);
break;
case 'userman':
$data['userman'] =& $this->Userman;
$data_return = load_view(__DIR__ . '/views/view.userman.user.php', $data);
break;
default:
$data_return = sprintf(_("Page Not Found (%s)!!!!"), $page);
}
return $data_return;
}
public function usermanShowPage()
{
$request = $_REQUEST;
if(isset($request['action']))
{
switch($request['action'])
{
case 'adduser':
case 'showuser':
$sipSmsEnabled = null;
$defaultDid = null;
$emailOffline = null;
if(isset($request['user']))
{
$user = $this->Userman->getUserByID($request['user']);
$sipSmsEnabled = $this->Userman->getModuleSettingByID($user['id'],'smsconnector','sipsmsenabled',true);
$defaultDid = $this->Userman->getModuleSettingByID($user['id'],'smsconnector','sipsmsdefaultdid',true);
$emailOffline = $this->Userman->getModuleSettingById($user['id'],'smsconnector','sipsmsemailoffline',true);
}
return array(
array(
'title' => _('SMS Connector'),
'rawname' => 'smsconnector',
'content' => $this->showPage('userman', array(
'error' => empty($error)?'':$error,
'sipsmsenabled' => $sipSmsEnabled,
'sipsmsdefaultdid' => $defaultDid,
'sipsmsemailoffline' => $emailOffline,
'dids' => !empty($request['user']) ? $this->getDidsByUser($request['user']) : array()
)),
)
);
break;
}
}
}
public function usermanAddUser($id, $display, $data)
{
$this->usermanUpdateUser($id, $display, $data);
}
public function usermanUpdateUser($id, $display, $data)
{
$post = $_POST;
if($display == 'userman' && isset($post['type']) && $post['type'] == 'user')
{
if(isset($post['sipsmsenabled']))
{
if($post['sipsmsenabled'] == "true")
{
$this->Userman->setModuleSettingByID($id, 'smsconnector', 'sipsmsenabled', true);
$this->Userman->setModuleSettingByID($id, 'smsconnector', 'sipsmsdefaultdid', !empty($post['sipsmsdefaultdid']) ? $post['sipsmsdefaultdid'] : null);
if (!empty($post['sipsmsemailoffline']))
{
$this->Userman->setModuleSettingByID($id, 'smsconnector', 'sipsmsemailoffline', ($post['sipsmsemailoffline'] == "true") ? true : false);
}
else
{
$this->Userman->setModuleSettingByID($id, 'smsconnector', 'sipsmsemailoffline', null);
}
}
elseif($post['sipsmsenabled'] == "false")
{
$this->Userman->setModuleSettingByID($id,'smsconnector','sipsmsenabled',false);
}
else
{
$this->Userman->setModuleSettingByID($id, 'smsconnector', 'sipsmsenabled', null);
$this->Userman->setModuleSettingByID($id, 'smsconnector', 'sipsmsdefaultdid', null);
$this->Userman->setModuleSettingByID($id, 'smsconnector', 'sipsmsemailoffline', null);
}
}
}
}
public function usermanDelUser($id, $display, $data) {
freepbx_log(FPBX_LOG_INFO, sprintf(_("SMS Connector received delete request for user id %s ; removing all DID assignments"), $id));
$this->FreePBX->Sms->addUserRouting($id,array(),'Smsconnector'); // "add" with empty array is delete
}
private function loadProviders()
{
include_once dirname(__FILE__) . "/providers/providerBase.php";
$this->providers = array();
foreach (glob(dirname(__FILE__) . "/providers/provider-*.php") as $filename)
{
if (file_exists($filename))
{
include_once $filename;
preg_match('/provider-(.*)\.php/i', $filename, $matches);
$this_provider_name = $matches[1];
$this_provider_name_full = sprintf("FreePBX\modules\Smsconnector\Provider\%s", $this_provider_name);
$this_provider_name_lower = strtolower($this_provider_name);
if(class_exists($this_provider_name_full))
{
$this_provider_class = new $this_provider_name_full();
$this->providers[$this_provider_name_lower]['name'] = $this_provider_class->getName();
$this->providers[$this_provider_name_lower]['nameraw'] = $this_provider_class->getNameRaw();
$this->providers[$this_provider_name_lower]['configs'] = $this_provider_class->getConfigInfo();
$this->providers[$this_provider_name_lower]['webhook'] = $this_provider_class->getWebHookUrl();
$this->providers[$this_provider_name_lower]['class_full'] = $this_provider_name_full;
$this->providers[$this_provider_name_lower]['class_name'] = $this_provider_name;
$this->providers[$this_provider_name_lower]['class'] = $this_provider_class;
}
}
}
}
public function listProviders()
{
return array_keys($this->providers);
}
public function getProvider($name)
{
$return_data = array();
if (empty($name))
{
$return_data = $this->providers;
}
else
{
if (array_key_exists($name, $this->providers))
{
$return_data = $this->providers[$name];
}
}
return $return_data;
}
public function getProviderConfigDefault($name)
{
$data_return = array();
$info = $this->getProvider($name);
foreach ($info['configs'] as $config => $options)
{
$data_return[$config] = isset($options['default']) ? $options['default'] : '';
}
return $data_return;
}
public function getProviderConfig($name)
{
$data_return = array();
$default = $this->getProviderConfigDefault($name);
$setting = $this->getConfig($name, 'provider');
foreach ($default as $option => $value)
{
$data_return[$option] = isset($setting[$option]) ? $setting[$option] : $value;
}
return $data_return;
}
public function setProviderConfig($name, $config)
{
$this->setConfig($name, $config, 'provider');
}
public function myDialplanHooks()
{
return true;
}
public function doDialplanHook(&$ext, $engine, $priority)
{
foreach ($this->getUsersWithDids() as $uid)
{
if ($this->Userman->getModuleSettingByID($uid, 'smsconnector', 'sipsmsenabled', false, false))
{
$user = $this->Userman->getUserByID($uid);
$extension = $user['default_extension'];
$device = $this->FreePBX->Core->getDevice($extension);
if ($device['tech'] == 'pjsip')
{
$de = $this->kvArrayifyDeviceValues($device);
$de['message_context']['value'] = 'smsconnector-messages';
$this->FreePBX->Core->delDevice($extension, true);
$this->FreePBX->Core->addDevice($extension, 'pjsip', $de, true);
}
}
}
}
private function kvArrayifyDeviceValues($values) { // stolen verbatim from Core module
$response = array();
$flag = 2;
$ignoreTheseKeys = array('id', 'tech');
foreach($values as $key => $value) {
if (in_array($key, $ignoreTheseKeys)) {
continue;
}
$response[$key] = array(
'value' => $value,
'flag' => $flag++
);
}
return $response;
}
}