-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibAuer.php
405 lines (357 loc) · 10.8 KB
/
libAuer.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
<?php
/**
* libAuer: Auerswald API functions for PHP
*
* Used for limited control over the Auerswald PBX system
*
* @author Andreas Thienemann
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
include "group_manager.cfg.php";
/**
* The language codes the PBX uses
*
* @link: http://192.168.0.240/languages_state
*/
$auer_languages = array(
0 => "deutsch",
3 => "français",
7 => "english",
);
$auer_hw_typ = array(
0 => "unbekannt",
1 => "Analog",
2 => "ISDN",
3 => "VOIP",
);
$auer_module_type = array(
0 => 'MODULE_TYPE_NONE',
4 => 'MODULE_TYPE_8AB',
8 => 'MODULE_TYPE_4S0',
10 => 'MODULE_TYPE_8S0',
12 => 'MODULE_TYPE_8UP0',
14 => 'MODULE_TYPE_2TSM',
15 => 'MODULE_TYPE_ETH1',
36 => 'MODULE_TYPE_S2M',
56 => 'MODULE_TYPE_VOIP8',
58 => 'MODULE_TYPE_VOIP16',
60 => 'MODULE_TYPE_VMF',
74 => 'MODULE_TYPE_4AB',
76 => 'MODULE_TYPE_2S0UP0',
78 => 'MODULE_TYPE_2POTS',
80 => 'MODULE_TYPE_4DSP',
128 => 'MODULE_TYPE_CPU',
254 => 'MODULE_TYPE_UNKNOWN',
);
$auer_hw_type = array(
0 => 'AUERDB_HW_TYP_UNDEF',
1 => 'AUERDB_HW_TYP_ANALOG',
2 => 'AUERDB_HW_TYP_ISDN',
3 => 'AUERDB_HW_TYP_VOIP',
4 => 'AUERDB_HW_TYP_TSM',
);
$auer_device_type = array(
0 => 'AUERDB_TN_TYP_ANALOG_UNSPZ',
33 => 'AUERDB_TN_TYP_ANRUFBEANTW',
16 => 'AUERDB_TN_TYP_ISDN_TELEFON',
34 => 'AUERDB_TN_TYP_COMFORT',
35 => 'AUERDB_TN_TYP_ISDN_PC',
36 => 'AUERDB_TN_TYP_COMFORT_DECT',
37 => 'AUERDB_TN_TYP_SIP_AUER',
38 => 'AUERDB_TN_TYP_SIP_OTHER',
39 => 'AUERDB_TN_TYP_FAX',
40 => 'AUERDB_TN_TYP_IP_DECT_AUER',
41 => 'AUERDB_TN_TYP_IP_DECT',
42 => 'AUERDB_TN_TYP_SIP_CFT_2500',
43 => 'AUERDB_TN_TYP_SIP_1200IP',
44 => 'AUERDB_TN_TYP_SIP_CFT_C_400',
45 => 'AUERDB_TN_TYP_IP_M5x0_M7x0',
46 => 'AUERDB_TN_TYP_SOFTPHONE',
);
/**
* Generic function to read data from Auerswald PBX
*
* @param string $url URL to fetch
* @param string $username Username to authenticate with
* @param string $password Password to authenticate with
* @return string The server response.
*/
function fetch_auer($url, $username, $password) {
static $storedCookie = null;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_USERPWD => "$username:$password",
CURLOPT_COOKIE => $storedCookie ? "AUERSessionID=$storedCookie" : '',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
exit();
}
curl_close($ch);
if (preg_match('/^Set-Cookie:\s*AUERSessionID=([^;]*)/mi', $response, $matches)) {
$storedCookie = $matches[1];
}
return $response;
}
/**
* Performs an HTTP POST request with digest authentication and cookie handling.
*
* @param string $url The URL to which the request will be sent.
* @param string $username The username for HTTP authentication.
* @param string $password The password for HTTP authentication.
* @param array $data The data to be sent in the POST request.
* @return string The response from the server.
*/
function post_auer($url, $username, $password, $data) {
static $storedCookie = null;
$ch = curl_init();
// The Auerswald device seems to need the bang character unencoded.
$postData = http_build_query($data, "", "&", PHP_QUERY_RFC3986);
$postData = str_replace("%21", "!", $postData);
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_USERPWD => "$username:$password",
CURLOPT_COOKIE => $storedCookie ? "AUERSessionID=$storedCookie" : '',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'),
CURLOPT_POSTFIELDS => $postData,
));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
exit();
}
curl_close($ch);
if (preg_match('/^Set-Cookie:\s*AUERSessionID=([^;]*)/mi', $response, $matches)) {
$storedCookie = $matches[1];
}
return $response;
}
/**
* Get all internal users/extensions
*
* @return mixed[]
*/
function get_extensions() {
global $auer_address, $auer_admin_user, $auer_admin_pass, $auer_hw_typ;
$devices = array();
foreach (json_decode(fetch_auer(sprintf("https://%s/devices_state", $auer_address),
$auer_admin_user, $auer_admin_pass))->{"rows"} as $row) {
$device = array();
// CELL_DEV_ID 0
$device["id"] = $row->{"data"}[0];
// CELL_DEV_TYPE 1
switch ($row->{"data"}[1]) {
case 0:
$device["typ"] = "Teilnehmer";
break;
case 1:
$device["typ"] = "Türstation";
break;
}
// CELL_DEV_NR 2
$device["nr"] = $row->{"data"}[2];
// CELL_DEV_NAME 3
$device["name"] = $row->{"data"}[3];
// CELL_DEV_MODULE 4
// $device["modulePort"] = $row->{"data"}[4];
$device["modulePort"] = $row->{"userdata"}->{"module"};
// CELL_DEV_HWTYPE 5
// $device["hw"] = $row->{"data"}[5];
$device["hw"] = $auer_hw_typ[$row->{"userdata"}->{"hwTyp"}] ?? $auer_hw_typ[0];
$device["date"] = "";
// CELL_DEV_SYSTEL
$device["systel"] = $row->{"data"}[6];
// CELL_DEV_STATE
// $device["state"] = $row->{"data"}[7];
$device["state"] = array();
$device["userdata"] = (array)$row->{"userdata"};
$devices[] = $device;
}
usort($devices, function ($a, $b) { return $a['nr'] - $b['nr']; });
return $devices;
}
/**
* Get info about an extension identified by IP address
*
* @param string $ip The IP address of the VOIP phone
* @return mixed[]|false The single extension information
*/
function find_extension_by_ip($ip) {
foreach (get_extensions() as $k => $v) {
if ($v["userdata"]["ipAddr"] == $ip) {
return $v;
}
}
return false;
}
/**
* Get info about an extension identified by DECT IPUI
*
* This only works on a COMfortel WS-500 system
*
* @param string $ipui The IPUI address of the DECT phone
* @return mixed[]|false The single extension information
*/
function find_extension_by_ipui($ipui) {
foreach (get_extensions() as $k => $v) {
if ($v["userdata"]["ipui"] == $ipui) {
return $v;
}
}
return false;
}
/**
* Get groups configuration from PBX
*
* @return mixed[] The groups configuration from the PBX.
*/
function get_groups_configuration() {
global $auer_address, $auer_admin_user, $auer_admin_pass;
$groups = array();
foreach (json_decode(fetch_auer(sprintf("https://%s/cfg_data_gruppen", $auer_address),
$auer_admin_user, $auer_admin_pass)) as $row) {
$groups[$row->{"nr"}] = array("id" => $row->{"id"}, "name" => $row->{"name"});
}
return $groups;
}
/**
* Get group members from PBX
*
* @param int $gid Group ID
* @return mixed[]
*/
function get_group_members($gid) {
global $auer_address, $auer_admin_user, $auer_admin_pass;
$members = array();
foreach (json_decode(fetch_auer(sprintf("https://%s/gruppetnzuordnung_tngrp?grpId=%d", $auer_address, $gid),
$auer_admin_user, $auer_admin_pass))->{"rows"} as $mb) {
$member = array(
"id" => $mb->{"data"}[0],
"name_raw" => $mb->{"data"}[1],
"type" => null,
"nr" => null,
"name" => null,
"prio" => $mb->{"data"}[3],
"coming" => $mb->{"data"}[4],
"going" => $mb->{"data"}[5],
"delay" => $mb->{"data"}[6],
);
list($member["type"], $tmp) = explode(":", $mb->{"data"}[1]);
list($member["nr"], $member["name"]) = array_map("trim", explode("|", $tmp));
$members[$member["nr"]] = $member;
}
return $members;
}
/**
* Get a groups overview for an extension number
*
* @param int $extension The pbx extension to fetch group info for.
* @return mixed[] The groups info for the specific extension.
*/
function get_groups_for_extension($extension) {
global $auer_address, $auer_admin_user, $auer_admin_pass, $auer_hw_typ;
foreach (json_decode(fetch_auer(sprintf("https://%s/tngrpuebersicht_state", $auer_address),
$auer_admin_user, $auer_admin_pass))->{"rows"} as $row) {
$id = $row->{"id"};
list($nr, $name) = array_map("trim", explode("|", $row->{"data"}[0]));
$g_nrs = array();
$g_names = array();
foreach (explode("<br>", $row->{"data"}[1]) as $group) {
list($g_nr, $g_name) = array_map("trim", explode("|", $group));
$g_nrs[] = $g_nr;
$g_names[] = $g_name;
}
// Translate german strings into boolean values
$coming = array_map(function($value) {
return $value === "Eingeloggt" ? true : false;
}, explode("<br>", $row->{"data"}[2]));
$going = array_map(function($value) {
return $value === "Eingeloggt" ? true : false;
}, explode("<br>", $row->{"data"}[3]));
// Combine data into $groups array
$groups = array();
for ($i = 0; $i < count($g_nrs); $i++) {
$groups[$g_nrs[$i]] = array(
"name" => $g_names[$i],
"coming" => $coming[$i],
"going" => $going[$i],
);
}
if ($nr == $extension) {
return $groups;
}
}
}
/**
* Log into a group
*
* @param int $group Group number to log into.
* @param int $extension The extension that should be logged in.
* @param string $direction Direction to log into for _coming_ or _going_.
* @param int $state Whether to be logged out (0), logged in (1) or fixed (2).
* @param bool $delay Ring delay. (not implemented)
* return bool
*/
function _group_update($group, $extension, $direction, $state, $delay=false) {
global $auer_address, $auer_admin_user, $auer_admin_pass;
$groups = get_groups_configuration();
$gid = $groups[$group]["id"];
$mb = get_group_members($gid)[$extension] or die("Extension not found...\n");
$mbid = $mb["id"];
$data = array(
"${mbid}_gr_id" => $mbid,
"${mbid}_btnup" => "",
"${mbid}_rufnName" => $mb["name_raw"],
"${mbid}_btndown" => "",
"${mbid}_prio" => $mb["prio"],
"${mbid}_grpKomEingeloggt" => $mb["coming"],
"${mbid}_grpGehEingeloggt" => $mb["going"],
"${mbid}_klingelVerz" => $mb["delay"],
"${mbid}_realTn" => $mb["type"] == "Tn" ? 1 : 0,
"${mbid}_!nativeeditor_status" => "updated",
"ids" => $mbid,
);
if ($direction == "going") {
$data["${mbid}_grpGehEingeloggt"] = $state;
} elseif ($direction == "coming") {
$data["${mbid}_grpKomEingeloggt"] = $state;
} else {
die("Incorrect destination");
}
post_auer(sprintf("https://%s/gruppetnzuordnung_save?grpId=%d", $auer_address, $gid),
$auer_admin_user, $auer_admin_pass, $data) or die("Error updating");
return true;
}
/**
* Log-In to a group
*
* @param int $group Group extension numbers
* @param int $extension Extension number
* @param string $direction _coming_ or _going_ group.
* @return bool
*/
function login_group($group, $extension, $direction) {
return _group_update($group, $extension, $direction, 1);
}
/**
* Log-Out of a group
*
* @param int $group Group extension numbers
* @param int $extension Extension number
* @param string $direction _coming_ or _going_ group.
* @return bool
*/
function logout_group($group, $extension, $direction) {
return _group_update($group, $extension, $direction, 0);
}