/
StringUtils.php
287 lines (258 loc) · 10.9 KB
/
StringUtils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
***********************************************************************************************
* @copyright 2004-2021 The Admidio Team
* @see https://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
final class StringUtils
{
/**
* In case the multibyte functions are not supported, we fallback to a no-multibyte function
* IMPORTANT: If the fallback is used, the conversion of umlauts not work!
* StringUtils::strToLower\(([\w$\[\]()]+)\) -> mb_strtolower($1, 'UTF-8')
* @param string $string
* @return string
*/
public static function strToLower($string)
{
if (function_exists('mb_strtolower'))
{
return mb_strtolower($string, 'UTF-8');
}
return strtolower($string);
}
/**
* In case the multibyte functions are not supported, we fallback to a no-multibyte function
* IMPORTANT: If the fallback is used, the conversion of umlauts not work!
* @param string $string
* @return string
*/
public static function strToUpper($string)
{
if (function_exists('mb_strtoupper'))
{
return mb_strtoupper($string, 'UTF-8');
}
return strtoupper($string);
}
/**
* Checks if a string contains another given string
* @param string $string The string to check
* @param string $contains The containing string pattern
* @param bool $caseSensitive Flag to change between case-sensitive and case-insensitive
* @return bool Returns true if the string contains the other string
*/
public static function strContains($string, $contains, $caseSensitive = true)
{
if ($caseSensitive)
{
return str_contains($string, $contains);
}
return $contains === '' || stripos($string, $contains) !== false;
}
/**
* Checks if a string starts with another given string
* @param string $string The string to check
* @param string $start The starting string pattern
* @param bool $caseSensitive Flag to change between case-sensitive and case-insensitive
* @return bool Returns true if the string starts with the other string
*/
public static function strStartsWith($string, $start, $caseSensitive = true)
{
if ($caseSensitive)
{
return str_starts_with($string, $start);
}
return substr_compare($string, $start, 0, strlen($start), true) === 0;
}
/**
* Checks if a string ends with another given string
* @param string $string The string to check
* @param string $end The ending string pattern
* @param bool $caseSensitive Flag to change between case-sensitive and case-insensitive
* @return bool Returns true if the string ends with the other string
*/
public static function strEndsWith($string, $end, $caseSensitive = true)
{
if ($caseSensitive)
{
return str_ends_with($string, $end);
}
return $end === '' || ($string !== '' && substr_compare($string, $end, -strlen($end), true) === 0);
}
/**
* Easy way for multiple replacements in a string.
* @param string $string The string where to replace strings
* @param array<string,string> $replaces An array with search and replace values
* @return string The modified string
*/
public static function strMultiReplace($string, $replaces)
{
return str_replace(array_keys($replaces), array_values($replaces), $string);
}
/**
* removes html, php code and blancs at beginning and end
* of string or all elements of array without ckeditor variables !!!
* @param array<string,string|array<mixed,string>> $srcArray
* @return array<string,string|array<mixed,string>>
*/
public static function strStripSpecialTags(array $srcArray)
{
// "ecard_message" => ckeditor-variable
$specialKeys = array(
'ecard_message', 'ann_description', 'dat_description', 'gbc_text', 'gbo_text', 'lnk_description',
'msg_body', 'plugin_CKEditor', 'room_description', 'usf_description', 'mail_smtp_password'
);
foreach ($srcArray as $key => $value)
{
if (!in_array($key, $specialKeys, true))
{
$srcArray[$key] = self::strStripTags($value);
}
}
return $srcArray;
}
/**
* removes html, php code and whitespaces at beginning and end of string or all elements of array
* @param string|array<mixed,string> $value
* @return string|array<mixed,string>
*/
public static function strStripTags($value)
{
if (is_array($value))
{
// call function for every array element
$value = array_map('self::strStripTags', $value);
}
else
{
// remove whitespaces at beginning and end
$value = trim($value);
// removes html and php code
$value = strip_tags($value);
}
return $value;
}
/**
* Check if a string contains only valid characters. Therefore the string is
* compared with a hard coded list of valid characters for each datatype.
* @param string $string The string that should be checked.
* @param string $checkType The type **noSpecialChar**, **email**, **file**, **url** or **phone** that will be checked.
* Each type has a different valid character list.
* @return bool Returns **true** if all characters of **string** match the internal character list.
*/
public static function strValidCharacters($string, $checkType)
{
if (trim($string) === '')
{
return false;
}
switch ($checkType)
{
case 'noSpecialChar': // a simple e-mail address should still be possible (like username)
$validRegex = '/^[\w.@+-]+$/i';
break;
case 'email':
$validRegex = '/^[\wáàâåäæçéèêîñóòôöõøœúùûüß.@+-]+$/i';
break;
case 'file':
$validRegex = '=^[^/?*;:~<>|\"\\\\]+\.[^/?*;:~<>|‚\"\\\\]+$=';
break;
case 'folder':
$validRegex = '=^[^/?*;:~<>|\"\\\\]+$=';
break;
case 'url':
//$validRegex = '/^[\wáàâåäæçéèêîñóòôöõøœúùûüß$&!?() \/%=#:~.@+-]+$/i';
$validRegex = '/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i';
break;
case 'phone':
$validRegex = '/^[\d() \/+-]+$/i';
break;
default:
return false;
}
// check if string contains only valid characters
if (!preg_match($validRegex, $string))
{
return false;
}
switch ($checkType)
{
case 'email':
return filter_var(trim($string), FILTER_VALIDATE_EMAIL) !== false;
case 'url':
return filter_var(trim($string), FILTER_VALIDATE_URL) !== false;
default:
return true;
}
}
/**
* Check if a filename contains invalid characters. The characters will be checked with StringUtils::strValidCharacters.
* In addition the function checks if the name contains .. or a . at the beginning.
* @param string $filename Name of the file that should be checked.
* @param bool $checkExtension If set to **true** then the extension will be checked against a block-list of extensions:
* php, php3, php4, php5, html, htm, htaccess, htpasswd, pl, js, vbs, asp, cgi, ssi, phar
* @throws AdmException SYS_FILENAME_EMPTY : Filename was empty
* SYS_FILENAME_INVALID : Filename contains invalid characters
* SYS_FILE_EXTENSION_INVALID : Filename contains invalid extension
* @return true Returns @true if filename contains only valid characters. Otherwise an AdmException is thrown
*/
public static function strIsValidFileName($filename, $checkExtension = true)
{
$filename = urldecode($filename);
// If the filename was not empty
if (trim($filename) === '')
{
throw new AdmException('SYS_FILENAME_EMPTY');
}
// filename should only contains valid characters and don't start with a dot
if (
basename($filename) !== $filename ||
self::strStartsWith($filename, '.') ||
self::strContains($filename, '//') ||
self::strContains($filename, '\\') ||
(!self::strValidCharacters($filename, 'file') && $checkExtension) ||
(!self::strValidCharacters($filename, 'folder') && !$checkExtension)
)
{
throw new AdmException('SYS_FILENAME_INVALID', array(StringUtils::strStripTags($filename)));
}
if ($checkExtension)
{
// check if the extension is not listed as blocked
$extensionBlocklist = array('php', 'php3', 'php4', 'php5', 'pht', 'html', 'htm', 'phtml',
'shtml', 'htaccess', 'htpasswd', 'pl', 'js', 'vbs', 'asp',
'asa', 'cer', 'asax', 'swf', 'xap', 'cgi', 'ssi', 'phar', 'svg');
$fileExtension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (in_array($fileExtension, $extensionBlocklist, true))
{
throw new AdmException('SYS_FILE_EXTENSION_INVALID');
}
}
return true;
}
/**
* Check if a filename contains invalid characters. The characters will be checked with StringUtils::strValidCharacters.
* In addition the function checks if the name contains .. or a . at the beginning.
* @param string $filename Name of the file that should be checked.
* @throws AdmException SYS_FILENAME_EMPTY : Filename was empty
* SYS_FILENAME_INVALID : Filename contains invalid characters
* @return true Returns @true if filename contains only valid characters. Otherwise an AdmException is thrown
*/
public static function strIsValidFolderName($filename)
{
// If the filename was not empty
if (trim($filename) === '')
{
throw new AdmException('SYS_FOLDER_NAME_EMPTY');
}
// filename should only contains valid characters and don't start with a dot
if (basename($filename) !== $filename || self::strStartsWith($filename, '.') || !self::strValidCharacters($filename, 'folder'))
{
throw new AdmException('SYS_FOLDER_NAME_INVALID', array($filename));
}
return true;
}
}