Copyright (C) 2016 All Rights Reserved. */ //includes require_once "root.php"; require_once "resources/require.php"; //check permissions require_once "resources/check_auth.php"; if (permission_exists('device_key_add') || permission_exists('device_key_edit')) { //access granted } else { echo "access denied"; exit; } //add multi-lingual support $language = new text; $text = $language->get($_SESSION['domain']['language']['code'], 'app/devices'); //include the device class //require_once "app/devices/resources/classes/device.php"; //get the vendor functions $sql = "select v.name as vendor_name, f.name, f.value "; $sql .= "from v_device_vendors as v, v_device_vendor_functions as f "; $sql .= "where v.device_vendor_uuid = f.device_vendor_uuid "; $sql .= "and f.device_vendor_function_uuid in "; $sql .= "("; $sql .= " select device_vendor_function_uuid from v_device_vendor_function_groups "; $sql .= " where device_vendor_function_uuid = f.device_vendor_function_uuid "; $sql .= " and ( "; if (is_array($_SESSION['groups'])) { foreach($_SESSION['groups'] as $index => $row) { $sql_where_or[] = "group_name = :group_name_".$index; $parameters['group_name_'.$index] = $row['group_name']; } if (is_array($sql_where_or) && @sizeof($sql_where_or) != 0) { $sql .= implode(' or ', $sql_where_or); } } $sql .= " ) "; $sql .= ") "; $sql .= "and v.enabled = 'true' "; $sql .= "and f.enabled = 'true' "; $sql .= "order by v.name asc, f.name asc "; $database = new database; $vendor_functions = $database->select($sql, (is_array($parameters) ? $parameters : null), 'all'); unset($sql, $sql_where_or, $parameters); //add or update the database if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) { //add or update the database if ($_POST["persistformvar"] != "true") { //get device $sql = "select device_uuid, device_profile_uuid from v_devices "; $sql .= "where device_user_uuid = :device_user_uuid "; $parameters['device_user_uuid'] = $_SESSION['user_uuid']; $database = new database; $row = $database->select($sql, $parameters, 'row'); if (is_array($row) && @sizeof($row) != 0) { $device_uuid = $row['device_uuid']; $device_profile_uuid = $row['device_profile_uuid']; } unset($sql, $parameters, $row); //get device profile keys if (is_uuid($device_profile_uuid)) { $sql = "select * from v_device_keys "; $sql .= "where device_profile_uuid = :device_profile_uuid "; $parameters['device_profile_uuid'] = $device_profile_uuid; $database = new database; $device_profile_keys = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); } //get device keys if (is_uuid($device_uuid)) { $sql = "select * from v_device_keys "; $sql .= "where device_uuid = :device_uuid "; $parameters['device_uuid'] = $device_uuid; $database = new database; $device_keys = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); } //create a list of protected keys - device keys if (is_array($device_keys) && @sizeof($device_keys) != 0) { foreach($device_keys as $row) { //determine if the key is allowed $device_key_authorized = false; foreach($vendor_functions as $function) { if ($function['vendor_name'] == $row['device_key_vendor'] && $function['value'] == $row['device_key_type']) { $device_key_authorized = true; } } //add the protected keys if (!$device_key_authorized) { $protected_keys[$row['device_key_id']] = 'true'; } //add to protected if ($row['device_key_protected'] == "true") { $protected_keys[$row['device_key_id']] = 'true'; } } } //create a list of protected keys - device proile keys if (is_array($device_profile_keys)) { foreach($device_profile_keys as $row) { //determine if the key is allowed $device_key_authorized = false; if (is_array($vendor_functions)) { foreach($vendor_functions as $function) { if ($function['vendor_name'] == $row['device_key_vendor'] && $function['value'] == $row['device_key_type']) { $device_key_authorized = true; } } } //add the protected keys if (!$device_key_authorized) { $protected_keys[$row['device_key_id']] = 'true'; } } } //remove the keys the user is not allowed to edit based on the authorized vendor keys $x=0; if (is_array($_POST['device_keys'])) { foreach($_POST['device_keys'] as $row) { //loop through the authorized vendor functions if ($protected_keys[$row['device_key_id']] == "true") { unset($_POST['device_keys'][$x]); } //increment the row id $x++; } } //add or update the device keys if (is_array($_POST['device_keys'])) { foreach ($_POST['device_keys'] as &$row) { //validate the data $save = true; //if (!is_uuid($row["device_key_uuid"])) { $save = false; } if (isset($row["device_key_id"])) { if (!is_numeric($row["device_key_id"])) { $save = false; echo $row["device_key_id"]." id "; } } if (strlen($row["device_key_type"]) > 25) { $save = false; echo "type "; } if (strlen($row["device_key_value"]) > 25) { $save = false; echo "value "; } if (strlen($row["device_key_label"]) > 25) { $save = false; echo "label "; } if (strlen($row["device_key_icon"]) > 25) { $save = false; echo "icon "; } //escape characters in the string $device_uuid = $row["device_uuid"]; $device_key_uuid = $row["device_key_uuid"]; $device_key_id = $row["device_key_id"]; $device_key_type = $row["device_key_type"]; $device_key_line = $row["device_key_line"]; $device_key_value = $row["device_key_value"]; $device_key_label = $row["device_key_label"]; $device_key_icon = $row["device_key_icon"]; $device_key_category = $row["device_key_category"]; $device_key_vendor = $row["device_key_vendor"]; //process the profile keys if (strlen($row["device_profile_uuid"]) > 0) { //get the profile key settings from the array foreach ($device_profile_keys as &$field) { if ($device_key_uuid == $field["device_key_uuid"]) { $database = $field; break; } } //determine what to do with the profile key if ($device_key_id == $database["device_key_id"] && $device_key_value == $database["device_key_value"] && $device_key_label == $database["device_key_label"] && $device_key_icon == $database["device_key_icon"]) { //profile key unchanged don't save $save = false; } else { //profile key has changed remove save the settings to the device $device_key_uuid = ''; } } //sql add or update if (!is_uuid($device_key_uuid)) { if (permission_exists('device_key_add') && strlen($device_key_type) > 0 && strlen($device_key_value) > 0) { //if the device_uuid is not in the array then get the device_uuid from the database if (strlen($device_uuid) == 0) { $sql = "select device_uuid from v_devices "; $sql .= "where device_user_uuid = :device_user_uuid "; $parameters['device_user_uuid'] = $_SESSION['user_uuid']; $database = new database; $device_uuid = $database->select($sql, $parameters, 'column'); unset($sql, $parameters); } //insert the keys $device_key_uuid = uuid(); $array['device_keys'][0]['device_key_uuid'] = $device_key_uuid; $array['device_keys'][0]['device_uuid'] = $device_uuid; $array['device_keys'][0]['domain_uuid'] = $_SESSION['domain_uuid']; $array['device_keys'][0]['device_key_id'] = $device_key_id; $array['device_keys'][0]['device_key_type'] = $device_key_type; $array['device_keys'][0]['device_key_line'] = $device_key_line; $array['device_keys'][0]['device_key_value'] = $device_key_value; $array['device_keys'][0]['device_key_label'] = $device_key_label; $array['device_keys'][0]['device_key_icon'] = $device_key_icon; $array['device_keys'][0]['device_key_category'] = $device_key_category; $array['device_keys'][0]['device_key_vendor'] = $device_key_vendor; //action add or update $action = "add"; } } else { //action add or update $action = "update"; //update the device keys $array['device_keys'][0]['device_key_uuid'] = $device_key_uuid; $array['device_keys'][0]['domain_uuid'] = $_SESSION['domain_uuid']; if (permission_exists('device_key_id')) { $array['device_keys'][0]['device_key_id'] = $device_key_id; } $array['device_keys'][0]['device_key_type'] = $device_key_type; $array['device_keys'][0]['device_key_value'] = $device_key_value; $array['device_keys'][0]['device_key_label'] = $device_key_label; $array['device_keys'][0]['device_key_icon'] = $device_key_icon; } if ($save) { $database = new database; $database->app_name = 'devices'; $database->app_uuid = '4efa1a1a-32e7-bf83-534b-6c8299958a8e'; $database->save($array); } unset($array); } } //write the provision files if (strlen($_SESSION['provision']['path']['text']) > 0) { $prov = new provision; $prov->domain_uuid = $domain_uuid; $response = $prov->write(); } //set the message message::add($text["message-$action"]); //redirect the browser header("Location: /core/user_settings/user_dashboard.php"); exit; } } //set the sub array index $x = "999"; //get device $sql = "select device_uuid, device_profile_uuid from v_devices "; $sql .= "where device_user_uuid = :device_user_uuid "; $parameters['device_user_uuid'] = $_SESSION['user_uuid']; $database = new database; $row = $database->select($sql, $parameters, 'row'); if (is_array($row) && @sizeof($row) != 0) { $device_uuid = $row['device_uuid']; $device_profile_uuid = $row['device_profile_uuid']; } unset($sql, $parameters, $row); //get device lines if (is_uuid($device_uuid)) { $sql = "select * from v_device_lines "; $sql .= "where device_uuid = :device_uuid "; $parameters['device_uuid'] = $device_uuid; $database = new database; $device_lines = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); } //get the user if (is_array($device_lines)) { foreach ($device_lines as $row) { if ($_SESSION['domain_name'] == $row['server_address']) { $user_id = $row['user_id']; $server_address = $row['server_address']; break; } } } //set the sip profile name $sip_profile_name = 'internal'; //get device keys in the right order where device keys are listed after the profile keys if (is_uuid($device_uuid)) { $sql = "select * from v_device_keys "; $sql .= "where ("; $sql .= "device_uuid = :device_uuid "; $sql .= is_uuid($device_profile_uuid) ? "or device_profile_uuid = :device_profile_uuid " : null; $sql .= ") "; $sql .= "order by "; $sql .= "device_key_vendor asc, "; $sql .= "case device_key_category "; $sql .= "when 'line' then 1 "; $sql .= "when 'memory' then 2 "; $sql .= "when 'programmable' then 3 "; $sql .= "when 'expansion' then 4 "; $sql .= "else 100 end, "; $sql .= $db_type == "mysql" ? "device_key_id asc " : "cast(device_key_id as numeric) asc, "; $sql .= "case when device_uuid is null then 0 else 1 end asc "; $parameters['device_uuid'] = $device_uuid; $parameters['device_profile_uuid'] = $device_profile_uuid; $database = new database; $keys = $database->select($sql, $parameters, 'all'); unset($sql, $parameters); } //override profile keys with device keys if (is_array($device_keys) && @sizeof($device_keys) != 0) { foreach($keys as $row) { $id = $row['device_key_id']; $device_keys[$id] = $row; if (is_uuid($row['device_profile_uuid'])) { $device_keys[$id]['device_key_owner'] = "profile"; } else { $device_keys[$id]['device_key_owner'] = "device"; } } unset($keys); } //get the vendor count and last and device information if (is_array($device_keys) && @sizeof($device_keys) != 0) { $vendor_count = 0; foreach($device_keys as $row) { if ($previous_vendor != $row['device_key_vendor']) { $previous_vendor = $row['device_key_vendor']; $device_uuid = $row['device_uuid']; $device_key_vendor = $row['device_key_vendor']; $device_key_id = $row['device_key_id']; $device_key_line = $row['device_key_line']; $device_key_category = $row['device_key_category']; $vendor_count++; } } } //add a new key if (permission_exists('device_key_add')) { $device_keys[$x]['device_key_category'] = $device_key_category; $device_keys[$x]['device_key_id'] = ''; $device_keys[$x]['device_uuid'] = $device_uuid; $device_keys[$x]['device_key_vendor'] = $device_key_vendor; $device_keys[$x]['device_key_type'] = ''; $device_keys[$x]['device_key_line'] = ''; $device_keys[$x]['device_key_value'] = ''; $device_keys[$x]['device_key_extension'] = ''; $device_keys[$x]['device_key_label'] = ''; $device_keys[$x]['device_key_icon'] = ''; } //remove the keys the user is not allowed to edit based on the authorized vendor keys if (is_array($device_keys) && @sizeof($device_keys) != 0) { foreach($device_keys as $row) { //loop through the authorized vendor functions $device_key_authorized = false; if (is_array($vendor_functions)) { foreach($vendor_functions as $function) { if (strlen($row['device_key_type'] == 0)) { $device_key_authorized = true; } else { if ($function['vendor_name'] == $row['device_key_vendor'] && $function['value'] == $row['device_key_type']) { $device_key_authorized = true; } } } } //unset vendor functions the is not allowed to edit if (!$device_key_authorized) { unset($device_keys[$row['device_key_id']]); } //hide protected keys if ($row['device_key_protected'] == "true") { unset($device_keys[$row['device_key_id']]); } } } //show the header //require_once "resources/header.php"; //show the content echo "
"; //show the footer //require_once "resources/footer.php"; ?>