IP Reg is a IPAM tool to keep track of assets, nodes (IP addresses, MAC addresses, DNS aliases) within different subnets, over different locations or even VLAN's. Written in PHP, used with a MySQL-database to have a unique insight in your local network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
ipreg/lib/functions.php

149 lines
4.2 KiB

<?php
/*****************************************************************************
IP Reg, a PHP/MySQL IPAM tool
Copyright (C) 2007-2009 Wietse Warendorff (up to v0.5)
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;
// trim whitespaces
$input = @trim($input);
// magic quotes enabled?
if(get_magic_quotes_gpc()) {
// strip slashes
$input = stripslashes($input);
}
// convert to utf-8
iconv("UTF-8", "UTF-8", $input);
// convert special chars
$input = htmlentities($input,ENT_QUOTES,'UTF-8');
// and return
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['children']) {
$output .= "\n" . print_tree_rec($node['children'], $level+1);
}
$output .= "</li>\n";
}
$output .= "</ul>\n";
return $output;
}
function print_tree($params, Smarty_Internal_Template $template) {
if (empty($params['level'])) {
$level = 0;
} else {
$level = $params['level'];
}
if (empty($params['tree'])) {
return '';
} else {
return print_tree_rec($params['tree'], $level);
}
}
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();
}