template_dir = 'tpl'; $smarty->compile_dir = 'tpl_c'; $smarty->registerPlugin('function', 'treelist', 'print_tree'); $smarty->registerPlugin('function', 'msgout', 'msgout'); if (!empty($_SESSION['suser_id'])) { $smarty->assign("suser_name", $_SESSION['suser_displayname']); $smarty->assign("suser_tooltips", $_SESSION['suser_tooltips'] ?? 'off'); $smarty->assign("suser_add", $_SESSION['suser_role_add']); $smarty->assign("suser_edit", $_SESSION['suser_role_edit']); $smarty->assign("suser_delete", $_SESSION['suser_role_delete']); $smarty->assign("suser_manage", $_SESSION['suser_role_manage']); $smarty->assign("suser_admin", $_SESSION['suser_role_admin']); } // prepare global message system $g_message = new Message; $g_warning = new MessageWarning; $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 { var $count = 0; var $text = array(); var $caption; function Message() { $this->caption = 'Information'; } function SetCaption($str) { $this->caption = $str; } function Add($msg) { $this->count++; $this->text[$this->count] = $msg; } function GetCount() { return $this->count; } function PrintOut() { if ($this->count > 0) { echo '
', "\n"; echo '

', $this->caption, "

\n"; echo "\n"; echo "
\n"; } } } class MessageWarning extends Message { function MessageWarning() { $this->caption = 'Warning'; } function PrintOut() { if ($this->count > 0) { echo '
', "\n"; echo '

', $this->caption, "

\n"; echo "\n"; echo "
\n"; } } } class MessageError extends Message { function MessageError() { $this->caption = 'Error'; } function PrintOut() { if ($this->count > 0) { echo '
', "\n"; echo '

', $this->caption, "

\n"; echo "\n"; echo "
\n"; } } } 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() { if (!isset($_POST['submit'])) { if (isset($_GET['f'])) { $submit = $_GET['f']; } else { $submit = NULL; } } else { $submit = $_POST['submit']; } if (is_array($submit)) { $submit = key($submit); } return strtolower($submit); } function submit_error($action) { /* Submit buttons that return an unknown value end up in this 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)); } // ========== DATABASE FUCTIONS =============================================== function db_load_enum($table, $column) { // returns array of enum-values as defined in database global $dbh; $sql = "SELECT TRIM(TRAILING ')' FROM SUBSTRING(column_type,6)) FROM information_schema.columns WHERE table_name=? AND column_name=?"; $sth = $dbh->prepare($sql); $sth->execute([$table, $column]); // Für PHP < 7.4 // return array_map(function($x) { return trim($x, "'"); }, explode(',', $sth->fetchColumn())); return array_map(fn($x) => trim($x, "'"), explode(',', $sth->fetchColumn())); } function db_get_options_asset() { global $dbh; $sql = "SELECT asset_id, asset_name FROM asset ORDER BY asset_name"; $sth = $dbh->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $options[$rec[0]] = $rec[1]; } return $options; } function db_get_options_assetclass() { global $dbh; $sql = "SELECT assetclass_id, assetclass_name FROM assetclass ORDER BY assetclass_name"; $sth = $dbh->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $options[$rec[0]] = $rec[1]; } return $options; } function db_get_options_assetclassgroup() { global $dbh; $sql = "SELECT assetclassgroup_id, assetclassgroup_name FROM assetclassgroup ORDER BY assetclassgroup_name"; $sth = $dbh->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $options[$rec[0]] = $rec[1]; } return $options; } function db_get_options_location($default = NULL) { global $dbh; $options = array(); if ($default != NULL) { $options[0] = $default; } $sql = "SELECT location_id, location_name FROM location ORDER BY location_name"; $sth = $dbh->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $options[$rec[0]] = $rec[1]; } return $options; } function db_get_options_subnet() { global $dbh; $sql = "SELECT subnet_id, CONCAT_WS('/', subnet_address, subnet_mask) AS subnet_name FROM subnet ORDER BY INET_ATON(subnet_address)"; $sth = $dbh->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $options[$rec[0]] = $rec[1]; } return $options; } function db_get_options_vlan($default = NULL) { global $dbh; $options = array(); if ($default != NULL) { $options[0] = $default; } $sql = "SELECT vlan_id, vlan_name FROM vlan ORDER BY vlan_name"; $sth = $dbh->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $options[$rec[0]] = $rec[1]; } return $options; } function db_get_options_zone($default = NULL) { global $dbh; $options = array(); if ($default != NULL) { $options[0] = $default; } $sql = "SELECT zone_id, zone_origin FROM zone ORDER BY zone_origin"; $sth = $dbh->query($sql); foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) { $options[$rec[0]] = $rec[1]; } 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; }