From a2baae3ccfdd3eb9fa08c7d3593087baad6fd5e2 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sat, 13 Jun 2015 22:50:37 +1000 Subject: [PATCH] Move convenience functions into common; d_echo no longer adds "\n" automatically --- includes/common.php | 90 ++++++++++++++++++++++++++++++++++++++++++ includes/functions.php | 34 ---------------- includes/snmp.inc.php | 17 -------- 3 files changed, 90 insertions(+), 51 deletions(-) diff --git a/includes/common.php b/includes/common.php index 1d1896148d..43c24d97f5 100644 --- a/includes/common.php +++ b/includes/common.php @@ -605,4 +605,94 @@ function edit_service($service, $descr, $service_ip, $service_param = "", $servi } +/* + * convenience function - please use this instead of 'if ($debug) { echo ...; }' + */ +function d_echo($text, $no_debug_text = null) +{ + global $debug; + if ($debug) { + echo "$text"; + } + elseif ($no_debug_text) { + echo "$no_debug_text"; + } +} + +/* + * convenience function - please use this instead of 'if ($debug) { print_r ...; }' + */ +function d_print_r($var, $no_debug_text = null) +{ + global $debug; + if ($debug) { + print_r($var); + } + elseif ($no_debug_text) { + echo "$no_debug_text"; + } +} + +/* + * Shorten the name so it works as an RRD data source name (limited to 19 chars by default). + * Substitute for $subst if necessary. + * @return the shortened name + */ +function name_shorten($name, $common, $subst = "mibval", $len = 19) +{ + if (strlen($name) > $len && strpos($name, $common) >= 0) { + $newname = str_replace($common, '', $name); + $name = $newname; + } + if (strlen($name) > $len) { + $name = $subst; + } + return $name; +} + +/* + * @return the name of the rrd file for $host's $extra component + * @param host Host name + * @param extra Components of RRD filename - will be separated with "-" + */ +function rrd_name($host, $extra, $exten = ".rrd") +{ + global $config; + $filename = safename(is_array($extra) ? implode("-", $extra) : $extra); + return implode("/", array($config['rrd_dir'], $host, $filename.$exten)); +} + +/* + * @return true if the given graph type is a dynamic MIB graph + */ +function is_mib_graph($type, $subtype) +{ + global $config; + return $config['graph_types'][$type][$subtype]['section'] == 'mib'; +} + +/* + * @return true if client IP address is authorized to access graphs + */ +function is_client_authorized($clientip) +{ + + global $config; + if (isset($config['allow_unauth_graphs']) && $config['allow_unauth_graphs']) { + d_echo("Unauthorized graphs allowed\n"); + return true; + } + + if (isset($config['allow_unauth_graphs_cidr'])) { + foreach ($config['allow_unauth_graphs_cidr'] as $range) { + if (Net_IPv4::ipInNetwork($clientip, $range)) { + d_echo("Unauthorized graphs allowed from $range\n"); + return true; + } + } + } + + return false; +} + ?> diff --git a/includes/functions.php b/includes/functions.php index d3846c980b..96d6a52275 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1247,37 +1247,3 @@ function ip_exists($ip) { } return true; } - -/* - * convenience function - please use this instead of 'if ($debug) { echo ...; }' - */ -function d_echo($text) -{ - global $debug; - if ($debug) { - echo "$text\n"; - } -} - -/* - * convenience function - please use this instead of 'if ($debug) { print_r ...; }' - */ -function d_print_r($var) -{ - global $debug; - if ($debug) { - print_r($var); - } -} - -/* - * @return the name of the rrd file for $host's $extra component - * @param host Host name - * @param extra Components of RRD filename - will be separated with "-" - */ -function rrdname($host, $extra) -{ - global $config; - return implode("/", array($config['rrd_dir'], $host, safename(implode("-", $extra)).".rrd")); -} - diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 4be24da1f4..8db5abde38 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -805,23 +805,6 @@ function snmp_gen_auth (&$device) return $cmd; } -/* - * Shorten the name to so it works as an RRD data source. - * Substitute for $subst if necessary. - * @return the shortened name - */ -function name_shorten($name, $common, $subst = "mibval", $len = 19) -{ - if (strlen($name) > $len && strpos($name, $common) >= 0) { - $newname = str_replace($common, '', $name); - $name = $newname; - } - if (strlen($name) > $len) { - $name = $subst; - } - return $name; -} - /* * Translate the given MIB into a vaguely useful PHP array. Each keyword becomes an array index. *