From 709de9a16628c53f5bd1222197980404bf42b5d5 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Mon, 15 Aug 2016 14:04:39 -0500 Subject: [PATCH] Use namespaces for classes CollectdColor, move into it's own file, change functions to camelCase Plugins use LibreNMS\Plugins --- html/includes/collectd/CollectdColor.php | 109 ++++++++++++++++++++ html/includes/collectd/functions.php | 121 ++--------------------- html/includes/plugins.inc.php | 11 ++- html/index.php | 2 + html/plugins/Test/Test.php | 2 + 5 files changed, 132 insertions(+), 113 deletions(-) create mode 100644 html/includes/collectd/CollectdColor.php diff --git a/html/includes/collectd/CollectdColor.php b/html/includes/collectd/CollectdColor.php new file mode 100644 index 0000000000..1ea18d2e0d --- /dev/null +++ b/html/includes/collectd/CollectdColor.php @@ -0,0 +1,109 @@ +r = $value['r'] > 0 ? ($value['r'] > 1 ? 1 : $value['r']) : 0; + } + + if (isset($value['g'])) { + $this->g = $value['g'] > 0 ? ($value['g'] > 1 ? 1 : $value['g']) : 0; + } + + if (isset($value['b'])) { + $this->b = $value['b'] > 0 ? ($value['b'] > 1 ? 1 : $value['b']) : 0; + } + } else { + if (is_string($value)) { + $matches = array(); + if ($value == 'random') { + $this->randomize(); + } else { + if (preg_match('/([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/', + $value, $matches)) { + $this->r = (('0x' . $matches[1]) / 255.0); + $this->g = (('0x' . $matches[2]) / 255.0); + $this->b = (('0x' . $matches[3]) / 255.0); + } + } + } else { + if (is_a($value, 'CollectdColor')) { + $this->r = $value->r; + $this->g = $value->g; + $this->b = $value->b; + } + } + } + }//end if + + }//end __construct() + + + public function randomize() + { + $this->r = (rand(0, 255) / 255.0); + $this->g = (rand(0, 255) / 255.0); + $this->b = 0.0; + $min = 0.0; + $max = 1.0; + + if (($this->r + $this->g) < 1.0) { + $min = (1.0 - ($this->r + $this->g)); + } else { + $max = (2.0 - ($this->r + $this->g)); + } + + $this->b = ($min + ((rand(0, 255) / 255.0) * ($max - $min))); + + }//end randomize() + + + public function fade($bkgnd = null, $alpha = 0.25) + { + if (is_null($bkgnd) || !is_a($bkgnd, 'CollectdColor')) { + $bg_r = 1.0; + $bg_g = 1.0; + $bg_b = 1.0; + } else { + $bg_r = $bkgnd->r; + $bg_g = $bkgnd->g; + $bg_b = $bkgnd->b; + } + + $this->r = ($alpha * $this->r + ((1.0 - $alpha) * $bg_r)); + $this->g = ($alpha * $this->g + ((1.0 - $alpha) * $bg_g)); + $this->b = ($alpha * $this->b + ((1.0 - $alpha) * $bg_b)); + + }//end fade() + + + public function toArray() + { + return array( + 'r' => $this->r, + 'g' => $this->g, + 'b' => $this->b, + ); + + }//end as_array() + + + public function toString() + { + $r = (int)($this->r * 255); + $g = (int)($this->g * 255); + $b = (int)($this->b * 255); + return sprintf('%02x%02x%02x', $r > 255 ? 255 : $r, $g > 255 ? 255 : $g, $b > 255 ? 255 : $b); + + } +} diff --git a/html/includes/collectd/functions.php b/html/includes/collectd/functions.php index e0cad177b4..7c945a4332 100644 --- a/html/includes/collectd/functions.php +++ b/html/includes/collectd/functions.php @@ -1,5 +1,4 @@ * @@ -17,6 +16,10 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +require 'includes/collectd/CollectdColor.php'; + +use LibreNMS\CollectdColor; + define('REGEXP_HOST', '/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/'); define('REGEXP_PLUGIN', '/^[a-zA-Z0-9_.-]+$/'); @@ -368,112 +371,6 @@ function collectd_flush($identifier) { }//end collectd_flush() - -class CollectdColor { - - private $r = 0; - - private $g = 0; - - private $b = 0; - - - function __construct($value=null) { - if (is_null($value)) { - } - else if (is_array($value)) { - if (isset($value['r'])) { - $this->r = $value['r'] > 0 ? ($value['r'] > 1 ? 1 : $value['r']) : 0; - } - - if (isset($value['g'])) { - $this->g = $value['g'] > 0 ? ($value['g'] > 1 ? 1 : $value['g']) : 0; - } - - if (isset($value['b'])) { - $this->b = $value['b'] > 0 ? ($value['b'] > 1 ? 1 : $value['b']) : 0; - } - } - else if (is_string($value)) { - $matches = array(); - if ($value == 'random') { - $this->randomize(); - } - else if (preg_match('/([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/', $value, $matches)) { - $this->r = (('0x'.$matches[1]) / 255.0); - $this->g = (('0x'.$matches[2]) / 255.0); - $this->b = (('0x'.$matches[3]) / 255.0); - } - } - else if (is_a($value, 'CollectdColor')) { - $this->r = $value->r; - $this->g = $value->g; - $this->b = $value->b; - }//end if - - }//end __construct() - - - function randomize() { - $this->r = (rand(0, 255) / 255.0); - $this->g = (rand(0, 255) / 255.0); - $this->b = 0.0; - $min = 0.0; - $max = 1.0; - - if (($this->r + $this->g) < 1.0) { - $min = (1.0 - ($this->r + $this->g)); - } - else { - $max = (2.0 - ($this->r + $this->g)); - } - - $this->b = ($min + ((rand(0, 255) / 255.0) * ($max - $min))); - - }//end randomize() - - - function fade($bkgnd=null, $alpha=0.25) { - if (is_null($bkgnd) || !is_a($bkgnd, 'CollectdColor')) { - $bg_r = 1.0; - $bg_g = 1.0; - $bg_b = 1.0; - } - else { - $bg_r = $bkgnd->r; - $bg_g = $bkgnd->g; - $bg_b = $bkgnd->b; - } - - $this->r = ($alpha * $this->r + ((1.0 - $alpha) * $bg_r)); - $this->g = ($alpha * $this->g + ((1.0 - $alpha) * $bg_g)); - $this->b = ($alpha * $this->b + ((1.0 - $alpha) * $bg_b)); - - }//end fade() - - - function as_array() { - return array( - 'r' => $this->r, - 'g' => $this->g, - 'b' => $this->b, - ); - - }//end as_array() - - - function as_string() { - $r = (int) ($this->r * 255); - $g = (int) ($this->g * 255); - $b = (int) ($this->b * 255); - return sprintf('%02x%02x%02x', $r > 255 ? 255 : $r, $g > 255 ? 255 : $g, $b > 255 ? 255 : $b); - - }//end as_string() - - -}//end class - - /** * Helper function to strip quotes from RRD output * @str RRD-Info generated string @@ -564,8 +461,8 @@ function rrd_get_color($code, $line=true) { $c_f = new CollectdColor('random'); $c_h = new CollectdColor($c_f); $c_h->fade(); - $config['rrd_colors']['f_'.$code] = $c_f->as_string(); - $config['rrd_colors']['h_'.$code] = $c_h->as_string(); + $config['rrd_colors']['f_'.$code] = $c_f->toString(); + $config['rrd_colors']['h_'.$code] = $c_h->toString(); } return $config['rrd_colors'][$name]; @@ -952,8 +849,8 @@ function collectd_draw_meta_stack(&$opts, &$sources) { $area_color = new CollectdColor($line_color); $area_color->fade(); - $cmd[] = 'AREA:'.$inst_name.'_stk#'.$area_color->as_string(); - $cmd[] = 'LINE1:'.$inst_name.'_stk#'.$line_color->as_string().':'.$legend; + $cmd[] = 'AREA:'.$inst_name.'_stk#'.$area_color->toString(); + $cmd[] = 'LINE1:'.$inst_name.'_stk#'.$line_color->toString().':'.$legend; if (!(isset($opts['tinylegend']) && $opts['tinylegend'])) { $cmd[] = 'GPRINT:'.$inst_name.'_avg:LAST:'.$number_format.''; $cmd[] = 'GPRINT:'.$inst_name.'_avg:AVERAGE:'.$number_format.''; @@ -1072,7 +969,7 @@ function collectd_draw_meta_line(&$opts, &$sources) { $line_color = new CollectdColor('random'); } - $cmd[] = 'LINE1:'.$inst_name.'_avg#'.$line_color->as_string().':'.$legend; + $cmd[] = 'LINE1:'.$inst_name.'_avg#'.$line_color->toString().':'.$legend; if (!(isset($opts['tinylegend']) && $opts['tinylegend'])) { $cmd[] = 'GPRINT:'.$inst_name.'_min:MIN:'.$number_format.''; $cmd[] = 'GPRINT:'.$inst_name.'_avg:AVERAGE:'.$number_format.''; diff --git a/html/includes/plugins.inc.php b/html/includes/plugins.inc.php index a10762c5cf..e52015b199 100644 --- a/html/includes/plugins.inc.php +++ b/html/includes/plugins.inc.php @@ -1,5 +1,7 @@