Merge pull request #4873 from laf/issue-4868

fix: Toner graphs with invalid chars
This commit is contained in:
Tony Murray
2016-11-01 14:19:31 -05:00
committed by GitHub
4 changed files with 18 additions and 2 deletions

View File

@@ -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'];

View File

@@ -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']));

View File

@@ -490,6 +490,16 @@ 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);
}
function zeropad($num, $length = 2)
{
while (strlen($num) < $length) {

View File

@@ -77,4 +77,10 @@ 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:CR_UM-16021314488.';
$this->assertEquals('Toner, S/N CR_UM-16021314488.', safedescr($data));
}
}