Copyright (C) 2008-2013 All Rights Reserved. Contributor(s): Mark J Crane */ require_once "root.php"; require_once "resources/require.php"; //check permissions require_once "resources/check_auth.php"; if (permission_exists('device_add') || permission_exists('device_edit')) { //access granted } else { echo "access denied"; exit; } //add multi-lingual support require_once "app_languages.php"; foreach($text as $key => $value) { $text[$key] = $value[$_SESSION['domain']['language']['code']]; } //include the device class require_once "app/devices/resources/classes/device.php"; //action add or update if (isset($_REQUEST["id"])) { $action = "update"; $device_uuid = check_str($_REQUEST["id"]); } else { $action = "add"; } //get http post variables and set them to php variables if (count($_POST) > 0) { //devices $device_mac_address = check_str($_POST["device_mac_address"]); $device_mac_address = strtolower($device_mac_address); $device_mac_address = preg_replace('#[^a-fA-F0-9./]#', '', $device_mac_address); $_POST["device_mac_address"] = $device_mac_address; $device_label = check_str($_POST["device_label"]); $device_vendor = check_str($_POST["device_vendor"]); $device_model = check_str($_POST["device_model"]); $device_firmware_version = check_str($_POST["device_firmware_version"]); $device_provision_enable = check_str($_POST["device_provision_enable"]); $device_template = check_str($_POST["device_template"]); $device_description = check_str($_POST["device_description"]); //lines $line_number = check_str($_POST["line_number"]); $server_address = check_str($_POST["server_address"]); $outbound_proxy = check_str($_POST["outbound_proxy"]); $display_name = check_str($_POST["display_name"]); $user_id = check_str($_POST["user_id"]); $auth_id = check_str($_POST["auth_id"]); $password = check_str($_POST["password"]); //keys $device_key_category = check_str($_POST["device_key_category"]); $device_key_id = check_str($_POST["device_key_id"]); $device_key_type = check_str($_POST["device_key_type"]); $device_key_line = check_str($_POST["device_key_line"]); $device_key_value = check_str($_POST["device_key_value"]); $device_key_extension = check_str($_POST["device_key_extension"]); $device_key_label = check_str($_POST["device_key_label"]); //settings //$device_setting_category = check_str($_POST["device_setting_category"]); $device_setting_subcategory = check_str($_POST["device_setting_subcategory"]); //$device_setting_name = check_str($_POST["device_setting_name"]); $device_setting_value = check_str($_POST["device_setting_value"]); $device_setting_enabled = check_str($_POST["device_setting_enabled"]); $device_setting_description = check_str($_POST["device_setting_description"]); } //use the mac address to get the vendor if (strlen($device_vendor) == 0) { $device_vendor = device::get_vendor($device_mac_address); } //add or update the database if (count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) { //check for all required data $msg = ''; if (strlen($device_mac_address) == 0) { $msg .= $text['message-required'].$text['label-extension']."
\n"; } //if (strlen($device_label) == 0) { $msg .= "Please provide: Label
\n"; } //if (strlen($device_vendor) == 0) { $msg .= "Please provide: Vendor
\n"; } //if (strlen($device_model) == 0) { $msg .= "Please provide: Model
\n"; } //if (strlen($device_firmware_version) == 0) { $msg .= "Please provide: Firmware Version
\n"; } //if (strlen($device_provision_enable) == 0) { $msg .= "Please provide: Enabled
\n"; } //if (strlen($device_template) == 0) { $msg .= "Please provide: Template
\n"; } //if (strlen($device_username) == 0) { $msg .= "Please provide: Username
\n"; } //if (strlen($device_password) == 0) { $msg .= "Please provide: Password
\n"; } //if (strlen($device_description) == 0) { $msg .= "Please provide: Description
\n"; } if (strlen($msg) > 0) { require_once "resources/header.php"; require_once "resources/persist_form_var.php"; echo "
\n"; echo "
\n"; echo $msg."
"; echo "
\n"; persistformvar($_POST); echo "
\n"; require_once "resources/footer.php"; return; } //add or update the database if ($_POST["persistformvar"] != "true") { //add domain_uuid to the array if (!isset($_POST["domain_uuid"])) { $_POST["domain_uuid"] = $_SESSION["domain_uuid"]; } foreach ($_POST as $key => $value) { if (is_array($value)) { $y = 0; foreach ($value as $k => $v) { if (!isset($v["domain_uuid"])) { $_POST[$key][$y]["domain_uuid"] = $_SESSION["domain_uuid"]; } $y++; } } } //array cleanup $x = 0; foreach ($_POST["device_lines"] as $row) { //unset the empty row if (strlen($row["line_number"]) == 0) { unset($_POST["device_lines"][$x]); } //unset device_detail_uuid if the field has no value if (strlen($row["device_line_uuid"]) == 0) { unset($_POST["device_lines"][$x]["device_line_uuid"]); } //increment the row $x++; } $x = 0; foreach ($_POST["device_keys"] as $row) { //unset the empty row if (strlen($row["device_key_category"]) == 0) { unset($_POST["device_keys"][$x]); } //unset device_detail_uuid if the field has no value if (strlen($row["device_key_uuid"]) == 0) { unset($_POST["device_keys"][$x]["device_key_uuid"]); } //increment the row $x++; } $x = 0; foreach ($_POST["device_settings"] as $row) { //unset the empty row if (strlen($row["device_setting_subcategory"]) == 0) { unset($_POST["device_settings"][$x]); } //unset device_detail_uuid if the field has no value if (strlen($row["device_setting_uuid"]) == 0) { unset($_POST["device_settings"][$x]["device_setting_uuid"]); } //increment the row $x++; } //set the default $save = true; //check to see if the mac address exists if ($action == "add") { $sql = "SELECT count(*) AS num_rows FROM v_devices "; $sql .= "WHERE device_mac_address = '".$device_mac_address."' "; $prep_statement = $db->prepare($sql); if ($prep_statement) { $prep_statement->execute(); $row = $prep_statement->fetch(PDO::FETCH_ASSOC); if ($row['num_rows'] == "0") { $save = true; } else { $save = false; $_SESSION['message'] = $text['message-duplicate']; } } unset($prep_statement); } else { $save = true; } //save the device if ($save) { $orm = new orm; $orm->name('devices'); if (strlen($device_uuid) > 0) { $orm->uuid($device_uuid); } $orm->save($_POST); $response = $orm->message; if (strlen($response['uuid']) > 0) { $device_uuid = $response['uuid']; } } //write the provision files require_once "app/provision/provision_write.php"; //set the message if (!isset($_SESSION['message'])) { if ($save) { if ($action == "add") { //save the message to a session variable $_SESSION['message'] = $text['message-add']; //redirect the browser header("Location: device_edit.php?id=$device_uuid"); exit; } if ($action == "update") { //save the message to a session variable $_SESSION['message'] = $text['message-update']; } } } } //if ($_POST["persistformvar"] != "true") } //(count($_POST) > 0 && strlen($_POST["persistformvar"]) == 0) //pre-populate the form if (count($_GET) > 0 && $_POST["persistformvar"] != "true") { $orm = new orm; $orm->name('devices'); $orm->uuid($device_uuid); $result = $orm->find()->get(); //$message = $orm->message; foreach ($result as &$row) { $device_mac_address = $row["device_mac_address"]; //$device_mac_address = substr($device_mac_address, 0,2).'-'.substr($device_mac_address, 2,2).'-'.substr($device_mac_address, 4,2).'-'.substr($device_mac_address, 6,2).'-'.substr($device_mac_address, 8,2).'-'.substr($device_mac_address, 10,2); $device_label = $row["device_label"]; $device_vendor = $row["device_vendor"]; $device_model = $row["device_model"]; $device_firmware_version = $row["device_firmware_version"]; $device_provision_enable = $row["device_provision_enable"]; $device_template = $row["device_template"]; $device_description = $row["device_description"]; } unset ($prep_statement); } //use the mac address to get the vendor if (strlen($device_vendor) == 0) { $template_array = explode("/", $device_template); $device_vendor = $template_array[0]; } //set the sub array index $x = "999"; //get device lines $sql = "SELECT * FROM v_device_lines "; $sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' "; $sql .= "and device_uuid = '".$device_uuid."' "; $sql .= "order by line_number asc "; $prep_statement = $db->prepare(check_sql($sql)); $prep_statement->execute(); $device_lines = $prep_statement->fetchAll(PDO::FETCH_NAMED); $device_lines[$x]['line_number'] = ''; $device_lines[$x]['server_address'] = ''; $device_lines[$x]['outbound_proxy'] = ''; $device_lines[$x]['display_name'] = ''; $device_lines[$x]['user_id'] = ''; $device_lines[$x]['auth_id'] = ''; $device_lines[$x]['password'] = ''; //get device keys $sql = "SELECT * FROM v_device_keys "; $sql .= "WHERE domain_uuid = '".$_SESSION['domain_uuid']."' "; $sql .= "AND device_uuid = '".$device_uuid."' "; $sql .= "ORDER by "; $sql .= "CASE device_key_category "; $sql .= "WHEN 'line' THEN 1 "; $sql .= "WHEN 'memort' THEN 2 "; $sql .= "WHEN 'programmable' THEN 3 "; $sql .= "WHEN 'expansion' THEN 4 "; $sql .= "ELSE 100 END, "; $sql .= "cast(device_key_id as numeric) asc "; $prep_statement = $db->prepare(check_sql($sql)); $prep_statement->execute(); $device_keys = $prep_statement->fetchAll(PDO::FETCH_NAMED); $device_keys[$x]['device_key_category'] = ''; $device_keys[$x]['device_key_id'] = ''; $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'] = ''; //get device settings $sql = "SELECT * FROM v_device_settings "; $sql .= "WHERE domain_uuid = '".$_SESSION['domain_uuid']."' "; $sql .= "AND device_uuid = '".$device_uuid."' "; $sql .= "ORDER by device_setting_subcategory asc "; $prep_statement = $db->prepare(check_sql($sql)); $prep_statement->execute(); $device_settings = $prep_statement->fetchAll(PDO::FETCH_NAMED); $device_settings[$x]['device_setting_name'] = ''; $device_settings[$x]['device_setting_value'] = ''; $device_settings[$x]['enabled'] = ''; $device_settings[$x]['device_setting_description'] = ''; //use the mac address to get the vendor if (strlen($device_vendor) == 0) { $device_vendor = device::get_vendor($device_mac_address); } //show the header require_once "resources/header.php"; //javascript to change select to input and back again ?> "; echo "\n"; echo "\n"; echo " "; echo " "; echo "
\n"; echo "
"; echo "
\n"; echo "
\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo " "; echo " "; echo " "; echo " "; if (permission_exists('device_key_add') || permission_exists('device_key_edit')) { echo " "; echo " "; echo " "; echo " "; } //device settings if (permission_exists('device_setting_add')) { echo " "; echo " "; echo " \n"; echo " \n"; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; /* echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; */ echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo " "; echo "
".$text['header-device']."\n"; echo " \n"; if ($action != "add") { echo " \n"; } echo " \n"; echo "
\n"; echo $text['description-device']."

