forked from waynemerricks/Cisco7942FreePBX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddressbook.php
223 lines (156 loc) · 6.27 KB
/
addressbook.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
<?php
//Make sure we're xml otherwise the phone will not parse correctly
header('Content-type: text/xml');
//Get the url of this page so we can do page forward/back requests
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
//Mode if we're searching by first, last or number
$mode = '';
if(isset($_GET['name']))
$mode = 'name';
else if(isset($_GET['number']))
$mode = 'number';
else{
/* Nothing was searched for on this page so return an error via the
* CiscoIPPhoneText item */
$error = showCiscoPhoneError('Search Error',
'Please check and try again',
'There was an error searching the address book, no search terms were set');
echo $error -> asXML();
exit(1);
}
$xml = showAddresses($mode, $url);
echo $xml -> asXML();
/**
* Returns an error message in the form of a CiscoIPPhoneText XML snippet
* @param $title: Title shown at top of phone screen
* @param $prompt: Prompt shown at bottom of phone screen
* @param $text: Text to show in middle of phone screen
* @return Returns properly formatted XML snippet for Cisco 7942 and
* compatible phones */
function showCiscoPhoneError($title, $prompt, $text){
$xml = new SimpleXMLElement('<CiscoIPPhoneText/>');
$xml -> addChild('Title', $title);
$xml -> addChild('Prompt', $prompt);
$xml -> addChild('Text', $text);
addSoftKey($xml, 'Exit', 'SoftKey:Exit', 1);
return $xml;
}
/**
* Adds a Cisco SoftKeyItem to the given XML object
* @param $xml: SimpleXMLElement to act upon
* @param $name: Name of the Key displayed on the phone (8 char limit)
* @param $url: URL to call when key pressed
* Built in URLs:
* SoftKey:Exit - Exits
* SoftKey:Dial - Dials selected
* @param $position: Position for soft key 1 - 4
*/
function addSoftKey($xml, $name, $url, $position){
$softKey = $xml -> addChild('SoftKeyItem');
$softKey -> addChild('Name', $name);
$softKey -> addChild('URL', $url);
$softKey -> addChild('Position', $position);
}
//Reads the /etc/freepbx.conf and parses out the DB array config
function getFreePBXDatabase(){
$freepbx = file_get_contents('/etc/freepbx.conf');
$lines = explode("\n", $freepbx);
$DB = array();
//for each line strip to VAR = "BLAH"; then keep the BLAH bit
foreach($lines as $line){
$parts = explode(' = ', $line);
if($parts[0] == '$amp_conf["AMPDBUSER"]')
$DB['AMPDBUSER'] = substr(str_replace('"', '', $parts[1]), 0, -1);
else if($parts[0] == '$amp_conf["AMPDBPASS"]')
$DB['AMPDBPASS'] = substr(str_replace('"', '', $parts[1]), 0, -1);
else if($parts[0] == '$amp_conf["AMPDBHOST"]')
$DB['AMPDBHOST'] = substr(str_replace('"', '', $parts[1]), 0, -1);
else if($parts[0] == '$amp_conf["AMPDBNAME"]')
$DB['AMPDBNAME'] = substr(str_replace('"', '', $parts[1]), 0, -1);
}
return $DB;
}
/**
* Queries FreePBX and produces xml suitable for Cisco 7942s
* More Info and models available from Cisco:
* https://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cuipph/all_models/xsi/8_5_1/xsi_dev_guide/xmlobjects.html
*/
function showAddresses($mode, $url){
$DB = getFreePBXDatabase();
//Setup PDO connection and options
$dsn = 'mysql:host=' . $DB['AMPDBHOST'] . ';dbname=' . $DB['AMPDBNAME'];
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = NULL;
//Connect to MySQL
try {
$pdo = new PDO($dsn, $DB['AMPDBUSER'], $DB['AMPDBPASS'], $options);
} catch (\PDOException $e) {
//Format Exception as XML
return showCiscoPhoneError('MySQL Connect Error', 'Please inform IT',
'There was an error connecting to the MySQL database (' . $e->getCode() . '): ' . $e->getMessage());
}
$stmt = NULL;
$query = NULL;
switch($mode) {
case 'number':
$sql = 'SELECT name, extension FROM users WHERE extension LIKE :user
UNION
SELECT description AS name, grpnum AS extension FROM ringgroups WHERE grpnum LIKE :group
ORDER BY extension, name';
$query = '%' . $_GET['number'] . '%';
break;
case 'name':
$sql = 'SELECT name, extension FROM users WHERE name LIKE :user
UNION
SELECT description AS name, grpnum AS extension FROM ringgroups WHERE description LIKE :group
ORDER BY name, extension';
$query = '%' . $_GET['name'] . '%';
break;
}
$stmt = $pdo->prepare($sql);
$stmt->execute([$query, $query]);
$extensions = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(sizeof($extensions) == 0){
return showCiscoPhoneError('No results', 'Try again', 'No results found for ' . $query);
} else {
//We have results format as phonedirectory
$xml = new SimpleXMLElement('<CiscoIPPhoneDirectory/>');
$xml -> addChild('Title', 'Grace Academy');
$xml -> addChild('Prompt', 'Dial selected');
//Paginate results, 31 items max (need 1 result extra for next page item)
$start = 0;
$page = 0;
if(isset($_GET['page']))
$page = (int)$_GET['page'];
$start = $page * 32;
//Check to see if we need more pages
$morePages = false;
if(sizeof($extensions) > $start + 31)
$morePages = true;
$row = $start;
while($row < sizeof($extensions) && $row < $start + 31){
$entry = $xml -> addChild('DirectoryEntry');
$entry -> addChild('Name', $extensions[$row]['name']);
$entry -> addChild('Telephone', $extensions[$row]['extension']);
$row++;
}
//Add the softkeys to the results
addSoftKey($xml, 'Dial', 'SoftKey:Dial', 1);
addSoftKey($xml, 'Exit', 'SoftKey:Exit', 2);
//Check if we need a previous page button
if($page > 0)
addSoftKey($xml, 'Previous', 'SoftKey:Exit', 3);
//Check if we need a next page button
if($morePages){
$query = str_replace('%', '', $query);
//& required as & not valid xml
addSoftKey($xml, 'Next', $url . '?' . $mode . '=' . $query . '&page=' . ++$page, 4);
}
}
return $xml;
}
?>