From 4266a211e06f3621fa272be2eb72fdb96ea84148 Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Sun, 12 Mar 2023 12:38:43 +0100 Subject: [PATCH] Changed language detection code, added some small features --- asset.php | 29 +++++++----- header.php | 3 ++ includes.php | 2 +- install/mysql_sample.sql | 14 ++++-- lang/de.php | 3 +- lang/en.php | 3 +- lib.php | 95 +++++++++++++++++++++++++++++++++++++- lib/functions.php | 94 ++----------------------------------- location.php | 3 +- login.php | 3 +- options.php | 20 +++++++- tpl/asset.tpl | 2 +- tpl/options.tpl | 10 ++-- tpl/optionseditdisplay.tpl | 8 +++- user.php | 1 + 15 files changed, 168 insertions(+), 122 deletions(-) diff --git a/asset.php b/asset.php index fe14c7b..a2a9f76 100644 --- a/asset.php +++ b/asset.php @@ -106,32 +106,39 @@ if ($action == ACT_DEFAULT): // ========== VARIANT: default behavior ======================================= // create letter links -$sql = "SELECT DISTINCT SUBSTRING(UPPER(asset_name),1,1) AS asset_letter +$sql = "SELECT DISTINCT SUBSTRING(UPPER(asset_name),1,1) AS bst FROM asset - ORDER BY asset_letter"; + ORDER BY bst"; $sth = $dbh->query($sql); $alphabet = $sth->fetchAll(); +$alphabet[] = ['bst' => '*']; $smarty->assign("alphabet", $alphabet); // total asset count $sth = $dbh->query("SELECT COUNT(*) FROM asset"); -$smarty->assign("assetcount", $sth->fetchColumn()); +$assetcount = $sth->fetchColumn(); +$smarty->assign("assetcount", $sassetcount); // assets for current letter -if (isset($_GET['asset_letter'])) { - $asset_letter = sanitize($_GET['asset_letter']); +if (isset($_GET['bst'])) { + $bst = sanitize($_GET['bst']); } else { - $asset_letter = $alphabet[0]['asset_letter']; + $bst = $alphabet[0]['bst']; } - + $sql = "SELECT a.asset_id, IF(LENGTH(a.asset_name)>0, a.asset_name, '...') AS asset_name, a.asset_info, c.assetclass_id, c.assetclass_name - FROM asset AS a LEFT OUTER JOIN assetclass AS c USING (assetclass_id) - WHERE SUBSTRING(a.asset_name,1,1)=? - ORDER BY a.asset_name"; + FROM asset AS a LEFT OUTER JOIN assetclass AS c USING (assetclass_id)"; +if ($bst != '*') { + $sql .= " WHERE SUBSTRING(a.asset_name,1,1)=?"; + $p = array($bst); +} else { + $p = array(); +} +$sql .= " ORDER BY a.asset_name"; $sth = $dbh->prepare($sql); -$sth->execute([$asset_letter]); +$sth->execute($p); $smarty->assign("assets", $sth->fetchAll()); $smarty->display("asset.tpl"); diff --git a/header.php b/header.php index 95a57b0..b502918 100644 --- a/header.php +++ b/header.php @@ -43,6 +43,9 @@ if ($_SESSION['suser_menu_locations']) { if ($_SESSION['suser_menu_nodes']) { $menu[] = '' . $lang['lang_nodes'] . "\n"; } +if ($_SESSION['suser_menu_nats']) { + $menu[] = '' . $lang['lang_nats'] . "\n"; +} if ($_SESSION['suser_menu_subnets']) { $menu[] = '' . $lang['lang_subnets'] . "\n"; } diff --git a/includes.php b/includes.php index 8f359ed..5a0cee3 100644 --- a/includes.php +++ b/includes.php @@ -32,4 +32,4 @@ $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); include("lib.php"); -$language = lang_getfrombrowser($config_lang, $config_lang_default, null, false); +// $language = lang_getfrombrowser($config_lang, $config_lang_default); diff --git a/install/mysql_sample.sql b/install/mysql_sample.sql index 424baf4..8b21765 100644 --- a/install/mysql_sample.sql +++ b/install/mysql_sample.sql @@ -1,8 +1,11 @@ INSERT INTO asset (asset_name, assetclass_id) VALUES -('Computer', 1), +('Computer Alice', 1), +('Computer Bob', 1), +('Computer Admin', 1), ('Server', 3), ('Printer', 4), -('Firewall', 6); +('Firewall', 6), +('Air Condition System', 8); INSERT INTO assetclass (assetclassgroup_id, assetclass_name) VALUES (1, 'Desktop'), @@ -11,7 +14,8 @@ INSERT INTO assetclass (assetclassgroup_id, assetclass_name) VALUES (3, 'Printer'), (4, 'Switch'), (4, 'Firewall'), -(5, 'Scanner'); +(5, 'Scanner'), +(5, 'Other'); INSERT INTO assetclassgroup (assetclassgroup_name, assetclassgroup_color) VALUES ('Personal Computer', '000000'), @@ -54,7 +58,9 @@ INSERT INTO user (user_name, user_pass, user_displayname) VALUES ('bob', '$2y$10$hl4NN4lOyuz7KN0ZjLHbOuCqGi08GVaTvl/RiMcL1mbFqGmtzDN76', 'Bob'); INSERT INTO vlan (vlan_number, vlan_name) VALUES -(1, 'DEFAULT_VLAN'); +(1, 'DEFAULT_VLAN'), +(2, 'WLAN'), +(3, 'DMZ'); INSERT INTO zone (zone_soa, zone_origin, zone_hostmaster, zone_serial, zone_ns1) VALUES ('ns1.example.com.', 'example.com.', 'hostmaster.example.com.', '2023021301', 'ns1.example.com'); diff --git a/lang/de.php b/lang/de.php index 6dabdda..137a73b 100644 --- a/lang/de.php +++ b/lang/de.php @@ -14,6 +14,8 @@ $lang = array( 'lang_location' => 'Standort', 'lang_locations' => 'Standorte', 'lang_menu' => 'Menü', + 'lang_nat' => 'NAT', + 'lang_nats' => 'NATs', 'lang_node' => 'Knoten', 'lang_nodes' => 'Knoten', 'lang_user' => 'Benutzer', @@ -128,7 +130,6 @@ $lang = array( 'lang_mac' => 'MAC-Adresse', 'lang_proto_vers' => 'Protokollversion', - 'lang_nat' => 'NAT', 'lang_nat_add' => 'NAT hinzufügen', 'lang_nat_del' => 'NAT löschen', 'lang_nat_edit' => 'NAT ändern', diff --git a/lang/en.php b/lang/en.php index a7d9ea5..ccd242f 100644 --- a/lang/en.php +++ b/lang/en.php @@ -14,6 +14,8 @@ $lang = array( 'lang_location' => 'Location', 'lang_locations' => 'Locations', 'lang_menu' => 'Menu', + 'lang_nat' => 'NAT', + 'lang_nats' => 'NATs', 'lang_node' => 'Node', 'lang_nodes' => 'Nodes', 'lang_user' => 'User', @@ -128,7 +130,6 @@ $lang = array( 'lang_mac' => 'MAC Address', 'lang_proto_vers' => 'Protocol version', - 'lang_nat' => 'NAT', 'lang_nat_add' => 'Add NAT', 'lang_nat_del' => 'Delete NAT', 'lang_nat_edit' => 'Modify NAT', diff --git a/lib.php b/lib.php index f624e1e..91e2211 100644 --- a/lib.php +++ b/lib.php @@ -30,7 +30,7 @@ define ('ACT_PASSWORD', 14); // ========== GLOBAL PAGE START CODE ========================================== // global version string -$config_version = 'v0.9'; +$config_version = 'v0.9.1'; // available languages $config_lang = array('de', 'en'); @@ -60,6 +60,42 @@ $g_error = new MessageError; $action = ACT_DEFAULT; +// ========== LANGUAGE FUNCTIONS ============================================== + +function lang_getfrombrowser($allowed, $default) { + // get browser most preferred language if possible + if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + return $default; + } + $accepted = preg_split('/,\s*/', $_SERVER['HTTP_ACCEPT_LANGUAGE']); + $current_lang = $default; + $current_q = 0; + foreach ($accepted as $lang) { + $res = preg_match ('/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', + $lang, $matches); + if (!$res) { + continue; + } + $lang_code = explode ('-', $matches[1]); + if (isset($matches[2])) { + $lang_quality = (float)$matches[2]; + } else { + $lang_quality = 1.0; + } + while (count($lang_code)) { + if (in_array(strtolower(join ('-', $lang_code)), $allowed)) { + if ($lang_quality > $current_q) { + $current_lang = strtolower (join ('-', $lang_code)); + $current_q = $lang_quality; + break; + } + } + array_pop($lang_code); + } + } + return $current_lang; +} + // ========== FEEDBACK FUNCTIONS ============================================== class Message { @@ -136,6 +172,13 @@ class MessageError extends Message { } } +function msgout(array $parameters, Smarty_Internal_Template $smarty) { + // This is just a quick hack around missing {php} in Smarty3 + $GLOBALS['g_error']->PrintOut(); + $GLOBALS['g_warning']->PrintOut(); + $GLOBALS['g_message']->PrintOut(); +} + // ========== FORM FUNCTIONS ================================================== function form_get_action() { @@ -159,7 +202,7 @@ function submit_error($action) { function by default. An exit() is conscious here *not* installed, since it could be that despite such an error the program execution should be continued. */ - return sprintf('The action "%s" is unknown. It is probably a program error.
Please inform your administrator of the exact circumstances of how this situation came about.', strtoupper($action)); + return sprintf('The action "%s" is unknown. It is probably a program error.
Please inform your administrator of the exact circumstances of how this situation came about.', strtoupper($action)); } // ========== DATABASE FUCTIONS =============================================== @@ -261,3 +304,51 @@ function db_get_options_zone($default = NULL) { } return $options; } + +// ========== MISC FUCTIONS =================================================== + +function strip_mac($mac, $caps=true) { + // strip mac address to 12 char string + // strip chars we don't need + $mac = preg_replace('/[^a-fA-F0-9]/', '', $mac); + if ($caps) { + $mac = strtoupper($mac); + } else { + $mac = strtolower($mac); + } + return $mac; +} + +function write_mac($mac, $user_mac='xx:xx:xx:xx:xx:xx') { + // rebuild mac address using user supplied format + + if (strlen($mac) != 12) { + // if the MAC is empty, or for whatever reason incorrect, just return + return $mac; + } + + // check format of user mac: count upper or lower char + $chars = count_chars($user_mac, 1); + if (array_key_exists(88, $chars) and $chars[88] == 12) { + $pattern = '/X/'; + $mac = strtoupper($mac); + } elseif (array_key_exists(120, $chars) and $chars[120] == 12) { + $pattern = '/x/'; + $mac = strtolower($mac); + } else { + // invalid format + return $mac; + } + + for($i=0; $i<12; $i++) { + $user_mac = preg_replace($pattern, $mac[$i], $user_mac, 1); + } + + return $user_mac; +} + +function header_location($location) { + // redirect page + header('location:' . $location); + exit; +} diff --git a/lib/functions.php b/lib/functions.php index b3ab83f..1f6dafe 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -7,52 +7,6 @@ Copyright (C) 2011-2023 Thomas Hooge SPDX-License-Identifier: GPL-3.0-or-later *****************************************************************************/ -function strip_mac($mac, $caps=true) { - // strip mac address to 12 char string - // strip chars we don't need - $mac = preg_replace('/[^a-fA-F0-9]/', '', $mac); - if ($caps) { - $mac = strtoupper($mac); - } else { - $mac = strtolower($mac); - } - return $mac; -} - -function write_mac($mac, $user_mac='xx:xx:xx:xx:xx:xx') { - // rebuild mac address using user supplied format - - if (strlen($mac) != 12) { - // if the MAC is empty, or for whatever reason incorrect, just return - return $mac; - } - - // check format of user mac: count upper or lower char - $chars = count_chars($user_mac, 1); - if (array_key_exists(88, $chars) and $chars[88] == 12) { - $pattern = '/X/'; - $mac = strtoupper($mac); - } elseif (array_key_exists(120, $chars) and $chars[120] == 12) { - $pattern = '/x/'; - $mac = strtolower($mac); - } else { - // invalid format - return $mac; - } - - for($i=0; $i<12; $i++) { - $user_mac = preg_replace($pattern, $mac[$i], $user_mac, 1); - } - - return $user_mac; -} - -function header_location($location) { - // redirect page - header('location:' . $location); - exit; -} - // sanitize input function sanitize($input) { global $dblink; @@ -76,49 +30,13 @@ function sanitize($input) { return $input; } -function lang_getfrombrowser ($allowed_languages, $default_language, $lang_variable = null, $strict_mode = true) { - if ($lang_variable === null) { - $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE']; - } - if (empty($lang_variable)) { - return $default_language; - } - $accepted_languages = preg_split('/,\s*/', $lang_variable); - $current_lang = $default_language; - $current_q = 0; - foreach ($accepted_languages as $accepted_language) { - $res = preg_match ('/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', - $accepted_language, $matches); - if (!$res) { - continue; - } - $lang_code = explode ('-', $matches[1]); - if (isset($matches[2])) { - $lang_quality = (float)$matches[2]; - } else { - $lang_quality = 1.0; - } - while (count ($lang_code)) { - if (in_array (strtolower (join ('-', $lang_code)), $allowed_languages)) { - if ($lang_quality > $current_q) { - $current_lang = strtolower (join ('-', $lang_code)); - $current_q = $lang_quality; - break; - } - } - if ($strict_mode) { - break; - } - array_pop ($lang_code); - } - } - return $current_lang; -} - function print_tree_rec($tree, $level) { $output = '