\n"; echo "
\n"; echo " ".$text['label-device_mac_address'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_mac_address']."\n"; echo "
\n"; echo " ".$text['label-device_label'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_label']."\n"; echo "
\n"; echo " ".$text['label-device_template'].":\n"; echo "\n"; $device = new device; $template_dir = $device->get_template_dir(); echo "\n"; echo "
\n"; echo $text['description-device_template']."\n"; echo "
".$text['label-lines'].":"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; $x = 0; foreach($device_lines as $row) { //determine whether to hide the element if (strlen($device_line_uuid) == 0) { $element['hidden'] = false; $element['visibility'] = "visibility:visible;"; } else { $element['hidden'] = true; $element['visibility'] = "visibility:hidden;"; } //add the primary key uuid if (strlen($row['device_line_uuid']) > 0) { echo " \n"; } //show each row in the array echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; //echo " \n"; echo " \n"; echo " \n"; $x++; } echo "
".$text['label-line']."".$text['label-server_address']."".$text['label-outbound_proxy']."".$text['label-display_name']."".$text['label-user_id']."".$text['label-auth_id']."".$text['label-password']." 
\n"; $selected = "selected=\"selected\" "; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; //echo " \n"; //echo " \n"; if (strlen($row['device_line_uuid']) > 0) { if (permission_exists('device_edit')) { echo " $v_link_label_edit\n"; } if (permission_exists('device_delete')) { echo " $v_link_label_delete\n"; } } echo "
\n"; if (strlen($text['description-lines']) > 0) { echo "
".$text['description-lines']."\n"; } echo "
".$text['label-keys'].":"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; $x = 0; foreach($device_keys as $row) { //determine whether to hide the element if (strlen($device_key_uuid) == 0) { $element['hidden'] = false; $element['visibility'] = "visibility:visible;"; } else { $element['hidden'] = true; $element['visibility'] = "visibility:hidden;"; } //add the primary key uuid if (strlen($row['device_key_uuid']) > 0) { echo " \n"; } //show all the rows in the array echo " \n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; //echo " \n"; echo " \n"; echo " \n"; //increment the array key $x++; } echo "
".$text['label-device_key_category']."".$text['label-device_key_id']."".$text['label-device_key_type']."".$text['label-device_key_line']."".$text['label-device_key_value']."".$text['label-device_key_extension']."".$text['label-device_key_label']." 
\n"; echo " \n"; echo "\n"; $selected = "selected='selected'"; echo " \n"; echo "\n"; //echo " \n"; ?> \n"; $selected = "selected='selected'"; echo "\n"; echo " \n"; echo "\n"; echo " \n"; echo "\n"; echo " \n"; echo "\n"; echo " \n"; echo "\n"; //echo " \n"; //echo " \n"; if (strlen($row['device_key_uuid']) > 0) { if (permission_exists('device_key_edit')) { echo " $v_link_label_edit\n"; } if (permission_exists('device_key_delete')) { echo " $v_link_label_delete\n"; } } echo "
\n"; if (strlen($text['description-keys']) > 0) { echo "
".$text['description-keys']."\n"; } echo "
".$text['label-settings'].":"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; $x = 0; foreach($device_settings as $row) { //determine whether to hide the element if (strlen($device_setting_uuid) == 0) { $element['hidden'] = false; $element['visibility'] = "visibility:visible;"; } else { $element['hidden'] = true; $element['visibility'] = "visibility:hidden;"; } //add the primary key uuid if (strlen($row['device_setting_uuid']) > 0) { echo " \n"; } //show alls rows in the array echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; if (strlen($text['description-settings']) > 0) { echo "
".$text['description-settings']."\n"; } echo " "; echo " \n"; echo " \n"; $x++; } /* echo "
".$text['label-device_setting_name']."".$text['label-device_setting_value']."".$text['label-enabled']."".$text['label-device_setting_description']." 
\n"; echo " \n"; echo "\n"; echo " \n"; echo "\n"; echo " \n"; echo "\n"; echo " \n"; echo "\n"; if (strlen($row['device_setting_uuid']) > 0) { if (permission_exists('device_edit')) { echo " $v_link_label_edit\n"; } if (permission_exists('device_delete')) { echo " $v_link_label_delete\n"; } } echo "
\n"; echo " \n"; */ echo "
\n"; echo "
\n"; echo " ".$text['label-device_vendor'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_vendor']."\n"; echo "
\n"; echo " ".$text['label-device_model'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_model']."\n"; echo "
\n"; echo " ".$text['label-device_firmware_version'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_firmware_version']."\n"; echo "
\n"; echo " ".$text['label-device_username'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_username']."\n"; echo "
\n"; echo " ".$text['label-device_password'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_password']."\n"; echo "
\n"; echo " ".$text['label-device_provision_enable'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_provision_enable']."\n"; echo "
\n"; echo " ".$text['label-device_description'].":\n"; echo "\n"; echo " \n"; echo "
\n"; echo $text['description-device_description']."\n"; echo "
\n"; if ($action == "update") { echo " \n"; } echo " \n"; echo "
"; echo ""; echo "
"; echo ""; //show the footer require_once "resources/footer.php"; ?>