X-Git-Url: https://git-public.kairo.at/?p=php-utility-classes.git;a=blobdiff_plain;f=classes%2Frrdstat.php-class;h=1ed450451ec7d9d2e1758a9ecba3915685928584;hp=0a71f13da11858fc9456fc7e44ed759fd891d90e;hb=0ffe81c3abbc0605cf010a4872a01363a26f408f;hpb=5dd242eff6de2d320c75ec83e5f988cd7c769cea diff --git a/classes/rrdstat.php-class b/classes/rrdstat.php-class index 0a71f13..1ed4504 100644 --- a/classes/rrdstat.php-class +++ b/classes/rrdstat.php-class @@ -206,11 +206,11 @@ class rrdstat { $this->status = 'graphonly'; } elseif (isset($iinfo['file'])) { - $this->rrd_file = (($iinfo['file']{0} != '/')?$this->basedir:'').$iinfo['file']; + $this->rrd_file = (($iinfo['file'][0] != '/')?$this->basedir:'').$iinfo['file']; $this->basename = basename((substr($this->rrd_file, -4) == '.rrd')?substr($this->rrd_file, 0, -4):$this->rrd_file); } elseif (!is_null($conf_id) && file_exists($conf_id.'.rrd')) { - $this->rrd_file = (($iinfo['file']{0} != '/')?$this->basedir:'').$conf_id.'.rrd'; + $this->rrd_file = (($iinfo['file'][0] != '/')?$this->basedir:'').$conf_id.'.rrd'; $this->basename = $conf_id; } else { @@ -326,12 +326,17 @@ class rrdstat { if ($this->status != 'ok') { trigger_error('Cannot update non-writeable file', E_USER_WARNING); return false; } $upvals = array(); if (isset($this->config_raw['update'])) { - if (preg_match('/^\s*function\s+{(.*)}\s*$/is', $this->config_raw['update'], $regs)) { - $upfunc = create_function('', $regs[1]); - $upvals = $upfunc(); + if (is_object($this->config_raw['update'])) { + // We assume it's an anonymous function + $upvals = $this->config_raw['update'](); } else { - $evalcode = $this->config_raw['update']; + if (preg_match('/^\s*function\s+{(.*)}\s*$/is', $this->config_raw['update'], $regs)) { + $evalcode = '$upfunc = function() {'."\n".$regs[1]."\n".'};'."\n".'print(implode("\n", $upfunc()));'; + } + else { + $evalcode = $this->config_raw['update']; + } if (!is_null($evalcode)) { ob_start(); eval($evalcode); @@ -345,9 +350,13 @@ class rrdstat { foreach ($this->rrd_fields as $ds) { if (is_array($upArray) && isset($upArray[$ds['name']])) { $val = $upArray[$ds['name']]; } elseif (isset($ds['update'])) { - $val = null; $evalcode = null; - if (substr($ds['update'], 0, 4) == 'val:') { - $evalcode = 'function { return trim('.substr($ds['update'], 4).')); }'; + $val = null; + if (is_object($ds['update'])) { + // We assume it's an anonymous function + $val = $ds['update'](); + } + elseif (substr($ds['update'], 0, 4) == 'val:') { + $val = trim(substr($ds['update'], 4)); } elseif (substr($ds['update'], 0, 8) == 'snmp-if:') { if (substr_count($ds['update'], ':') >= 4) { @@ -366,19 +375,22 @@ class rrdstat { if ($valtype == 'in') { $oid = '1.3.6.1.2.1.2.2.1.10.'.$ifnr; } elseif ($valtype == 'out') { $oid = '1.3.6.1.2.1.2.2.1.16.'.$ifnr; } if (!is_null($ifnr) && !is_null($oid)) { - $evalcode = 'function { return trim(substr(strrchr(`snmpget -v2c -c '.$snmpcomm.' '.$snmphost.' '.$oid.'`,":"),1)); }'; + $val = trim(substr(strrchr(`snmpget -v2c -c $snmpcomm $snmphost $oid`,":"),1)); } } - else { $evalcode = $ds['update']; } - if (preg_match('/^\s*function\s+{(.*)}\s*$/is', $evalcode, $regs)) { - $upfunc = create_function('', $regs[1]); - $val = $upfunc(); - } - elseif (!is_null($evalcode)) { - ob_start(); - eval($evalcode); - $val = ob_get_contents(); - ob_end_clean(); + else { + if (preg_match('/^\s*function\s+{(.*)}\s*$/is', $ds['update'], $regs)) { + $evalcode = '$upfunc = function() {'."\n".$regs[1]."\n".'};'."\n".'print($upfunc());'; + } + else { + $evalcode = $ds['update']; + } + if (!is_null($evalcode)) { + ob_start(); + eval($evalcode); + $val = ob_get_contents(); + ob_end_clean(); + } } } else { $val = null; } @@ -396,8 +408,7 @@ class rrdstat { $upvals[$akey] = $keys_have_names?$lastvals[$akey]:$lastvals[$rowids[$akey]]; } } - $walkfunc = create_function('&$val,$key', '$val = is_numeric(trim($val))?trim($val):"U";'); - array_walk($upvals, $walkfunc); + array_walk($upvals, function(&$val, $key) { $val = is_numeric(trim($val)) ? trim($val) : "U"; }); $return = null; if (count($upvals)) { $update_cmd = $this->rrdtool_bin.' update '.$this->rrd_file @@ -509,8 +520,8 @@ class rrdstat { $fname = str_replace('%t', $timeframe, $fname); $fname = str_replace('%f', $fmt_ext, $fname); if (substr($fname, -strlen($fmt_ext)) != $fmt_ext) { $fname .= $fmt_ext; } - if (isset($gconf['path']) && ($fname{0} != '/')) { $fname = $gconf['path'].'/'.$fname; } - if ($fname{0} != '/') { $fname = $this->basedir.$fname; } + if (isset($gconf['path']) && ($fname[0] != '/')) { $fname = $gconf['path'].'/'.$fname; } + if ($fname[0] != '/') { $fname = $this->basedir.$fname; } $fname = str_replace('//', '/', $fname); $graphrows = array(); $specialrows = array(); $gC = 0; @@ -565,7 +576,7 @@ class rrdstat { } foreach (array('scale_time_src','scale_time_tgt') as $st) { if (!isset($erow[$st]) || !is_numeric($erow[$st])) { - switch (@$erow[$st]) { + switch ($erow[$st] ?? null) { case 'dyn': case 'auto': $erow[$st] = $slice; @@ -774,7 +785,7 @@ class rrdstat { $legendlines = ''; foreach ($graphrows as $grow) { $legendline = isset($grow['desc'])?$grow['desc']:(isset($grow['legend'])?$grow['legend']:$grow['name']); - $legendline .= '|'.@$grow['color']; + $legendline .= '|'.($grow['color'] ?? ''); $legendline .= '|'.(isset($grow['color_bg'])?$grow['color_bg']:''); $legendline .= '|'.(isset($grow['legend_long'])?$grow['legend_long']:''); $legendlines .= 'legend:'.$legendline."\n"; @@ -834,7 +845,7 @@ class rrdstat { $pconf = $pconf + (array)$this->config_page; $return = null; - switch (@$pconf['type']) { + switch ($pconf['type'] ?? null) { case 'index': $return = $this->page_index($pconf); break; @@ -960,9 +971,9 @@ class rrdstat { if (isset($pconf['stats_url_add'])) { $sURL_add = $pconf['stats_url_add']; } else { $sURL_add = '&sub=%s'; } - $default_num_cols = $GLOBALS['ua']->isMobile()?1:2; - $num_cols = is_numeric(@$pconf['num_rows'])?$pconf['num_rows']:$default_num_cols; - $num_rows = ceil(count($stats)/$num_cols); + $default_num_cols = $GLOBALS['ua']->isMobile() ? 1 : 2; + $num_cols = is_numeric($pconf['num_rows'] ?? null) ? $pconf['num_rows'] : $default_num_cols; + $num_rows = ceil(count($stats) / $num_cols); $out .= ''."\n"; for ($row = 0; $row < $num_rows; $row++) { @@ -973,7 +984,7 @@ class rrdstat { if ($idx < count($stats)) { @list($sname, $s_psub) = explode('|', $stats[$idx]['name'], 2); $s_psname = 'page'.(isset($s_psub)?'.'.$s_psub:''); - $g_sub = @$this->config_all[$sname][$s_psname]['graph_sub']; + $g_sub = $this->config_all[$sname][$s_psname]['graph_sub'] ?? ''; if (isset($this->config_all[$sname][$s_psname]['title_page'])) { $s_ptitle = $this->config_all[$sname][$s_psname]['title_page']; @@ -1178,7 +1189,7 @@ class rrdstat { $snames = array(); $s_exclude = array(); $sfiles = array(); if (isset($pconf['index_ids'])) { foreach (explode(',', $pconf['index_ids']) as $iid) { - if ($iid{0} == '-') { $s_exclude[] = substr($iid, 1); } + if ($iid[0] == '-') { $s_exclude[] = substr($iid, 1); } else { $snames[] = $iid; } } } @@ -1193,7 +1204,7 @@ class rrdstat { foreach ($snames as $iname) { $newstat = array('name'=>$iname); $sfiles[] = isset($this->config_all[$iname]['file'])?$this->config_all[$iname]['file']:$iname.'.rrd'; - if (is_array(@$this->config_all[$iname])) { + if (is_array($this->config_all[$iname] ?? null)) { foreach ($this->config_all[$iname] as $key=>$val) { if (substr($key, 0, 5) == 'page.') { $newstat['sub'][] = substr($key, 5); } }