-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbulk_account_settings_destinations.php
More file actions
526 lines (470 loc) · 20.5 KB
/
bulk_account_settings_destinations.php
File metadata and controls
526 lines (470 loc) · 20.5 KB
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
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2025
the Initial Developer. All Rights Reserved.
Contributor(s):
KonradSC <konrd@yahoo.com>
*/
//includes files
require_once dirname(__DIR__, 2) . "/resources/require.php";
require_once "resources/check_auth.php";
require_once "resources/paging.php";
//check permissions
if (!permission_exists('bulk_account_settings_destinations')) {
die("access denied");
}
//add multi-lingual support
$language = new text;
$text = $language->get();
//set options
$destination_options = [];
$destination_options[] = 'destination_actions';
$destination_options[] = 'destination_hold_music';
$destination_options[] = 'destination_record';
$destination_options[] = 'destination_cid_name_prefix';
$destination_options[] = 'destination_enabled';
//use connected database
$domain_uuid = $_SESSION['domain_uuid'] ?? '';
$user_uuid = $_SESSION['user_uuid'] ?? '';
$database = database::new(['config' => config::load(), 'domain_uuid' => $domain_uuid]);
$settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid, 'user_uuid' => $user_uuid]);
//get the http values and set them as variables
$order_by = preg_replace('#[^a-zA-Z0-9_]#', '', $_GET["order_by"] ?? '');
$order = preg_replace('#[^a-zA-Z0-9_]#', '', $_GET["order"] ?? '');
$option_selected = preg_replace('#[^a-zA-Z0-9_]#', '', $_GET["option_selected"] ?? '');
//validate the option_selected
if (!empty($option_selected) && !in_array($option_selected, $destination_options)) {
header("HTTP/1.1 400 Bad Request");
echo "<!DOCTYPE html>\n";
echo "<html>\n";
echo " <head><title>400 Bad Request</title></head>\n";
echo " <body bgcolor=\"white\">\n";
echo " <center><h1>400 Bad Request</h1></center>\n";
echo " </body>\n";
echo "</html>\n";
exit();
}
//handle search term
$parameters = [];
$sql_mod = '';
$search = preg_replace('#[^a-zA-Z0-9_ \-\+]#', '', $_GET["search"] ?? '');
if (!empty($search)) {
$sql_mod = "and ( ";
$sql_mod .= "lower(destination_number) like :search ";
$sql_mod .= "or lower(destination_description) like :search ";
$sql_mod .= "or lower(destination_accountcode) like :search ";
$sql_mod .= ") ";
$parameters['search'] = '%'.strtolower($search).'%';
}
if (empty($order_by)) {
$order_by = "destination_number";
}
//ensure only two possible values for $order
if ($order != 'DESC') {
$order = 'ASC';
}
//get total destination count from the database
$sql = "select count(destination_uuid) as num_rows from v_destinations where domain_uuid = :domain_uuid and destination_type = 'inbound' ".$sql_mod." ";
$parameters['domain_uuid'] = $domain_uuid;
$result = $database->select($sql, $parameters, 'column');
if (!empty($result)) {
$total_destinations = intval($result);
} else {
$total_destinations = 0;
}
unset($sql);
//prepare to page the results
$rows_per_page = intval($settings->get('domain', 'paging', 50));
$param = (!empty($search) ? "&search=".$search : '').(!empty($option_selected) ? "&option_selected=".$option_selected : '');
$page = intval(preg_replace('#[^0-9]#', '', $_GET['page'] ?? 0));
list($paging_controls, $rows_per_page) = paging($total_destinations, $param, $rows_per_page);
list($paging_controls_mini, $rows_per_page) = paging($total_destinations, $param, $rows_per_page, true);
$offset = $rows_per_page * $page;
//get all the destinations from the database
$sql = "SELECT ";
$sql .= "destination_uuid, ";
$sql .= "destination_number, ";
$sql .= "destination_description, ";
$sql .= "destination_accountcode, ";
$sql .= "destination_cid_name_prefix, ";
$sql .= "destination_hold_music, ";
$sql .= "destination_record, ";
$sql .= "destination_actions, ";
$sql .= "destination_enabled ";
$sql .= "FROM v_destinations ";
$sql .= "WHERE domain_uuid = :domain_uuid ";
$sql .= "AND destination_type = 'inbound' ";
//add search mod from above
if (!empty($sql_mod)) {
$sql .= $sql_mod;
}
if ($rows_per_page > 0) {
$sql .= "ORDER BY $order_by $order ";
$sql .= "limit $rows_per_page offset $offset ";
}
$parameters['domain_uuid'] = $domain_uuid;
$destinations = $database->select($sql, $parameters, 'all');
if ($destinations === false) {
$destinations = [];
}
//get the destination select list for action name display
$destination_obj = new destinations;
$destination_array = $destination_obj->all('dialplan');
//function to return the action names in the order defined
function action_name($destination_array, $destination_actions, $settings) {
$actions = [];
if (!empty($destination_array) && is_array($destination_array)) {
if (!empty($destination_actions) && is_array($destination_actions)) {
foreach ($destination_actions as $destination_action) {
if (!empty($destination_action)) {
foreach ($destination_array as $group => $row) {
if (!empty($row) && is_array($row)) {
foreach ($row as $key => $value) {
if ($destination_action == $value) {
if ($group == 'other') {
if (file_exists(dirname(__DIR__, 2)."/app/dialplans/app_languages.php")) {
$language2 = new text;
$text2 = $language2->get($settings->get('domain', 'language', 'en-us'), 'app/dialplans');
}
$actions[] = $text2['title-other'].' › '.$text2['option-'.str_replace('_','_',$key)];
}
else {
// For extensions, just show the extension number and description (the $key value)
// For other apps, show the app name with the key
if ($group == 'extensions') {
$actions[] = $key;
}
else {
if (file_exists(dirname(__DIR__, 2)."/app/".$group."/app_languages.php")) {
$language3 = new text;
$text3 = $language3->get($settings->get('domain', 'language', 'en-us'), 'app/'.$group);
$actions[] = $text3['title-'.$group].' › '.$key;
}
}
}
}
}
}
}
}
}
}
}
return $actions;
}
//additional includes
$document['title'] = $text['title-destination_settings'];
require_once "resources/header.php";
//show the content
echo "<div class='action_bar' id='action_bar'>\n";
echo " <div class='heading'>\n";
echo " <b>".$text['header-destinations']."</b><div class='count'>".number_format($total_destinations)."</div><br><br>\n";
echo " ".$text['description-destination_settings']."\n";
echo " </div>\n";
echo " <div class='actions'>\n";
echo " <form method='get' action=''>\n";
echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$settings->get('theme', 'button_icon_back'),'id'=>'btn_back','style'=>'margin-right: 15px; position: sticky; z-index: 5;','onclick'=>"window.location='bulk_account_settings.php'"]);
echo "<input type='text' class='txt list-search' name='search' id='search' style='margin-left: 0 !important;' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown=''>";
echo " <input type='hidden' class='txt' style='width: 150px' name='option_selected' id='option_selected' value='".escape($option_selected)."'>";
echo " <form id='form_search' class='inline' method='get'>\n";
echo button::create(['label'=>$text['button-search'],'icon'=>$settings->get('theme', 'button_icon_search'),'type'=>'submit','id'=>'btn_search']);
if (!empty($paging_controls_mini)) {
echo " <span style='margin-left: 15px;'>".$paging_controls_mini."</span>\n";
}
echo " </form>\n";
echo " </form>\n";
echo " </div>\n";
echo " <div style='clear: both;'></div>\n";
echo "</div>\n";
//options list
echo "<div class='card'>\n";
echo "<div class='form_grid'>\n";
echo " <div class='form_set'>\n";
echo " <div class='label'>\n";
echo " ".$text['label-setting']."\n";
echo " </div>\n";
echo " <div class='field'>\n";
echo " <form name='frm' method='get' id='option_selected'>\n";
echo " <select class='formfld' name='option_selected' onchange=\"this.form.submit();\">\n";
echo " <option value=''></option>\n";
foreach ($destination_options as $option) {
echo " <option value='".$option."' ".($option_selected === $option ? "selected='selected'" : null).">".$text['label-'.$option]."</option>\n";
}
echo " </select>\n";
echo " </form>\n";
echo " </div>\n";
echo " </div>\n";
if (!empty($option_selected)) {
echo " <div class='form_set'>\n";
echo " <div class='label'>\n";
echo " ".$text['label-value']."";
echo " </div>\n";
echo " <div class='field'>\n";
echo " <form name='destinations' method='post' action='bulk_account_settings_destinations_update.php'>\n";
echo " <input class='formfld' type='hidden' name='option_selected' maxlength='255' value=\"".escape($option_selected)."\">\n";
//destination_actions - use destinations select
if ($option_selected == 'destination_actions') {
$destination_obj = new destinations;
echo $destination_obj->select('dialplan', 'new_setting', '');
}
//text input
if ($option_selected == 'destination_cid_name_prefix') {
echo " <input class='formfld' type='text' name='new_setting' maxlength='255' value=''>\n";
}
//enabled
if ($option_selected === 'destination_enabled') {
echo " <select class='formfld' name='new_setting'>\n";
echo " <option value='true'>".$text['label-true']."</option>\n";
echo " <option value='false'>".$text['label-false']."</option>\n";
echo " </select>\n";
}
//record
if ($option_selected == 'destination_record') {
echo " <select class='formfld' name='new_setting'>\n";
echo " <option value=''></option>\n";
echo " <option value='true'>".$text['label-true']."</option>\n";
echo " <option value='false'>".$text['label-false']."</option>\n";
echo " </select>\n";
}
//hold music
if ($option_selected == 'destination_hold_music') {
if (is_dir($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/app/music_on_hold')) {
require_once "app/music_on_hold/resources/classes/switch_music_on_hold.php";
$options = '';
$moh = new switch_music_on_hold;
echo $moh->select('new_setting', '', $options);
}
}
echo " </div>\n";
echo " </div>\n";
echo "</div>\n";
echo "<div style='display: flex; justify-content: flex-end; padding-top: 15px; margin-left: 20px; white-space: nowrap;'>\n";
echo button::create(['label'=>$text['button-reset'],'icon'=>$settings->get('theme', 'button_icon_reset'),'type'=>'button','link'=>'bulk_account_settings_destinations.php']);
echo button::create(['label'=>$text['button-update'],'icon'=>$settings->get('theme', 'button_icon_save'),'type'=>'submit','id'=>'btn_update','click'=>"if (confirm('".$text['confirm-update_destinations']."')) { document.forms.destinations.submit(); }"]);
echo "</div>\n";
}
else {
echo "</div>\n";
}
echo "</div>\n";
echo "<br />\n";
echo "<div class='card'>\n";
echo "<table class='list'>\n";
echo "<tr class='list-header'>\n";
if (!empty($destinations)) {
echo "<th style='width: 30px; text-align: center; padding: 0px;'><input type='checkbox' id='chk_all' onchange=\"(this.checked) ? check('all') : check('none');\"></th>";
}
echo th_order_by('destination_number', $text['label-destination_number'], $order_by, $order, null, null, $param);
if (!empty($option_selected) && $option_selected != 'destination_description') {
echo th_order_by($option_selected, $text["label-".$option_selected.""], $order_by, $order, null, null, $param);
}
echo th_order_by('destination_description', $text['label-description'], $order_by, $order, null, null, $param);
echo "</tr>\n";
$dest_ids = [];
if (!empty($destinations)) {
foreach($destinations as $key => $row) {
$list_row_url = permission_exists('destination_edit') ? "/app/destinations/destination_edit.php?id=".urlencode($row['destination_uuid']) : null;
echo "<tr class='list-row' href='".$list_row_url."'>\n";
echo " <td class='checkbox'>";
echo " <input type='checkbox' name='id[]' id='checkbox_".escape($row['destination_uuid'])."' value='".escape($row['destination_uuid'])."' onclick=\"if (!this.checked) { document.getElementById('chk_all').checked = false; }\">";
echo " </td>";
$dest_ids[] = 'checkbox_'.$row['destination_uuid'];
echo " <td><a href='".$list_row_url."'>".escape(format_phone($row['destination_number']))."</a></td>\n";
if (!empty($option_selected) && $option_selected != 'destination_description') {
$value = $row[$option_selected] ?? '';
// DEBUG: Show raw value for destination_actions
if ($option_selected == 'destination_actions' && !empty($value)) {
// Uncomment this line temporarily to see the raw data:
// echo "<!-- RAW DATA: " . htmlspecialchars($value) . " -->";
}
// Convert boolean-like values to true/false labels for display
if ($option_selected == 'destination_record' || $option_selected == 'destination_enabled') {
if ($value === '1' || $value === 'true' || $value === true) {
$value = $text['label-true'];
} elseif ($value === '0' || $value === 'false' || $value === false || $value === '' || $value === null) {
$value = $text['label-false'];
}
}
// Clean up hold music display - show just the name without local_stream:// prefix
if ($option_selected == 'destination_hold_music') {
if (empty($value) || $value === 'silence') {
$value = $text['label-none'] ?? 'None';
} elseif (strpos($value, 'local_stream://') === 0) {
$value = str_replace('local_stream://', '', $value);
}
}
// Parse destination_actions JSON and display readable names
if ($option_selected == 'destination_actions') {
if (!empty($value)) {
// Decode the JSON
$actions_json = json_decode($value, true);
if (json_last_error() === JSON_ERROR_NONE && !empty($actions_json) && is_array($actions_json)) {
$action_display = [];
foreach ($actions_json as $action) {
if (!empty($action['destination_app'])) {
$app = $action['destination_app'];
$data = $action['destination_data'] ?? '';
// Handle each action type
if ($app === 'transfer' || $app === 'bridge') {
// Check for voicemail first (*99 pattern)
if (preg_match('/^\*99(\d+)/', $data, $matches)) {
$ext_number = $matches[1];
// Look up extension description
$sql_ext = "SELECT description FROM v_extensions WHERE domain_uuid = :domain_uuid AND extension = :extension LIMIT 1";
$ext_result = $database->select($sql_ext, ['domain_uuid' => $domain_uuid, 'extension' => $ext_number], 'row');
if (!empty($ext_result['description'])) {
$action_display[] = $ext_number . ' ' . $ext_result['description'] . ' (Voicemail)';
} else {
$action_display[] = $ext_number . ' (Voicemail)';
}
}
// Check for *98 check voicemail
elseif (preg_match('/^\*98\s+XML/i', $data)) {
$action_display[] = 'Check Voicemail';
}
// Check for *732 record
elseif (preg_match('/^\*732/i', $data)) {
$action_display[] = 'Record';
}
// Check for *411 company directory
elseif (strpos($data, '*411') !== false) {
$action_display[] = 'Company Directory';
}
// Regular transfer - try to parse extension
else {
$transfer_dest = $data;
// Clean up common prefixes
$transfer_dest = preg_replace('/\s*XML.*$/i', '', $transfer_dest);
$transfer_dest = preg_replace('/sofia\/internal\//i', '', $transfer_dest);
$transfer_dest = preg_replace('/@.*$/', '', $transfer_dest);
$transfer_dest = trim($transfer_dest);
// If it's numeric, look up extension description
if (is_numeric($transfer_dest)) {
$sql_ext = "SELECT description FROM v_extensions WHERE domain_uuid = :domain_uuid AND extension = :extension LIMIT 1";
$ext_result = $database->select($sql_ext, ['domain_uuid' => $domain_uuid, 'extension' => $transfer_dest], 'row');
if (!empty($ext_result['description'])) {
$action_display[] = $transfer_dest . ' ' . $ext_result['description'];
} else {
$action_display[] = $transfer_dest;
}
} else {
$action_display[] = $transfer_dest;
}
}
}
elseif ($app === 'voicemail') {
// Direct voicemail app (legacy format)
$ext_number = '';
if (preg_match('/\*99(\d+)/', $data, $matches)) {
$ext_number = $matches[1];
} elseif (preg_match('/^(\d+)\s/', $data, $matches)) {
$ext_number = $matches[1];
}
if (!empty($ext_number)) {
$sql_ext = "SELECT description FROM v_extensions WHERE domain_uuid = :domain_uuid AND extension = :extension LIMIT 1";
$ext_result = $database->select($sql_ext, ['domain_uuid' => $domain_uuid, 'extension' => $ext_number], 'row');
if (!empty($ext_result['description'])) {
$action_display[] = $ext_number . ' ' . $ext_result['description'] . ' (Voicemail)';
} else {
$action_display[] = $ext_number . ' (Voicemail)';
}
} else {
$action_display[] = 'Voicemail';
}
}
elseif ($app === 'playback' || $app === 'play') {
// For playback/tones, use action_name to get the proper tone name
if (!empty($data)) {
$destination_app_data = [$app.':'.$data];
$action_names = action_name($destination_array, $destination_app_data, $settings);
if (!empty($action_names)) {
// Use the tone name from action_name
$action_display[] = $action_names[0];
} else {
// Fallback: try to extract from path or show filename
if (preg_match('/\$\{([^}]+)\}/i', $data, $matches)) {
$action_display[] = $matches[1];
} else {
$basename = basename($data);
$action_display[] = $basename;
}
}
} else {
$action_display[] = 'Playback';
}
}
elseif ($app === 'hangup') {
$action_display[] = 'Hangup';
}
elseif ($app === 'answer') {
$action_display[] = 'Answer';
}
elseif ($app === 'record' || $app === 'record_session') {
$action_display[] = 'Record';
}
elseif ($app === 'check_voicemail') {
$action_display[] = 'Check Voicemail';
}
elseif ($app === 'directory' || $app === 'company_directory') {
$action_display[] = 'Company Directory';
}
else {
// Default: format the app name nicely
$action_display[] = ucwords(str_replace('_', ' ', $app));
}
}
}
$value = (!empty($action_display)) ? implode(', ', $action_display) : '';
}
}
if (empty($value)) {
$value = $text['label-none'] ?? 'None';
}
}
// Output the value
echo " <td>".escape($value)." </td>\n";
}
echo " <td>".escape($row['destination_description'])." </td>\n";
echo "</tr>\n";
}
}
echo "</table>\n";
echo "</div>\n";
echo "</form>\n";
if (!empty($paging_controls)) {
echo "<br />\n";
echo $paging_controls."\n";
}
echo "<br /><br />".((!empty($destinations)) ? "<br /><br />" : null);
// check or uncheck all checkboxes
if (!empty($dest_ids)) {
echo "<script>\n";
echo " function check(what) {\n";
echo " document.getElementById('chk_all').checked = (what == 'all') ? true : false;\n";
foreach ($dest_ids as $dest_id) {
echo " document.getElementById('".$dest_id."').checked = (what == 'all') ? true : false;\n";
}
echo " }\n";
echo "</script>\n";
}
if (!empty($destinations)) {
// check all checkboxes
key_press('ctrl+a', 'down', 'document', null, null, "check('all');", true);
// delete checked
key_press('delete', 'up', 'document', array('#search'), $text['confirm-delete'], 'document.forms.frm.submit();', true);
}
//show the footer
require_once "resources/footer.php";