-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebookDialer.php
132 lines (126 loc) · 3.69 KB
/
phonebookDialer.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
<?php
// Phonebook Dialer.
// (c) Mat Phillips 2016
// ----- configure values:
$startExtension = 200; // lowest extension number
$endExtension = 399; // highest extension number
$startGroup = 500; // lowest group number
$endGroup = 520; // highest group number
// -----
$xtn = array_key_exists('xtn', $_GET) ? $_GET['xtn'] : NULL;
$mode = array_key_exists('mode', $_GET) ? $_GET['mode'] : "list";
header ("content-type: application/json");
header('Access-Control-Allow-Origin: *');
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
include_once('/etc/asterisk/freepbx.conf');
}
if ($mode == "list") {
global $db;
$sql = "
SELECT /* regular extension numbers */
users.name AS 'name'
,users.extension
,'xtn' AS 'type'
FROM users
WHERE extension BETWEEN $startExtension AND $endExtension
UNION
SELECT /* additional cell numbers */
userman_users.displayname AS 'name'
,userman_users.cell AS 'number'
,'Cell' AS 'type'
FROM userman_users
WHERE userman_users.cell IS NOT NULL
UNION
SELECT /* additional work numbers */
userman_users.displayname AS 'name'
,userman_users.work AS 'number'
,'Work' AS 'type'
FROM userman_users
WHERE userman_users.work IS NOT NULL
UNION
SELECT /* additional fax numbers */
userman_users.displayname AS 'name'
,userman_users.fax AS 'fax'
,'Fax' AS 'type'
FROM userman_users
WHERE userman_users.fax IS NOT NULL
UNION
SELECT /* additional home numbers */
userman_users.displayname AS 'name'
,userman_users.home AS 'number'
,'Home' AS 'type'
FROM userman_users
WHERE userman_users.home IS NOT NULL
UNION
SELECT /* ring groups */
ringgroups.description AS 'name'
,ringgroups.grpnum
,'Group' AS 'type'
FROM ringgroups
WHERE grpnum < 599
UNION
SELECT /* custom contacts */
CONCAT(contactmanager_group_entries.displayname, ' (custom)') AS 'name'
,contactmanager_entry_numbers.number
,contactmanager_entry_numbers.type AS 'type'
FROM contactmanager_groups
LEFT JOIN contactmanager_group_entries ON contactmanager_groups.id = contactmanager_group_entries.groupid
LEFT JOIN contactmanager_entry_numbers ON contactmanager_group_entries.id = contactmanager_entry_numbers.entryid
WHERE contactmanager_groups.name = '$xtn'
ORDER BY name, type DESC
";
$results = $db->getAll($sql, DB_FETCHMODE_ORDERED);
$numrows = count($results);
$data = array();
$oldName = "";
$numbersArray = array();
for ($i=0; $i < $numrows; $i++) {
$number = $results[$i][1];
$name = $results[$i][0];
$type = $results[$i][2];
if ($name && $number) {
if ($name != $oldName && $oldName != "") {
array_push(
$data, array(
"name" => $oldName,
"numbers" => $numbersArray
)
);
$numbersArray = array();
}
array_push(
$numbersArray, array(
"type" => $type,
"number" => $number
)
);
$oldName = $name;
}
}
echo json_encode($data, JSON_HEX_QUOT | JSON_HEX_TAG);
}
if ($mode == "dial") {
global $astman;
$destination = array_key_exists('destination', $_GET) ? $_GET['destination'] : "";
$strContext = "from-internal";
$strChannel = "local/$xtn@$strContext";
$strWaitTime = "5000";
$strPriority = "1";
$strMaxRetry = "1";
$strAsync = 'no';
if (strlen($destination) > 2 && strlen($xtn) > 2) {
$destination = filter_var($destination, FILTER_SANITIZE_NUMBER_INT);
$destination = preg_replace("/[^0-9,.]/", "", $destination);
$dial = array(
'Channel' => $strChannel
,'Context' => $strContext
,'Exten' => $destination
,'Priority' => $strPriority
,'Async' => $strAsync
,'Timeout' => $strWaitTime
,'CallerID' => '"phonebookDialer" <'.$destination.'>'
);
var_dump ($astman->Originate($dial));
}
}
?>