From e287930c3b04ddc8a75b0fd62c4846122c558a24 Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 24 Oct 2016 21:40:39 +0000 Subject: [PATCH 1/6] fix: Toner graphs with invalid chars --- html/includes/graphs/toner/usage.inc.php | 2 +- includes/common.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/html/includes/graphs/toner/usage.inc.php b/html/includes/graphs/toner/usage.inc.php index 274845db7b..914e4c9d31 100644 --- a/html/includes/graphs/toner/usage.inc.php +++ b/html/includes/graphs/toner/usage.inc.php @@ -11,7 +11,7 @@ if ($colour['left'] == null) { $colour['left'] = 'CC0000'; } -$descr = substr(str_pad($toner['toner_descr'], 26), 0, 26); +$descr = safedescr(substr(str_pad($toner['toner_descr'], 26), 0, 26)); $background = get_percentage_colours((100 - $toner['toner_current'])); diff --git a/includes/common.php b/includes/common.php index 62e8a279e6..95a673b3d5 100644 --- a/includes/common.php +++ b/includes/common.php @@ -490,6 +490,11 @@ function safename($name) return preg_replace('/[^a-zA-Z0-9,._\-]/', '_', $name); } +function safedescr($descr) +{ + return safename($descr); +} + function zeropad($num, $length = 2) { while (strlen($num) < $length) { From 02ab4df54a2a78687aeccaad918ef750aabe68b6 Mon Sep 17 00:00:00 2001 From: laf Date: Tue, 25 Oct 2016 13:19:48 +0000 Subject: [PATCH 2/6] Updated function + added unit test --- includes/common.php | 2 +- tests/CommonFunctionsTest.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/includes/common.php b/includes/common.php index 95a673b3d5..3a0bf155b0 100644 --- a/includes/common.php +++ b/includes/common.php @@ -492,7 +492,7 @@ function safename($name) function safedescr($descr) { - return safename($descr); + return preg_replace('/[^a-zA-Z0-9,._\-\/\ ]/', ' ', $descr); } function zeropad($num, $length = 2) diff --git a/tests/CommonFunctionsTest.php b/tests/CommonFunctionsTest.php index 99a82e7e30..fd1dea28f0 100644 --- a/tests/CommonFunctionsTest.php +++ b/tests/CommonFunctionsTest.php @@ -77,4 +77,11 @@ class CommonFunctionsTest extends \PHPUnit_Framework_TestCase $this->assertTrue(ends_with($data, array('this', 'tesTing', 'no'), true)); $this->assertFalse(ends_with($data, array('this', 'Test'), true)); } + + public function testRrdDescriptions() + { + $data = 'Toner S/N:CRUM-1'; + + $this->assertEquals('Toner S/N CRUM-1', safedescr($data)); + } } From f7c79307db1f09731f8615a03a2225cf12f741bf Mon Sep 17 00:00:00 2001 From: laf Date: Tue, 25 Oct 2016 13:26:20 +0000 Subject: [PATCH 3/6] Updated function + added unit test --- tests/CommonFunctionsTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/CommonFunctionsTest.php b/tests/CommonFunctionsTest.php index fd1dea28f0..57535c853a 100644 --- a/tests/CommonFunctionsTest.php +++ b/tests/CommonFunctionsTest.php @@ -80,8 +80,7 @@ class CommonFunctionsTest extends \PHPUnit_Framework_TestCase public function testRrdDescriptions() { - $data = 'Toner S/N:CRUM-1'; - - $this->assertEquals('Toner S/N CRUM-1', safedescr($data)); + $data = 'Toner, S/N:CR_UM-1.'; + $this->assertEquals('Toner, S/N CR_UM-1.', safedescr($data)); } } From f9b5f80152f35eb3fb6b2654ad1149dc70d61cee Mon Sep 17 00:00:00 2001 From: laf Date: Tue, 25 Oct 2016 13:31:12 +0000 Subject: [PATCH 4/6] Updated function + added unit test --- tests/CommonFunctionsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CommonFunctionsTest.php b/tests/CommonFunctionsTest.php index 57535c853a..9041d43189 100644 --- a/tests/CommonFunctionsTest.php +++ b/tests/CommonFunctionsTest.php @@ -80,7 +80,7 @@ class CommonFunctionsTest extends \PHPUnit_Framework_TestCase public function testRrdDescriptions() { - $data = 'Toner, S/N:CR_UM-1.'; - $this->assertEquals('Toner, S/N CR_UM-1.', safedescr($data)); + $data = 'Toner, S/N:CR_UM-16021314488.'; + $this->assertEquals('Toner, S/N CR_UM-16021314488.', safedescr($data)); } } From a7a76c998a26ce379dec41369e8e81b40067e26c Mon Sep 17 00:00:00 2001 From: laf Date: Tue, 25 Oct 2016 15:09:49 +0000 Subject: [PATCH 5/6] update graph file --- html/includes/graphs/device/toner.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/includes/graphs/device/toner.inc.php b/html/includes/graphs/device/toner.inc.php index f577bbf97e..6a9229928a 100644 --- a/html/includes/graphs/device/toner.inc.php +++ b/html/includes/graphs/device/toner.inc.php @@ -46,7 +46,7 @@ foreach (dbFetchRows('SELECT * FROM toner where device_id = ?', array($device['d $hostname = gethostbyid($toner['device_id']); - $descr = substr(str_pad($toner['toner_descr'], 16), 0, 16); + $descr = safedescr(substr(str_pad($toner['toner_descr'], 16), 0, 16)); $rrd_filename = rrd_name($device['hostname'], array('toner', $toner['toner_index'])); $toner_id = $toner['toner_id']; From 8190a3c7b2b529876fb4ea32ebfd9a551b718649 Mon Sep 17 00:00:00 2001 From: laf Date: Tue, 1 Nov 2016 16:37:19 +0000 Subject: [PATCH 6/6] Added phpdoc to safedescr() function --- includes/common.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/common.php b/includes/common.php index 3a0bf155b0..c2eb2be57b 100644 --- a/includes/common.php +++ b/includes/common.php @@ -490,6 +490,11 @@ function safename($name) return preg_replace('/[^a-zA-Z0-9,._\-]/', '_', $name); } +/** + * Function format the rrdtool description text correctly. + * @param $descr + * @return mixed + */ function safedescr($descr) { return preg_replace('/[^a-zA-Z0-9,._\-\/\ ]/', ' ', $descr);