Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
class HTMLPurifier_AttrDef_Integer extends HTMLPurifier_AttrDef
11
{
12
 
13
    /**
14
     * Whether or not negative values are allowed.
15
     * @type bool
16
     */
17
    protected $negative = true;
18
 
19
    /**
20
     * Whether or not zero is allowed.
21
     * @type bool
22
     */
23
    protected $zero = true;
24
 
25
    /**
26
     * Whether or not positive values are allowed.
27
     * @type bool
28
     */
29
    protected $positive = true;
30
 
31
    /**
32
     * @param $negative Bool indicating whether or not negative values are allowed
33
     * @param $zero Bool indicating whether or not zero is allowed
34
     * @param $positive Bool indicating whether or not positive values are allowed
35
     */
36
    public function __construct($negative = true, $zero = true, $positive = true)
37
    {
38
        $this->negative = $negative;
39
        $this->zero = $zero;
40
        $this->positive = $positive;
41
    }
42
 
43
    /**
44
     * @param string $integer
45
     * @param HTMLPurifier_Config $config
46
     * @param HTMLPurifier_Context $context
47
     * @return bool|string
48
     */
49
    public function validate($integer, $config, $context)
50
    {
51
        $integer = $this->parseCDATA($integer);
52
        if ($integer === '') {
53
            return false;
54
        }
55
 
56
        // we could possibly simply typecast it to integer, but there are
57
        // certain fringe cases that must not return an integer.
58
 
59
        // clip leading sign
60
        if ($this->negative && $integer[0] === '-') {
61
            $digits = substr($integer, 1);
62
            if ($digits === '0') {
63
                $integer = '0';
64
            } // rm minus sign for zero
65
        } elseif ($this->positive && $integer[0] === '+') {
66
            $digits = $integer = substr($integer, 1); // rm unnecessary plus
67
        } else {
68
            $digits = $integer;
69
        }
70
 
71
        // test if it's numeric
72
        if (!ctype_digit($digits)) {
73
            return false;
74
        }
75
 
76
        // perform scope tests
77
        if (!$this->zero && $integer == 0) {
78
            return false;
79
        }
80
        if (!$this->positive && $integer > 0) {
81
            return false;
82
        }
83
        if (!$this->negative && $integer < 0) {
84
            return false;
85
        }
86
 
87
        return $integer;
88
    }
89
}
90
 
91
// vim: et sw=4 sts=4