fusionpbx/app/devices/devices.php

372 lines
15 KiB
PHP
Raw Normal View History

<?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>
2019-11-07 17:00:29 +01:00
Portions created by the Initial Developer are Copyright (C) 2008 - 2019
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
*/
//includes
require_once "root.php";
require_once "resources/require.php";
require_once "resources/check_auth.php";
2019-11-07 17:00:29 +01:00
require_once "resources/paging.php";
//check permissions
if (permission_exists('device_view')) {
//access granted
}
else {
echo "access denied";
exit;
}
//add multi-lingual support
$language = new text;
$text = $language->get();
2019-11-07 17:00:29 +01:00
//get posted data
if (is_array($_POST['devices'])) {
$action = $_POST['action'];
$search = $_POST['search'];
$devices = $_POST['devices'];
}
//toggle the devices
if (permission_exists('device_edit')) {
if ($action == 'toggle' && is_array($devices) && @sizeof($devices) != 0) {
//toggle
$obj = new device;
$obj->toggle($devices);
//redirect
header('Location: devices.php'.($search != '' ? '?search='.urlencode($search) : null));
exit;
}
}
//delete the devices
if (permission_exists('device_delete')) {
if ($action == 'delete' && is_array($devices) && @sizeof($devices) != 0) {
//delete
$obj = new device;
$obj->delete($devices);
//redirect
header('Location: devices.php'.($search != '' ? '?search='.urlencode($search) : null));
exit;
}
}
//get order and order by and sanatize the values
2019-08-04 04:21:56 +02:00
$order_by = $_GET["order_by"];
$order = $_GET["order"];
2015-09-05 06:19:11 +02:00
//get total devices count from the database
2019-08-04 04:21:56 +02:00
$sql = "select count(*) from v_devices ";
$sql .= "where domain_uuid = :domain_uuid ";
if (!permission_exists('device_all') && !permission_exists('device_domain_all')) {
$sql .= "and device_user_uuid = :user_uuid ";
$parameters['user_uuid'] = $_SESSION['user_uuid'];
}
2019-08-04 04:21:56 +02:00
$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
$database = new database;
$total_devices = $database->select($sql, $parameters, 'column');
unset($sql, $parameters);
2015-09-05 06:19:11 +02:00
2018-05-28 08:14:13 +02:00
//get the devices profiles
$sql = "select * from v_device_profiles ";
2019-08-04 04:21:56 +02:00
$sql .= "where domain_uuid = :domain_uuid ";
$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
$database = new database;
$device_profiles = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
2018-05-28 08:14:13 +02:00
2015-09-05 06:19:11 +02:00
//prepare to page the results
2019-08-04 04:21:56 +02:00
$sql = "select count(*) from v_devices as d ";
2019-10-02 02:31:04 +02:00
if (isset($_GET['show']) && $_GET['show'] == "all" && permission_exists('device_all')) {
2015-09-05 06:19:11 +02:00
if (strlen($search) > 0) {
$sql .= "where ";
}
2019-08-04 04:21:56 +02:00
}
else {
2015-09-05 06:19:11 +02:00
$sql .= "where (";
2019-08-04 04:21:56 +02:00
$sql .= " d.domain_uuid = :domain_uuid ";
2015-09-05 06:19:11 +02:00
if (permission_exists('device_all')) {
$sql .= " or d.domain_uuid is null ";
}
$sql .= ") ";
if (strlen($search) > 0) {
$sql .= "and ";
}
2019-08-04 04:21:56 +02:00
$parameters['domain_uuid'] = $domain_uuid;
2015-09-05 06:19:11 +02:00
}
if (strlen($search) > 0) {
$sql .= "(";
2019-08-04 04:21:56 +02:00
$sql .= " lower(d.device_mac_address) like :search ";
$sql .= " or lower(d.device_label) like :search ";
$sql .= " or lower(d.device_vendor) like :search ";
$sql .= " or lower(d.device_enabled) like :search ";
$sql .= " or lower(d.device_template) like :search ";
$sql .= " or lower(d.device_description) like :search ";
$sql .= " or lower(d.device_provisioned_method) like :search ";
$sql .= " or lower(d.device_provisioned_ip) like :search ";
2015-09-05 06:19:11 +02:00
$sql .= ") ";
2019-08-04 04:21:56 +02:00
$parameters['search'] = '%'.strtolower($search).'%';
2015-09-05 06:19:11 +02:00
}
2019-08-04 04:21:56 +02:00
$database = new database;
$num_rows = $database->select($sql, $parameters, 'column');
unset($sql, $parameters);
2015-09-05 06:19:11 +02:00
//prepare to page the results
$rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50;
2019-10-02 02:31:04 +02:00
if (isset($_GET['show']) && $_GET['show'] == "all" && permission_exists('device_all')) {
2017-12-09 20:08:08 +01:00
$param = "&search=".$search."&show=all";
2019-11-07 17:00:29 +01:00
}
else {
$param = "&search=".$search;
2015-12-09 23:01:35 +01:00
}
2015-09-05 06:19:11 +02:00
$page = $_GET['page'];
if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; }
2019-11-07 17:00:29 +01:00
list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page);
list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true);
2015-09-05 06:19:11 +02:00
$offset = $rows_per_page * $page;
//get the list
$sql = "select d.*, d2.device_label as alternate_label ";
$sql .= "from v_devices as d, v_devices as d2 ";
$sql .= "where ( ";
$sql .= " d.device_uuid_alternate = d2.device_uuid ";
$sql .= " or d.device_uuid_alternate is null and d.device_uuid = d2.device_uuid ";
$sql .= ") ";
2019-10-02 02:31:04 +02:00
if (isset($_GET['show']) && $_GET['show'] == "all" && permission_exists('device_all')) {
2015-09-05 06:19:11 +02:00
//echo __line__."<br \>\n";
2019-08-04 04:21:56 +02:00
}
else {
2015-09-05 06:19:11 +02:00
$sql .= "and (";
2019-08-04 04:21:56 +02:00
$sql .= " d.domain_uuid = :domain_uuid ";
2015-09-05 06:19:11 +02:00
if (permission_exists('device_all')) {
$sql .= " or d.domain_uuid is null ";
}
$sql .= ") ";
2019-08-04 04:21:56 +02:00
$parameters['domain_uuid'] = $domain_uuid;
2015-09-05 06:19:11 +02:00
}
if (!permission_exists('device_all') && !permission_exists('device_domain_all')) {
$sql .= "and d.device_user_uuid = :user_uuid ";
$parameters['user_uuid'] = $_SESSION['user_uuid'];
}
2015-09-05 06:19:11 +02:00
if (strlen($search) > 0) {
$sql .= "and (";
2019-08-04 04:21:56 +02:00
$sql .= " lower(d.device_mac_address) like :search ";
$sql .= " or lower(d.device_label) like :search ";
$sql .= " or lower(d.device_vendor) like :search ";
$sql .= " or lower(d.device_enabled) like :search ";
$sql .= " or lower(d.device_template) like :search ";
$sql .= " or lower(d.device_description) like :search ";
$sql .= " or lower(d.device_provisioned_method) like :search ";
$sql .= " or lower(d.device_provisioned_ip) like :search ";
2015-09-05 06:19:11 +02:00
$sql .= ") ";
2019-08-04 04:21:56 +02:00
$parameters['search'] = '%'.strtolower($search).'%';
2015-09-05 06:19:11 +02:00
}
if (strlen($order_by) == 0) {
$sql .= "order by d.device_label, d.device_description asc ";
}
else {
$sql .= "order by $order_by $order ";
}
2019-08-04 04:21:56 +02:00
$sql .= limit_offset($rows_per_page, $offset);
$database = new database;
$devices = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
2015-09-05 06:19:11 +02:00
//alternate_found
$device_alternate = false;
foreach($devices as $row) {
2019-08-04 04:21:56 +02:00
if (is_uuid($row['device_uuid_alternate'])) {
2015-09-05 06:19:11 +02:00
$device_alternate = true;
break;
}
}
2019-11-07 17:00:29 +01:00
//create token
$object = new token;
$token = $object->create($_SERVER['PHP_SELF']);
//include the header
require_once "resources/header.php";
//show the content
2019-11-07 17:00:29 +01:00
echo "<div class='action_bar' id='action_bar'>\n";
echo " <div class='heading'><b>".$text['header-devices']." (".$num_rows.")</b></div>\n";
echo " <div class='actions'>\n";
2019-11-08 06:31:54 +01:00
if (permission_exists('device_import')) {
2019-11-08 07:01:35 +01:00
echo button::create(['type'=>'button','label'=>$text['button-import'],'icon'=>$_SESSION['theme']['button_icon_import'],'link'=>'device_imports.php']);
2019-11-08 06:31:54 +01:00
}
if (permission_exists('device_export')) {
2019-11-08 07:01:35 +01:00
echo button::create(['type'=>'button','label'=>$text['button-export'],'icon'=>$_SESSION['theme']['button_icon_export'],'link'=>'device_download.php']);
2019-11-08 06:31:54 +01:00
}
if (permission_exists('device_vendor_view')) {
2019-11-08 07:01:35 +01:00
echo button::create(['type'=>'button','label'=>$text['button-vendors'],'icon'=>'fax','link'=>'device_vendors.php']);
2019-11-08 06:31:54 +01:00
}
if (permission_exists('device_profile_view')) {
2019-11-08 07:01:35 +01:00
echo button::create(['type'=>'button','label'=>$text['button-profiles'],'icon'=>'clone','link'=>'device_profiles.php']);
2019-11-08 06:31:54 +01:00
}
2019-11-08 07:01:35 +01:00
$margin_left = permission_exists('device_import') || permission_exists('device_export') || permission_exists('device_vendor_view') || permission_exists('device_profile_view') ? "margin-left: 15px;" : null;
2019-11-07 17:00:29 +01:00
if (permission_exists('device_add') && (!is_numeric($_SESSION['limit']['devices']['numeric']) || ($total_devices < $_SESSION['limit']['devices']['numeric']))) {
2019-11-08 07:01:35 +01:00
echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'style'=>$margin_left,'link'=>'device_edit.php']);
unset($margin_left);
2019-11-07 17:00:29 +01:00
}
if (permission_exists('device_edit') && $devices) {
2019-11-08 07:01:35 +01:00
echo button::create(['type'=>'button','label'=>$text['button-toggle'],'icon'=>$_SESSION['theme']['button_icon_toggle'],'style'=>$margin_left,'onclick'=>"if (confirm('".$text['confirm-toggle']."')) { list_action_set('toggle'); list_form_submit('form_list'); } else { this.blur(); return false; }"]);
unset($margin_left);
2019-11-07 17:00:29 +01:00
}
if (permission_exists('device_delete') && $devices) {
2019-11-08 07:01:35 +01:00
echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'style'=>$margin_left,'onclick'=>"if (confirm('".$text['confirm-delete']."')) { list_action_set('delete'); list_form_submit('form_list'); } else { this.blur(); return false; }"]);
unset($margin_left);
2019-11-07 17:00:29 +01:00
}
echo "<form id='form_search' class='inline' method='get'>\n";
if (permission_exists('device_all')) {
2019-11-07 17:00:29 +01:00
if ($_GET['show'] == 'all') {
echo " <input type='hidden' name='show' value='all'>";
}
else {
2019-11-07 17:00:29 +01:00
echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?show=all']);
2015-03-07 23:51:32 +01:00
}
}
2019-11-08 06:31:54 +01:00
2019-11-07 17:00:29 +01:00
echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown='list_search_reset();'>";
echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search','style'=>($search != '' ? 'display: none;' : null)]);
echo button::create(['label'=>$text['button-reset'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'btn_reset','link'=>'devices.php','style'=>($search == '' ? 'display: none;' : null)]);
if ($paging_controls_mini != '') {
echo "<span style='margin-left: 15px;'>".$paging_controls_mini."</span>";
}
2019-11-07 17:00:29 +01:00
echo " </form>\n";
echo " </div>\n";
echo " <div style='clear: both;'></div>\n";
echo "</div>\n";
2019-11-07 17:00:29 +01:00
echo $text['description-devices']."\n";
echo "<br /><br />\n";
2019-11-07 17:00:29 +01:00
echo "<form id='form_list' method='post'>\n";
echo "<input type='hidden' id='action' name='action' value=''>\n";
echo "<input type='hidden' name='search' value=\"".escape($search)."\">\n";
echo "<table class='list'>\n";
echo "<tr class='list-header'>\n";
if (permission_exists('device_edit') || permission_exists('device_delete')) {
echo " <th class='checkbox'>\n";
echo " <input type='checkbox' id='checkbox_all' name='checkbox_all' onclick='list_all_toggle();' ".($devices ?: "style='visibility: hidden;'").">\n";
echo " </th>\n";
}
if ($_GET['show'] == "all" && permission_exists('device_all')) {
echo th_order_by('domain_name', $text['label-domain'], $order_by, $order, $param);
}
echo th_order_by('device_mac_address', $text['label-device_mac_address'], $order_by, $order);
echo th_order_by('device_label', $text['label-device_label'], $order_by, $order);
2015-05-22 06:10:43 +02:00
if ($device_alternate) {
2015-05-22 06:34:31 +02:00
echo th_order_by('device_template', $text['label-device_uuid_alternate'], $order_by, $order);
2015-05-22 06:10:43 +02:00
}
echo th_order_by('device_vendor', $text['label-device_vendor'], $order_by, $order);
echo th_order_by('device_template', $text['label-device_template'], $order_by, $order);
2018-05-28 08:14:13 +02:00
echo "<th>". $text['label-device_profiles']."</th>\n";
2019-11-07 17:00:29 +01:00
echo th_order_by('device_enabled', $text['label-device_enabled'], $order_by, $order, null, "class='center'");
echo th_order_by('device_status', $text['label-device_status'], $order_by, $order);
2019-11-07 17:00:29 +01:00
echo th_order_by('device_description', $text['label-device_description'], $order_by, $order, null, "class='hide-sm-dn'");
if (permission_exists('device_edit') && $_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') {
echo " <td class='action-button'>&nbsp;</td>\n";
}
2019-11-07 17:00:29 +01:00
echo "</tr>\n";
2019-08-04 04:21:56 +02:00
if (is_array($devices) && @sizeof($devices) != 0) {
2019-11-07 17:00:29 +01:00
$x = 0;
2015-05-22 05:08:02 +02:00
foreach($devices as $row) {
2018-05-28 08:14:13 +02:00
$device_profile_name = '';
foreach($device_profiles as $profile) {
if ($profile['device_profile_uuid'] == $row['device_profile_uuid']) {
$device_profile_name = $profile['device_profile_name'];
}
}
2019-11-07 17:00:29 +01:00
if (permission_exists('device_edit')) {
$list_row_url = "device_edit.php?id=".urlencode($row['device_uuid']);
}
echo "<tr class='list-row' href='".$list_row_url."'>\n";
if (permission_exists('device_edit') || permission_exists('device_delete')) {
echo " <td class='checkbox'>\n";
echo " <input type='checkbox' name='devices[$x][checked]' id='checkbox_".$x."' value='true' onclick=\"if (!this.checked) { document.getElementById('checkbox_all').checked = false; }\">\n";
echo " <input type='hidden' name='devices[$x][uuid]' value='".escape($row['device_uuid'])."' />\n";
echo " </td>\n";
}
if ($_GET['show'] == "all" && permission_exists('device_all')) {
echo " <td>".escape($_SESSION['domains'][$row['domain_uuid']]['domain_name'])."</td>\n";
}
2019-11-07 17:00:29 +01:00
echo " <td class='no-wrap'>";
echo permission_exists('device_edit') ? "<a href='".$list_row_url."'>".escape(format_mac($row['device_mac_address']))."</a>" : escape(format_mac($row['device_mac_address']));
echo " </td>\n";
2019-11-07 17:00:29 +01:00
echo " <td>".escape($row['device_label'])."&nbsp;</td>\n";
2015-05-22 06:10:43 +02:00
if ($device_alternate) {
if (strlen($row['device_uuid_alternate']) > 0) {
2019-11-07 17:00:29 +01:00
echo " <td class='no-link'>\n";
echo " <a href='device_edit.php?id=".urlencode($row['device_uuid_alternate'])."'>".escape($row['alternate_label'])."</a>\n";
echo " </td>\n";
}
else {
echo " <td>&nbsp;</td>\n";
2015-05-22 06:10:43 +02:00
}
}
2019-11-07 17:00:29 +01:00
echo " <td>".escape($row['device_vendor'])."&nbsp;</td>\n";
echo " <td>".escape($row['device_template'])."&nbsp;</td>\n";
echo " <td>".escape($device_profile_name)."&nbsp;</td>\n";
if (permission_exists('device_edit')) {
2019-11-07 17:00:29 +01:00
echo " <td class='no-link center'>";
echo button::create(['type'=>'submit','class'=>'link','label'=>$text['label-'.$row['device_enabled']],'title'=>$text['button-toggle'],'onclick'=>"list_self_check('checkbox_".$x."'); list_action_set('toggle'); list_form_submit('form_list')"]);
}
2019-11-07 17:00:29 +01:00
else {
echo " <td class='center'>";
echo $text['label-'.$row['device_enabled']];
}
echo " </td>\n";
2019-11-07 17:00:29 +01:00
echo " <td class='no-link'>".escape($row['device_provisioned_date'])." - ".escape($row['device_provisioned_method'])." - <a href='http://".escape($row['device_provisioned_ip'])."' target='_blank'>".escape($row['device_provisioned_ip'])."</a>&nbsp;</td>\n";
echo " <td class='description overflow hide-sm-dn'>".escape($row['device_description'])."&nbsp;</td>\n";
if (permission_exists('device_edit') && $_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') {
echo " <td class='action-button'>";
echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]);
echo " </td>\n";
}
echo "</tr>\n";
2019-11-07 17:00:29 +01:00
$x++;
2019-08-04 04:21:56 +02:00
}
}
2019-11-07 17:00:29 +01:00
unset($devices);
echo "</table>\n";
echo "<br />\n";
2019-11-07 17:00:29 +01:00
echo "<div align='center'>".$paging_controls."</div>\n";
2019-11-07 17:00:29 +01:00
echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
echo "</form>\n";
//include the footer
require_once "resources/footer.php";
2015-03-07 23:51:32 +01:00
2019-11-08 06:31:54 +01:00
?>