1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Util |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf\Util; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* For handling Character and String |
13
|
|
|
* |
14
|
|
|
* @package Util |
15
|
|
|
* @author Gjero Krsteski <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Character |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Check value to find if it was serialized. |
21
|
|
|
* |
22
|
|
|
* @param mixed $string Value to check to see if was serialized |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public static function isSerialized($string) |
27
|
|
|
{ |
28
|
|
|
return (unserialize($string) !== false || $string == 'b:0;'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check for invalid UTF8 encoding and invalid byte . |
33
|
|
|
* |
34
|
|
|
* @param string $string Your string. |
35
|
|
|
* |
36
|
|
|
* @return boolean |
37
|
|
|
*/ |
38
|
|
|
public static function checkUtf8Encoding($string) |
39
|
|
|
{ |
40
|
|
|
if (!mb_check_encoding($string, 'UTF-8') || |
41
|
|
|
!$string == mb_convert_encoding(mb_convert_encoding($string, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') |
42
|
|
|
) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Ensure that a string is ends with a special string. |
51
|
|
|
* |
52
|
|
|
* <code> |
53
|
|
|
* - ensureTrailing('/', 'http://www.example.com') -> 'http://www.example.com/' |
54
|
|
|
* - ensureTrailing('/', 'http://www.example.com/') -> 'http://www.example.com/' |
55
|
|
|
* </code> |
56
|
|
|
* |
57
|
|
|
* @param string $needle The needle. |
58
|
|
|
* @param string $haystack The haystack. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public static function ensureTrailing($needle, $haystack) |
63
|
|
|
{ |
64
|
|
|
$needleLength = strlen($needle); |
65
|
|
|
$needlePart = substr($haystack, -1 * $needleLength); |
66
|
|
|
|
67
|
|
|
if ($needlePart !== $needle) { |
68
|
|
|
// append missing trailing character. |
69
|
|
|
$haystack .= $needle; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $haystack; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Ensure that a string is starts with a special string. |
77
|
|
|
* |
78
|
|
|
* <code> |
79
|
|
|
* - ensureLeading('#', '1#2#3#4#5') -> '#1#2#3#4#5' |
80
|
|
|
* - ensureLeading('#', '#1#2#3#4#5') -> '#1#2#3#4#5' |
81
|
|
|
* </code> |
82
|
|
|
* |
83
|
|
|
* @param string $needle The needle. |
84
|
|
|
* @param string $haystack The haystack |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public static function ensureLeading($needle, $haystack) |
89
|
|
|
{ |
90
|
|
|
$needleLength = strlen($needle); |
91
|
|
|
$needlePart = substr($haystack, 0, $needleLength); |
92
|
|
|
|
93
|
|
|
if ($needlePart !== $needle) { |
94
|
|
|
// append missing trailing string |
95
|
|
|
$haystack = $needle . $haystack; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $haystack; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete trailing characters. |
103
|
|
|
* |
104
|
|
|
* <code> |
105
|
|
|
* - deleteTrailing('|', '|1|2|3|4|5|') -> '|1|2|3|4|5' |
106
|
|
|
* - deleteTrailing(array('|','5'), '|1|2|3|4|5|555') -> '|1|2|3|4' |
107
|
|
|
* </code> |
108
|
|
|
* |
109
|
|
|
* @param string|array $needle The needle. |
110
|
|
|
* @param string $haystack The haystack. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public static function deleteTrailing($needle, $haystack) |
115
|
|
|
{ |
116
|
|
|
$pattern = '#(' . self::pregQuote($needle, '#') . ')+$#'; |
117
|
|
|
$result = preg_replace($pattern, '', $haystack); |
118
|
|
|
|
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Delete leading characters. |
124
|
|
|
* |
125
|
|
|
* <code> |
126
|
|
|
* - deleteTrailing('#', '#1#2#3#4#5') -> '1#2#3#4#5' |
127
|
|
|
* - deleteTrailing(array('#', '1'), '##11#2#3#4#5') -> '2#3#4#5' |
128
|
|
|
* </code> |
129
|
|
|
* |
130
|
|
|
* @param string|array $needle The needle. |
131
|
|
|
* @param string $haystack The haystack. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public static function deleteLeading($needle, $haystack) |
136
|
|
|
{ |
137
|
|
|
$pattern = '#^(' . self::pregQuote($needle, '#') . ')+#'; |
138
|
|
|
$result = preg_replace($pattern, '', $haystack); |
139
|
|
|
|
140
|
|
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Wrapper for preg_quote supporting strings and array of strings. |
145
|
|
|
* |
146
|
|
|
* @param mixed $values The values. |
147
|
|
|
* @param null|string $delimiter (Optional) The delimiter. |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public static function pregQuote($values, $delimiter = null) |
152
|
|
|
{ |
153
|
|
|
if (!is_array($values)) { |
154
|
|
|
return preg_quote($values, $delimiter); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// Case: needle is array |
158
|
|
|
foreach ($values as $key => $value) { |
159
|
|
|
$values[$key] = preg_quote($value, $delimiter); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return implode('|', $values); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* An aggressive cleaning - all tags and stuff inside will be removed. |
167
|
|
|
* |
168
|
|
|
* @param string $string The string. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public static function cleanAggressive($string) |
173
|
|
|
{ |
174
|
|
|
return \Pimf\Util\Character\Clean::aggressive($string); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Cleans against XSS. |
179
|
|
|
* Info: use it on showing your request data. |
180
|
|
|
* |
181
|
|
|
* @param string $string String to check |
182
|
|
|
* @param string $charset Character set (default ISO-8859-1) |
183
|
|
|
* |
184
|
|
|
* @return string $value Sanitized string |
185
|
|
|
*/ |
186
|
|
|
public static function cleanXss($string, $charset = 'ISO-8859-1') |
187
|
|
|
{ |
188
|
|
|
return \Pimf\Util\Character\Clean::xss($string, $charset); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param int $length |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public static function random($length = 32) |
197
|
|
|
{ |
198
|
|
|
return substr( |
199
|
|
|
str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 5)), 0, $length |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Determine if a given string contains a given sub-string. |
205
|
|
|
* |
206
|
|
|
* @param string $haystack |
207
|
|
|
* @param string|array $needle |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public static function contains($haystack, $needle) |
212
|
|
|
{ |
213
|
|
|
foreach ((array)$needle as $n) { |
214
|
|
|
if (strpos($haystack, $n) !== false) { |
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Determine if a given string begins with a given value. |
224
|
|
|
* |
225
|
|
|
* @param string $haystack |
226
|
|
|
* @param string|array $needle |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
|
|
public static function startsWith($haystack, $needle) |
231
|
|
|
{ |
232
|
|
|
return strpos($haystack, $needle) === 0; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Determine if a given string ends with a given value. |
237
|
|
|
* |
238
|
|
|
* @param string $haystack |
239
|
|
|
* @param string|array $needle |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
public static function endsWith($haystack, $needle) |
244
|
|
|
{ |
245
|
|
|
return $needle == substr($haystack, strlen($haystack) - strlen($needle)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Determine if a given string matches a given pattern. |
250
|
|
|
* |
251
|
|
|
* Asterisks are translated into zero-or-more regular expression wildcards |
252
|
|
|
* to make it convenient to check if string such as "library/*". |
253
|
|
|
* |
254
|
|
|
* @param string $pattern Pattern or wildcard |
255
|
|
|
* @param string $value |
256
|
|
|
* |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
public static function is($pattern, $value) |
260
|
|
|
{ |
261
|
|
|
if ($pattern !== '/') { |
262
|
|
|
$pattern = str_replace('*', '(.*)', $pattern) . '\z'; |
263
|
|
|
} else { |
264
|
|
|
$pattern = '^/$'; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return (bool)preg_match('#' . $pattern . '#', $value); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Check if strange things happening. |
272
|
|
|
* |
273
|
|
|
* @param string $path |
274
|
|
|
* |
275
|
|
|
* @return bool |
276
|
|
|
*/ |
277
|
|
|
public static function isEvilPath($path) |
278
|
|
|
{ |
279
|
|
|
return self::contains($path, |
280
|
|
|
array('../', "..\\", '/..', '\..', ' ', '%2e%2e%2f', '%2e%2e%5C', '%2F%2e%2e', '%5C%2e%2e', '%20')); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|