|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Aura for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Aura\Html\Escaper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
* An escaper for HTML output. |
|
14
|
|
|
* |
|
15
|
|
|
* Based almost entirely on Zend\Escaper by Padraic Brady et al. and modified |
|
16
|
|
|
* for conceptual integrity with the rest of Aura. Some portions copyright |
|
17
|
|
|
* (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) |
|
18
|
|
|
* under the New BSD License (http://framework.zend.com/license/new-bsd). |
|
19
|
|
|
* |
|
20
|
|
|
* @package Aura.Html |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
class HtmlEscaper extends AbstractEscaper |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* Flags for `htmlspecialchars()`. |
|
28
|
|
|
* |
|
29
|
|
|
* @var mixed |
|
30
|
|
|
* |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $flags = ENT_QUOTES; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param int $flags Flags for `htmlspecialchars()`. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $encoding The encoding to use for raw and escaped strings. |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
114 |
|
public function __construct($flags = null, $encoding = null) |
|
44
|
|
|
{ |
|
45
|
114 |
|
if ($flags !== null) { |
|
46
|
|
|
// use custom flags only |
|
47
|
1 |
|
$this->setFlags($flags); |
|
48
|
114 |
|
} elseif (defined('ENT_SUBSTITUTE')) { |
|
49
|
|
|
// add ENT_SUBSTITUTE if available (PHP 5.4) |
|
50
|
|
|
$this->setFlags(ENT_QUOTES | ENT_SUBSTITUTE); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
114 |
|
parent::__construct($encoding); |
|
54
|
114 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* |
|
58
|
|
|
* Escapes an HTML value. |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed $raw The value to be escaped. |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed The escaped value. |
|
63
|
|
|
* |
|
64
|
|
|
*/ |
|
65
|
78 |
|
public function __invoke($raw) |
|
66
|
|
|
{ |
|
67
|
|
|
// pre-empt escaping |
|
68
|
78 |
|
if ($raw === '' || ctype_digit($raw)) { |
|
69
|
2 |
|
return $raw; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// return the escaped string |
|
73
|
78 |
|
return htmlspecialchars( |
|
74
|
78 |
|
$raw, |
|
75
|
78 |
|
$this->flags, |
|
76
|
78 |
|
$this->encoding |
|
77
|
78 |
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* |
|
82
|
|
|
* Sets the flags for `htmlspecialchars()`. |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $flags The flags for `htmlspecialchars()`. |
|
85
|
|
|
* |
|
86
|
|
|
* @return null |
|
87
|
|
|
* |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function setFlags($flags) |
|
90
|
|
|
{ |
|
91
|
3 |
|
$this->flags = $flags; |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* |
|
96
|
|
|
* Gets the flags for `htmlspecialchars()`. |
|
97
|
|
|
* |
|
98
|
|
|
* @return int |
|
99
|
|
|
* |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function getFlags() |
|
102
|
|
|
{ |
|
103
|
3 |
|
return $this->flags; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|