1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Helper\HTML\System; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\App; |
6
|
|
|
use Ffcms\Core\Helper\Security; |
7
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
9
|
|
|
use Ffcms\Core\Helper\Url; |
10
|
|
|
|
11
|
|
|
abstract class NativeGenerator |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Make data "safe" - all dangerous html/js/etc will be removed |
16
|
|
|
* @param string $data |
17
|
|
|
* @param bool $quotes |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public static function safe($data, $quotes = false) |
21
|
|
|
{ |
22
|
|
|
$data = App::$Security->secureHtml($data); |
23
|
|
|
return $quotes ? $data : App::$Security->escapeQuotes($data); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Remove all html tags from data |
28
|
|
|
* @param string $data |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public static function nohtml($data) |
32
|
|
|
{ |
33
|
|
|
return App::$Security->escapeQuotes(App::$Security->strip_tags($data)); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Build property for html element from array. |
38
|
|
|
* IMPORTANT: $property can be null-string (some times this happend's) - do not remove NULL!! |
39
|
|
|
* @param array $property |
40
|
|
|
* @return null|string |
41
|
|
|
*/ |
42
|
|
|
public static function applyProperty(array $property = null) |
43
|
|
|
{ |
44
|
|
|
if (!Obj::isArray($property) || count($property) < 1) { |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$build = null; |
49
|
|
|
foreach ($property as $p => $v) { |
|
|
|
|
50
|
|
|
if ($v === null || $v === false) { |
51
|
|
|
$build .= ' ' . self::nohtml($p); |
52
|
|
|
} else { |
53
|
|
|
$build .= ' ' . self::nohtml($p) . '="' . self::nohtml($v) . '"'; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
return $build; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Fast building single tag based on property's array |
61
|
|
|
* @param string $tagName |
62
|
|
|
* @param array|null $property |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public static function buildSingleTag($tagName, array $property = null) |
66
|
|
|
{ |
67
|
|
|
return '<' . self::nohtml($tagName) . self::applyProperty($property) . '/>'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Fast building container type tag based on property's and value |
72
|
|
|
* @param string $tagName |
73
|
|
|
* @param array|null $property |
74
|
|
|
* @param null|string $value |
75
|
|
|
* @param bool $valueHtml |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public static function buildContainerTag($tagName, array $property = null, $value = null, $valueHtml = false) |
79
|
|
|
{ |
80
|
|
|
$tagName = self::nohtml($tagName); |
81
|
|
|
if ($valueHtml !== true) { |
82
|
|
|
$value = self::nohtml($value); |
83
|
|
|
} |
84
|
|
|
return '<' . $tagName . self::applyProperty($property) . '>' . $value . '</' . $tagName . '>'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Make parts of URI safe and usable |
89
|
|
|
* @param string $string |
90
|
|
|
* @param bool $encode |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public static function safeUri($string, $encode = true) |
94
|
|
|
{ |
95
|
|
|
$string = Str::lowerCase($string); |
96
|
|
|
$string = self::nohtml($string); |
97
|
|
|
if ($encode === true) { |
98
|
|
|
$string = urlencode($string); |
99
|
|
|
} |
100
|
|
|
return $string; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Check if uri $source is equal to current uri point with array of $aliases and active $order set |
105
|
|
|
* @param null $source |
106
|
|
|
* @param array|null $aliases |
107
|
|
|
* @param bool $order |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public static function isCurrentLink($source = null, array $aliases = null, $order = false) |
111
|
|
|
{ |
112
|
|
|
$elementPoint = Url::buildPathway($source); |
113
|
|
|
$currentPoint = Url::buildPathwayFromRequest(); |
114
|
|
|
|
115
|
|
|
// use special active element order type: controller, action |
116
|
|
|
switch ($order) { |
117
|
|
|
case 'controller': |
118
|
|
|
$elementPoint = Str::firstIn($elementPoint, '/'); |
119
|
|
|
$active = Str::startsWith($elementPoint, $currentPoint); |
120
|
|
|
break; |
121
|
|
|
case 'action': |
122
|
|
|
$elementArray = explode('/', $elementPoint); |
123
|
|
|
if (!Str::contains('/', $elementPoint) || count($elementArray) < 2) { |
124
|
|
|
$active = $elementPoint === $currentPoint; |
125
|
|
|
} else { |
126
|
|
|
$elementPoint = $elementArray[0] . '/' . $elementArray[1]; |
127
|
|
|
$active = Str::startsWith($elementPoint, $currentPoint); |
128
|
|
|
} |
129
|
|
|
break; |
130
|
|
|
case 'id': |
131
|
|
|
$elementArray = explode('/', $elementPoint); |
132
|
|
|
$elementPoint = $elementArray[0] . '/' . $elementArray[1]; |
133
|
|
|
if (null !== $elementArray[2]) { |
134
|
|
|
$elementPoint .= '/' . $elementArray[2]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$active = Str::startsWith($elementPoint, $currentPoint); |
138
|
|
|
break; |
139
|
|
|
default: |
140
|
|
|
$active = $elementPoint === $currentPoint; |
141
|
|
|
break; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// check if current uri equals with aliases |
145
|
|
|
if (Obj::isArray($aliases) && count($aliases) > 0) { |
146
|
|
|
foreach ($aliases as $activeUri) { |
|
|
|
|
147
|
|
|
$activeUri = trim($activeUri, '/'); |
148
|
|
|
if (Str::endsWith('*', $activeUri)) { |
149
|
|
|
$activeUri = rtrim($activeUri, '*'); |
150
|
|
|
if (Str::startsWith($activeUri, $currentPoint)) { |
151
|
|
|
$active = true; |
152
|
|
|
} |
153
|
|
|
} else { |
154
|
|
|
if ($activeUri === $currentPoint) { |
155
|
|
|
$active = true; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $active; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Apply security for string to output as html |
166
|
|
|
* @param string|null $object |
167
|
|
|
* @param bool $html |
168
|
|
|
* @param bool $secure |
169
|
|
|
* @return null|string |
170
|
|
|
*/ |
171
|
|
|
public static function applyEscape($object = null, $html = false, $secure = false) |
172
|
|
|
{ |
173
|
|
|
$object = (string)$object; |
174
|
|
|
if ($html !== true) { |
175
|
|
|
$object = self::nohtml($object); |
176
|
|
|
} elseif ($secure !== true) { |
177
|
|
|
$object = self::safe($object, true); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $object; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Convert link-binding type to classic link with security filter |
185
|
|
|
* @param string|array $uri |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public static function convertLink($uri) |
189
|
|
|
{ |
190
|
|
|
$link = App::$Alias->baseUrl . '/'; |
191
|
|
|
if (Obj::isArray($uri)) { |
192
|
|
|
$link .= Url::buildPathway($uri); |
|
|
|
|
193
|
|
|
} elseif (Str::startsWith('http', $uri)) { |
|
|
|
|
194
|
|
|
$link = self::nohtml($uri); |
|
|
|
|
195
|
|
|
} elseif (Str::startsWith('#', $uri)) { // allow pass #part |
|
|
|
|
196
|
|
|
$link = self::nohtml($uri); |
|
|
|
|
197
|
|
|
} else { |
198
|
|
|
$link .= self::nohtml(trim($uri, '/')); |
199
|
|
|
} |
200
|
|
|
return $link; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
} |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.