File indexing completed on 2024-12-22 05:36:18
0001 <?php 0002 0003 /** 0004 * Validates an integer. 0005 * @note While this class was modeled off the CSS definition, no currently 0006 * allowed CSS uses this type. The properties that do are: widows, 0007 * orphans, z-index, counter-increment, counter-reset. Some of the 0008 * HTML attributes, however, find use for a non-negative version of this. 0009 */ 0010 class HTMLPurifier_AttrDef_Integer extends HTMLPurifier_AttrDef 0011 { 0012 0013 /** 0014 * Whether or not negative values are allowed. 0015 * @type bool 0016 */ 0017 protected $negative = true; 0018 0019 /** 0020 * Whether or not zero is allowed. 0021 * @type bool 0022 */ 0023 protected $zero = true; 0024 0025 /** 0026 * Whether or not positive values are allowed. 0027 * @type bool 0028 */ 0029 protected $positive = true; 0030 0031 /** 0032 * @param $negative Bool indicating whether or not negative values are allowed 0033 * @param $zero Bool indicating whether or not zero is allowed 0034 * @param $positive Bool indicating whether or not positive values are allowed 0035 */ 0036 public function __construct($negative = true, $zero = true, $positive = true) 0037 { 0038 $this->negative = $negative; 0039 $this->zero = $zero; 0040 $this->positive = $positive; 0041 } 0042 0043 /** 0044 * @param string $integer 0045 * @param HTMLPurifier_Config $config 0046 * @param HTMLPurifier_Context $context 0047 * @return bool|string 0048 */ 0049 public function validate($integer, $config, $context) 0050 { 0051 $integer = $this->parseCDATA($integer); 0052 if ($integer === '') { 0053 return false; 0054 } 0055 0056 // we could possibly simply typecast it to integer, but there are 0057 // certain fringe cases that must not return an integer. 0058 0059 // clip leading sign 0060 if ($this->negative && $integer[0] === '-') { 0061 $digits = substr($integer, 1); 0062 if ($digits === '0') { 0063 $integer = '0'; 0064 } // rm minus sign for zero 0065 } elseif ($this->positive && $integer[0] === '+') { 0066 $digits = $integer = substr($integer, 1); // rm unnecessary plus 0067 } else { 0068 $digits = $integer; 0069 } 0070 0071 // test if it's numeric 0072 if (!ctype_digit($digits)) { 0073 return false; 0074 } 0075 0076 // perform scope tests 0077 if (!$this->zero && $integer == 0) { 0078 return false; 0079 } 0080 if (!$this->positive && $integer > 0) { 0081 return false; 0082 } 0083 if (!$this->negative && $integer < 0) { 0084 return false; 0085 } 0086 0087 return $integer; 0088 } 0089 } 0090 0091 // vim: et sw=4 sts=4