2009-10-02 13:20:24 +00:00
< ? php
2012-05-25 10:34:01 +00:00
/**
2013-10-28 18:32:21 +00:00
* LibreNMS
2012-05-25 10:34:01 +00:00
*
2013-10-28 18:32:21 +00:00
* This file is part of LibreNMS
2012-05-25 10:34:01 +00:00
*
2013-10-28 18:32:21 +00:00
* @ package librenms
2012-05-25 10:34:01 +00:00
* @ subpackage functions
2013-10-28 18:32:21 +00:00
* @ author LibreNMS Contributors < librenms - project @ google . groups . com >
* @ copyright ( C ) 2006 - 2012 Adam Armstrong ( as Observium )
* @ copyright ( C ) 2013 LibreNMS Group
2012-05-25 10:34:01 +00:00
*
*/
2012-05-11 15:21:42 +00:00
function data_uri ( $file , $mime )
{
$contents = file_get_contents ( $file );
$base64 = base64_encode ( $contents );
return ( 'data:' . $mime . ';base64,' . $base64 );
}
2012-05-25 10:34:01 +00:00
function nicecase ( $item )
{
switch ( $item )
{
case " dbm " :
return " dBm " ;
case " mysql " :
return " MySQL " ;
case " powerdns " :
return " PowerDNS " ;
case " bind " :
return " BIND " ;
default :
return ucfirst ( $item );
}
}
2012-04-14 23:28:23 +00:00
function toner2colour ( $descr , $percent )
2012-04-09 23:00:31 +00:00
{
2012-04-14 23:28:23 +00:00
$colour = get_percentage_colours ( 100 - $percent );
2012-04-09 23:00:31 +00:00
2012-04-14 23:28:23 +00:00
if ( substr ( $descr , - 1 ) == 'C' || stripos ( $descr , " cyan " ) !== false ) { $colour [ 'left' ] = " 55D6D3 " ; $colour [ 'right' ] = " 33B4B1 " ; }
if ( substr ( $descr , - 1 ) == 'M' || stripos ( $descr , " magenta " ) !== false ) { $colour [ 'left' ] = " F24AC8 " ; $colour [ 'right' ] = " D028A6 " ; }
2012-04-09 23:00:31 +00:00
if ( substr ( $descr , - 1 ) == 'Y' || stripos ( $descr , " yellow " ) !== false
|| stripos ( $descr , " giallo " ) !== false
2012-04-14 23:28:23 +00:00
|| stripos ( $descr , " gul " ) !== false ) { $colour [ 'left' ] = " FFF200 " ; $colour [ 'right' ] = " DDD000 " ; }
2012-04-09 23:00:31 +00:00
if ( substr ( $descr , - 1 ) == 'K' || stripos ( $descr , " black " ) !== false
2012-04-14 23:28:23 +00:00
|| stripos ( $descr , " nero " ) !== false ) { $colour [ 'left' ] = " 000000 " ; $colour [ 'right' ] = " 222222 " ; }
2012-04-09 23:00:31 +00:00
2012-04-17 15:27:17 +00:00
return $colour ;
2012-04-09 23:00:31 +00:00
}
2011-09-18 13:11:04 +00:00
function generate_link ( $text , $vars , $new_vars = array ())
{
return '<a href="' . generate_url ( $vars , $new_vars ) . '">' . $text . '</a>' ;
}
2011-09-17 19:14:44 +00:00
2011-09-14 13:38:01 +00:00
function generate_url ( $vars , $new_vars = array ())
{
$vars = array_merge ( $vars , $new_vars );
2011-09-17 18:31:49 +00:00
$url = $vars [ 'page' ] . " / " ;
2011-09-14 13:38:01 +00:00
unset ( $vars [ 'page' ]);
2011-10-04 09:10:21 +00:00
foreach ( $vars as $var => $value )
2011-09-14 13:38:01 +00:00
{
2011-12-06 23:02:31 +00:00
if ( $value == " 0 " || $value != " " && strstr ( $var , " opt " ) === FALSE && is_numeric ( $var ) === FALSE )
2011-09-14 13:38:01 +00:00
{
$url .= $var . " = " . $value . " / " ;
}
}
return ( $url );
}
2013-11-18 22:57:43 +00:00
function escape_quotes ( $text )
{
2013-11-26 01:03:00 +00:00
return str_replace ( '"' , " \ ' " , str_replace ( " ' " , " \ ' " , $text ));
2013-11-18 22:57:43 +00:00
}
2011-09-17 19:14:44 +00:00
function generate_overlib_content ( $graph_array , $text )
{
global $config ;
2013-11-18 22:57:43 +00:00
$overlib_content = '<div class=overlib><span class=overlib-text>' . $text . '</span><br />' ;
2011-09-17 19:14:44 +00:00
foreach ( array ( 'day' , 'week' , 'month' , 'year' ) as $period )
{
$graph_array [ 'from' ] = $config [ 'time' ][ $period ];
2013-11-18 22:57:43 +00:00
$overlib_content .= escape_quotes ( generate_graph_tag ( $graph_array ));
2011-09-17 19:14:44 +00:00
}
2013-11-18 22:57:43 +00:00
$overlib_content .= '</div>' ;
2011-09-17 19:14:44 +00:00
return $overlib_content ;
}
2011-04-25 17:15:36 +00:00
function get_percentage_colours ( $percentage )
{
2015-02-22 16:45:40 +00:00
$background = array ();
2011-04-25 17:15:36 +00:00
if ( $percentage > '90' ) { $background [ 'left' ] = 'c4323f' ; $background [ 'right' ] = 'C96A73' ; }
elseif ( $percentage > '75' ) { $background [ 'left' ] = 'bf5d5b' ; $background [ 'right' ] = 'd39392' ; }
elseif ( $percentage > '50' ) { $background [ 'left' ] = 'bf875b' ; $background [ 'right' ] = 'd3ae92' ; }
elseif ( $percentage > '25' ) { $background [ 'left' ] = '5b93bf' ; $background [ 'right' ] = '92b7d3' ; }
else { $background [ 'left' ] = '9abf5b' ; $background [ 'right' ] = 'bbd392' ; }
return ( $background );
}
2013-11-26 05:32:53 +00:00
function generate_minigraph_image ( $device , $start , $end , $type , $legend = 'no' , $width = 275 , $height = 100 , $sep = '&' , $class = " minigraph-image " )
2013-11-10 04:08:02 +00:00
{
2013-11-26 05:32:53 +00:00
return '<img class="' . $class . '" src="graph.php?' .
2014-03-14 00:02:41 +00:00
implode ( $sep , array ( 'device=' . $device [ 'device_id' ], " from= $start " , " to= $end " , " width= $width " , " height= $height " , " type= $type " , " legend= $legend " )) . '">' ;
2013-11-10 04:08:02 +00:00
}
2011-09-18 13:11:04 +00:00
function generate_device_url ( $device , $vars = array ())
2011-05-17 19:21:20 +00:00
{
2011-09-18 13:11:04 +00:00
return generate_url ( array ( 'page' => 'device' , 'device' => $device [ 'device_id' ]), $vars );
2011-05-17 19:21:20 +00:00
}
2015-04-18 09:29:44 +00:00
function generate_device_link ( $device , $text = NULL , $vars = array (), $start = 0 , $end = 0 , $escape_text = 1 , $overlib = 1 )
2010-06-24 18:04:42 +00:00
{
2011-05-17 19:21:20 +00:00
global $config ;
if ( ! $start ) { $start = $config [ 'time' ][ 'day' ]; }
if ( ! $end ) { $end = $config [ 'time' ][ 'now' ]; }
2011-02-02 11:59:08 +00:00
2010-06-24 18:04:42 +00:00
$class = devclass ( $device );
if ( ! $text ) { $text = $device [ 'hostname' ]; }
2011-02-02 11:59:08 +00:00
2010-07-23 15:05:01 +00:00
if ( isset ( $config [ 'os' ][ $device [ 'os' ]][ 'over' ]))
2010-06-24 18:04:42 +00:00
{
2010-07-23 15:05:01 +00:00
$graphs = $config [ 'os' ][ $device [ 'os' ]][ 'over' ];
2010-06-24 18:04:42 +00:00
}
2011-05-05 14:39:50 +00:00
elseif ( isset ( $device [ 'os_group' ]) && isset ( $config [ 'os' ][ $device [ 'os_group' ]][ 'over' ]))
2010-06-25 13:04:03 +00:00
{
2010-07-23 15:05:01 +00:00
$graphs = $config [ 'os' ][ $device [ 'os_group' ]][ 'over' ];
2011-03-16 18:28:52 +00:00
}
2010-06-24 18:04:42 +00:00
else
{
2011-03-16 18:28:52 +00:00
$graphs = $config [ 'os' ][ 'default' ][ 'over' ];
2010-06-24 18:04:42 +00:00
}
2011-09-18 13:11:04 +00:00
$url = generate_device_url ( $device , $vars );
2013-11-10 00:37:13 +00:00
// beginning of overlib box contains large hostname followed by hardware & OS details
$contents = " <div><span class=list-large> " . $device [ 'hostname' ] . " </span> " ;
2011-03-16 18:28:52 +00:00
if ( $device [ 'hardware' ]) { $contents .= " - " . $device [ 'hardware' ]; }
2013-11-10 00:37:13 +00:00
if ( $device [ 'os' ]) { $contents .= " - " . mres ( $config [ 'os' ][ $device [ 'os' ]][ 'text' ]); }
2011-03-16 18:28:52 +00:00
if ( $device [ 'version' ]) { $contents .= " " . mres ( $device [ 'version' ]); }
if ( $device [ 'features' ]) { $contents .= " ( " . mres ( $device [ 'features' ]) . " ) " ; }
2013-11-10 00:37:13 +00:00
if ( isset ( $device [ 'location' ])) { $contents .= " - " . htmlentities ( $device [ 'location' ]); }
2010-07-23 13:40:31 +00:00
$contents .= " </div> " ;
2010-07-23 15:05:01 +00:00
foreach ( $graphs as $entry )
2010-06-24 18:04:42 +00:00
{
2010-07-23 15:05:01 +00:00
$graph = $entry [ 'graph' ];
2011-03-16 18:28:52 +00:00
$graphhead = $entry [ 'text' ];
2013-11-10 04:08:02 +00:00
$contents .= '<div class=overlib-box>' ;
$contents .= '<span class=overlib-title>' . $graphhead . '</span><br />' ;
2013-11-26 01:03:00 +00:00
$contents .= generate_minigraph_image ( $device , $start , $end , $graph );
$contents .= generate_minigraph_image ( $device , $config [ 'time' ][ 'week' ], $end , $graph );
2010-07-23 15:05:01 +00:00
$contents .= '</div>' ;
2010-06-24 18:04:42 +00:00
}
2011-03-16 18:28:52 +00:00
2013-11-26 04:35:23 +00:00
if ( $escape_text ) { $text = htmlentities ( $text ); }
2015-04-18 09:29:44 +00:00
if ( $overlib == 0 ) {
2015-04-17 15:24:51 +00:00
$link = $contents ;
} else {
$link = overlib_link ( $url , $text , escape_quotes ( $contents ), $class );
}
2011-03-16 18:28:52 +00:00
if ( device_permitted ( $device [ 'device_id' ]))
{
2010-06-24 18:04:42 +00:00
return $link ;
} else {
return $device [ 'hostname' ];
}
}
2011-03-16 18:28:52 +00:00
function overlib_link ( $url , $text , $contents , $class )
{
2010-05-02 23:24:47 +00:00
global $config ;
2011-03-16 18:28:52 +00:00
2010-05-02 23:24:47 +00:00
$contents = str_replace ( " \" " , " \ ' " , $contents );
2012-05-09 16:18:23 +00:00
$output = '<a class="' . $class . '" href="' . $url . '"' ;
2015-04-28 20:14:31 +00:00
if ( $config [ 'web_mouseover' ] === FALSE ) {
$output .= " > " ;
} else {
$output .= " onmouseover= \" return overlib(' " . $contents . " ' " . $config [ 'overlib_defaults' ] . " , WRAP,HAUTO,VAUTO); \" onmouseout= \" return nd(); \" > " ;
}
2010-05-02 23:24:47 +00:00
$output .= $text . " </a> " ;
2011-03-16 18:28:52 +00:00
2010-05-02 23:24:47 +00:00
return $output ;
}
2011-03-16 18:28:52 +00:00
function generate_graph_popup ( $graph_array )
2010-05-02 23:24:47 +00:00
{
global $config ;
2011-09-20 14:37:54 +00:00
2012-05-25 12:24:34 +00:00
// Take $graph_array and print day,week,month,year graps in overlib, hovered over graph
2010-05-02 23:24:47 +00:00
2012-05-14 09:40:13 +00:00
$original_from = $graph_array [ 'from' ];
2010-05-02 23:24:47 +00:00
$graph = generate_graph_tag ( $graph_array );
$content = " <div class=list-large> " . $graph_array [ 'popup_title' ] . " </div> " ;
$content .= " <div style= \ 'width: 850px \ '> " ;
$graph_array [ 'legend' ] = " yes " ;
$graph_array [ 'height' ] = " 100 " ;
$graph_array [ 'width' ] = " 340 " ;
2011-05-17 19:21:20 +00:00
$graph_array [ 'from' ] = $config [ 'time' ][ 'day' ];
2010-05-02 23:24:47 +00:00
$content .= generate_graph_tag ( $graph_array );
2011-05-17 19:21:20 +00:00
$graph_array [ 'from' ] = $config [ 'time' ][ 'week' ];
2010-05-02 23:24:47 +00:00
$content .= generate_graph_tag ( $graph_array );
2011-05-17 19:21:20 +00:00
$graph_array [ 'from' ] = $config [ 'time' ][ 'month' ];
2010-05-02 23:24:47 +00:00
$content .= generate_graph_tag ( $graph_array );
2011-05-17 19:21:20 +00:00
$graph_array [ 'from' ] = $config [ 'time' ][ 'year' ];
2010-05-02 23:24:47 +00:00
$content .= generate_graph_tag ( $graph_array );
$content .= " </div> " ;
2012-05-15 15:18:57 +00:00
2012-05-14 09:40:13 +00:00
$graph_array [ 'from' ] = $original_from ;
2011-09-22 15:05:11 +00:00
2012-05-11 13:26:14 +00:00
$graph_array [ 'link' ] = generate_url ( $graph_array , array ( 'page' => 'graphs' , 'height' => NULL , 'width' => NULL , 'bg' => NULL ));
# $graph_array['link'] = "graphs/type=" . $graph_array['type'] . "/id=" . $graph_array['id'];
2010-05-02 23:24:47 +00:00
2011-03-31 10:51:02 +00:00
return overlib_link ( $graph_array [ 'link' ], $graph , $content , NULL );
2010-05-02 23:24:47 +00:00
}
function print_graph_popup ( $graph_array )
{
2010-11-20 14:04:07 +00:00
echo ( generate_graph_popup ( $graph_array ));
2010-05-02 23:24:47 +00:00
}
2011-03-16 18:28:52 +00:00
function permissions_cache ( $user_id )
2010-07-31 21:08:35 +00:00
{
2010-04-24 14:23:16 +00:00
$permissions = array ();
2011-05-12 20:16:03 +00:00
foreach ( dbFetchRows ( " SELECT * FROM devices_perms WHERE user_id = ' " . $user_id . " ' " ) as $device )
2011-03-16 18:28:52 +00:00
{
$permissions [ 'device' ][ $device [ 'device_id' ]] = 1 ;
2010-04-24 14:23:16 +00:00
}
2011-05-12 20:16:03 +00:00
foreach ( dbFetchRows ( " SELECT * FROM ports_perms WHERE user_id = ' " . $user_id . " ' " ) as $port )
2011-03-16 18:28:52 +00:00
{
2012-05-16 13:25:50 +00:00
$permissions [ 'port' ][ $port [ 'port_id' ]] = 1 ;
2010-04-24 14:23:16 +00:00
}
2011-05-12 20:16:03 +00:00
foreach ( dbFetchRows ( " SELECT * FROM bill_perms WHERE user_id = ' " . $user_id . " ' " ) as $bill )
2011-03-16 18:28:52 +00:00
{
2010-07-31 21:08:35 +00:00
$permissions [ 'bill' ][ $bill [ 'bill_id' ]] = 1 ;
}
2011-03-31 10:51:02 +00:00
2010-04-24 14:23:16 +00:00
return $permissions ;
}
2011-03-16 18:28:52 +00:00
function bill_permitted ( $bill_id )
2010-07-31 21:08:35 +00:00
{
2011-03-31 17:19:54 +00:00
global $permissions ;
2011-03-16 18:28:52 +00:00
2010-07-31 21:08:35 +00:00
if ( $_SESSION [ 'userlevel' ] >= " 5 " ) {
$allowed = TRUE ;
2011-03-16 18:28:52 +00:00
} elseif ( $permissions [ 'bill' ][ $bill_id ]) {
2010-07-31 21:08:35 +00:00
$allowed = TRUE ;
} else {
$allowed = FALSE ;
}
2011-03-16 18:28:52 +00:00
return $allowed ;
2010-07-31 21:08:35 +00:00
}
2011-03-16 18:28:52 +00:00
2012-05-16 13:25:50 +00:00
function port_permitted ( $port_id , $device_id = NULL )
2010-04-24 22:43:25 +00:00
{
2011-03-31 17:19:54 +00:00
global $permissions ;
2010-08-01 14:17:06 +00:00
2012-05-16 13:25:50 +00:00
if ( ! is_numeric ( $device_id )) { $device_id = get_device_id_by_port_id ( $port_id ); }
2011-03-16 18:28:52 +00:00
if ( $_SESSION [ 'userlevel' ] >= " 5 " )
{
2010-04-24 22:43:25 +00:00
$allowed = TRUE ;
2011-03-16 18:28:52 +00:00
} elseif ( device_permitted ( $device_id )) {
2010-04-24 22:43:25 +00:00
$allowed = TRUE ;
2012-05-16 13:25:50 +00:00
} elseif ( $permissions [ 'port' ][ $port_id ]) {
2010-04-24 22:43:25 +00:00
$allowed = TRUE ;
} else {
$allowed = FALSE ;
}
2011-03-16 18:28:52 +00:00
2010-04-24 22:43:25 +00:00
return $allowed ;
}
2010-08-01 14:17:06 +00:00
function application_permitted ( $app_id , $device_id = NULL )
{
2011-03-31 17:19:54 +00:00
global $permissions ;
2011-09-20 14:37:54 +00:00
2011-03-16 18:28:52 +00:00
if ( is_numeric ( $app_id ))
2010-08-01 14:17:06 +00:00
{
2011-12-02 13:57:13 +00:00
if ( ! $device_id ) { $device_id = get_device_id_by_app_id ( $app_id ); }
2010-08-01 14:17:06 +00:00
if ( $_SESSION [ 'userlevel' ] >= " 5 " ) {
$allowed = TRUE ;
2011-03-16 18:28:52 +00:00
} elseif ( device_permitted ( $device_id )) {
2010-08-01 14:17:06 +00:00
$allowed = TRUE ;
2011-03-16 18:28:52 +00:00
} elseif ( $permissions [ 'application' ][ $app_id ]) {
2010-08-01 14:17:06 +00:00
$allowed = TRUE ;
} else {
$allowed = FALSE ;
}
} else {
$allowed = FALSE ;
}
2011-03-31 10:51:02 +00:00
2010-08-01 14:17:06 +00:00
return $allowed ;
}
function device_permitted ( $device_id )
2010-04-24 22:43:25 +00:00
{
2011-03-31 17:19:54 +00:00
global $permissions ;
2011-03-16 18:28:52 +00:00
if ( $_SESSION [ 'userlevel' ] >= " 5 " )
{
2010-04-24 22:43:25 +00:00
$allowed = true ;
2011-03-27 10:21:19 +00:00
} elseif ( $permissions [ 'device' ][ $device_id ]) {
2010-04-24 22:43:25 +00:00
$allowed = true ;
} else {
$allowed = false ;
}
2011-03-16 18:28:52 +00:00
return $allowed ;
2010-04-24 22:43:25 +00:00
}
2011-04-22 20:46:19 +00:00
function print_graph_tag ( $args )
2010-03-11 21:58:50 +00:00
{
2011-04-22 20:46:19 +00:00
echo ( generate_graph_tag ( $args ));
2010-03-11 21:58:50 +00:00
}
2011-04-22 20:46:19 +00:00
function generate_graph_tag ( $args )
2010-03-11 21:58:50 +00:00
{
2015-02-22 16:45:40 +00:00
$urlargs = array ();
2011-03-16 18:28:52 +00:00
foreach ( $args as $key => $arg )
2010-03-11 21:58:50 +00:00
{
2011-04-22 20:46:19 +00:00
$urlargs [] = $key . " = " . $arg ;
2010-03-11 21:58:50 +00:00
}
2011-03-31 10:51:02 +00:00
2011-04-22 20:46:19 +00:00
return '<img src="graph.php?' . implode ( '&' , $urlargs ) . '" border="0" />' ;
2010-03-11 21:58:50 +00:00
}
2011-06-14 14:04:56 +00:00
function generate_graph_js_state ( $args ) {
// we are going to assume we know roughly what the graph url looks like here.
// TODO: Add sensible defaults
$from = ( is_numeric ( $args [ 'from' ]) ? $args [ 'from' ] : 0 );
$to = ( is_numeric ( $args [ 'to' ]) ? $args [ 'to' ] : 0 );
$width = ( is_numeric ( $args [ 'width' ]) ? $args [ 'width' ] : 0 );
$height = ( is_numeric ( $args [ 'height' ]) ? $args [ 'height' ] : 0 );
$legend = str_replace ( " ' " , " " , $args [ 'legend' ]);
$state = <<< STATE
< script type = " text/javascript " language = " JavaScript " >
document . graphFrom = $from ;
document . graphTo = $to ;
document . graphWidth = $width ;
document . graphHeight = $height ;
document . graphLegend = '$legend' ;
</ script >
STATE ;
return $state ;
}
2011-04-22 20:46:19 +00:00
function print_percentage_bar ( $width , $height , $percent , $left_text , $left_colour , $left_background , $right_text , $right_colour , $right_background )
2010-02-16 00:44:10 +00:00
{
2011-09-16 22:45:01 +00:00
2011-09-20 14:37:54 +00:00
if ( $percent > " 100 " ) { $size_percent = " 100 " ; } else { $size_percent = $percent ; }
2011-09-16 22:45:01 +00:00
2010-02-16 00:44:10 +00:00
$output = '
2015-01-22 16:22:19 +00:00
< div class = " container " style = " width:'. $width .'px; height:'. $height .'px; " >
< div class = " progress " style = " min-width: 2em; background-color:#'. $right_background .'; height:'. $height .'px; " >
< div class = " progress-bar " role = " progressbar " aria - valuenow = " '. $size_percent .' " aria - valuemin = " 0 " aria - valuemax = " 100 " style = " min-width: 2em; width:'. $size_percent .'%; background-color: #'. $left_background .'; " >
2010-02-16 00:44:10 +00:00
</ div >
2015-01-22 16:22:19 +00:00
</ div >
< b class = " pull-left " style = " padding-left: 4px; height: '. $height .'px;margin-top:-'.( $height *2).'px; color:#'. $left_colour .'; " > '.$left_text.' </ b >
< b class = " pull-right " style = " padding-right: 4px; height: '. $height .'px;margin-top:-'.( $height *2).'px; color:#'. $right_colour .'; " > '.$right_text.' </ b >
</ div >
' ;
2011-03-31 10:51:02 +00:00
2010-02-13 23:55:56 +00:00
return $output ;
}
2012-05-16 16:24:43 +00:00
function generate_entity_link ( $type , $entity , $text = NULL , $graph_type = NULL )
2009-10-02 13:20:24 +00:00
{
2012-05-20 23:14:08 +00:00
global $config , $entity_cache ;
2011-03-31 10:51:02 +00:00
2012-05-20 23:14:08 +00:00
if ( is_numeric ( $entity ))
2012-05-16 16:24:43 +00:00
{
$entity = get_entity_by_id_cache ( $type , $entity );
}
switch ( $type )
{
case " port " :
$link = generate_port_link ( $entity , $text , $graph_type );
break ;
case " storage " :
2012-05-20 23:14:08 +00:00
if ( empty ( $text )) { $text = $entity [ 'storage_descr' ]; }
2012-05-17 10:33:56 +00:00
$link = generate_link ( $text , array ( 'page' => 'device' , 'device' => $entity [ 'device_id' ], 'tab' => 'health' , 'metric' => 'storage' ));
break ;
2012-05-16 16:24:43 +00:00
default :
$link = $entity [ $type . '_id' ];
}
return ( $link );
}
2015-04-18 09:29:44 +00:00
function generate_port_link ( $port , $text = NULL , $type = NULL , $overlib = 1 , $single_graph = 0 )
2012-05-16 16:24:43 +00:00
{
global $config ;
2015-02-22 16:45:40 +00:00
$graph_array = array ();
2012-05-16 16:24:43 +00:00
$port = ifNameDescr ( $port );
if ( ! $text ) { $text = fixIfName ( $port [ 'label' ]); }
if ( $type ) { $port [ 'graph_type' ] = $type ; }
if ( ! isset ( $port [ 'graph_type' ])) { $port [ 'graph_type' ] = 'port_bits' ; }
2011-03-16 18:28:52 +00:00
2012-05-16 16:24:43 +00:00
$class = ifclass ( $port [ 'ifOperStatus' ], $port [ 'ifAdminStatus' ]);
2011-05-19 10:02:55 +00:00
2012-05-16 16:24:43 +00:00
if ( ! isset ( $port [ 'hostname' ])) { $port = array_merge ( $port , device_by_id_cache ( $port [ 'device_id' ])); }
2010-05-02 23:24:47 +00:00
2012-05-16 16:24:43 +00:00
$content = " <div class=list-large> " . $port [ 'hostname' ] . " - " . fixifName ( $port [ 'label' ]) . " </div> " ;
if ( $port [ 'ifAlias' ]) { $content .= $port [ 'ifAlias' ] . " <br /> " ; }
2010-05-02 23:24:47 +00:00
$content .= " <div style= \ 'width: 850px \ '> " ;
2012-05-16 16:24:43 +00:00
$graph_array [ 'type' ] = $port [ 'graph_type' ];
2010-05-02 23:24:47 +00:00
$graph_array [ 'legend' ] = " yes " ;
$graph_array [ 'height' ] = " 100 " ;
$graph_array [ 'width' ] = " 340 " ;
2011-09-20 14:37:54 +00:00
$graph_array [ 'to' ] = $config [ 'time' ][ 'now' ];
2011-05-17 19:21:20 +00:00
$graph_array [ 'from' ] = $config [ 'time' ][ 'day' ];
2012-05-16 16:24:43 +00:00
$graph_array [ 'id' ] = $port [ 'port_id' ];
2010-05-02 23:24:47 +00:00
$content .= generate_graph_tag ( $graph_array );
2015-04-17 15:24:51 +00:00
if ( $single_graph == 0 ) {
$graph_array [ 'from' ] = $config [ 'time' ][ 'week' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'month' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'year' ];
$content .= generate_graph_tag ( $graph_array );
}
2010-05-02 23:24:47 +00:00
$content .= " </div> " ;
2011-03-16 18:28:52 +00:00
2012-05-16 16:24:43 +00:00
$url = generate_port_url ( $port );
2010-05-02 23:24:47 +00:00
2015-04-18 09:29:44 +00:00
if ( $overlib == 0 ) {
2015-04-17 15:24:51 +00:00
return $content ;
} elseif ( port_permitted ( $port [ 'port_id' ], $port [ 'device_id' ])) {
2010-05-02 23:24:47 +00:00
return overlib_link ( $url , $text , $content , $class );
} else {
return fixifName ( $text );
}
2009-10-02 13:20:24 +00:00
}
2011-09-18 13:11:04 +00:00
function generate_port_url ( $port , $vars = array ())
{
2012-05-16 13:25:50 +00:00
return generate_url ( array ( 'page' => 'device' , 'device' => $port [ 'device_id' ], 'tab' => 'port' , 'port' => $port [ 'port_id' ]), $vars );
2011-05-17 19:21:20 +00:00
}
2014-03-18 22:36:22 +00:00
function generate_peer_url ( $peer , $vars = array ())
{
return generate_url ( array ( 'page' => 'device' , 'device' => $peer [ 'device_id' ], 'tab' => 'routing' , 'proto' => 'bgp' ), $vars );
}
2013-11-26 01:03:00 +00:00
function generate_port_image ( $args )
2010-03-11 21:58:50 +00:00
{
2011-09-26 14:06:48 +00:00
if ( ! $args [ 'bg' ]) { $args [ 'bg' ] = " FFFFFF " ; }
2013-11-26 01:03:00 +00:00
return " <img src='graph.php?type= " . $args [ 'graph_type' ] . " &id= " . $args [ 'port_id' ] . " &from= " . $args [ 'from' ] . " &to= " . $args [ 'to' ] . " &width= " . $args [ 'width' ] . " &height= " . $args [ 'height' ] . " &bg= " . $args [ 'bg' ] . " '> " ;
}
function generate_port_thumbnail ( $port )
{
global $config ;
$port [ 'graph_type' ] = 'port_bits' ;
$port [ 'from' ] = $config [ 'time' ][ 'day' ];
$port [ 'to' ] = $config [ 'time' ][ 'now' ];
$port [ 'width' ] = 150 ;
2013-11-26 01:21:21 +00:00
$port [ 'height' ] = 21 ;
2013-11-26 01:03:00 +00:00
return generate_port_image ( $port );
}
function print_port_thumbnail ( $args )
{
echo ( generate_port_link ( $args , generate_port_image ( $args )));
2009-10-02 13:20:24 +00:00
}
2011-09-14 13:38:01 +00:00
function print_optionbar_start ( $height = 0 , $width = 0 , $marginbottom = 5 )
2010-03-11 21:58:50 +00:00
{
2014-02-27 19:49:24 +00:00
echo ( '
2015-02-22 21:39:00 +00:00
< div class = " well well-sm " >
2014-02-27 19:49:24 +00:00
' );
2009-10-02 13:20:24 +00:00
}
2011-03-16 18:28:52 +00:00
function print_optionbar_end ()
2010-03-11 21:58:50 +00:00
{
2011-09-15 23:05:06 +00:00
echo ( ' </div>' );
2009-10-02 13:20:24 +00:00
}
2011-03-16 18:28:52 +00:00
function geteventicon ( $message )
2010-07-07 13:58:11 +00:00
{
if ( $message == " Device status changed to Down " ) { $icon = " server_connect.png " ; }
if ( $message == " Device status changed to Up " ) { $icon = " server_go.png " ; }
2011-03-27 10:21:19 +00:00
if ( $message == " Interface went down " || $message == " Interface changed state to Down " ) { $icon = " if-disconnect.png " ; }
if ( $message == " Interface went up " || $message == " Interface changed state to Up " ) { $icon = " if-connect.png " ; }
2010-07-07 13:58:11 +00:00
if ( $message == " Interface disabled " ) { $icon = " if-disable.png " ; }
if ( $message == " Interface enabled " ) { $icon = " if-enable.png " ; }
if ( isset ( $icon )) { return $icon ; } else { return false ; }
}
2011-03-16 18:28:52 +00:00
function overlibprint ( $text )
{
return " onmouseover= \" return overlib(' " . $text . " '); \" onmouseout= \" return nd(); \" " ;
2010-07-07 13:58:11 +00:00
}
2011-03-16 18:28:52 +00:00
function humanmedia ( $media )
2010-07-07 13:58:11 +00:00
{
2011-09-20 09:55:11 +00:00
array_preg_replace ( $rewrite_iftype , $media );
2010-07-07 13:58:11 +00:00
return $media ;
}
2011-03-16 18:28:52 +00:00
function humanspeed ( $speed )
2010-07-07 13:58:11 +00:00
{
$speed = formatRates ( $speed );
if ( $speed == " " ) { $speed = " - " ; }
return $speed ;
}
2011-03-16 18:28:52 +00:00
function devclass ( $device )
2010-07-17 22:53:53 +00:00
{
2011-03-16 18:28:52 +00:00
if ( isset ( $device [ 'status' ]) && $device [ 'status' ] == '0' ) { $class = " list-device-down " ; } else { $class = " list-device " ; }
if ( isset ( $device [ 'ignore' ]) && $device [ 'ignore' ] == '1' )
{
2010-07-17 22:53:53 +00:00
$class = " list-device-ignored " ;
if ( isset ( $device [ 'status' ]) && $device [ 'status' ] == '1' ) { $class = " list-device-ignored-up " ; }
2011-03-16 18:28:52 +00:00
}
if ( isset ( $device [ 'disabled' ]) && $device [ 'disabled' ] == '1' ) { $class = " list-device-disabled " ; }
2010-07-17 22:53:53 +00:00
return $class ;
}
2011-03-31 17:19:54 +00:00
function getlocations ()
{
2015-02-22 16:45:40 +00:00
$ignore_dev_location = array ();
$locations = array ();
2011-03-31 17:19:54 +00:00
# Fetch override locations, not through get_dev_attrib, this would be a huge number of queries
2011-05-12 20:16:03 +00:00
$rows = dbFetchRows ( " SELECT attrib_type,attrib_value,device_id FROM devices_attribs WHERE attrib_type LIKE 'override_sysLocation%' ORDER BY attrib_type " );
foreach ( $rows as $row )
2011-03-31 17:19:54 +00:00
{
if ( $row [ 'attrib_type' ] == 'override_sysLocation_bool' && $row [ 'attrib_value' ] == 1 )
{
$ignore_dev_location [ $row [ 'device_id' ]] = 1 ;
}
# We can do this because of the ORDER BY, "bool" will be handled before "string"
2015-03-01 17:06:38 +00:00
elseif ( $row [ 'attrib_type' ] == 'override_sysLocation_string' && ( isset ( $ignore_dev_location [ $row [ 'device_id' ]]) && $ignore_dev_location [ $row [ 'device_id' ]] == 1 ))
2011-03-31 17:19:54 +00:00
{
2011-03-31 17:35:09 +00:00
if ( ! in_array ( $row [ 'attrib_value' ], $locations )) { $locations [] = $row [ 'attrib_value' ]; }
2011-03-31 17:19:54 +00:00
}
}
# Fetch regular locations
if ( $_SESSION [ 'userlevel' ] >= '5' )
{
2011-05-12 20:16:03 +00:00
$rows = dbFetchRows ( " SELECT D.device_id,location FROM devices AS D GROUP BY location ORDER BY location " );
2011-03-31 17:19:54 +00:00
} else {
2011-05-12 20:16:03 +00:00
$rows = dbFetchRows ( " SELECT D.device_id,location FROM devices AS D, devices_perms AS P WHERE D.device_id = P.device_id AND P.user_id = ? GROUP BY location ORDER BY location " , array ( $_SESSION [ 'user_id' ]));
2011-03-31 17:19:54 +00:00
}
2011-05-12 20:16:03 +00:00
foreach ( $rows as $row )
2011-03-31 17:19:54 +00:00
{
# Only add it as a location if it wasn't overridden (and not already there)
2015-03-01 17:06:38 +00:00
if ( $row [ 'location' ] != '' && ! isset ( $ignore_dev_location [ $row [ 'device_id' ]]))
2011-03-31 17:19:54 +00:00
{
if ( ! in_array ( $row [ 'location' ], $locations )) { $locations [] = $row [ 'location' ]; }
}
}
sort ( $locations );
return $locations ;
}
2011-09-23 10:16:31 +00:00
function foldersize ( $path )
{
$total_size = 0 ;
$files = scandir ( $path );
$total_files = 0 ;
2011-10-04 09:10:21 +00:00
foreach ( $files as $t )
2011-09-23 10:16:31 +00:00
{
if ( is_dir ( rtrim ( $path , '/' ) . '/' . $t ))
{
if ( $t <> " . " && $t <> " .. " )
{
$size = foldersize ( rtrim ( $path , '/' ) . '/' . $t );
$total_size += $size ;
}
} else {
$size = filesize ( rtrim ( $path , '/' ) . '/' . $t );
$total_size += $size ;
$total_files ++ ;
}
}
return array ( $total_size , $total_files );
}
2012-05-21 18:17:23 +00:00
function generate_ap_link ( $args , $text = NULL , $type = NULL )
{
global $config ;
$args = ifNameDescr ( $args );
if ( ! $text ) { $text = fixIfName ( $args [ 'label' ]); }
if ( $type ) { $args [ 'graph_type' ] = $type ; }
if ( ! isset ( $args [ 'graph_type' ])) { $args [ 'graph_type' ] = 'port_bits' ; }
if ( ! isset ( $args [ 'hostname' ])) { $args = array_merge ( $args , device_by_id_cache ( $args [ 'device_id' ])); }
$content = " <div class=list-large> " . $args [ 'text' ] . " - " . fixifName ( $args [ 'label' ]) . " </div> " ;
if ( $args [ 'ifAlias' ]) { $content .= $args [ 'ifAlias' ] . " <br /> " ; }
$content .= " <div style= \ 'width: 850px \ '> " ;
2015-02-22 16:45:40 +00:00
$graph_array = array ();
2012-05-21 18:17:23 +00:00
$graph_array [ 'type' ] = $args [ 'graph_type' ];
$graph_array [ 'legend' ] = " yes " ;
$graph_array [ 'height' ] = " 100 " ;
$graph_array [ 'width' ] = " 340 " ;
$graph_array [ 'to' ] = $config [ 'time' ][ 'now' ];
$graph_array [ 'from' ] = $config [ 'time' ][ 'day' ];
$graph_array [ 'id' ] = $args [ 'accesspoint_id' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'week' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'month' ];
$content .= generate_graph_tag ( $graph_array );
$graph_array [ 'from' ] = $config [ 'time' ][ 'year' ];
$content .= generate_graph_tag ( $graph_array );
$content .= " </div> " ;
$url = generate_ap_url ( $args );
if ( port_permitted ( $args [ 'interface_id' ], $args [ 'device_id' ])) {
return overlib_link ( $url , $text , $content , $class );
} else {
return fixifName ( $text );
}
}
function generate_ap_url ( $ap , $vars = array ())
{
return generate_url ( array ( 'page' => 'device' , 'device' => $ap [ 'device_id' ], 'tab' => 'accesspoint' , 'ap' => $ap [ 'accesspoint_id' ]), $vars );
}
2013-11-04 23:33:32 +00:00
function report_this ( $message )
{
2014-03-13 23:04:14 +00:00
global $config ;
return '<h2>' . $message . ' Please <a href="' . $config [ 'project_issues' ] . '">report this</a> to the ' . $config [ 'project_name' ] . ' developers.</h2>' ;
2013-11-04 23:33:32 +00:00
}
function report_this_text ( $message )
{
2014-03-13 23:04:14 +00:00
global $config ;
return $message . '\nPlease report this to the ' . $config [ 'project_name' ] . ' developers at ' . $config [ 'project_issues' ] . '\n' ;
2013-11-04 23:33:32 +00:00
}
2012-05-21 18:17:23 +00:00
2013-11-26 04:35:23 +00:00
# Find all the files in the given directory that match the pattern
function get_matching_files ( $dir , $match = " / \ .php $ / " )
{
global $config ;
2014-03-13 23:04:14 +00:00
$list = array ();
2013-11-26 04:35:23 +00:00
if ( $handle = opendir ( $dir ))
{
while ( false !== ( $file = readdir ( $handle )))
{
if ( $file != " . " && $file != " .. " && preg_match ( $match , $file ) === 1 )
{
$list [] = $file ;
}
}
closedir ( $handle );
}
return $list ;
}
# Include all the files in the given directory that match the pattern
function include_matching_files ( $dir , $match = " / \ .php $ / " )
{
foreach ( get_matching_files ( $dir , $match ) as $file ) {
include_once ( $file );
}
}
2014-10-29 18:30:42 +00:00
function generate_pagination ( $count , $limit , $page , $links = 2 ) {
$end_page = ceil ( $count / $limit );
$start = (( $page - $links ) > 0 ) ? $page - $links : 1 ;
$end = (( $page + $links ) < $end_page ) ? $page + $links : $end_page ;
$return = '<ul class="pagination">' ;
$link_class = ( $page == 1 ) ? " disabled " : " " ;
$return .= " <li><a href='' onClick='changePage(1,event);'>«</a></li> " ;
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $page - 1,event);'><</a></li> " ;
if ( $start > 1 ) {
$return .= " <li><a href='' onClick='changePage(1,event);'>1</a></li> " ;
$return .= " <li class='disabled'><span>...</span></li> " ;
}
for ( $x = $start ; $x <= $end ; $x ++ ) {
$link_class = ( $page == $x ) ? " active " : " " ;
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $x ,event);'> $x </a></li> " ;
}
if ( $end < $end_page ) {
$return .= " <li class='disabled'><span>...</span></li> " ;
$return .= " <li><a href='' onClick='changePage( $end_page ,event);'> $end_page </a></li> " ;
}
$link_class = ( $page == $end_page ) ? " disabled " : " " ;
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $page + 1,event);'>></a></li> " ;
$return .= " <li class=' $link_class '><a href='' onClick='changePage( $end_page ,event);'>»</a></li> " ;
$return .= '</ul>' ;
return ( $return );
}
2014-11-30 17:49:52 +00:00
function is_admin () {
2015-02-16 23:45:28 +00:00
if ( $_SESSION [ 'userlevel' ] >= '10' ) {
2014-11-30 17:49:52 +00:00
$allowed = true ;
} else {
$allowed = false ;
}
2014-12-16 20:49:55 +00:00
return $allowed ;
2014-11-30 17:49:52 +00:00
}
2015-04-22 19:39:09 +00:00
function is_read () {
if ( $_SESSION [ 'userlevel' ] == '5' ) {
$allowed = true ;
} else {
$allowed = false ;
}
return $allowed ;
}
2015-02-16 23:45:28 +00:00
function demo_account () {
print_error ( " You are logged in as a demo account, this page isn't accessible to you " );
}
2015-03-18 19:14:51 +00:00
function get_client_ip () {
if ( isset ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ])) {
$client_ip = $_SERVER [ 'HTTP_X_FORWARDED_FOR' ];
} else {
$client_ip = $_SERVER [ 'REMOTE_ADDR' ];
}
return $client_ip ;
}
2015-04-08 01:46:03 +00:00
function shorten_interface_type ( $string ) {
return str_ireplace (
array ( 'FastEthernet' , 'GigbitEthernet' , 'TenGigabitEthernet' , 'Port-Channel' , 'Ethernet' ),
array ( 'Fa' , 'Gi' , 'Te' , 'Po' , 'Eth' ),
$string
);
}
2015-04-13 14:32:39 +00:00
function clean_bootgrid ( $string ) {
$output = str_replace ( array ( " \r " , " \n " ), " " , $string );
$output = addslashes ( $output );
return $output ;
}
2011-04-25 17:15:36 +00:00
?>