Changed language detection code, added some small features

master
Thomas Hooge 12 months ago
parent aabd37bd1a
commit 4266a211e0
  1. 29
      asset.php
  2. 3
      header.php
  3. 2
      includes.php
  4. 14
      install/mysql_sample.sql
  5. 3
      lang/de.php
  6. 3
      lang/en.php
  7. 95
      lib.php
  8. 94
      lib/functions.php
  9. 3
      location.php
  10. 3
      login.php
  11. 20
      options.php
  12. 2
      tpl/asset.tpl
  13. 10
      tpl/options.tpl
  14. 8
      tpl/optionseditdisplay.tpl
  15. 1
      user.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");

@ -43,6 +43,9 @@ if ($_SESSION['suser_menu_locations']) {
if ($_SESSION['suser_menu_nodes']) {
$menu[] = '<a href="node.php">' . $lang['lang_nodes'] . "</a>\n";
}
if ($_SESSION['suser_menu_nats']) {
$menu[] = '<a href="nat.php">' . $lang['lang_nats'] . "</a>\n";
}
if ($_SESSION['suser_menu_subnets']) {
$menu[] = '<a href="subnet.php">' . $lang['lang_subnets'] . "</a>\n";
}

@ -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);

@ -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');

@ -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',

@ -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',

@ -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.<br /> 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.<br> 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;
}

@ -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 = '<ul class="treelvl' . $level. '">' . "\n";
foreach ($tree as $node) {
$output .= '<li><a href="' . $node['href'] . '">' . $node['value'] . '</a>';
if ($node['info']) {
$output .= ' - ' . $node['info'];
}
if ($node['children']) {
$output .= "\n" . print_tree_rec($node['children'], $level+1);
}
@ -141,9 +59,3 @@ function print_tree($params, Smarty_Internal_Template $template) {
}
}
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();
}

@ -142,7 +142,8 @@ include("header.php");
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
$sql = "SELECT location_id AS id, location_name AS value, location_parent AS parent_id
$sql = "SELECT location_id AS id, location_name AS value, location_parent AS parent_id,
CONCAT(LEFT(location_info,40), IF(CHAR_LENGTH(location_info)>40,'...','')) AS info
FROM location
ORDER BY location_parent, location_sort, location_name";
$sth = $dbh->query($sql);

