1 : <?php
2 :
3 : /**
4 : * Validates an integer.
5 : * @note While this class was modeled off the CSS definition, no currently
6 : * allowed CSS uses this type. The properties that do are: widows,
7 : * orphans, z-index, counter-increment, counter-reset. Some of the
8 : * HTML attributes, however, find use for a non-negative version of this.
9 : */
10 1 : class HTMLPurifier_AttrDef_Integer extends HTMLPurifier_AttrDef
11 : {
12 :
13 : /**
14 : * Bool indicating whether or not negative values are allowed
15 : */
16 : protected $negative = true;
17 :
18 : /**
19 : * Bool indicating whether or not zero is allowed
20 : */
21 : protected $zero = true;
22 :
23 : /**
24 : * Bool indicating whether or not positive values are allowed
25 : */
26 : protected $positive = true;
27 :
28 : /**
29 : * @param $negative Bool indicating whether or not negative values are allowed
30 : * @param $zero Bool indicating whether or not zero is allowed
31 : * @param $positive Bool indicating whether or not positive values are allowed
32 : */
33 : public function __construct(
34 : $negative = true, $zero = true, $positive = true
35 : ) {
36 : $this->negative = $negative;
37 : $this->zero = $zero;
38 : $this->positive = $positive;
39 : }
40 :
41 : public function validate($integer, $config, $context) {
42 :
43 0 : $integer = $this->parseCDATA($integer);
44 0 : if ($integer === '') return false;
45 :
46 : // we could possibly simply typecast it to integer, but there are
47 : // certain fringe cases that must not return an integer.
48 :
49 : // clip leading sign
50 0 : if ( $this->negative && $integer[0] === '-' ) {
51 0 : $digits = substr($integer, 1);
52 0 : if ($digits === '0') $integer = '0'; // rm minus sign for zero
53 0 : } elseif( $this->positive && $integer[0] === '+' ) {
54 0 : $digits = $integer = substr($integer, 1); // rm unnecessary plus
55 0 : } else {
56 0 : $digits = $integer;
57 : }
58 :
59 : // test if it's numeric
60 0 : if (!ctype_digit($digits)) return false;
61 :
62 : // perform scope tests
63 0 : if (!$this->zero && $integer == 0) return false;
64 0 : if (!$this->positive && $integer > 0) return false;
65 0 : if (!$this->negative && $integer < 0) return false;
66 :
67 0 : return $integer;
68 :
69 : }
70 :
71 : }
72 :
|