Portions created by the Initial Developer are Copyright (C) 2008-2024 the Initial Developer. All Rights Reserved. Contributor(s): Mark J Crane */ //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('device_view')) { //access granted } else { echo "access denied"; exit; } //set the current domain and user information $domain_name = $_SESSION['domain_name'] ?? ''; $domain_uuid = $_SESSION['domain_uuid'] ?? ''; $user_uuid = $_SESSION['user_uuid'] ?? ''; $user_name = $_SESSION['username'] ?? ''; //create database connection and settings object $database = database::new(); $settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid, 'user_uuid' => $user_uuid]); //set all permissions $has_device_import = permission_exists('device_import'); $has_device_edit = permission_exists('device_edit'); $has_device_all = permission_exists('device_all'); $has_device_delete = permission_exists('device_delete'); $has_device_domain_all = permission_exists('device_domain_all'); $has_device_export = permission_exists('device_export'); $has_device_vendor_view = permission_exists('device_vendor_view'); $has_device_profile_view = permission_exists('device_profile_view'); $has_device_add = permission_exists('device_add'); $has_show_all = &$has_device_domain_all; //add multi-lingual support $language = new text; $text = $language->get(); //get posted data if (!empty($_POST['devices']) && is_array($_POST['devices'])) { $action = $_POST['action']; $devices = $_POST['devices']; } //get the search $search = strtolower($_REQUEST["search"] ?? ''); $fields = strtolower($_REQUEST["fields"] ?? ''); //process the http post data by action if (!empty($action) && !empty($devices) && is_array($devices) && @sizeof($devices) != 0) { switch ($action) { case 'toggle': if ($has_device_edit) { $obj = new device; $obj->toggle($devices); } break; case 'delete': if ($has_device_delete) { $obj = new device; $obj->delete($devices); } break; } header('Location: devices.php'.($search != '' ? '?search='.urlencode($search).'&fields='.urlencode($fields) : null)); exit; } //get order and order by and sanatize the values $order_by = $_GET["order_by"] ?? ''; $order = $_GET["order"] ?? ''; //set the time zone $time_zone = $settings->get('domain', 'time_zone', date_default_timezone_get()); //get total devices count from the database $sql = "select count(*) from v_devices "; $sql .= "where domain_uuid = :domain_uuid "; if (!$has_device_all && !$has_device_domain_all) { $sql .= "and device_user_uuid = :user_uuid "; $parameters['user_uuid'] = $user_uuid; } $parameters['domain_uuid'] = $domain_uuid; $total_devices = $database->select($sql, $parameters, 'column'); unset($sql, $parameters); //update the has_device_add permission if the total device count is greater then set limit $device_limit = $settings->get('limit', 'devices', null); if ($has_device_add && $device_limit !== null) { $has_device_add = $total_devices > $device_limit; } //get the domains if user has permission for show all $domains = []; if ($has_device_domain_all) { $rows = $database->select("select domain_uuid, domain_name from v_domains where domain_enabled = 'true'"); if (!empty($rows)) { foreach ($rows as $row) { $domains[$row['domain_uuid']] = $row['domain_name']; } } } //get the devices profiles $sql = "select * from v_device_profiles "; $sql .= "where true "; if (!permission_exists('device_profile_all')) { $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) "; $parameters['domain_uuid'] = $domain_uuid; } $device_profiles = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); //prepare to page the results $sql = "select count(*) from v_devices as d "; if (isset($_GET['show']) && $_GET['show'] == "all" && $has_device_all) { if (!empty($search)) { $sql .= "where "; } } else { $sql .= "where ("; $sql .= " d.domain_uuid = :domain_uuid "; if ($has_device_all) { $sql .= " or d.domain_uuid is null "; } $sql .= ") "; if (!empty($search)) { $sql .= "and "; } $parameters['domain_uuid'] = $domain_uuid; } if (!empty($search)) { $sql .= "("; $sql .= " lower(d.device_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 "; if ($fields == 'all' || $fields == 'lines') { $sql .= " or d.device_uuid in ( "; $sql .= " select dl.device_uuid from v_device_lines as dl "; $sql .= " where dl.display_name like :search "; $sql .= " or dl.user_id like :search "; $sql .= " or dl.auth_id like :search "; $sql .= " ) "; } if ($fields == 'all' || $fields == 'keys') { $sql .= " or d.device_uuid in ( "; $sql .= " select dk.device_uuid from v_device_keys as dk "; $sql .= " where dk.device_key_value like :search "; $sql .= " or dk.device_key_label like :search "; $sql .= " ) "; } if ($fields == 'all' || $fields == 'settings') { $sql .= " or d.device_uuid in ( "; $sql .= " select ds.device_uuid from v_device_settings as ds "; $sql .= " where ds.device_setting_subcategory like :search "; $sql .= " or ds.device_setting_value like :search "; $sql .= " or ds.device_setting_description like :search "; $sql .= " ) "; } $sql .= ") "; $parameters['search'] = '%'.strtolower($search).'%'; } $num_rows = $database->select($sql, $parameters ?? null, 'column'); unset($sql, $parameters); //prepare to page the results $rows_per_page = intval($settings->get('domain', 'paging', 50)); $param = ''; if ($search) { $param = "&search=".$search; $param .= "&fields=".$fields; } if (!empty($_GET['show']) && $_GET['show'] == "all" && $has_device_all) { $param .= "&show=all"; } $page = $_GET['page'] ?? 0; 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); $offset = $rows_per_page * $page; //get the list $sql = "select d.*, d2.device_label as alternate_label, "; $sql .= "to_char(timezone(:time_zone, d.device_provisioned_date), 'DD Mon YYYY') as provisioned_date_formatted, \n"; $sql .= "to_char(timezone(:time_zone, d.device_provisioned_date), 'HH12:MI:SS am') as provisioned_time_formatted \n"; $sql .= "from v_devices as d, v_devices as d2 "; if (isset($_GET['show']) && $_GET['show'] == "all" && $has_device_all) { $sql .= ", v_domains as d3 "; } $sql .= "where ( "; $sql .= " d.device_uuid_alternate = d2.device_uuid "; $sql .= " or ( "; $sql .= " d.device_uuid_alternate is null and "; $sql .= " d.device_uuid = d2.device_uuid "; $sql .= " ) "; $sql .= ") "; if (isset($_GET['show']) && $_GET['show'] == "all" && $has_device_all) { $sql .= " and d.domain_uuid = d3.domain_uuid "; } else { $sql .= "and ("; $sql .= " d.domain_uuid = :domain_uuid "; if ($has_device_all) { $sql .= " or d.domain_uuid is null "; } $sql .= ") "; $parameters['domain_uuid'] = $domain_uuid; } if (!$has_device_all && !$has_device_domain_all) { $sql .= "and d.device_user_uuid = :user_uuid "; $parameters['user_uuid'] = $user_uuid; } if (!empty($search)) { $sql .= "and ("; $sql .= " lower(d.device_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 "; if ($fields == 'all' || $fields == 'lines') { $sql .= " or d.device_uuid in ( "; $sql .= " select dl.device_uuid from v_device_lines as dl "; $sql .= " where dl.display_name like :search "; $sql .= " or dl.user_id like :search "; $sql .= " or dl.auth_id like :search "; $sql .= " ) "; } if ($fields == 'all' || $fields == 'keys') { $sql .= " or d.device_uuid in ( "; $sql .= " select dk.device_uuid from v_device_keys as dk "; $sql .= " where dk.device_key_value like :search "; $sql .= " or dk.device_key_label like :search "; $sql .= " ) "; } if ($fields == 'all' || $fields == 'settings') { $sql .= " or d.device_uuid in ( "; $sql .= " select ds.device_uuid from v_device_settings as ds "; $sql .= " where ds.device_setting_subcategory like :search "; $sql .= " or ds.device_setting_value like :search "; $sql .= " or ds.device_setting_description like :search "; $sql .= " ) "; } $sql .= ") "; $parameters['search'] = '%'.strtolower($search).'%'; } if (empty($order_by)) { $sql .= "order by d.device_label, d.device_description asc "; } else { $sql .= "order by $order_by $order "; } $sql .= limit_offset($rows_per_page, $offset); $parameters['time_zone'] = $time_zone; $devices = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); //alternate_found $device_alternate = false; if (is_array($devices)) { foreach($devices as $row) { if (is_uuid($row['device_uuid_alternate'])) { $device_alternate = true; break; } } } //create token $object = new token; $token = $object->create($_SERVER['PHP_SELF']); //include the header $document['title'] = $text['title-devices']; require_once "resources/header.php"; //show the content echo "
\n"; echo "
".$text['header-devices']."
".number_format($num_rows)."
\n"; echo "
\n"; if ($has_device_import) { echo button::create(['type'=>'button','label'=>$text['button-import'],'icon'=>$settings->get('theme', 'button_icon_import'),'link'=>'device_imports.php']); } if ($has_device_export) { echo button::create(['type'=>'button','label'=>$text['button-export'],'icon'=>$settings->get('theme', 'button_icon_export'),'link'=>'device_download.php']); } if ($has_device_vendor_view) { echo button::create(['type'=>'button','label'=>$text['button-vendors'],'icon'=>'fax','link'=>'device_vendors.php']); } if ($has_device_profile_view) { echo button::create(['type'=>'button','label'=>$text['button-profiles'],'icon'=>'clone','link'=>'device_profiles.php']); } $margin_left = $has_device_import || $has_device_export || $has_device_vendor_view || $has_device_profile_view ? "margin-left: 15px;" : null; if ($has_device_add) { echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$settings->get('theme', 'button_icon_add'),'id'=>'btn_add','style'=>$margin_left,'link'=>'device_edit.php']); unset($margin_left); } if ($has_device_edit && $devices) { echo button::create(['type'=>'button','label'=>$text['button-toggle'],'icon'=>$settings->get('theme', 'button_icon_toggle'),'id'=>'btn_toggle','name'=>'btn_toggle','style'=>'display: none; '.($margin_left ?? null),'onclick'=>"modal_open('modal-toggle','btn_toggle');"]); unset($margin_left); } if ($has_device_delete && $devices) { echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$settings->get('theme', 'button_icon_delete'),'id'=>'btn_delete','name'=>'btn_delete','style'=>'display: none; '.($margin_left ?? null),'onclick'=>"modal_open('modal-delete','btn_delete');"]); unset($margin_left); } echo "\n"; echo "
\n"; echo "
\n"; echo "
\n"; if ($has_device_edit && $devices) { echo modal::create(['id'=>'modal-toggle','type'=>'toggle','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_toggle','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('toggle'); list_form_submit('form_list');"])]); } if ($has_device_delete && $devices) { echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'button','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','onclick'=>"modal_close(); list_action_set('delete'); list_form_submit('form_list');"])]); } echo $text['description-devices']."\n"; echo "

