2006-08-06 01:03:48 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Validates an integer representation of pixels according to the HTML spec.
|
|
|
|
*/
|
2007-02-14 20:38:51 +00:00
|
|
|
class HTMLPurifier_AttrDef_HTML_Pixels extends HTMLPurifier_AttrDef
|
2006-08-06 01:03:48 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-05-23 02:09:43 +00:00
|
|
|
protected $max;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-05-23 02:09:43 +00:00
|
|
|
public function __construct($max = null) {
|
|
|
|
$this->max = $max;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validate($string, $config, $context) {
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-06 01:03:48 +00:00
|
|
|
$string = trim($string);
|
|
|
|
if ($string === '0') return $string;
|
|
|
|
if ($string === '') return false;
|
|
|
|
$length = strlen($string);
|
|
|
|
if (substr($string, $length - 2) == 'px') {
|
|
|
|
$string = substr($string, 0, $length - 2);
|
|
|
|
}
|
|
|
|
if (!is_numeric($string)) return false;
|
|
|
|
$int = (int) $string;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-06 01:03:48 +00:00
|
|
|
if ($int < 0) return '0';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-15 22:53:12 +00:00
|
|
|
// upper-bound value, extremely high values can
|
|
|
|
// crash operating systems, see <http://ha.ckers.org/imagecrash.html>
|
|
|
|
// WARNING, above link WILL crash you if you're using Windows
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-05-23 02:09:43 +00:00
|
|
|
if ($this->max !== null && $int > $this->max) return (string) $this->max;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-06 01:03:48 +00:00
|
|
|
return (string) $int;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-06 01:03:48 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-05-23 02:09:43 +00:00
|
|
|
public function make($string) {
|
|
|
|
if ($string === '') $max = null;
|
|
|
|
else $max = (int) $string;
|
|
|
|
$class = get_class($this);
|
|
|
|
return new $class($max);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-06 01:03:48 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|