1
|
|
|
<?php namespace Gears\String\Methods; |
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
3
|
|
|
// __________ __ ________ __________ |
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
9
|
|
|
// ----------------------------------------------------------------------------- |
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
11
|
|
|
// ----------------------------------------------------------------------------- |
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
13
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
15
|
|
|
|
16
|
|
|
trait Html |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Convert all HTML entities to their applicable characters. |
20
|
|
|
* |
21
|
|
|
* @link http://php.net/manual/en/function.html-entity-decode.php |
22
|
|
|
* |
23
|
|
|
* @param int|null $flags Optional flags |
24
|
|
|
* |
25
|
|
|
* @return static String after being html decoded. |
26
|
|
|
*/ |
27
|
|
|
public function htmlDecode($flags = ENT_COMPAT) |
28
|
|
|
{ |
29
|
|
|
return $this->newSelf |
30
|
|
|
( |
31
|
|
|
UTF8::html_entity_decode |
32
|
|
|
( |
33
|
|
|
$this->scalarString, |
34
|
|
|
$flags, |
35
|
|
|
$this->encoding |
36
|
|
|
) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Convert all applicable characters to HTML entities. |
42
|
|
|
* |
43
|
|
|
* @link http://php.net/manual/en/function.htmlentities.php |
44
|
|
|
* |
45
|
|
|
* @param int|null $flags Optional flags. |
46
|
|
|
* |
47
|
|
|
* @param bool $doubleEncode When double_encode is turned off PHP |
48
|
|
|
* will not encode existing html entities. |
49
|
|
|
* The default is to convert everything. |
50
|
|
|
* |
51
|
|
|
* @return static String after being html encoded. |
52
|
|
|
*/ |
53
|
|
|
public function htmlEncode($flags = null, $doubleEncode = true) |
54
|
|
|
{ |
55
|
|
|
if ($flags === null) $flags = ENT_QUOTES | ENT_SUBSTITUTE; |
56
|
|
|
|
57
|
|
|
return $this->newSelf |
58
|
|
|
( |
59
|
|
|
UTF8::htmlentities |
60
|
|
|
( |
61
|
|
|
$this->scalarString, |
62
|
|
|
$flags, |
63
|
|
|
$this->encoding, |
64
|
|
|
$doubleEncode |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sanitizes data so that Cross Site Scripting Hacks can be prevented. |
71
|
|
|
* |
72
|
|
|
* This method does a fair amount of work and it is extremely thorough, |
73
|
|
|
* designed to prevent even the most obscure XSS attempts. Nothing is ever |
74
|
|
|
* 100 percent foolproof, of course, but I haven't been able to get anything |
75
|
|
|
* passed the filter. |
76
|
|
|
* |
77
|
|
|
* > NOTE: Should only be used to deal with data upon submission. |
78
|
|
|
* > It's not something that should be used for general runtime processing. |
79
|
|
|
* |
80
|
|
|
* __In other words it is still critically important |
81
|
|
|
* to escape anything that you output!!!__ |
82
|
|
|
* |
83
|
|
|
* This uses a packaged version of the Anti XSS Library from CodeIgniter. |
84
|
|
|
* @link https://github.com/voku/anti-xss |
85
|
|
|
* |
86
|
|
|
* @return static |
87
|
|
|
*/ |
88
|
|
|
public function htmlXssClean() |
89
|
|
|
{ |
90
|
|
|
static $antiXss = null; |
91
|
|
|
|
92
|
|
|
if ($antiXss === null) |
93
|
|
|
{ |
94
|
|
|
if (class_exists('\\voku\\helper\\AntiXSS')) |
95
|
|
|
{ |
96
|
|
|
$antiXss = new \voku\helper\AntiXSS(); |
97
|
|
|
} |
98
|
|
|
else |
99
|
|
|
{ |
100
|
|
|
throw new \RuntimeException |
101
|
|
|
( |
102
|
|
|
"This method requires \voku\helper\AntiXSS. ". |
103
|
|
|
"Install with: composer require voku/anti-xss" |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->newSelf($antiXss->xss_clean($this->scalarString)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Strip HTML and PHP tags from a string. |
113
|
|
|
* |
114
|
|
|
* This function tries to return a string with all NULL bytes, |
115
|
|
|
* HTML and PHP tags stripped from a given str. |
116
|
|
|
* |
117
|
|
|
* @param string|null $allowableTags You can use the optional second |
118
|
|
|
* parameter to specify tags which |
119
|
|
|
* should not be stripped. |
120
|
|
|
* |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
|
|
public function htmlStripTags($allowableTags = null) |
124
|
|
|
{ |
125
|
|
|
return $this->newSelf |
126
|
|
|
( |
127
|
|
|
UTF8::strip_tags($this->scalarString, $allowableTags) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|