\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "
\n"; echo "\n"; echo "\n"; if ($has_device_edit || $has_device_delete) { echo " \n"; } if (!empty($_GET['show']) && $_GET['show'] == "all" && $has_device_all) { echo th_order_by('domain_name', $text['label-domain'], $order_by, $order, null, null, $param); } echo th_order_by('device_address', $text['label-device_address'], $order_by, $order, null, null, $param ?? null); echo th_order_by('device_label', $text['label-device_label'], $order_by, $order, null, null, $param ?? null); if ($device_alternate) { echo th_order_by('device_template', $text['label-device_uuid_alternate'], $order_by, $order, null, null, $param ?? null); } echo th_order_by('device_vendor', $text['label-device_vendor'], $order_by, $order, null, null, $param ?? null); echo th_order_by('device_template', $text['label-device_template'], $order_by, $order, null, null, $param ?? null); echo "\n"; echo th_order_by('device_enabled', $text['label-device_enabled'], $order_by, $order, null, "class='center'", $param ?? null); echo th_order_by('device_provisioned_date', $text['label-device_status'], $order_by, $order, null, null, $param ?? null); echo th_order_by('device_description', $text['label-device_description'], $order_by, $order, null, "class='hide-sm-dn'", $param ?? null); if ($has_device_edit && $settings->get('theme', 'list_row_edit_button', 'false') === 'true') { echo " \n"; } echo "\n"; if (!empty($devices) && @sizeof($devices) != 0) { $x = 0; foreach($devices as $row) { $device_profile_name = ''; foreach($device_profiles as $profile) { if ($profile['device_profile_uuid'] == $row['device_profile_uuid']) { $device_profile_name = $profile['device_profile_name']; } } if ($has_device_edit) { $list_row_url = "device_edit.php?id=".urlencode($row['device_uuid']); } $device_provisioned_method = ''; if (isset($row['device_provisioned_method']) && ($row['device_provisioned_method'] == 'http' || $row['device_provisioned_method'] == 'https')) { $device_provisioned_method = $row['device_provisioned_method']; } $device_provisioned_ip = ''; if (isset($row['device_provisioned_ip']) && filter_var($row['device_provisioned_ip'], FILTER_VALIDATE_IP)) { $device_provisioned_ip = $row['device_provisioned_ip']; } echo "\n"; if ($has_device_edit || $has_device_delete) { echo " \n"; } if (!empty($_GET['show']) && $_GET['show'] == "all" && $has_device_all) { echo " \n"; } echo " \n"; echo " \n"; if ($device_alternate) { if (!empty($row['device_uuid_alternate'])) { echo " \n"; } else { echo " \n"; } } echo " \n"; echo " \n"; echo " \n"; if ($has_device_edit) { echo " \n"; echo " \n"; echo " \n"; if ($has_device_edit && $settings->get('theme', 'list_row_edit_button', 'false') === 'true') { echo " \n"; } echo "\n"; $x++; } } unset($devices); echo "
\n"; echo " \n"; echo " ". $text['label-device_profiles']." 
\n"; echo " \n"; echo " \n"; echo " ".escape($domains[$row['domain_uuid']]).""; echo $has_device_edit ? "".escape(format_device_address($row['device_address']))."" : escape(format_device_address($row['device_address'])); echo " ".escape($row['device_label'])."  ".escape($row['device_vendor'])." ".escape($row['device_template'])." ".escape($device_profile_name)." "; echo $text['label-'.$row['device_enabled']]; } echo " ".escape($row['device_description'])." "; echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$settings->get('theme','button_icon_edit'),'link'=>$list_row_url]); echo "
\n"; echo "
\n"; echo "
\n"; echo "
".$paging_controls."
\n"; echo "\n"; echo "
\n"; //include the footer require_once "resources/footer.php"; ?>