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
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @type int
|
|
|
|
*/
|
2008-05-23 02:09:43 +00:00
|
|
|
protected $max;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @param int $max
|
|
|
|
*/
|
|
|
|
public function __construct($max = null)
|
|
|
|
{
|
2008-05-23 02:09:43 +00:00
|
|
|
$this->max = $max;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @param string $string
|
|
|
|
* @param HTMLPurifier_Config $config
|
|
|
|
* @param HTMLPurifier_Context $context
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
public function validate($string, $config, $context)
|
|
|
|
{
|
2006-08-06 01:03:48 +00:00
|
|
|
$string = trim($string);
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($string === '0') {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
if ($string === '') {
|
|
|
|
return false;
|
|
|
|
}
|
2006-08-06 01:03:48 +00:00
|
|
|
$length = strlen($string);
|
|
|
|
if (substr($string, $length - 2) == 'px') {
|
|
|
|
$string = substr($string, 0, $length - 2);
|
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
if (!is_numeric($string)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$int = (int)$string;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +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
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($this->max !== null && $int > $this->max) {
|
|
|
|
return (string)$this->max;
|
|
|
|
}
|
|
|
|
return (string)$int;
|
2006-08-06 01:03:48 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @param string $string
|
|
|
|
* @return HTMLPurifier_AttrDef
|
|
|
|
*/
|
|
|
|
public function make($string)
|
|
|
|
{
|
|
|
|
if ($string === '') {
|
|
|
|
$max = null;
|
|
|
|
} else {
|
|
|
|
$max = (int)$string;
|
|
|
|
}
|
2008-05-23 02:09:43 +00:00
|
|
|
$class = get_class($this);
|
|
|
|
return new $class($max);
|
|
|
|
}
|
2006-08-06 01:03:48 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|