@ -136,6 +136,7 @@ function user_login ($user_name, $user_pass) {
$_SESSION['suser_menu_cables'] = in_array('cable', $menu);
$_SESSION['suser_menu_locations'] = in_array('location', $menu);
$_SESSION['suser_menu_nodes'] = in_array('node', $menu);
$_SESSION['suser_menu_nats'] = in_array('nat', $menu);
$_SESSION['suser_menu_subnets'] = in_array('subnet', $menu);
$_SESSION['suser_menu_vlans'] = in_array('vlan', $menu);
$_SESSION['suser_menu_zones'] = in_array('zone', $menu);
@ -147,7 +148,7 @@ function user_login ($user_name, $user_pass) {
// ========== LOGIN: HERE BE DRAGONS ==========================================
$language = lang_getfrombrowser($config_lang, $config_lang_default, null, false);
$language = lang_getfrombrowser($config_lang, $config_lang_default);
include('lang/' . $language . '.php');
if ($_SERVER['REQUEST_METHOD'] == "POST" ) {

@ -34,6 +34,7 @@ switch ($submit = form_get_action()) {
$menu_cables = sanitize($_POST['user_menu_cables']);
$menu_locations = sanitize($_POST['user_menu_locations']);
$menu_nodes = sanitize($_POST['user_menu_nodes']);
$menu_nats = sanitize($_POST['user_menu_nats']);
$menu_subnets = sanitize($_POST['user_menu_subnets']);
$menu_vlans = sanitize($_POST['user_menu_vlans']);
$menu_zones = sanitize($_POST['user_menu_zones']);
@ -46,6 +47,7 @@ switch ($submit = form_get_action()) {
if ($menu_cables) $menu[] = 'cable';
if ($menu_locations) $menu[] = 'location';
if ($menu_nodes) $menu[] = 'node';
if ($menu_nats) $menu[] = 'nat';
if ($menu_subnets) $menu[] = 'subnet';
if ($menu_vlans) $menu[] = 'vlan';
if ($menu_zones) $menu[] = 'zone';
@ -76,6 +78,7 @@ switch ($submit = form_get_action()) {
$_SESSION['suser_menu_cables'] = $menu_cables;
$_SESSION['suser_menu_locations'] = $menu_locations;
$_SESSION['suser_menu_nodes'] = $menu_nodes;
$_SESSION['suser_menu_nats'] = $menu_nats;
$_SESSION['suser_menu_subnets'] = $menu_subnets;
$_SESSION['suser_menu_vlans'] = $menu_vlans;
$_SESSION['suser_menu_zones'] = $menu_zones;
@ -133,7 +136,15 @@ $smarty->display("options.tpl");
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit display options ===================================
$smarty->assign("language", $language);
$smarty->assign("language", lang_getfrombrowser($config_lang, $config_lang_default));
// available languages
$lang_options = array();
foreach ($config_lang as $lang) {
$lang_options[$lang] = $lang;
}
$smarty->assign("lang_options", $lang_options);
if($_SESSION['suser_menu_assets']=='on') {
$user_menu_assets_checked = 'checked';
@ -170,6 +181,12 @@ if($_SESSION['suser_menu_nodes']=='on') {
} else {
$user_menu_nodes_checked = '';
}
// nats
if($_SESSION['suser_menu_nats']=='on') {
$user_menu_nats_checked = 'checked';
} else {
$user_menu_nats_checked = '';
}
// subnets
if($_SESSION['suser_menu_subnets']=='on') {
$user_menu_subnets_checked = 'checked';
@ -209,6 +226,7 @@ $smarty->assign("user_menu_assetclassgroups_checked", $user_menu_assetclassgroup
$smarty->assign("user_menu_cables_checked", $user_menu_cables_checked);
$smarty->assign("user_menu_locations_checked", $user_menu_locations_checked);
$smarty->assign("user_menu_nodes_checked", $user_menu_nodes_checked);
$smarty->assign("user_menu_nats_checked", $user_menu_nats_checked);
$smarty->assign("user_menu_subnets_checked", $user_menu_subnets_checked);
$smarty->assign("user_menu_vlans_checked", $user_menu_vlans_checked);
$smarty->assign("user_menu_zones_checked", $user_menu_zones_checked);

@ -16,7 +16,7 @@
<tr>
{foreach item=letter from=$alphabet}
<td>
<a href="asset.php?asset_letter={$letter.asset_letter}">{$letter.asset_letter}</a>&nbsp;
<a href="asset.php?bst={$letter.bst}">{$letter.bst}</a>&nbsp;
</td>
{/foreach}
</tr>

@ -47,19 +47,19 @@
</td>
<td class="label">
{if $role_add}
<img src="images/page_add.png" alt="[Add]"> {$lang_user_role_add} <br>
<img src="images/add.png" alt="[Add]"> {$lang_user_role_add}<br>
{/if}
{if $role_edit}
<img src="images/page_edit.png" alt="[Edit]"> {$lang_user_role_edit} <br>
<img src="images/edit.png" alt="[Edit]"> {$lang_user_role_edit}<br>
{/if}
{if $role_edit}
<img src="images/page_delete.png" alt="[Del]"> {$lang_user_role_delete} <br>
<img src="images/delete.png" alt="[Del]"> {$lang_user_role_delete}<br>
{/if}
{if $role_manage}
<img src="images/manage.png" alt="[Manage]"> {$lang_user_role_manage} <br>
<img src="images/manage.png" alt="[Manage]"> {$lang_user_role_manage}<br>
{/if}
{if $role_manage}
<img src="images/admin.png" alt="[Admin]"> {$lang_user_role_admin}
<img src="images/admin.png" alt="[Admin]"> {$lang_user_role_admin}
{/if}
</td>
</tr>

@ -27,7 +27,7 @@
<span class="help" title="{$lang_options_language_help}">{$lang_user_language}</span>
</td>
<td class="value">
<input type="text" name="user_language" value="{$user_language}" size="2">
{html_options name=user_language options=$lang_options selected={$user_language}}
({$language})
</td>
</tr>
@ -53,6 +53,7 @@
</td>
<td class="value">
<input type="text" name="user_mac" value="{$user_mac}">
(xx:xx:xx:xx:xx:xx)
</td>
</tr>
<tr>
@ -88,10 +89,13 @@
<input type="checkbox" name="user_menu_assetclasses" {$user_menu_assetclasses_checked}>{$lang_assetclasses}<br />
<input type="checkbox" name="user_menu_assetclassgroups" {$user_menu_assetclassgroups_checked}>{$lang_assetclassgroups}<br />
{if $suser_admin}
<input type="checkbox" name="user_menu_cables" {$user_menu_cables_checked}>{$lang_cables}<br />
<input type="checkbox" name="user_menu_cables" {$user_menu_cables_checked}>{$lang_cables}<br />
{/if}
<input type="checkbox" name="user_menu_locations" {$user_menu_locations_checked}>{$lang_locations}<br />
<input type="checkbox" name="user_menu_nodes" {$user_menu_nodes_checked}>{$lang_nodes}<br />
{if $suser_admin}
<input type="checkbox" name="user_menu_nats" {$user_menu_nats_checked}>{$lang_nats}<br />
{/if}
<input type="checkbox" name="user_menu_subnets" {$user_menu_subnets_checked}>{$lang_subnets}<br />
<input type="checkbox" name="user_menu_vlans" {$user_menu_vlans_checked}>{$lang_vlans}<br />
<input type="checkbox" name="user_menu_zones" {$user_menu_zones_checked}>{$lang_zones}

@ -156,6 +156,7 @@ $user = $sth->fetch(PDO::FETCH_OBJ);
$user->role = explode(',', $user->role);
$smarty->assign("user", $user);
// auth realms
$smarty->assign("realm_ids", ['local', 'ldap']);
$smarty->assign("realm_names", ['Local', 'LDAP']);