1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
+-----------------------------------------------------------------------+
|
|
|
5 |
| This file is part of the Roundcube Webmail client |
|
|
|
6 |
| |
|
|
|
7 |
| Copyright (C) The Roundcube Dev Team |
|
|
|
8 |
| Copyright (C) Kolab Systems AG |
|
|
|
9 |
| |
|
|
|
10 |
| Licensed under the GNU General Public License version 3 or |
|
|
|
11 |
| any later version with exceptions for skins & plugins. |
|
|
|
12 |
| See the README file for a full license statement. |
|
|
|
13 |
| |
|
|
|
14 |
| PURPOSE: |
|
|
|
15 |
| Utility class providing common functions |
|
|
|
16 |
+-----------------------------------------------------------------------+
|
|
|
17 |
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
|
18 |
| Author: Aleksander Machniak <alec@alec.pl> |
|
|
|
19 |
+-----------------------------------------------------------------------+
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Utility class providing common functions
|
|
|
24 |
*
|
|
|
25 |
* @package Framework
|
|
|
26 |
* @subpackage Utils
|
|
|
27 |
*/
|
|
|
28 |
class rcube_utils
|
|
|
29 |
{
|
|
|
30 |
// define constants for input reading
|
|
|
31 |
const INPUT_GET = 1;
|
|
|
32 |
const INPUT_POST = 2;
|
|
|
33 |
const INPUT_COOKIE = 4;
|
|
|
34 |
const INPUT_GP = 3; // GET + POST
|
|
|
35 |
const INPUT_GPC = 7; // GET + POST + COOKIE
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* A wrapper for PHP's explode() that does not throw a warning
|
|
|
40 |
* when the separator does not exist in the string
|
|
|
41 |
*
|
|
|
42 |
* @param string $separator Separator string
|
|
|
43 |
* @param string $string The string to explode
|
|
|
44 |
*
|
|
|
45 |
* @return array Exploded string. Still an array if there's no separator in the string
|
|
|
46 |
*/
|
|
|
47 |
public static function explode($separator, $string)
|
|
|
48 |
{
|
|
|
49 |
if (strpos($string, $separator) !== false) {
|
|
|
50 |
return explode($separator, $string);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return [$string, null];
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Helper method to set a cookie with the current path and host settings
|
|
|
58 |
*
|
|
|
59 |
* @param string $name Cookie name
|
|
|
60 |
* @param string $value Cookie value
|
|
|
61 |
* @param int $exp Expiration time
|
|
|
62 |
* @param bool $http_only HTTP Only
|
|
|
63 |
*/
|
|
|
64 |
public static function setcookie($name, $value, $exp = 0, $http_only = true)
|
|
|
65 |
{
|
|
|
66 |
if (headers_sent()) {
|
|
|
67 |
return;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
$attrib = session_get_cookie_params();
|
|
|
71 |
$attrib['expires'] = $exp;
|
|
|
72 |
$attrib['secure'] = $attrib['secure'] || self::https_check();
|
|
|
73 |
$attrib['httponly'] = $http_only;
|
|
|
74 |
|
|
|
75 |
// session_get_cookie_params() return includes 'lifetime' but setcookie() does not use it, instead it uses 'expires'
|
|
|
76 |
unset($attrib['lifetime']);
|
|
|
77 |
|
|
|
78 |
setcookie($name, $value, $attrib);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* E-mail address validation.
|
|
|
83 |
*
|
|
|
84 |
* @param string $email Email address
|
|
|
85 |
* @param bool $dns_check True to check dns
|
|
|
86 |
*
|
|
|
87 |
* @return bool True on success, False if address is invalid
|
|
|
88 |
*/
|
|
|
89 |
public static function check_email($email, $dns_check = true)
|
|
|
90 |
{
|
|
|
91 |
// Check for invalid (control) characters
|
|
|
92 |
if (preg_match('/\p{Cc}/u', $email)) {
|
|
|
93 |
return false;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// Check for length limit specified by RFC 5321 (#1486453)
|
|
|
97 |
if (strlen($email) > 254) {
|
|
|
98 |
return false;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$pos = strrpos($email, '@');
|
|
|
102 |
if (!$pos) {
|
|
|
103 |
return false;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$domain_part = substr($email, $pos + 1);
|
|
|
107 |
$local_part = substr($email, 0, $pos);
|
|
|
108 |
|
|
|
109 |
// quoted-string, make sure all backslashes and quotes are
|
|
|
110 |
// escaped
|
|
|
111 |
if (substr($local_part, 0, 1) == '"') {
|
|
|
112 |
$local_quoted = preg_replace('/\\\\(\\\\|\")/','', substr($local_part, 1, -1));
|
|
|
113 |
if (preg_match('/\\\\|"/', $local_quoted)) {
|
|
|
114 |
return false;
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
// dot-atom portion, make sure there's no prohibited characters
|
|
|
118 |
else if (preg_match('/(^\.|\.\.|\.$)/', $local_part)
|
|
|
119 |
|| preg_match('/[\\ ",:;<>@]/', $local_part)
|
|
|
120 |
) {
|
|
|
121 |
return false;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// Validate domain part
|
|
|
125 |
if (preg_match('/^\[((IPv6:[0-9a-f:.]+)|([0-9.]+))\]$/i', $domain_part, $matches)) {
|
|
|
126 |
return self::check_ip(preg_replace('/^IPv6:/i', '', $matches[1])); // valid IPv4 or IPv6 address
|
|
|
127 |
}
|
|
|
128 |
else {
|
|
|
129 |
// If not an IP address
|
|
|
130 |
$domain_array = explode('.', $domain_part);
|
|
|
131 |
// Not enough parts to be a valid domain
|
|
|
132 |
if (count($domain_array) < 2) {
|
|
|
133 |
return false;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
foreach ($domain_array as $part) {
|
|
|
137 |
if (!preg_match('/^((xn--)?([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $part)) {
|
|
|
138 |
return false;
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
// last domain part (allow extended TLD)
|
|
|
143 |
$last_part = array_pop($domain_array);
|
|
|
144 |
if (strpos($last_part, 'xn--') !== 0
|
|
|
145 |
&& (preg_match('/[^a-zA-Z0-9]/', $last_part) || preg_match('/^[0-9]+$/', $last_part))
|
|
|
146 |
) {
|
|
|
147 |
return false;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
$rcube = rcube::get_instance();
|
|
|
151 |
|
|
|
152 |
if (!$dns_check || !function_exists('checkdnsrr') || !$rcube->config->get('email_dns_check')) {
|
|
|
153 |
return true;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
// Check DNS record(s)
|
|
|
157 |
// Note: We can't use ANY (#6581)
|
|
|
158 |
foreach (['A', 'MX', 'CNAME', 'AAAA'] as $type) {
|
|
|
159 |
if (checkdnsrr($domain_part, $type)) {
|
|
|
160 |
return true;
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
return false;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Validates IPv4 or IPv6 address
|
|
|
170 |
*
|
|
|
171 |
* @param string $ip IP address in v4 or v6 format
|
|
|
172 |
*
|
|
|
173 |
* @return bool True if the address is valid
|
|
|
174 |
*/
|
|
|
175 |
public static function check_ip($ip)
|
|
|
176 |
{
|
|
|
177 |
return filter_var($ip, FILTER_VALIDATE_IP) !== false;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Replacing specials characters to a specific encoding type
|
|
|
182 |
*
|
|
|
183 |
* @param string $str Input string
|
|
|
184 |
* @param string $enctype Encoding type: text|html|xml|js|url
|
|
|
185 |
* @param string $mode Replace mode for tags: show|remove|strict
|
|
|
186 |
* @param bool $newlines Convert newlines
|
|
|
187 |
*
|
|
|
188 |
* @return string The quoted string
|
|
|
189 |
*/
|
|
|
190 |
public static function rep_specialchars_output($str, $enctype = '', $mode = '', $newlines = true)
|
|
|
191 |
{
|
|
|
192 |
static $html_encode_arr = false;
|
|
|
193 |
static $js_rep_table = false;
|
|
|
194 |
static $xml_rep_table = false;
|
|
|
195 |
|
|
|
196 |
if (!is_string($str)) {
|
|
|
197 |
$str = strval($str);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
// encode for HTML output
|
|
|
201 |
if ($enctype == 'html') {
|
|
|
202 |
if (!$html_encode_arr) {
|
|
|
203 |
$html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS);
|
|
|
204 |
unset($html_encode_arr['?']);
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
$encode_arr = $html_encode_arr;
|
|
|
208 |
|
|
|
209 |
if ($mode == 'remove') {
|
|
|
210 |
$str = strip_tags($str);
|
|
|
211 |
}
|
|
|
212 |
else if ($mode != 'strict') {
|
|
|
213 |
// don't replace quotes and html tags
|
|
|
214 |
$ltpos = strpos($str, '<');
|
|
|
215 |
if ($ltpos !== false && strpos($str, '>', $ltpos) !== false) {
|
|
|
216 |
unset($encode_arr['"']);
|
|
|
217 |
unset($encode_arr['<']);
|
|
|
218 |
unset($encode_arr['>']);
|
|
|
219 |
unset($encode_arr['&']);
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
$out = strtr($str, $encode_arr);
|
|
|
224 |
|
|
|
225 |
return $newlines ? nl2br($out) : $out;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
// if the replace tables for XML and JS are not yet defined
|
|
|
229 |
if ($js_rep_table === false) {
|
|
|
230 |
$js_rep_table = $xml_rep_table = [];
|
|
|
231 |
$xml_rep_table['&'] = '&';
|
|
|
232 |
|
|
|
233 |
// can be increased to support more charsets
|
|
|
234 |
for ($c=160; $c<256; $c++) {
|
|
|
235 |
$xml_rep_table[chr($c)] = "&#$c;";
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
$xml_rep_table['"'] = '"';
|
|
|
239 |
$js_rep_table['"'] = '\\"';
|
|
|
240 |
$js_rep_table["'"] = "\\'";
|
|
|
241 |
$js_rep_table["\\"] = "\\\\";
|
|
|
242 |
// Unicode line and paragraph separators (#1486310)
|
|
|
243 |
$js_rep_table[chr(hexdec('E2')).chr(hexdec('80')).chr(hexdec('A8'))] = '
';
|
|
|
244 |
$js_rep_table[chr(hexdec('E2')).chr(hexdec('80')).chr(hexdec('A9'))] = '
';
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
// encode for javascript use
|
|
|
248 |
if ($enctype == 'js') {
|
|
|
249 |
return preg_replace(["/\r?\n/", "/\r/", '/<\\//'], ['\n', '\n', '<\\/'], strtr($str, $js_rep_table));
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
// encode for plaintext
|
|
|
253 |
if ($enctype == 'text') {
|
|
|
254 |
return str_replace("\r\n", "\n", $mode == 'remove' ? strip_tags($str) : $str);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
if ($enctype == 'url') {
|
|
|
258 |
return rawurlencode($str);
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
// encode for XML
|
|
|
262 |
if ($enctype == 'xml') {
|
|
|
263 |
return strtr($str, $xml_rep_table);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
// no encoding given -> return original string
|
|
|
267 |
return $str;
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
/**
|
|
|
271 |
* Read input value and make sure it is a string.
|
|
|
272 |
*
|
|
|
273 |
* @param string $fname Field name to read
|
|
|
274 |
* @param int $source Source to get value from (see self::INPUT_*)
|
|
|
275 |
* @param bool $allow_html Allow HTML tags in field value
|
|
|
276 |
* @param string $charset Charset to convert into
|
|
|
277 |
*
|
|
|
278 |
* @return string Request parameter value
|
|
|
279 |
* @see self::get_input_value()
|
|
|
280 |
*/
|
|
|
281 |
public static function get_input_string($fname, $source, $allow_html = false, $charset = null)
|
|
|
282 |
{
|
|
|
283 |
$value = self::get_input_value($fname, $source, $allow_html, $charset);
|
|
|
284 |
|
|
|
285 |
return is_string($value) ? $value : '';
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Read request parameter value and convert it for internal use
|
|
|
290 |
* Performs stripslashes() and charset conversion if necessary
|
|
|
291 |
*
|
|
|
292 |
* @param string $fname Field name to read
|
|
|
293 |
* @param int $source Source to get value from (see self::INPUT_*)
|
|
|
294 |
* @param bool $allow_html Allow HTML tags in field value
|
|
|
295 |
* @param string $charset Charset to convert into
|
|
|
296 |
*
|
|
|
297 |
* @return string|array|null Request parameter value or NULL if not set
|
|
|
298 |
*/
|
|
|
299 |
public static function get_input_value($fname, $source, $allow_html = false, $charset = null)
|
|
|
300 |
{
|
|
|
301 |
$value = null;
|
|
|
302 |
|
|
|
303 |
if (($source & self::INPUT_GET) && isset($_GET[$fname])) {
|
|
|
304 |
$value = $_GET[$fname];
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
if (($source & self::INPUT_POST) && isset($_POST[$fname])) {
|
|
|
308 |
$value = $_POST[$fname];
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
if (($source & self::INPUT_COOKIE) && isset($_COOKIE[$fname])) {
|
|
|
312 |
$value = $_COOKIE[$fname];
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
return self::parse_input_value($value, $allow_html, $charset);
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* Parse/validate input value. See self::get_input_value()
|
|
|
320 |
* Performs stripslashes() and charset conversion if necessary
|
|
|
321 |
*
|
|
|
322 |
* @param string $value Input value
|
|
|
323 |
* @param bool $allow_html Allow HTML tags in field value
|
|
|
324 |
* @param string $charset Charset to convert into
|
|
|
325 |
*
|
|
|
326 |
* @return string Parsed value
|
|
|
327 |
*/
|
|
|
328 |
public static function parse_input_value($value, $allow_html = false, $charset = null)
|
|
|
329 |
{
|
|
|
330 |
if (empty($value)) {
|
|
|
331 |
return $value;
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
if (is_array($value)) {
|
|
|
335 |
foreach ($value as $idx => $val) {
|
|
|
336 |
$value[$idx] = self::parse_input_value($val, $allow_html, $charset);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
return $value;
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
// remove HTML tags if not allowed
|
|
|
343 |
if (!$allow_html) {
|
|
|
344 |
$value = strip_tags($value);
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
$rcube = rcube::get_instance();
|
|
|
348 |
$output_charset = is_object($rcube->output) ? $rcube->output->get_charset() : null;
|
|
|
349 |
|
|
|
350 |
// remove invalid characters (#1488124)
|
|
|
351 |
if ($output_charset == 'UTF-8') {
|
|
|
352 |
$value = rcube_charset::clean($value);
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
// convert to internal charset
|
|
|
356 |
if ($charset && $output_charset) {
|
|
|
357 |
$value = rcube_charset::convert($value, $output_charset, $charset);
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
return $value;
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
/**
|
|
|
364 |
* Convert array of request parameters (prefixed with _)
|
|
|
365 |
* to a regular array with non-prefixed keys.
|
|
|
366 |
*
|
|
|
367 |
* @param int $mode Source to get value from (GPC)
|
|
|
368 |
* @param string $ignore PCRE expression to skip parameters by name
|
|
|
369 |
* @param bool $allow_html Allow HTML tags in field value
|
|
|
370 |
*
|
|
|
371 |
* @return array Hash array with all request parameters
|
|
|
372 |
*/
|
|
|
373 |
public static function request2param($mode = null, $ignore = 'task|action', $allow_html = false)
|
|
|
374 |
{
|
|
|
375 |
$out = [];
|
|
|
376 |
$src = $mode == self::INPUT_GET ? $_GET : ($mode == self::INPUT_POST ? $_POST : $_REQUEST);
|
|
|
377 |
|
|
|
378 |
foreach (array_keys($src) as $key) {
|
|
|
379 |
$fname = $key[0] == '_' ? substr($key, 1) : $key;
|
|
|
380 |
if ($ignore && !preg_match('/^(' . $ignore . ')$/', $fname)) {
|
|
|
381 |
$out[$fname] = self::get_input_value($key, $mode, $allow_html);
|
|
|
382 |
}
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
return $out;
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
/**
|
|
|
389 |
* Convert the given string into a valid HTML identifier
|
|
|
390 |
* Same functionality as done in app.js with rcube_webmail.html_identifier()
|
|
|
391 |
*
|
|
|
392 |
* @param string $str String input
|
|
|
393 |
* @param bool $encode Use base64 encoding
|
|
|
394 |
*
|
|
|
395 |
* @return string Valid HTML identifier
|
|
|
396 |
*/
|
|
|
397 |
public static function html_identifier($str, $encode = false)
|
|
|
398 |
{
|
|
|
399 |
if ($encode) {
|
|
|
400 |
return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
return asciiwords($str, true, '_');
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
/**
|
|
|
407 |
* Replace all css definitions with #container [def]
|
|
|
408 |
* and remove css-inlined scripting, make position style safe
|
|
|
409 |
*
|
|
|
410 |
* @param string $source CSS source code
|
|
|
411 |
* @param string $container_id Container ID to use as prefix
|
|
|
412 |
* @param bool $allow_remote Allow remote content
|
|
|
413 |
* @param string $prefix Prefix to be added to id/class identifier
|
|
|
414 |
*
|
|
|
415 |
* @return string Modified CSS source
|
|
|
416 |
*/
|
|
|
417 |
public static function mod_css_styles($source, $container_id, $allow_remote = false, $prefix = '')
|
|
|
418 |
{
|
|
|
419 |
$last_pos = 0;
|
|
|
420 |
$replacements = new rcube_string_replacer;
|
|
|
421 |
|
|
|
422 |
// ignore the whole block if evil styles are detected
|
|
|
423 |
$source = self::xss_entity_decode($source);
|
|
|
424 |
$stripped = preg_replace('/[^a-z\(:;]/i', '', $source);
|
|
|
425 |
$evilexpr = 'expression|behavior|javascript:|import[^a]' . (!$allow_remote ? '|url\((?!data:image)' : '');
|
|
|
426 |
|
|
|
427 |
if (preg_match("/$evilexpr/i", $stripped)) {
|
|
|
428 |
return '/* evil! */';
|
|
|
429 |
}
|
|
|
430 |
|
|
|
431 |
$strict_url_regexp = '!url\s*\(\s*["\']?(https?:)//[a-z0-9/._+-]+["\']?\s*\)!Uims';
|
|
|
432 |
|
|
|
433 |
// remove html comments
|
|
|
434 |
$source = preg_replace('/(^\s*<\!--)|(-->\s*$)/m', '', $source);
|
|
|
435 |
|
|
|
436 |
// cut out all contents between { and }
|
|
|
437 |
while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) {
|
|
|
438 |
$nested = strpos($source, '{', $pos+1);
|
|
|
439 |
if ($nested && $nested < $pos2) { // when dealing with nested blocks (e.g. @media), take the inner one
|
|
|
440 |
$pos = $nested;
|
|
|
441 |
}
|
|
|
442 |
$length = $pos2 - $pos - 1;
|
|
|
443 |
$styles = substr($source, $pos+1, $length);
|
|
|
444 |
$output = '';
|
|
|
445 |
|
|
|
446 |
// check every css rule in the style block...
|
|
|
447 |
foreach (self::parse_css_block($styles) as $rule) {
|
|
|
448 |
// Remove 'page' attributes (#7604)
|
|
|
449 |
if ($rule[0] == 'page') {
|
|
|
450 |
continue;
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
// Convert position:fixed to position:absolute (#5264)
|
|
|
454 |
if ($rule[0] == 'position' && strcasecmp($rule[1], 'fixed') === 0) {
|
|
|
455 |
$rule[1] = 'absolute';
|
|
|
456 |
}
|
|
|
457 |
else if ($allow_remote) {
|
|
|
458 |
$stripped = preg_replace('/[^a-z\(:;]/i', '', $rule[1]);
|
|
|
459 |
|
|
|
460 |
// allow data:image and strict url() values only
|
|
|
461 |
if (
|
|
|
462 |
stripos($stripped, 'url(') !== false
|
|
|
463 |
&& stripos($stripped, 'url(data:image') === false
|
|
|
464 |
&& !preg_match($strict_url_regexp, $rule[1])
|
|
|
465 |
) {
|
|
|
466 |
$rule[1] = '/* evil! */';
|
|
|
467 |
}
|
|
|
468 |
}
|
|
|
469 |
|
|
|
470 |
$output .= sprintf(" %s: %s;", $rule[0] , $rule[1]);
|
|
|
471 |
}
|
|
|
472 |
|
|
|
473 |
$key = $replacements->add($output . ' ');
|
|
|
474 |
$repl = $replacements->get_replacement($key);
|
|
|
475 |
$source = substr_replace($source, $repl, $pos+1, $length);
|
|
|
476 |
$last_pos = $pos2 - ($length - strlen($repl));
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
// add #container to each tag selector and prefix to id/class identifiers
|
|
|
480 |
if ($container_id || $prefix) {
|
|
|
481 |
// Exclude rcube_string_replacer pattern matches, this is needed
|
|
|
482 |
// for cases like @media { body { position: fixed; } } (#5811)
|
|
|
483 |
$excl = '(?!' . substr($replacements->pattern, 1, -1) . ')';
|
|
|
484 |
$regexp = '/(^\s*|,\s*|\}\s*|\{\s*)(' . $excl . ':?[a-z0-9\._#\*\[][a-z0-9\._:\(\)#=~ \[\]"\|\>\+\$\^-]*)/im';
|
|
|
485 |
$callback = function($matches) use ($container_id, $prefix) {
|
|
|
486 |
$replace = $matches[2];
|
|
|
487 |
|
|
|
488 |
if (stripos($replace, ':root') === 0) {
|
|
|
489 |
$replace = substr($replace, 5);
|
|
|
490 |
}
|
|
|
491 |
|
|
|
492 |
if ($prefix) {
|
|
|
493 |
$replace = str_replace(['.', '#'], [".$prefix", "#$prefix"], $replace);
|
|
|
494 |
}
|
|
|
495 |
|
|
|
496 |
if ($container_id) {
|
|
|
497 |
$replace = "#$container_id " . $replace;
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
// Remove redundant spaces (for simpler testing)
|
|
|
501 |
$replace = preg_replace('/\s+/', ' ', $replace);
|
|
|
502 |
|
|
|
503 |
return str_replace($matches[2], $replace, $matches[0]);
|
|
|
504 |
};
|
|
|
505 |
|
|
|
506 |
$source = preg_replace_callback($regexp, $callback, $source);
|
|
|
507 |
}
|
|
|
508 |
|
|
|
509 |
// replace body definition because we also stripped off the <body> tag
|
|
|
510 |
if ($container_id) {
|
|
|
511 |
$regexp = '/#' . preg_quote($container_id, '/') . '\s+body/i';
|
|
|
512 |
$source = preg_replace($regexp, "#$container_id", $source);
|
|
|
513 |
}
|
|
|
514 |
|
|
|
515 |
// put block contents back in
|
|
|
516 |
$source = $replacements->resolve($source);
|
|
|
517 |
|
|
|
518 |
return $source;
|
|
|
519 |
}
|
|
|
520 |
|
|
|
521 |
/**
|
|
|
522 |
* Explode css style. Property names will be lower-cased and trimmed.
|
|
|
523 |
* Values will be trimmed. Invalid entries will be skipped.
|
|
|
524 |
*
|
|
|
525 |
* @param string $style CSS style
|
|
|
526 |
*
|
|
|
527 |
* @return array List of CSS rule pairs, e.g. [['color', 'red'], ['top', '0']]
|
|
|
528 |
*/
|
|
|
529 |
public static function parse_css_block($style)
|
|
|
530 |
{
|
|
|
531 |
$pos = 0;
|
|
|
532 |
|
|
|
533 |
// first remove comments
|
|
|
534 |
while (($pos = strpos($style, '/*', $pos)) !== false) {
|
|
|
535 |
$end = strpos($style, '*/', $pos+2);
|
|
|
536 |
|
|
|
537 |
if ($end === false) {
|
|
|
538 |
$style = substr($style, 0, $pos);
|
|
|
539 |
}
|
|
|
540 |
else {
|
|
|
541 |
$style = substr_replace($style, '', $pos, $end - $pos + 2);
|
|
|
542 |
}
|
|
|
543 |
}
|
|
|
544 |
|
|
|
545 |
// Replace new lines with spaces
|
|
|
546 |
$style = preg_replace('/[\r\n]+/', ' ', $style);
|
|
|
547 |
|
|
|
548 |
$style = trim($style);
|
|
|
549 |
$length = strlen($style);
|
|
|
550 |
$result = [];
|
|
|
551 |
$pos = 0;
|
|
|
552 |
|
|
|
553 |
while ($pos < $length && ($colon_pos = strpos($style, ':', $pos))) {
|
|
|
554 |
// Property name
|
|
|
555 |
$name = strtolower(trim(substr($style, $pos, $colon_pos - $pos)));
|
|
|
556 |
|
|
|
557 |
// get the property value
|
|
|
558 |
$q = $s = false;
|
|
|
559 |
for ($i = $colon_pos + 1; $i < $length; $i++) {
|
|
|
560 |
if (($style[$i] == "\"" || $style[$i] == "'") && ($i == 0 || $style[$i-1] != "\\")) {
|
|
|
561 |
if ($q == $style[$i]) {
|
|
|
562 |
$q = false;
|
|
|
563 |
}
|
|
|
564 |
else if ($q === false) {
|
|
|
565 |
$q = $style[$i];
|
|
|
566 |
}
|
|
|
567 |
}
|
|
|
568 |
else if ($style[$i] == "(" && !$q && ($i == 0 || $style[$i-1] != "\\")) {
|
|
|
569 |
$q = "(";
|
|
|
570 |
}
|
|
|
571 |
else if ($style[$i] == ")" && $q == "(" && $style[$i-1] != "\\") {
|
|
|
572 |
$q = false;
|
|
|
573 |
}
|
|
|
574 |
|
|
|
575 |
if ($q === false && (($s = $style[$i] == ';') || $i == $length - 1)) {
|
|
|
576 |
break;
|
|
|
577 |
}
|
|
|
578 |
}
|
|
|
579 |
|
|
|
580 |
$value_length = $i - $colon_pos - ($s ? 1 : 0);
|
|
|
581 |
$value = trim(substr($style, $colon_pos + 1, $value_length));
|
|
|
582 |
|
|
|
583 |
if (strlen($name) && !preg_match('/[^a-z-]/', $name) && strlen($value) && $value !== ';') {
|
|
|
584 |
$result[] = [$name, $value];
|
|
|
585 |
}
|
|
|
586 |
|
|
|
587 |
$pos = $i + 1;
|
|
|
588 |
}
|
|
|
589 |
|
|
|
590 |
return $result;
|
|
|
591 |
}
|
|
|
592 |
|
|
|
593 |
/**
|
|
|
594 |
* Generate CSS classes from mimetype and filename extension
|
|
|
595 |
*
|
|
|
596 |
* @param string $mimetype Mimetype
|
|
|
597 |
* @param string $filename Filename
|
|
|
598 |
*
|
|
|
599 |
* @return string CSS classes separated by space
|
|
|
600 |
*/
|
|
|
601 |
public static function file2class($mimetype, $filename)
|
|
|
602 |
{
|
|
|
603 |
$mimetype = strtolower($mimetype);
|
|
|
604 |
$filename = strtolower($filename);
|
|
|
605 |
|
|
|
606 |
list($primary, $secondary) = rcube_utils::explode('/', $mimetype);
|
|
|
607 |
|
|
|
608 |
$classes = [$primary ?: 'unknown'];
|
|
|
609 |
|
|
|
610 |
if (!empty($secondary)) {
|
|
|
611 |
$classes[] = $secondary;
|
|
|
612 |
}
|
|
|
613 |
|
|
|
614 |
if (preg_match('/\.([a-z0-9]+)$/', $filename, $m)) {
|
|
|
615 |
if (!in_array($m[1], $classes)) {
|
|
|
616 |
$classes[] = $m[1];
|
|
|
617 |
}
|
|
|
618 |
}
|
|
|
619 |
|
|
|
620 |
return implode(' ', $classes);
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
/**
|
|
|
624 |
* Decode escaped entities used by known XSS exploits.
|
|
|
625 |
* See http://downloads.securityfocus.com/vulnerabilities/exploits/26800.eml for examples
|
|
|
626 |
*
|
|
|
627 |
* @param string $content CSS content to decode
|
|
|
628 |
*
|
|
|
629 |
* @return string Decoded string
|
|
|
630 |
*/
|
|
|
631 |
public static function xss_entity_decode($content)
|
|
|
632 |
{
|
|
|
633 |
$callback = function($matches) { return chr(hexdec($matches[1])); };
|
|
|
634 |
|
|
|
635 |
$out = html_entity_decode(html_entity_decode($content));
|
|
|
636 |
$out = trim(preg_replace('/(^<!--|-->$)/', '', trim($out)));
|
|
|
637 |
$out = preg_replace_callback('/\\\([0-9a-f]{2,6})\s*/i', $callback, $out);
|
|
|
638 |
$out = preg_replace('/\\\([^0-9a-f])/i', '\\1', $out);
|
|
|
639 |
$out = preg_replace('#/\*.*\*/#Ums', '', $out);
|
|
|
640 |
$out = strip_tags($out);
|
|
|
641 |
|
|
|
642 |
return $out;
|
|
|
643 |
}
|
|
|
644 |
|
|
|
645 |
/**
|
|
|
646 |
* Check if we can process not exceeding memory_limit
|
|
|
647 |
*
|
|
|
648 |
* @param int $need Required amount of memory
|
|
|
649 |
*
|
|
|
650 |
* @return bool True if memory won't be exceeded, False otherwise
|
|
|
651 |
*/
|
|
|
652 |
public static function mem_check($need)
|
|
|
653 |
{
|
|
|
654 |
$mem_limit = parse_bytes(ini_get('memory_limit'));
|
|
|
655 |
$memory = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
|
|
|
656 |
|
|
|
657 |
return $mem_limit > 0 && $memory + $need > $mem_limit ? false : true;
|
|
|
658 |
}
|
|
|
659 |
|
|
|
660 |
/**
|
|
|
661 |
* Check if working in SSL mode
|
|
|
662 |
*
|
|
|
663 |
* @param int $port HTTPS port number
|
|
|
664 |
* @param bool $use_https Enables 'use_https' option checking
|
|
|
665 |
*
|
|
|
666 |
* @return bool True in SSL mode, False otherwise
|
|
|
667 |
*/
|
|
|
668 |
public static function https_check($port = null, $use_https = true)
|
|
|
669 |
{
|
|
|
670 |
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') {
|
|
|
671 |
return true;
|
|
|
672 |
}
|
|
|
673 |
|
|
|
674 |
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])
|
|
|
675 |
&& strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https'
|
|
|
676 |
&& self::check_proxy_whitelist_ip()
|
|
|
677 |
) {
|
|
|
678 |
return true;
|
|
|
679 |
}
|
|
|
680 |
|
|
|
681 |
if ($port && isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == $port) {
|
|
|
682 |
return true;
|
|
|
683 |
}
|
|
|
684 |
|
|
|
685 |
if ($use_https && rcube::get_instance()->config->get('use_https')) {
|
|
|
686 |
return true;
|
|
|
687 |
}
|
|
|
688 |
|
|
|
689 |
return false;
|
|
|
690 |
}
|
|
|
691 |
|
|
|
692 |
/**
|
|
|
693 |
* Check if the reported REMOTE_ADDR is in the 'proxy_whitelist' config option
|
|
|
694 |
*/
|
|
|
695 |
public static function check_proxy_whitelist_ip() {
|
|
|
696 |
return in_array($_SERVER['REMOTE_ADDR'], (array) rcube::get_instance()->config->get('proxy_whitelist', []));
|
|
|
697 |
}
|
|
|
698 |
|
|
|
699 |
/**
|
|
|
700 |
* Replaces hostname variables.
|
|
|
701 |
*
|
|
|
702 |
* @param string $name Hostname
|
|
|
703 |
* @param string $host Optional IMAP hostname
|
|
|
704 |
*
|
|
|
705 |
* @return string Hostname
|
|
|
706 |
*/
|
|
|
707 |
public static function parse_host($name, $host = '')
|
|
|
708 |
{
|
|
|
709 |
if (!is_string($name)) {
|
|
|
710 |
return $name;
|
|
|
711 |
}
|
|
|
712 |
|
|
|
713 |
// %n - host
|
|
|
714 |
$n = self::server_name();
|
|
|
715 |
// %t - host name without first part, e.g. %n=mail.domain.tld, %t=domain.tld
|
|
|
716 |
// If %n=domain.tld then %t=domain.tld as well (remains valid)
|
|
|
717 |
$t = preg_replace('/^[^.]+\.(?![^.]+$)/', '', $n);
|
|
|
718 |
// %d - domain name without first part (up to domain.tld)
|
|
|
719 |
$d = preg_replace('/^[^.]+\.(?![^.]+$)/', '', self::server_name('HTTP_HOST'));
|
|
|
720 |
// %h - IMAP host
|
|
|
721 |
$h = !empty($_SESSION['storage_host']) ? $_SESSION['storage_host'] : $host;
|
|
|
722 |
// %z - IMAP domain without first part, e.g. %h=imap.domain.tld, %z=domain.tld
|
|
|
723 |
// If %h=domain.tld then %z=domain.tld as well (remains valid)
|
|
|
724 |
$z = preg_replace('/^[^.]+\.(?![^.]+$)/', '', $h);
|
|
|
725 |
// %s - domain name after the '@' from e-mail address provided at login screen.
|
|
|
726 |
// Returns FALSE if an invalid email is provided
|
|
|
727 |
$s = '';
|
|
|
728 |
if (strpos($name, '%s') !== false) {
|
|
|
729 |
$user_email = self::idn_to_ascii(self::get_input_value('_user', self::INPUT_POST));
|
|
|
730 |
$matches = preg_match('/(.*)@([a-z0-9\.\-\[\]\:]+)/i', $user_email, $s);
|
|
|
731 |
if ($matches < 1 || filter_var($s[1]."@".$s[2], FILTER_VALIDATE_EMAIL) === false) {
|
|
|
732 |
return false;
|
|
|
733 |
}
|
|
|
734 |
$s = $s[2];
|
|
|
735 |
}
|
|
|
736 |
|
|
|
737 |
return str_replace(['%n', '%t', '%d', '%h', '%z', '%s'], [$n, $t, $d, $h, $z, $s], $name);
|
|
|
738 |
}
|
|
|
739 |
|
|
|
740 |
/**
|
|
|
741 |
* Parse host specification URI.
|
|
|
742 |
*
|
|
|
743 |
* @param string $host Host URI
|
|
|
744 |
* @param int $plain_port Plain port number
|
|
|
745 |
* @param int $ssl_port SSL port number
|
|
|
746 |
*
|
|
|
747 |
* @return array An array with three elements (hostname, scheme, port)
|
|
|
748 |
*/
|
|
|
749 |
public static function parse_host_uri($host, $plain_port = null, $ssl_port = null)
|
|
|
750 |
{
|
|
|
751 |
if (preg_match('#^(unix|ldapi)://#i', $host, $matches)) {
|
|
|
752 |
return [$host, $matches[1], -1];
|
|
|
753 |
}
|
|
|
754 |
|
|
|
755 |
$url = parse_url($host);
|
|
|
756 |
$port = $plain_port;
|
|
|
757 |
$scheme = null;
|
|
|
758 |
|
|
|
759 |
if (!empty($url['host'])) {
|
|
|
760 |
$host = $url['host'];
|
|
|
761 |
$scheme = $url['scheme'] ?? null;
|
|
|
762 |
|
|
|
763 |
if (!empty($url['port'])) {
|
|
|
764 |
$port = $url['port'];
|
|
|
765 |
}
|
|
|
766 |
else if (
|
|
|
767 |
$scheme
|
|
|
768 |
&& $ssl_port
|
|
|
769 |
&& ($scheme === 'ssl' || ($scheme != 'tls' && $scheme[strlen($scheme) - 1] === 's'))
|
|
|
770 |
) {
|
|
|
771 |
// assign SSL port to ssl://, imaps://, ldaps://, but not tls://
|
|
|
772 |
$port = $ssl_port;
|
|
|
773 |
}
|
|
|
774 |
}
|
|
|
775 |
|
|
|
776 |
return [$host, $scheme, $port];
|
|
|
777 |
}
|
|
|
778 |
|
|
|
779 |
/**
|
|
|
780 |
* Returns the server name after checking it against trusted hostname patterns.
|
|
|
781 |
*
|
|
|
782 |
* Returns 'localhost' and logs a warning when the hostname is not trusted.
|
|
|
783 |
*
|
|
|
784 |
* @param string $type The $_SERVER key, e.g. 'HTTP_HOST', Default: 'SERVER_NAME'.
|
|
|
785 |
* @param bool $strip_port Strip port from the host name
|
|
|
786 |
*
|
|
|
787 |
* @return string Server name
|
|
|
788 |
*/
|
|
|
789 |
public static function server_name($type = null, $strip_port = true)
|
|
|
790 |
{
|
|
|
791 |
if (!$type) {
|
|
|
792 |
$type = 'SERVER_NAME';
|
|
|
793 |
}
|
|
|
794 |
|
|
|
795 |
$name = $_SERVER[$type] ?? '';
|
|
|
796 |
$rcube = rcube::get_instance();
|
|
|
797 |
$patterns = (array) $rcube->config->get('trusted_host_patterns');
|
|
|
798 |
|
|
|
799 |
if (!empty($name)) {
|
|
|
800 |
if ($strip_port) {
|
|
|
801 |
$name = preg_replace('/:\d+$/', '', $name);
|
|
|
802 |
}
|
|
|
803 |
|
|
|
804 |
if (empty($patterns)) {
|
|
|
805 |
return $name;
|
|
|
806 |
}
|
|
|
807 |
|
|
|
808 |
foreach ($patterns as $pattern) {
|
|
|
809 |
// the pattern might be a regular expression or just a host/domain name
|
|
|
810 |
if (preg_match('/[^a-zA-Z0-9.:-]/', $pattern)) {
|
|
|
811 |
if (preg_match("/$pattern/", $name)) {
|
|
|
812 |
return $name;
|
|
|
813 |
}
|
|
|
814 |
}
|
|
|
815 |
else if (strtolower($name) === strtolower($pattern)) {
|
|
|
816 |
return $name;
|
|
|
817 |
}
|
|
|
818 |
}
|
|
|
819 |
|
|
|
820 |
$rcube->raise_error([
|
|
|
821 |
'file' => __FILE__, 'line' => __LINE__,
|
|
|
822 |
'message' => "Specified host is not trusted. Using 'localhost'."
|
|
|
823 |
]
|
|
|
824 |
, true, false
|
|
|
825 |
);
|
|
|
826 |
}
|
|
|
827 |
|
|
|
828 |
return 'localhost';
|
|
|
829 |
}
|
|
|
830 |
|
|
|
831 |
/**
|
|
|
832 |
* Returns remote IP address and forwarded addresses if found
|
|
|
833 |
*
|
|
|
834 |
* @return string Remote IP address(es)
|
|
|
835 |
*/
|
|
|
836 |
public static function remote_ip()
|
|
|
837 |
{
|
|
|
838 |
$address = $_SERVER['REMOTE_ADDR'] ?? '';
|
|
|
839 |
|
|
|
840 |
// append the NGINX X-Real-IP header, if set
|
|
|
841 |
if (!empty($_SERVER['HTTP_X_REAL_IP']) && $_SERVER['HTTP_X_REAL_IP'] != $address) {
|
|
|
842 |
$remote_ip[] = 'X-Real-IP: ' . $_SERVER['HTTP_X_REAL_IP'];
|
|
|
843 |
}
|
|
|
844 |
|
|
|
845 |
// append the X-Forwarded-For header, if set
|
|
|
846 |
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
847 |
$remote_ip[] = 'X-Forwarded-For: ' . $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
848 |
}
|
|
|
849 |
|
|
|
850 |
if (!empty($remote_ip)) {
|
|
|
851 |
$address .= ' (' . implode(',', $remote_ip) . ')';
|
|
|
852 |
}
|
|
|
853 |
|
|
|
854 |
return $address;
|
|
|
855 |
}
|
|
|
856 |
|
|
|
857 |
/**
|
|
|
858 |
* Returns the real remote IP address
|
|
|
859 |
*
|
|
|
860 |
* @return string Remote IP address
|
|
|
861 |
*/
|
|
|
862 |
public static function remote_addr()
|
|
|
863 |
{
|
|
|
864 |
// Check if any of the headers are set first to improve performance
|
|
|
865 |
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !empty($_SERVER['HTTP_X_REAL_IP'])) {
|
|
|
866 |
$proxy_whitelist = (array) rcube::get_instance()->config->get('proxy_whitelist', []);
|
|
|
867 |
if (in_array($_SERVER['REMOTE_ADDR'], $proxy_whitelist)) {
|
|
|
868 |
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
869 |
foreach (array_reverse(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])) as $forwarded_ip) {
|
|
|
870 |
$forwarded_ip = trim($forwarded_ip);
|
|
|
871 |
if (!in_array($forwarded_ip, $proxy_whitelist)) {
|
|
|
872 |
return $forwarded_ip;
|
|
|
873 |
}
|
|
|
874 |
}
|
|
|
875 |
}
|
|
|
876 |
|
|
|
877 |
if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
|
|
|
878 |
return $_SERVER['HTTP_X_REAL_IP'];
|
|
|
879 |
}
|
|
|
880 |
}
|
|
|
881 |
}
|
|
|
882 |
|
|
|
883 |
if (!empty($_SERVER['REMOTE_ADDR'])) {
|
|
|
884 |
return $_SERVER['REMOTE_ADDR'];
|
|
|
885 |
}
|
|
|
886 |
|
|
|
887 |
return '';
|
|
|
888 |
}
|
|
|
889 |
|
|
|
890 |
/**
|
|
|
891 |
* Read a specific HTTP request header.
|
|
|
892 |
*
|
|
|
893 |
* @param string $name Header name
|
|
|
894 |
*
|
|
|
895 |
* @return string|null Header value or null if not available
|
|
|
896 |
*/
|
|
|
897 |
public static function request_header($name)
|
|
|
898 |
{
|
|
|
899 |
if (function_exists('apache_request_headers')) {
|
|
|
900 |
$headers = apache_request_headers();
|
|
|
901 |
$key = strtoupper($name);
|
|
|
902 |
}
|
|
|
903 |
else {
|
|
|
904 |
$headers = $_SERVER;
|
|
|
905 |
$key = 'HTTP_' . strtoupper(strtr($name, '-', '_'));
|
|
|
906 |
}
|
|
|
907 |
|
|
|
908 |
if (!empty($headers)) {
|
|
|
909 |
$headers = array_change_key_case($headers, CASE_UPPER);
|
|
|
910 |
|
|
|
911 |
return $headers[$key] ?? null;
|
|
|
912 |
}
|
|
|
913 |
}
|
|
|
914 |
|
|
|
915 |
/**
|
|
|
916 |
* Explode quoted string
|
|
|
917 |
*
|
|
|
918 |
* @param string $delimiter Delimiter expression string for preg_match()
|
|
|
919 |
* @param string $string Input string
|
|
|
920 |
*
|
|
|
921 |
* @return array String items
|
|
|
922 |
*/
|
|
|
923 |
public static function explode_quoted_string($delimiter, $string)
|
|
|
924 |
{
|
|
|
925 |
$result = [];
|
|
|
926 |
$strlen = strlen($string);
|
|
|
927 |
|
|
|
928 |
for ($q=$p=$i=0; $i < $strlen; $i++) {
|
|
|
929 |
if ($string[$i] == "\"" && (!isset($string[$i-1]) || $string[$i-1] != "\\")) {
|
|
|
930 |
$q = $q ? false : true;
|
|
|
931 |
}
|
|
|
932 |
else if (!$q && preg_match("/$delimiter/", $string[$i])) {
|
|
|
933 |
$result[] = substr($string, $p, $i - $p);
|
|
|
934 |
$p = $i + 1;
|
|
|
935 |
}
|
|
|
936 |
}
|
|
|
937 |
|
|
|
938 |
$result[] = (string) substr($string, $p);
|
|
|
939 |
|
|
|
940 |
return $result;
|
|
|
941 |
}
|
|
|
942 |
|
|
|
943 |
/**
|
|
|
944 |
* Improved equivalent to strtotime()
|
|
|
945 |
*
|
|
|
946 |
* @param string $date Date string
|
|
|
947 |
* @param DateTimeZone $timezone Timezone to use for DateTime object
|
|
|
948 |
*
|
|
|
949 |
* @return int Unix timestamp
|
|
|
950 |
*/
|
|
|
951 |
public static function strtotime($date, $timezone = null)
|
|
|
952 |
{
|
|
|
953 |
$date = self::clean_datestr($date);
|
|
|
954 |
$tzname = $timezone ? ' ' . $timezone->getName() : '';
|
|
|
955 |
|
|
|
956 |
// unix timestamp
|
|
|
957 |
if (is_numeric($date)) {
|
|
|
958 |
return (int) $date;
|
|
|
959 |
}
|
|
|
960 |
|
|
|
961 |
// It can be very slow when provided string is not a date and very long
|
|
|
962 |
if (strlen($date) > 128) {
|
|
|
963 |
$date = substr($date, 0, 128);
|
|
|
964 |
}
|
|
|
965 |
|
|
|
966 |
// if date parsing fails, we have a date in non-rfc format.
|
|
|
967 |
// remove token from the end and try again
|
|
|
968 |
while (($ts = @strtotime($date . $tzname)) === false || $ts < 0) {
|
|
|
969 |
if (($pos = strrpos($date, ' ')) === false) {
|
|
|
970 |
break;
|
|
|
971 |
}
|
|
|
972 |
|
|
|
973 |
$date = rtrim(substr($date, 0, $pos));
|
|
|
974 |
}
|
|
|
975 |
|
|
|
976 |
return (int) $ts;
|
|
|
977 |
}
|
|
|
978 |
|
|
|
979 |
/**
|
|
|
980 |
* Date parsing function that turns the given value into a DateTime object
|
|
|
981 |
*
|
|
|
982 |
* @param string $date Date string
|
|
|
983 |
* @param DateTimeZone $timezone Timezone to use for DateTime object
|
|
|
984 |
*
|
|
|
985 |
* @return DateTime|false DateTime object or False on failure
|
|
|
986 |
*/
|
|
|
987 |
public static function anytodatetime($date, $timezone = null)
|
|
|
988 |
{
|
|
|
989 |
if ($date instanceof DateTime) {
|
|
|
990 |
return $date;
|
|
|
991 |
}
|
|
|
992 |
|
|
|
993 |
$dt = false;
|
|
|
994 |
$date = self::clean_datestr($date);
|
|
|
995 |
|
|
|
996 |
// try to parse string with DateTime first
|
|
|
997 |
if (!empty($date)) {
|
|
|
998 |
try {
|
|
|
999 |
$_date = preg_match('/^[0-9]+$/', $date) ? "@$date" : $date;
|
|
|
1000 |
$dt = $timezone ? new DateTime($_date, $timezone) : new DateTime($_date);
|
|
|
1001 |
}
|
|
|
1002 |
catch (Exception $e) {
|
|
|
1003 |
// ignore
|
|
|
1004 |
}
|
|
|
1005 |
}
|
|
|
1006 |
|
|
|
1007 |
// try our advanced strtotime() method
|
|
|
1008 |
if (!$dt && ($timestamp = self::strtotime($date, $timezone))) {
|
|
|
1009 |
try {
|
|
|
1010 |
$dt = new DateTime("@".$timestamp);
|
|
|
1011 |
if ($timezone) {
|
|
|
1012 |
$dt->setTimezone($timezone);
|
|
|
1013 |
}
|
|
|
1014 |
}
|
|
|
1015 |
catch (Exception $e) {
|
|
|
1016 |
// ignore
|
|
|
1017 |
}
|
|
|
1018 |
}
|
|
|
1019 |
|
|
|
1020 |
return $dt;
|
|
|
1021 |
}
|
|
|
1022 |
|
|
|
1023 |
/**
|
|
|
1024 |
* Clean up date string for strtotime() input
|
|
|
1025 |
*
|
|
|
1026 |
* @param string $date Date string
|
|
|
1027 |
*
|
|
|
1028 |
* @return string Date string
|
|
|
1029 |
*/
|
|
|
1030 |
public static function clean_datestr($date)
|
|
|
1031 |
{
|
|
|
1032 |
$date = trim((string) $date);
|
|
|
1033 |
|
|
|
1034 |
// check for MS Outlook vCard date format YYYYMMDD
|
|
|
1035 |
if (preg_match('/^([12][90]\d\d)([01]\d)([0123]\d)$/', $date, $m)) {
|
|
|
1036 |
return sprintf('%04d-%02d-%02d 00:00:00', intval($m[1]), intval($m[2]), intval($m[3]));
|
|
|
1037 |
}
|
|
|
1038 |
|
|
|
1039 |
// Clean malformed data
|
|
|
1040 |
$date = preg_replace(
|
|
|
1041 |
[
|
|
|
1042 |
'/\(.*\)/', // remove RFC comments
|
|
|
1043 |
'/GMT\s*([+-][0-9]+)/', // support non-standard "GMTXXXX" literal
|
|
|
1044 |
'/[^a-z0-9\x20\x09:\/\.+-]/i', // remove any invalid characters
|
|
|
1045 |
'/\s*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*/i', // remove weekday names
|
|
|
1046 |
],
|
|
|
1047 |
[
|
|
|
1048 |
'',
|
|
|
1049 |
'\\1',
|
|
|
1050 |
'',
|
|
|
1051 |
'',
|
|
|
1052 |
],
|
|
|
1053 |
$date
|
|
|
1054 |
);
|
|
|
1055 |
|
|
|
1056 |
$date = trim($date);
|
|
|
1057 |
|
|
|
1058 |
// try to fix dd/mm vs. mm/dd discrepancy, we can't do more here
|
|
|
1059 |
if (preg_match('/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{4})(\s.*)?$/', $date, $m)) {
|
|
|
1060 |
$mdy = $m[2] > 12 && $m[1] <= 12;
|
|
|
1061 |
$day = $mdy ? $m[2] : $m[1];
|
|
|
1062 |
$month = $mdy ? $m[1] : $m[2];
|
|
|
1063 |
$date = sprintf('%04d-%02d-%02d%s', $m[3], $month, $day, $m[4] ?? ' 00:00:00');
|
|
|
1064 |
}
|
|
|
1065 |
// I've found that YYYY.MM.DD is recognized wrong, so here's a fix
|
|
|
1066 |
else if (preg_match('/^(\d{4})\.(\d{1,2})\.(\d{1,2})(\s.*)?$/', $date, $m)) {
|
|
|
1067 |
$date = sprintf('%04d-%02d-%02d%s', $m[1], $m[2], $m[3], $m[4] ?? ' 00:00:00');
|
|
|
1068 |
}
|
|
|
1069 |
|
|
|
1070 |
return $date;
|
|
|
1071 |
}
|
|
|
1072 |
|
|
|
1073 |
/**
|
|
|
1074 |
* Turns the given date-only string in defined format into YYYY-MM-DD format.
|
|
|
1075 |
*
|
|
|
1076 |
* Supported formats: 'Y/m/d', 'Y.m.d', 'd-m-Y', 'd/m/Y', 'd.m.Y', 'j.n.Y'
|
|
|
1077 |
*
|
|
|
1078 |
* @param string $date Date string
|
|
|
1079 |
* @param string $format Input date format
|
|
|
1080 |
*
|
|
|
1081 |
* @return string Date string in YYYY-MM-DD format, or the original string
|
|
|
1082 |
* if format is not supported
|
|
|
1083 |
*/
|
|
|
1084 |
public static function format_datestr($date, $format)
|
|
|
1085 |
{
|
|
|
1086 |
$format_items = preg_split('/[.-\/\\\\]/', $format);
|
|
|
1087 |
$date_items = preg_split('/[.-\/\\\\]/', $date);
|
|
|
1088 |
$iso_format = '%04d-%02d-%02d';
|
|
|
1089 |
|
|
|
1090 |
if (count($format_items) == 3 && count($date_items) == 3) {
|
|
|
1091 |
if ($format_items[0] == 'Y') {
|
|
|
1092 |
$date = sprintf($iso_format, $date_items[0], $date_items[1], $date_items[2]);
|
|
|
1093 |
}
|
|
|
1094 |
else if (strpos('dj', $format_items[0]) !== false) {
|
|
|
1095 |
$date = sprintf($iso_format, $date_items[2], $date_items[1], $date_items[0]);
|
|
|
1096 |
}
|
|
|
1097 |
else if (strpos('mn', $format_items[0]) !== false) {
|
|
|
1098 |
$date = sprintf($iso_format, $date_items[2], $date_items[0], $date_items[1]);
|
|
|
1099 |
}
|
|
|
1100 |
}
|
|
|
1101 |
|
|
|
1102 |
return $date;
|
|
|
1103 |
}
|
|
|
1104 |
|
|
|
1105 |
/**
|
|
|
1106 |
* Wrapper for idn_to_ascii with support for e-mail address.
|
|
|
1107 |
*
|
|
|
1108 |
* Warning: Domain names may be lowercase'd.
|
|
|
1109 |
* Warning: An empty string may be returned on invalid domain.
|
|
|
1110 |
*
|
|
|
1111 |
* @param string $str Decoded e-mail address
|
|
|
1112 |
*
|
|
|
1113 |
* @return string Encoded e-mail address
|
|
|
1114 |
*/
|
|
|
1115 |
public static function idn_to_ascii($str)
|
|
|
1116 |
{
|
|
|
1117 |
return self::idn_convert($str, true);
|
|
|
1118 |
}
|
|
|
1119 |
|
|
|
1120 |
/**
|
|
|
1121 |
* Wrapper for idn_to_utf8 with support for e-mail address
|
|
|
1122 |
*
|
|
|
1123 |
* @param string $str Decoded e-mail address
|
|
|
1124 |
*
|
|
|
1125 |
* @return string Encoded e-mail address
|
|
|
1126 |
*/
|
|
|
1127 |
public static function idn_to_utf8($str)
|
|
|
1128 |
{
|
|
|
1129 |
return self::idn_convert($str, false);
|
|
|
1130 |
}
|
|
|
1131 |
|
|
|
1132 |
/**
|
|
|
1133 |
* Convert a string to ascii or utf8 (using IDNA standard)
|
|
|
1134 |
*
|
|
|
1135 |
* @param string $input Decoded e-mail address
|
|
|
1136 |
* @param bool $is_utf Convert by idn_to_ascii if true and idn_to_utf8 if false
|
|
|
1137 |
*
|
|
|
1138 |
* @return string Encoded e-mail address
|
|
|
1139 |
*/
|
|
|
1140 |
public static function idn_convert($input, $is_utf = false)
|
|
|
1141 |
{
|
|
|
1142 |
if ($at = strpos($input, '@')) {
|
|
|
1143 |
$user = substr($input, 0, $at);
|
|
|
1144 |
$domain = substr($input, $at + 1);
|
|
|
1145 |
}
|
|
|
1146 |
else {
|
|
|
1147 |
$user = '';
|
|
|
1148 |
$domain = $input;
|
|
|
1149 |
}
|
|
|
1150 |
|
|
|
1151 |
// Note that in PHP 7.2/7.3 calling idn_to_* functions with default arguments
|
|
|
1152 |
// throws a warning, so we have to set the variant explicitly (#6075)
|
|
|
1153 |
$variant = INTL_IDNA_VARIANT_UTS46;
|
|
|
1154 |
$options = 0;
|
|
|
1155 |
|
|
|
1156 |
// Because php-intl extension lowercases domains and return false
|
|
|
1157 |
// on invalid input (#6224), we skip conversion when not needed
|
|
|
1158 |
|
|
|
1159 |
if ($is_utf) {
|
|
|
1160 |
if (preg_match('/[^\x20-\x7E]/', $domain)) {
|
|
|
1161 |
$options = IDNA_NONTRANSITIONAL_TO_ASCII;
|
|
|
1162 |
$domain = idn_to_ascii($domain, $options, $variant);
|
|
|
1163 |
}
|
|
|
1164 |
}
|
|
|
1165 |
else if (preg_match('/(^|\.)xn--/i', $domain)) {
|
|
|
1166 |
$options = IDNA_NONTRANSITIONAL_TO_UNICODE;
|
|
|
1167 |
$domain = idn_to_utf8($domain, $options, $variant);
|
|
|
1168 |
}
|
|
|
1169 |
|
|
|
1170 |
if ($domain === false) {
|
|
|
1171 |
return '';
|
|
|
1172 |
}
|
|
|
1173 |
|
|
|
1174 |
return $at ? $user . '@' . $domain : $domain;
|
|
|
1175 |
}
|
|
|
1176 |
|
|
|
1177 |
/**
|
|
|
1178 |
* Split the given string into word tokens
|
|
|
1179 |
*
|
|
|
1180 |
* @param string $str Input to tokenize
|
|
|
1181 |
* @param int $minlen Minimum length of a single token
|
|
|
1182 |
*
|
|
|
1183 |
* @return array List of tokens
|
|
|
1184 |
*/
|
|
|
1185 |
public static function tokenize_string($str, $minlen = 2)
|
|
|
1186 |
{
|
|
|
1187 |
if (!is_string($str)) {
|
|
|
1188 |
return [];
|
|
|
1189 |
}
|
|
|
1190 |
|
|
|
1191 |
$expr = ['/[\s;,"\'\/+-]+/ui', '/(\d)[-.\s]+(\d)/u'];
|
|
|
1192 |
$repl = [' ', '\\1\\2'];
|
|
|
1193 |
|
|
|
1194 |
if ($minlen > 1) {
|
|
|
1195 |
$minlen--;
|
|
|
1196 |
$expr[] = "/(^|\s+)\w{1,$minlen}(\s+|$)/u";
|
|
|
1197 |
$repl[] = ' ';
|
|
|
1198 |
}
|
|
|
1199 |
|
|
|
1200 |
$str = preg_replace($expr, $repl, $str);
|
|
|
1201 |
|
|
|
1202 |
return is_string($str) ? array_filter(explode(" ", $str)) : [];
|
|
|
1203 |
}
|
|
|
1204 |
|
|
|
1205 |
/**
|
|
|
1206 |
* Normalize the given string for fulltext search.
|
|
|
1207 |
* Currently only optimized for ISO-8859-1 and ISO-8859-2 characters; to be extended
|
|
|
1208 |
*
|
|
|
1209 |
* @param string $str Input string (UTF-8)
|
|
|
1210 |
* @param bool $as_array True to return list of words as array
|
|
|
1211 |
* @param int $minlen Minimum length of tokens
|
|
|
1212 |
*
|
|
|
1213 |
* @return string|array Normalized string or a list of normalized tokens
|
|
|
1214 |
*/
|
|
|
1215 |
public static function normalize_string($str, $as_array = false, $minlen = 2)
|
|
|
1216 |
{
|
|
|
1217 |
// replace 4-byte unicode characters with '?' character,
|
|
|
1218 |
// these are not supported in default utf-8 charset on mysql,
|
|
|
1219 |
// the chance we'd need them in searching is very low
|
|
|
1220 |
$str = preg_replace('/('
|
|
|
1221 |
. '\xF0[\x90-\xBF][\x80-\xBF]{2}'
|
|
|
1222 |
. '|[\xF1-\xF3][\x80-\xBF]{3}'
|
|
|
1223 |
. '|\xF4[\x80-\x8F][\x80-\xBF]{2}'
|
|
|
1224 |
. ')/', '?', $str);
|
|
|
1225 |
|
|
|
1226 |
// split by words
|
|
|
1227 |
$arr = self::tokenize_string($str, $minlen);
|
|
|
1228 |
|
|
|
1229 |
// detect character set
|
|
|
1230 |
if (rcube_charset::convert(rcube_charset::convert($str, 'UTF-8', 'ISO-8859-1'), 'ISO-8859-1', 'UTF-8') == $str) {
|
|
|
1231 |
// ISO-8859-1 (or ASCII)
|
|
|
1232 |
preg_match_all('/./u', 'äâàåáãæçéêëèïîìíñöôòøõóüûùúýÿ', $keys);
|
|
|
1233 |
preg_match_all('/./', 'aaaaaaaceeeeiiiinoooooouuuuyy', $values);
|
|
|
1234 |
|
|
|
1235 |
$mapping = array_combine($keys[0], $values[0]);
|
|
|
1236 |
$mapping = array_merge($mapping, ['ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u']);
|
|
|
1237 |
}
|
|
|
1238 |
else if (rcube_charset::convert(rcube_charset::convert($str, 'UTF-8', 'ISO-8859-2'), 'ISO-8859-2', 'UTF-8') == $str) {
|
|
|
1239 |
// ISO-8859-2
|
|
|
1240 |
preg_match_all('/./u', 'ąáâäćçčéęëěíîłľĺńňóôöŕřśšşťţůúűüźžżý', $keys);
|
|
|
1241 |
preg_match_all('/./', 'aaaaccceeeeiilllnnooorrsssttuuuuzzzy', $values);
|
|
|
1242 |
|
|
|
1243 |
$mapping = array_combine($keys[0], $values[0]);
|
|
|
1244 |
$mapping = array_merge($mapping, ['ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u']);
|
|
|
1245 |
}
|
|
|
1246 |
|
|
|
1247 |
foreach ($arr as $i => $part) {
|
|
|
1248 |
$part = mb_strtolower($part);
|
|
|
1249 |
|
|
|
1250 |
if (!empty($mapping)) {
|
|
|
1251 |
$part = strtr($part, $mapping);
|
|
|
1252 |
}
|
|
|
1253 |
|
|
|
1254 |
$arr[$i] = $part;
|
|
|
1255 |
}
|
|
|
1256 |
|
|
|
1257 |
return $as_array ? $arr : implode(' ', $arr);
|
|
|
1258 |
}
|
|
|
1259 |
|
|
|
1260 |
/**
|
|
|
1261 |
* Compare two strings for matching words (order not relevant)
|
|
|
1262 |
*
|
|
|
1263 |
* @param string $haystack Haystack
|
|
|
1264 |
* @param string $needle Needle
|
|
|
1265 |
*
|
|
|
1266 |
* @return bool True if match, False otherwise
|
|
|
1267 |
*/
|
|
|
1268 |
public static function words_match($haystack, $needle)
|
|
|
1269 |
{
|
|
|
1270 |
$a_needle = self::tokenize_string($needle, 1);
|
|
|
1271 |
$_haystack = implode(' ', self::tokenize_string($haystack, 1));
|
|
|
1272 |
$valid = strlen($_haystack) > 0;
|
|
|
1273 |
$hits = 0;
|
|
|
1274 |
|
|
|
1275 |
foreach ($a_needle as $w) {
|
|
|
1276 |
if ($valid) {
|
|
|
1277 |
if (stripos($_haystack, $w) !== false) {
|
|
|
1278 |
$hits++;
|
|
|
1279 |
}
|
|
|
1280 |
}
|
|
|
1281 |
else if (stripos($haystack, $w) !== false) {
|
|
|
1282 |
$hits++;
|
|
|
1283 |
}
|
|
|
1284 |
}
|
|
|
1285 |
|
|
|
1286 |
return $hits >= count($a_needle);
|
|
|
1287 |
}
|
|
|
1288 |
|
|
|
1289 |
/**
|
|
|
1290 |
* Parse commandline arguments into a hash array
|
|
|
1291 |
*
|
|
|
1292 |
* @param array $aliases Argument alias names
|
|
|
1293 |
*
|
|
|
1294 |
* @return array Argument values hash
|
|
|
1295 |
*/
|
|
|
1296 |
public static function get_opt($aliases = [])
|
|
|
1297 |
{
|
|
|
1298 |
$args = [];
|
|
|
1299 |
$bool = [];
|
|
|
1300 |
|
|
|
1301 |
// find boolean (no value) options
|
|
|
1302 |
foreach ($aliases as $key => $alias) {
|
|
|
1303 |
if ($pos = strpos($alias, ':')) {
|
|
|
1304 |
$aliases[$key] = substr($alias, 0, $pos);
|
|
|
1305 |
$bool[] = $key;
|
|
|
1306 |
$bool[] = $aliases[$key];
|
|
|
1307 |
}
|
|
|
1308 |
}
|
|
|
1309 |
|
|
|
1310 |
for ($i=1; $i < count($_SERVER['argv']); $i++) {
|
|
|
1311 |
$arg = $_SERVER['argv'][$i];
|
|
|
1312 |
$value = true;
|
|
|
1313 |
$key = null;
|
|
|
1314 |
|
|
|
1315 |
if (strlen($arg) && $arg[0] == '-') {
|
|
|
1316 |
$key = preg_replace('/^-+/', '', $arg);
|
|
|
1317 |
$sp = strpos($arg, '=');
|
|
|
1318 |
|
|
|
1319 |
if ($sp > 0) {
|
|
|
1320 |
$key = substr($key, 0, $sp - 2);
|
|
|
1321 |
$value = substr($arg, $sp+1);
|
|
|
1322 |
}
|
|
|
1323 |
else if (in_array($key, $bool)) {
|
|
|
1324 |
$value = true;
|
|
|
1325 |
}
|
|
|
1326 |
else if (
|
|
|
1327 |
isset($_SERVER['argv'][$i + 1])
|
|
|
1328 |
&& strlen($_SERVER['argv'][$i + 1])
|
|
|
1329 |
&& $_SERVER['argv'][$i + 1][0] != '-'
|
|
|
1330 |
) {
|
|
|
1331 |
$value = $_SERVER['argv'][++$i];
|
|
|
1332 |
}
|
|
|
1333 |
|
|
|
1334 |
$args[$key] = is_string($value) ? preg_replace(['/^["\']/', '/["\']$/'], '', $value) : $value;
|
|
|
1335 |
}
|
|
|
1336 |
else {
|
|
|
1337 |
$args[] = $arg;
|
|
|
1338 |
}
|
|
|
1339 |
|
|
|
1340 |
if (!empty($aliases[$key])) {
|
|
|
1341 |
$alias = $aliases[$key];
|
|
|
1342 |
$args[$alias] = $args[$key];
|
|
|
1343 |
}
|
|
|
1344 |
}
|
|
|
1345 |
|
|
|
1346 |
return $args;
|
|
|
1347 |
}
|
|
|
1348 |
|
|
|
1349 |
/**
|
|
|
1350 |
* Safe password prompt for command line
|
|
|
1351 |
* from http://blogs.sitepoint.com/2009/05/01/interactive-cli-password-prompt-in-php/
|
|
|
1352 |
*
|
|
|
1353 |
* @param string $prompt Prompt text
|
|
|
1354 |
*
|
|
|
1355 |
* @return string Password
|
|
|
1356 |
*/
|
|
|
1357 |
public static function prompt_silent($prompt = "Password:")
|
|
|
1358 |
{
|
|
|
1359 |
if (preg_match('/^win/i', PHP_OS)) {
|
|
|
1360 |
$vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
|
|
|
1361 |
$vbcontent = 'wscript.echo(InputBox("' . addslashes($prompt) . '", "", "password here"))';
|
|
|
1362 |
file_put_contents($vbscript, $vbcontent);
|
|
|
1363 |
|
|
|
1364 |
$command = "cscript //nologo " . escapeshellarg($vbscript);
|
|
|
1365 |
$password = rtrim(shell_exec($command));
|
|
|
1366 |
unlink($vbscript);
|
|
|
1367 |
|
|
|
1368 |
return $password;
|
|
|
1369 |
}
|
|
|
1370 |
|
|
|
1371 |
$command = "/usr/bin/env bash -c 'echo OK'";
|
|
|
1372 |
|
|
|
1373 |
if (rtrim(shell_exec($command)) !== 'OK') {
|
|
|
1374 |
echo $prompt;
|
|
|
1375 |
$pass = trim(fgets(STDIN));
|
|
|
1376 |
echo chr(8)."\r" . $prompt . str_repeat("*", strlen($pass))."\n";
|
|
|
1377 |
|
|
|
1378 |
return $pass;
|
|
|
1379 |
}
|
|
|
1380 |
|
|
|
1381 |
$command = "/usr/bin/env bash -c 'read -s -p \"" . addslashes($prompt) . "\" mypassword && echo \$mypassword'";
|
|
|
1382 |
$password = rtrim(shell_exec($command));
|
|
|
1383 |
echo "\n";
|
|
|
1384 |
|
|
|
1385 |
return $password;
|
|
|
1386 |
}
|
|
|
1387 |
|
|
|
1388 |
/**
|
|
|
1389 |
* Find out if the string content means true or false
|
|
|
1390 |
*
|
|
|
1391 |
* @param string $str Input value
|
|
|
1392 |
*
|
|
|
1393 |
* @return bool Boolean value
|
|
|
1394 |
*/
|
|
|
1395 |
public static function get_boolean($str)
|
|
|
1396 |
{
|
|
|
1397 |
$str = strtolower((string) $str);
|
|
|
1398 |
|
|
|
1399 |
return !in_array($str, ['false', '0', 'no', 'off', 'nein', ''], true);
|
|
|
1400 |
}
|
|
|
1401 |
|
|
|
1402 |
/**
|
|
|
1403 |
* OS-dependent absolute path detection
|
|
|
1404 |
*
|
|
|
1405 |
* @param string $path File path
|
|
|
1406 |
*
|
|
|
1407 |
* @return bool True if the path is absolute, False otherwise
|
|
|
1408 |
*/
|
|
|
1409 |
public static function is_absolute_path($path)
|
|
|
1410 |
{
|
|
|
1411 |
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
|
|
|
1412 |
return (bool) preg_match('!^[a-z]:[\\\\/]!i', $path);
|
|
|
1413 |
}
|
|
|
1414 |
|
|
|
1415 |
return isset($path[0]) && $path[0] == '/';
|
|
|
1416 |
}
|
|
|
1417 |
|
|
|
1418 |
/**
|
|
|
1419 |
* Resolve relative URL
|
|
|
1420 |
*
|
|
|
1421 |
* @param string $url Relative URL
|
|
|
1422 |
*
|
|
|
1423 |
* @return string Absolute URL
|
|
|
1424 |
*/
|
|
|
1425 |
public static function resolve_url($url)
|
|
|
1426 |
{
|
|
|
1427 |
// prepend protocol://hostname:port
|
|
|
1428 |
if (!preg_match('|^https?://|', $url)) {
|
|
|
1429 |
$schema = 'http';
|
|
|
1430 |
$default_port = 80;
|
|
|
1431 |
|
|
|
1432 |
if (self::https_check()) {
|
|
|
1433 |
$schema = 'https';
|
|
|
1434 |
$default_port = 443;
|
|
|
1435 |
}
|
|
|
1436 |
|
|
|
1437 |
$host = $_SERVER['HTTP_HOST'] ?? '';
|
|
|
1438 |
$port = $_SERVER['SERVER_PORT'] ?? 0;
|
|
|
1439 |
|
|
|
1440 |
$prefix = $schema . '://' . preg_replace('/:\d+$/', '', $host);
|
|
|
1441 |
if ($port && $port != $default_port && $port != 80) {
|
|
|
1442 |
$prefix .= ':' . $port;
|
|
|
1443 |
}
|
|
|
1444 |
|
|
|
1445 |
$url = $prefix . ($url[0] == '/' ? '' : '/') . $url;
|
|
|
1446 |
}
|
|
|
1447 |
|
|
|
1448 |
return $url;
|
|
|
1449 |
}
|
|
|
1450 |
|
|
|
1451 |
/**
|
|
|
1452 |
* Generate a random string
|
|
|
1453 |
*
|
|
|
1454 |
* @param int $length String length
|
|
|
1455 |
* @param bool $raw Return RAW data instead of ascii
|
|
|
1456 |
*
|
|
|
1457 |
* @return string The generated random string
|
|
|
1458 |
*/
|
|
|
1459 |
public static function random_bytes($length, $raw = false)
|
|
|
1460 |
{
|
|
|
1461 |
// Use PHP7 true random generator
|
|
|
1462 |
if ($raw) {
|
|
|
1463 |
return random_bytes($length);
|
|
|
1464 |
}
|
|
|
1465 |
|
|
|
1466 |
$hextab = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
1467 |
$tabsize = strlen($hextab);
|
|
|
1468 |
|
|
|
1469 |
$result = '';
|
|
|
1470 |
while ($length-- > 0) {
|
|
|
1471 |
$result .= $hextab[random_int(0, $tabsize - 1)];
|
|
|
1472 |
}
|
|
|
1473 |
|
|
|
1474 |
return $result;
|
|
|
1475 |
}
|
|
|
1476 |
|
|
|
1477 |
/**
|
|
|
1478 |
* Convert binary data into readable form (containing a-zA-Z0-9 characters)
|
|
|
1479 |
*
|
|
|
1480 |
* @param string $input Binary input
|
|
|
1481 |
*
|
|
|
1482 |
* @return string Readable output (Base62)
|
|
|
1483 |
* @deprecated since 1.3.1
|
|
|
1484 |
*/
|
|
|
1485 |
public static function bin2ascii($input)
|
|
|
1486 |
{
|
|
|
1487 |
$hextab = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
1488 |
$result = '';
|
|
|
1489 |
|
|
|
1490 |
for ($x = 0; $x < strlen($input); $x++) {
|
|
|
1491 |
$result .= $hextab[ord($input[$x]) % 62];
|
|
|
1492 |
}
|
|
|
1493 |
|
|
|
1494 |
return $result;
|
|
|
1495 |
}
|
|
|
1496 |
|
|
|
1497 |
/**
|
|
|
1498 |
* Format current date according to specified format.
|
|
|
1499 |
* This method supports microseconds (u).
|
|
|
1500 |
*
|
|
|
1501 |
* @param string $format Date format (default: 'd-M-Y H:i:s O')
|
|
|
1502 |
*
|
|
|
1503 |
* @return string Formatted date
|
|
|
1504 |
*/
|
|
|
1505 |
public static function date_format($format = null)
|
|
|
1506 |
{
|
|
|
1507 |
if (empty($format)) {
|
|
|
1508 |
$format = 'd-M-Y H:i:s O';
|
|
|
1509 |
}
|
|
|
1510 |
|
|
|
1511 |
if (strpos($format, 'u') !== false) {
|
|
|
1512 |
$dt = number_format(microtime(true), 6, '.', '');
|
|
|
1513 |
|
|
|
1514 |
try {
|
|
|
1515 |
$date = date_create_from_format('U.u', $dt);
|
|
|
1516 |
$date->setTimeZone(new DateTimeZone(date_default_timezone_get()));
|
|
|
1517 |
|
|
|
1518 |
return $date->format($format);
|
|
|
1519 |
}
|
|
|
1520 |
catch (Exception $e) {
|
|
|
1521 |
// ignore, fallback to date()
|
|
|
1522 |
}
|
|
|
1523 |
}
|
|
|
1524 |
|
|
|
1525 |
return date($format);
|
|
|
1526 |
}
|
|
|
1527 |
|
|
|
1528 |
/**
|
|
|
1529 |
* Parses socket options and returns options for specified hostname.
|
|
|
1530 |
*
|
|
|
1531 |
* @param array &$options Configured socket options
|
|
|
1532 |
* @param string $host Hostname
|
|
|
1533 |
*/
|
|
|
1534 |
public static function parse_socket_options(&$options, $host = null)
|
|
|
1535 |
{
|
|
|
1536 |
if (empty($host) || empty($options)) {
|
|
|
1537 |
return;
|
|
|
1538 |
}
|
|
|
1539 |
|
|
|
1540 |
// get rid of schema and port from the hostname
|
|
|
1541 |
$host_url = parse_url($host);
|
|
|
1542 |
if (isset($host_url['host'])) {
|
|
|
1543 |
$host = $host_url['host'];
|
|
|
1544 |
}
|
|
|
1545 |
|
|
|
1546 |
// find per-host options
|
|
|
1547 |
if ($host && array_key_exists($host, $options)) {
|
|
|
1548 |
$options = $options[$host];
|
|
|
1549 |
}
|
|
|
1550 |
}
|
|
|
1551 |
|
|
|
1552 |
/**
|
|
|
1553 |
* Get maximum upload size
|
|
|
1554 |
*
|
|
|
1555 |
* @return int Maximum size in bytes
|
|
|
1556 |
*/
|
|
|
1557 |
public static function max_upload_size()
|
|
|
1558 |
{
|
|
|
1559 |
// find max filesize value
|
|
|
1560 |
$max_filesize = parse_bytes(ini_get('upload_max_filesize'));
|
|
|
1561 |
$max_postsize = parse_bytes(ini_get('post_max_size'));
|
|
|
1562 |
|
|
|
1563 |
if ($max_postsize && $max_postsize < $max_filesize) {
|
|
|
1564 |
$max_filesize = $max_postsize;
|
|
|
1565 |
}
|
|
|
1566 |
|
|
|
1567 |
return $max_filesize;
|
|
|
1568 |
}
|
|
|
1569 |
|
|
|
1570 |
/**
|
|
|
1571 |
* Detect and log last PREG operation error
|
|
|
1572 |
*
|
|
|
1573 |
* @param array $error Error data (line, file, code, message)
|
|
|
1574 |
* @param bool $terminate Stop script execution
|
|
|
1575 |
*
|
|
|
1576 |
* @return bool True on error, False otherwise
|
|
|
1577 |
*/
|
|
|
1578 |
public static function preg_error($error = [], $terminate = false)
|
|
|
1579 |
{
|
|
|
1580 |
if (($preg_error = preg_last_error()) != PREG_NO_ERROR) {
|
|
|
1581 |
$errstr = "PCRE Error: $preg_error.";
|
|
|
1582 |
|
|
|
1583 |
if (function_exists('preg_last_error_msg')) {
|
|
|
1584 |
$errstr .= ' ' . preg_last_error_msg();
|
|
|
1585 |
}
|
|
|
1586 |
|
|
|
1587 |
if ($preg_error == PREG_BACKTRACK_LIMIT_ERROR) {
|
|
|
1588 |
$errstr .= " Consider raising pcre.backtrack_limit!";
|
|
|
1589 |
}
|
|
|
1590 |
if ($preg_error == PREG_RECURSION_LIMIT_ERROR) {
|
|
|
1591 |
$errstr .= " Consider raising pcre.recursion_limit!";
|
|
|
1592 |
}
|
|
|
1593 |
|
|
|
1594 |
$error = array_merge(['code' => 620, 'line' => __LINE__, 'file' => __FILE__], $error);
|
|
|
1595 |
|
|
|
1596 |
if (!empty($error['message'])) {
|
|
|
1597 |
$error['message'] .= ' ' . $errstr;
|
|
|
1598 |
}
|
|
|
1599 |
else {
|
|
|
1600 |
$error['message'] = $errstr;
|
|
|
1601 |
}
|
|
|
1602 |
|
|
|
1603 |
rcube::raise_error($error, true, $terminate);
|
|
|
1604 |
|
|
|
1605 |
return true;
|
|
|
1606 |
}
|
|
|
1607 |
|
|
|
1608 |
return false;
|
|
|
1609 |
}
|
|
|
1610 |
|
|
|
1611 |
/**
|
|
|
1612 |
* Generate a temporary file path in the Roundcube temp directory
|
|
|
1613 |
*
|
|
|
1614 |
* @param string $file_name String identifier for the type of temp file
|
|
|
1615 |
* @param bool $unique Generate unique file names based on $file_name
|
|
|
1616 |
* @param bool $create Create the temp file or not
|
|
|
1617 |
*
|
|
|
1618 |
* @return string temporary file path
|
|
|
1619 |
*/
|
|
|
1620 |
public static function temp_filename($file_name, $unique = true, $create = true)
|
|
|
1621 |
{
|
|
|
1622 |
$temp_dir = rcube::get_instance()->config->get('temp_dir');
|
|
|
1623 |
|
|
|
1624 |
// Fall back to system temp dir if configured dir is not writable
|
|
|
1625 |
if (!is_writable($temp_dir)) {
|
|
|
1626 |
$temp_dir = sys_get_temp_dir();
|
|
|
1627 |
}
|
|
|
1628 |
|
|
|
1629 |
// On Windows tempnam() uses only the first three characters of prefix so use uniqid() and manually add the prefix
|
|
|
1630 |
// Full prefix is required for garbage collection to recognise the file
|
|
|
1631 |
$temp_file = $unique ? str_replace('.', '', uniqid($file_name, true)) : $file_name;
|
|
|
1632 |
$temp_path = unslashify($temp_dir) . '/' . RCUBE_TEMP_FILE_PREFIX . $temp_file;
|
|
|
1633 |
|
|
|
1634 |
// Sanity check for unique file name
|
|
|
1635 |
if ($unique && file_exists($temp_path)) {
|
|
|
1636 |
return self::temp_filename($file_name, $unique, $create);
|
|
|
1637 |
}
|
|
|
1638 |
|
|
|
1639 |
// Create the file to prevent possible race condition like tempnam() does
|
|
|
1640 |
if ($create) {
|
|
|
1641 |
touch($temp_path);
|
|
|
1642 |
}
|
|
|
1643 |
|
|
|
1644 |
return $temp_path;
|
|
|
1645 |
}
|
|
|
1646 |
|
|
|
1647 |
/**
|
|
|
1648 |
* Clean the subject from reply and forward prefix
|
|
|
1649 |
*
|
|
|
1650 |
* @param string $subject Subject to clean
|
|
|
1651 |
* @param string $mode Mode of cleaning : reply, forward or both
|
|
|
1652 |
*
|
|
|
1653 |
* @return string Cleaned subject
|
|
|
1654 |
*/
|
|
|
1655 |
public static function remove_subject_prefix($subject, $mode = 'both')
|
|
|
1656 |
{
|
|
|
1657 |
$config = rcmail::get_instance()->config;
|
|
|
1658 |
|
|
|
1659 |
// Clean subject prefix for reply, forward or both
|
|
|
1660 |
if ($mode == 'both') {
|
|
|
1661 |
$reply_prefixes = $config->get('subject_reply_prefixes', ['Re:']);
|
|
|
1662 |
$forward_prefixes = $config->get('subject_forward_prefixes', ['Fwd:', 'Fw:']);
|
|
|
1663 |
$prefixes = array_merge($reply_prefixes, $forward_prefixes);
|
|
|
1664 |
}
|
|
|
1665 |
else if ($mode == 'reply') {
|
|
|
1666 |
$prefixes = $config->get('subject_reply_prefixes', ['Re:']);
|
|
|
1667 |
// replace (was: ...) (#1489375)
|
|
|
1668 |
$subject = preg_replace('/\s*\([wW]as:[^\)]+\)\s*$/', '', $subject);
|
|
|
1669 |
}
|
|
|
1670 |
else if ($mode == 'forward') {
|
|
|
1671 |
$prefixes = $config->get('subject_forward_prefixes', ['Fwd:', 'Fw:']);
|
|
|
1672 |
}
|
|
|
1673 |
|
|
|
1674 |
// replace Re:, Re[x]:, Re-x (#1490497)
|
|
|
1675 |
$pieces = array_map(function($prefix) {
|
|
|
1676 |
$prefix = strtolower(str_replace(':', '', $prefix));
|
|
|
1677 |
return "$prefix:|$prefix\[\d\]:|$prefix-\d:";
|
|
|
1678 |
}, $prefixes);
|
|
|
1679 |
$pattern = '/^('.implode('|', $pieces).')\s*/i';
|
|
|
1680 |
do {
|
|
|
1681 |
$subject = preg_replace($pattern, '', $subject, -1, $count);
|
|
|
1682 |
}
|
|
|
1683 |
while ($count);
|
|
|
1684 |
|
|
|
1685 |
return trim($subject);
|
|
|
1686 |
}
|
|
|
1687 |
}
|