Aleksander Machniak
2016-01-16 3f6fbdcc6d2ed2e5ad17f9ae4f7d2f4bb8a3104b
commit | author | age
9db0c8 1 <?php
1aceb9 2
A 3 /*
4  +-----------------------------------------------------------------------+
5  | This file is part of the Roundcube Webmail client                     |
6  | Copyright (C) 2008-2012, The Roundcube Dev Team                       |
7  | Copyright (C) 2011-2012, Kolab Systems AG                             |
8  |                                                                       |
9  | Licensed under the GNU General Public License version 3 or            |
10  | any later version with exceptions for skins & plugins.                |
11  | See the README file for a full license statement.                     |
12  |                                                                       |
13  | PURPOSE:                                                              |
14  |   Utility class providing common functions                            |
15  +-----------------------------------------------------------------------+
16  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17  | Author: Aleksander Machniak <alec@alec.pl>                            |
18  +-----------------------------------------------------------------------+
19 */
20
21 /**
22  * Utility class providing common functions
23  *
9ab346 24  * @package    Framework
AM 25  * @subpackage Utils
1aceb9 26  */
A 27 class rcube_utils
28 {
29     // define constants for input reading
30     const INPUT_GET  = 0x0101;
31     const INPUT_POST = 0x0102;
32     const INPUT_GPC  = 0x0103;
33
34     /**
35      * Helper method to set a cookie with the current path and host settings
36      *
37      * @param string Cookie name
38      * @param string Cookie value
39      * @param string Expiration time
40      */
41     public static function setcookie($name, $value, $exp = 0)
42     {
43         if (headers_sent()) {
44             return;
45         }
46
47         $cookie = session_get_cookie_params();
09e5fc 48         $secure = $cookie['secure'] || self::https_check();
1aceb9 49
A 50         setcookie($name, $value, $exp, $cookie['path'], $cookie['domain'], $secure, true);
51     }
52
53     /**
54      * E-mail address validation.
55      *
56      * @param string $email Email address
57      * @param boolean $dns_check True to check dns
58      *
59      * @return boolean True on success, False if address is invalid
60      */
61     public static function check_email($email, $dns_check=true)
62     {
63         // Check for invalid characters
64         if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $email)) {
65             return false;
66         }
67
68         // Check for length limit specified by RFC 5321 (#1486453)
69         if (strlen($email) > 254) {
70             return false;
71         }
72
73         $email_array = explode('@', $email);
74
75         // Check that there's one @ symbol
76         if (count($email_array) < 2) {
77             return false;
78         }
79
80         $domain_part = array_pop($email_array);
81         $local_part  = implode('@', $email_array);
82
83         // from PEAR::Validate
84         $regexp = '&^(?:
413df0 85             ("\s*(?:[^"\f\n\r\t\v\b\s]+\s*)+")|                             #1 quoted name
AM 86             ([-\w!\#\$%\&\'*+~/^`|{}=]+(?:\.[-\w!\#\$%\&\'*+~/^`|{}=]+)*))  #2 OR dot-atom (RFC5322)
87             $&xi';
1aceb9 88
A 89         if (!preg_match($regexp, $local_part)) {
90             return false;
91         }
92
da2812 93         // Validate domain part
AM 94         if (preg_match('/^\[((IPv6:[0-9a-f:.]+)|([0-9.]+))\]$/i', $domain_part, $matches)) {
a65ce5 95             return self::check_ip(preg_replace('/^IPv6:/i', '', $matches[1])); // valid IPv4 or IPv6 address
1aceb9 96         }
A 97         else {
98             // If not an IP address
99             $domain_array = explode('.', $domain_part);
100             // Not enough parts to be a valid domain
101             if (sizeof($domain_array) < 2) {
102                 return false;
103             }
104
105             foreach ($domain_array as $part) {
848e20 106                 if (!preg_match('/^((xn--)?([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/', $part)) {
1aceb9 107                     return false;
A 108                 }
109             }
110
c83b83 111             // last domain part
848e20 112             $last_part = array_pop($domain_array);
AM 113             if (strpos($last_part, 'xn--') !== 0 && preg_match('/[^a-zA-Z]/', $last_part)) {
c83b83 114                 return false;
1aceb9 115             }
A 116
117             $rcube = rcube::get_instance();
118
119             if (!$dns_check || !$rcube->config->get('email_dns_check')) {
120                 return true;
121             }
122
123             // find MX record(s)
9ff345 124             if (!function_exists('getmxrr') || getmxrr($domain_part, $mx_records)) {
1aceb9 125                 return true;
A 126             }
127
128             // find any DNS record
9ff345 129             if (!function_exists('checkdnsrr') || checkdnsrr($domain_part, 'ANY')) {
1aceb9 130                 return true;
A 131             }
132         }
133
134         return false;
135     }
136
da2812 137
AM 138     /**
139      * Validates IPv4 or IPv6 address
140      *
141      * @param string $ip IP address in v4 or v6 format
142      *
143      * @return bool True if the address is valid
144      */
a65ce5 145     public static function check_ip($ip)
da2812 146     {
AM 147         // IPv6, but there's no build-in IPv6 support
148         if (strpos($ip, ':') !== false && !defined('AF_INET6')) {
293a57 149             $parts = explode(':', $ip);
da2812 150             $count = count($parts);
AM 151
152             if ($count > 8 || $count < 2) {
153                 return false;
154             }
155
156             foreach ($parts as $idx => $part) {
157                 $length = strlen($part);
158                 if (!$length) {
159                     // there can be only one ::
160                     if ($found_empty) {
161                         return false;
162                     }
163                     $found_empty = true;
164                 }
165                 // last part can be an IPv4 address
166                 else if ($idx == $count - 1) {
167                     if (!preg_match('/^[0-9a-f]{1,4}$/i', $part)) {
168                         return @inet_pton($part) !== false;
169                     }
170                 }
171                 else if (!preg_match('/^[0-9a-f]{1,4}$/i', $part)) {
172                     return false;
173                 }
174             }
175
176             return true;
177         }
178
179         return @inet_pton($ip) !== false;
180     }
181
182
1aceb9 183     /**
A 184      * Check whether the HTTP referer matches the current request
185      *
186      * @return boolean True if referer is the same host+path, false if not
187      */
188     public static function check_referer()
189     {
190         $uri = parse_url($_SERVER['REQUEST_URI']);
be71ab 191         $referer = parse_url(self::request_header('Referer'));
AM 192         return $referer['host'] == self::request_header('Host') && $referer['path'] == $uri['path'];
1aceb9 193     }
A 194
195
196     /**
197      * Replacing specials characters to a specific encoding type
198      *
199      * @param  string  Input string
200      * @param  string  Encoding type: text|html|xml|js|url
201      * @param  string  Replace mode for tags: show|replace|remove
202      * @param  boolean Convert newlines
203      *
204      * @return string  The quoted string
205      */
206     public static function rep_specialchars_output($str, $enctype = '', $mode = '', $newlines = true)
207     {
208         static $html_encode_arr = false;
209         static $js_rep_table = false;
210         static $xml_rep_table = false;
211
fa4bf4 212         if (!is_string($str)) {
AM 213             $str = strval($str);
214         }
215
1aceb9 216         // encode for HTML output
A 217         if ($enctype == 'html') {
218             if (!$html_encode_arr) {
219                 $html_encode_arr = get_html_translation_table(HTML_SPECIALCHARS);
220                 unset($html_encode_arr['?']);
221             }
222
223             $encode_arr = $html_encode_arr;
224
225             // don't replace quotes and html tags
226             if ($mode == 'show' || $mode == '') {
227                 $ltpos = strpos($str, '<');
228                 if ($ltpos !== false && strpos($str, '>', $ltpos) !== false) {
229                     unset($encode_arr['"']);
230                     unset($encode_arr['<']);
231                     unset($encode_arr['>']);
232                     unset($encode_arr['&']);
233                 }
234             }
235             else if ($mode == 'remove') {
236                 $str = strip_tags($str);
237             }
238
239             $out = strtr($str, $encode_arr);
240
241             return $newlines ? nl2br($out) : $out;
242         }
243
244         // if the replace tables for XML and JS are not yet defined
245         if ($js_rep_table === false) {
246             $js_rep_table = $xml_rep_table = array();
247             $xml_rep_table['&'] = '&amp;';
248
249             // can be increased to support more charsets
250             for ($c=160; $c<256; $c++) {
251                 $xml_rep_table[chr($c)] = "&#$c;";
252             }
253
254             $xml_rep_table['"'] = '&quot;';
255             $js_rep_table['"']  = '\\"';
256             $js_rep_table["'"]  = "\\'";
257             $js_rep_table["\\"] = "\\\\";
258             // Unicode line and paragraph separators (#1486310)
259             $js_rep_table[chr(hexdec(E2)).chr(hexdec(80)).chr(hexdec(A8))] = '&#8232;';
260             $js_rep_table[chr(hexdec(E2)).chr(hexdec(80)).chr(hexdec(A9))] = '&#8233;';
261         }
262
263         // encode for javascript use
264         if ($enctype == 'js') {
265             return preg_replace(array("/\r?\n/", "/\r/", '/<\\//'), array('\n', '\n', '<\\/'), strtr($str, $js_rep_table));
266         }
267
268         // encode for plaintext
269         if ($enctype == 'text') {
270             return str_replace("\r\n", "\n", $mode=='remove' ? strip_tags($str) : $str);
271         }
272
273         if ($enctype == 'url') {
274             return rawurlencode($str);
275         }
276
277         // encode for XML
278         if ($enctype == 'xml') {
279             return strtr($str, $xml_rep_table);
280         }
281
282         // no encoding given -> return original string
283         return $str;
284     }
285
286
287     /**
288      * Read input value and convert it for internal use
289      * Performs stripslashes() and charset conversion if necessary
290      *
291      * @param  string   Field name to read
292      * @param  int      Source to get value from (GPC)
293      * @param  boolean  Allow HTML tags in field value
294      * @param  string   Charset to convert into
295      *
296      * @return string   Field value or NULL if not available
297      */
298     public static function get_input_value($fname, $source, $allow_html=FALSE, $charset=NULL)
299     {
300         $value = NULL;
301
302         if ($source == self::INPUT_GET) {
303             if (isset($_GET[$fname])) {
304                 $value = $_GET[$fname];
305             }
306         }
307         else if ($source == self::INPUT_POST) {
308             if (isset($_POST[$fname])) {
309                 $value = $_POST[$fname];
310             }
311         }
312         else if ($source == self::INPUT_GPC) {
313             if (isset($_POST[$fname])) {
314                 $value = $_POST[$fname];
315             }
316             else if (isset($_GET[$fname])) {
317                 $value = $_GET[$fname];
318             }
319             else if (isset($_COOKIE[$fname])) {
320                 $value = $_COOKIE[$fname];
321             }
322         }
323
324         return self::parse_input_value($value, $allow_html, $charset);
325     }
326
327
328     /**
329      * Parse/validate input value. See self::get_input_value()
330      * Performs stripslashes() and charset conversion if necessary
331      *
332      * @param  string   Input value
333      * @param  boolean  Allow HTML tags in field value
334      * @param  string   Charset to convert into
335      *
336      * @return string   Parsed value
337      */
338     public static function parse_input_value($value, $allow_html=FALSE, $charset=NULL)
339     {
340         global $OUTPUT;
341
342         if (empty($value)) {
343             return $value;
344         }
345
346         if (is_array($value)) {
347             foreach ($value as $idx => $val) {
348                 $value[$idx] = self::parse_input_value($val, $allow_html, $charset);
349             }
350             return $value;
351         }
352
353         // strip slashes if magic_quotes enabled
39b905 354         if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) {
1aceb9 355             $value = stripslashes($value);
A 356         }
357
358         // remove HTML tags if not allowed
359         if (!$allow_html) {
360             $value = strip_tags($value);
361         }
362
363         $output_charset = is_object($OUTPUT) ? $OUTPUT->get_charset() : null;
364
365         // remove invalid characters (#1488124)
366         if ($output_charset == 'UTF-8') {
367             $value = rcube_charset::clean($value);
368         }
369
370         // convert to internal charset
371         if ($charset && $output_charset) {
372             $value = rcube_charset::convert($value, $output_charset, $charset);
373         }
374
375         return $value;
376     }
377
378
379     /**
380      * Convert array of request parameters (prefixed with _)
381      * to a regular array with non-prefixed keys.
382      *
eafd5b 383      * @param int     $mode       Source to get value from (GPC)
AM 384      * @param string  $ignore     PCRE expression to skip parameters by name
385      * @param boolean $allow_html Allow HTML tags in field value
1aceb9 386      *
A 387      * @return array Hash array with all request parameters
388      */
eafd5b 389     public static function request2param($mode = null, $ignore = 'task|action', $allow_html = false)
1aceb9 390     {
A 391         $out = array();
392         $src = $mode == self::INPUT_GET ? $_GET : ($mode == self::INPUT_POST ? $_POST : $_REQUEST);
393
3725cf 394         foreach (array_keys($src) as $key) {
1aceb9 395             $fname = $key[0] == '_' ? substr($key, 1) : $key;
A 396             if ($ignore && !preg_match('/^(' . $ignore . ')$/', $fname)) {
eafd5b 397                 $out[$fname] = self::get_input_value($key, $mode, $allow_html);
1aceb9 398             }
A 399         }
400
401         return $out;
402     }
403
404
405     /**
406      * Convert the given string into a valid HTML identifier
407      * Same functionality as done in app.js with rcube_webmail.html_identifier()
408      */
409     public static function html_identifier($str, $encode=false)
410     {
411         if ($encode) {
412             return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
413         }
414         else {
415             return asciiwords($str, true, '_');
416         }
417     }
418
419
420     /**
421      * Replace all css definitions with #container [def]
422      * and remove css-inlined scripting
423      *
424      * @param string CSS source code
425      * @param string Container ID to use as prefix
426      *
427      * @return string Modified CSS source
428      */
429     public static function mod_css_styles($source, $container_id, $allow_remote=false)
430     {
431         $last_pos = 0;
432         $replacements = new rcube_string_replacer;
433
434         // ignore the whole block if evil styles are detected
435         $source   = self::xss_entity_decode($source);
436         $stripped = preg_replace('/[^a-z\(:;]/i', '', $source);
437         $evilexpr = 'expression|behavior|javascript:|import[^a]' . (!$allow_remote ? '|url\(' : '');
d1abd8 438
1aceb9 439         if (preg_match("/$evilexpr/i", $stripped)) {
A 440             return '/* evil! */';
441         }
442
d1abd8 443         $strict_url_regexp = '!url\s*\([ "\'](https?:)//[a-z0-9/._+-]+["\' ]\)!Uims';
AM 444
1aceb9 445         // cut out all contents between { and }
A 446         while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos))) {
fdb30f 447             $nested = strpos($source, '{', $pos+1);
TB 448             if ($nested && $nested < $pos2)  // when dealing with nested blocks (e.g. @media), take the inner one
449                 $pos = $nested;
ff6de9 450             $length = $pos2 - $pos - 1;
AM 451             $styles = substr($source, $pos+1, $length);
1aceb9 452
A 453             // check every line of a style block...
454             if ($allow_remote) {
455                 $a_styles = preg_split('/;[\r\n]*/', $styles, -1, PREG_SPLIT_NO_EMPTY);
d1abd8 456
1aceb9 457                 foreach ($a_styles as $line) {
A 458                     $stripped = preg_replace('/[^a-z\(:;]/i', '', $line);
459                     // ... and only allow strict url() values
d1abd8 460                     if (stripos($stripped, 'url(') && !preg_match($strict_url_regexp, $line)) {
1aceb9 461                         $a_styles = array('/* evil! */');
A 462                         break;
463                     }
464                 }
d1abd8 465
1aceb9 466                 $styles = join(";\n", $a_styles);
A 467             }
468
d1abd8 469             $key      = $replacements->add($styles);
AM 470             $repl     = $replacements->get_replacement($key);
471             $source   = substr_replace($source, $repl, $pos+1, $length);
472             $last_pos = $pos2 - ($length - strlen($repl));
1aceb9 473         }
A 474
475         // remove html comments and add #container to each tag selector.
476         // also replace body definition because we also stripped off the <body> tag
af79a7 477         $source = preg_replace(
1aceb9 478             array(
af79a7 479                 '/(^\s*<\!--)|(-->\s*$)/m',
1aceb9 480                 '/(^\s*|,\s*|\}\s*)([a-z0-9\._#\*][a-z0-9\.\-_]*)/im',
A 481                 '/'.preg_quote($container_id, '/').'\s+body/i',
482             ),
483             array(
484                 '',
485                 "\\1#$container_id \\2",
486                 $container_id,
487             ),
488             $source);
489
490         // put block contents back in
af79a7 491         $source = $replacements->resolve($source);
1aceb9 492
af79a7 493         return $source;
1aceb9 494     }
A 495
496
497     /**
498      * Generate CSS classes from mimetype and filename extension
499      *
500      * @param string $mimetype  Mimetype
501      * @param string $filename  Filename
502      *
503      * @return string CSS classes separated by space
504      */
505     public static function file2class($mimetype, $filename)
506     {
fe0f1d 507         $mimetype = strtolower($mimetype);
AM 508         $filename = strtolower($filename);
509
1aceb9 510         list($primary, $secondary) = explode('/', $mimetype);
A 511
512         $classes = array($primary ? $primary : 'unknown');
fe0f1d 513
1aceb9 514         if ($secondary) {
A 515             $classes[] = $secondary;
516         }
fe0f1d 517
AM 518         if (preg_match('/\.([a-z0-9]+)$/', $filename, $m)) {
519             if (!in_array($m[1], $classes)) {
520                 $classes[] = $m[1];
521             }
1aceb9 522         }
A 523
fe0f1d 524         return join(" ", $classes);
1aceb9 525     }
A 526
527
528     /**
529      * Decode escaped entities used by known XSS exploits.
530      * See http://downloads.securityfocus.com/vulnerabilities/exploits/26800.eml for examples
531      *
532      * @param string CSS content to decode
533      *
534      * @return string Decoded string
535      */
536     public static function xss_entity_decode($content)
537     {
538         $out = html_entity_decode(html_entity_decode($content));
539         $out = preg_replace_callback('/\\\([0-9a-f]{4})/i',
540             array(self, 'xss_entity_decode_callback'), $out);
541         $out = preg_replace('#/\*.*\*/#Ums', '', $out);
542
543         return $out;
544     }
545
546
547     /**
548      * preg_replace_callback callback for xss_entity_decode
549      *
550      * @param array $matches Result from preg_replace_callback
551      *
552      * @return string Decoded entity
553      */
554     public static function xss_entity_decode_callback($matches)
555     {
556         return chr(hexdec($matches[1]));
557     }
558
559
560     /**
561      * Check if we can process not exceeding memory_limit
562      *
563      * @param integer Required amount of memory
564      *
565      * @return boolean True if memory won't be exceeded, False otherwise
566      */
567     public static function mem_check($need)
568     {
569         $mem_limit = parse_bytes(ini_get('memory_limit'));
570         $memory    = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
571
572         return $mem_limit > 0 && $memory + $need > $mem_limit ? false : true;
573     }
574
575
576     /**
577      * Check if working in SSL mode
578      *
579      * @param integer $port      HTTPS port number
580      * @param boolean $use_https Enables 'use_https' option checking
581      *
582      * @return boolean
583      */
584     public static function https_check($port=null, $use_https=true)
585     {
586         if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') {
587             return true;
588         }
ef721f 589         if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])
FE 590             && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https'
591             && in_array($_SERVER['REMOTE_ADDR'], rcube::get_instance()->config->get('proxy_whitelist', array()))) {
1aceb9 592             return true;
A 593         }
594         if ($port && $_SERVER['SERVER_PORT'] == $port) {
595             return true;
596         }
30e6b9 597         if ($use_https && rcube::get_instance()->config->get('use_https')) {
1aceb9 598             return true;
A 599         }
600
601         return false;
602     }
603
604
605     /**
606      * Replaces hostname variables.
607      *
608      * @param string $name Hostname
609      * @param string $host Optional IMAP hostname
610      *
611      * @return string Hostname
612      */
613     public static function parse_host($name, $host = '')
614     {
f6d23a 615         if (!is_string($name)) {
AM 616             return $name;
617         }
618
1aceb9 619         // %n - host
A 620         $n = preg_replace('/:\d+$/', '', $_SERVER['SERVER_NAME']);
d359dc 621         // %t - host name without first part, e.g. %n=mail.domain.tld, %t=domain.tld
a13035 622         $t = preg_replace('/^[^\.]+\./', '', $n);
TB 623         // %d - domain name without first part
d359dc 624         $d = preg_replace('/^[^\.]+\./', '', $_SERVER['HTTP_HOST']);
1aceb9 625         // %h - IMAP host
A 626         $h = $_SESSION['storage_host'] ? $_SESSION['storage_host'] : $host;
627         // %z - IMAP domain without first part, e.g. %h=imap.domain.tld, %z=domain.tld
628         $z = preg_replace('/^[^\.]+\./', '', $h);
629         // %s - domain name after the '@' from e-mail address provided at login screen. Returns FALSE if an invalid email is provided
630         if (strpos($name, '%s') !== false) {
631             $user_email = self::get_input_value('_user', self::INPUT_POST);
a13035 632             $user_email = self::idn_convert($user_email, true);
1aceb9 633             $matches    = preg_match('/(.*)@([a-z0-9\.\-\[\]\:]+)/i', $user_email, $s);
A 634             if ($matches < 1 || filter_var($s[1]."@".$s[2], FILTER_VALIDATE_EMAIL) === false) {
635                 return false;
636             }
637         }
638
f6d23a 639         return str_replace(array('%n', '%t', '%d', '%h', '%z', '%s'), array($n, $t, $d, $h, $z, $s[2]), $name);
1aceb9 640     }
A 641
642
643     /**
644      * Returns remote IP address and forwarded addresses if found
645      *
646      * @return string Remote IP address(es)
647      */
648     public static function remote_ip()
649     {
650         $address = $_SERVER['REMOTE_ADDR'];
651
652         // append the NGINX X-Real-IP header, if set
653         if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
654             $remote_ip[] = 'X-Real-IP: ' . $_SERVER['HTTP_X_REAL_IP'];
655         }
656         // append the X-Forwarded-For header, if set
657         if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
658             $remote_ip[] = 'X-Forwarded-For: ' . $_SERVER['HTTP_X_FORWARDED_FOR'];
659         }
660
661         if (!empty($remote_ip)) {
662             $address .= '(' . implode(',', $remote_ip) . ')';
663         }
664
665         return $address;
666     }
667
668
669     /**
4d480b 670      * Returns the real remote IP address
TB 671      *
672      * @return string Remote IP address
673      */
674     public static function remote_addr()
675     {
ef721f 676         // Check if any of the headers are set first to improve performance
FE 677         if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !empty($_SERVER['HTTP_X_REAL_IP'])) {
678             $proxy_whitelist = rcube::get_instance()->config->get('proxy_whitelist', array());
679             if (in_array($_SERVER['REMOTE_ADDR'], $proxy_whitelist)) {
680                 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
681                     foreach(array_reverse(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])) as $forwarded_ip) {
682                         if (!in_array($forwarded_ip, $proxy_whitelist)) {
683                             return $forwarded_ip;
684                         }
685                     }
686                 }
a520f3 687
ef721f 688                 if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
FE 689                     return $_SERVER['HTTP_X_REAL_IP'];
690                 }
691             }
a520f3 692         }
AM 693
694         if (!empty($_SERVER['REMOTE_ADDR'])) {
695             return $_SERVER['REMOTE_ADDR'];
4d480b 696         }
TB 697
698         return '';
699     }
700
701     /**
1aceb9 702      * Read a specific HTTP request header.
A 703      *
704      * @param  string $name Header name
705      *
706      * @return mixed  Header value or null if not available
707      */
708     public static function request_header($name)
709     {
710         if (function_exists('getallheaders')) {
711             $hdrs = array_change_key_case(getallheaders(), CASE_UPPER);
712             $key  = strtoupper($name);
713         }
714         else {
715             $key  = 'HTTP_' . strtoupper(strtr($name, '-', '_'));
716             $hdrs = array_change_key_case($_SERVER, CASE_UPPER);
717         }
718
719         return $hdrs[$key];
720     }
721
722     /**
723      * Explode quoted string
724      *
725      * @param string Delimiter expression string for preg_match()
726      * @param string Input string
727      *
728      * @return array String items
729      */
730     public static function explode_quoted_string($delimiter, $string)
731     {
732         $result = array();
733         $strlen = strlen($string);
734
735         for ($q=$p=$i=0; $i < $strlen; $i++) {
736             if ($string[$i] == "\"" && $string[$i-1] != "\\") {
737                 $q = $q ? false : true;
738             }
739             else if (!$q && preg_match("/$delimiter/", $string[$i])) {
740                 $result[] = substr($string, $p, $i - $p);
741                 $p = $i + 1;
742             }
743         }
744
3a54cc 745         $result[] = (string) substr($string, $p);
1aceb9 746
A 747         return $result;
748     }
749
750
751     /**
752      * Improved equivalent to strtotime()
753      *
754      * @param string $date  Date string
09c58d 755      * @param object DateTimeZone to use for DateTime object
1aceb9 756      *
A 757      * @return int Unix timestamp
758      */
09c58d 759     public static function strtotime($date, $timezone = null)
1aceb9 760     {
b1f3c3 761         $date = self::clean_datestr($date);
09c58d 762         $tzname = $timezone ? ' ' . $timezone->getName() : '';
b32fab 763
AM 764         // unix timestamp
765         if (is_numeric($date)) {
896e2b 766             return (int) $date;
1aceb9 767         }
A 768
769         // if date parsing fails, we have a date in non-rfc format.
770         // remove token from the end and try again
09c58d 771         while ((($ts = @strtotime($date . $tzname)) === false) || ($ts < 0)) {
1aceb9 772             $d = explode(' ', $date);
A 773             array_pop($d);
774             if (!$d) {
775                 break;
776             }
777             $date = implode(' ', $d);
778         }
779
896e2b 780         return (int) $ts;
1aceb9 781     }
A 782
52830e 783     /**
TB 784      * Date parsing function that turns the given value into a DateTime object
785      *
786      * @param string $date  Date string
09c58d 787      * @param object DateTimeZone to use for DateTime object
52830e 788      *
TB 789      * @return object DateTime instance or false on failure
790      */
cc8502 791     public static function anytodatetime($date, $timezone = null)
52830e 792     {
TB 793         if (is_object($date) && is_a($date, 'DateTime')) {
794             return $date;
795         }
796
b1f3c3 797         $dt   = false;
AM 798         $date = self::clean_datestr($date);
52830e 799
TB 800         // try to parse string with DateTime first
801         if (!empty($date)) {
802             try {
787a42 803                 $dt = $timezone ? new DateTime($date, $timezone) : new DateTime($date);
52830e 804             }
TB 805             catch (Exception $e) {
806                 // ignore
807             }
808         }
809
810         // try our advanced strtotime() method
09c58d 811         if (!$dt && ($timestamp = self::strtotime($date, $timezone))) {
52830e 812             try {
TB 813                 $dt = new DateTime("@".$timestamp);
09c58d 814                 if ($timezone) {
TB 815                     $dt->setTimezone($timezone);
816                 }
52830e 817             }
TB 818             catch (Exception $e) {
819                 // ignore
820             }
821         }
822
823         return $dt;
824     }
1aceb9 825
b1f3c3 826     /**
AM 827      * Clean up date string for strtotime() input
828      *
829      * @param string $date Date string
830      *
831      * @return string Date string
832      */
833     public static function clean_datestr($date)
834     {
835         $date = trim($date);
836
837         // check for MS Outlook vCard date format YYYYMMDD
838         if (preg_match('/^([12][90]\d\d)([01]\d)([0123]\d)$/', $date, $m)) {
839             return sprintf('%04d-%02d-%02d 00:00:00', intval($m[1]), intval($m[2]), intval($m[3]));
840         }
841
842         // Clean malformed data
843         $date = preg_replace(
844             array(
845                 '/GMT\s*([+-][0-9]+)/',                     // support non-standard "GMTXXXX" literal
846                 '/[^a-z0-9\x20\x09:+-\/]/i',                  // remove any invalid characters
847                 '/\s*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*/i',   // remove weekday names
848             ),
849             array(
850                 '\\1',
851                 '',
852                 '',
853             ), $date);
854
855         $date = trim($date);
856
857         // try to fix dd/mm vs. mm/dd discrepancy, we can't do more here
858         if (preg_match('/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{4})$/', $date, $m)) {
859             $mdy   = $m[2] > 12 && $m[1] <= 12;
860             $day   = $mdy ? $m[2] : $m[1];
861             $month = $mdy ? $m[1] : $m[2];
862             $date  = sprintf('%04d-%02d-%02d 00:00:00', intval($m[3]), $month, $day);
863         }
864         // I've found that YYYY.MM.DD is recognized wrong, so here's a fix
865         else if (preg_match('/^(\d{4})\.(\d{1,2})\.(\d{1,2})$/', $date)) {
866             $date = str_replace('.', '-', $date) . ' 00:00:00';
867         }
868
869         return $date;
870     }
871
1aceb9 872     /*
A 873      * Idn_to_ascii wrapper.
874      * Intl/Idn modules version of this function doesn't work with e-mail address
875      */
876     public static function idn_to_ascii($str)
877     {
878         return self::idn_convert($str, true);
879     }
880
881
882     /*
883      * Idn_to_ascii wrapper.
884      * Intl/Idn modules version of this function doesn't work with e-mail address
885      */
886     public static function idn_to_utf8($str)
887     {
888         return self::idn_convert($str, false);
889     }
890
891
892     public static function idn_convert($input, $is_utf=false)
893     {
894         if ($at = strpos($input, '@')) {
895             $user   = substr($input, 0, $at);
896             $domain = substr($input, $at+1);
897         }
898         else {
899             $domain = $input;
900         }
901
902         $domain = $is_utf ? idn_to_ascii($domain) : idn_to_utf8($domain);
903
904         if ($domain === false) {
905             return '';
906         }
907
908         return $at ? $user . '@' . $domain : $domain;
909     }
910
ceb5b5 911     /**
TB 912      * Split the given string into word tokens
913      *
914      * @param string Input to tokenize
e8b82c 915      * @param integer Minimum length of a single token
ceb5b5 916      * @return array List of tokens
TB 917      */
e8b82c 918     public static function tokenize_string($str, $minlen = 2)
ceb5b5 919     {
e8b82c 920         $expr = array('/[\s;\/+-]+/ui', '/(\d)[-.\s]+(\d)/u');
TB 921         $repl = array(' ', '\\1\\2');
922
923         if ($minlen > 1) {
924             $minlen--;
925             $expr[] = "/(^|\s+)\w{1,$minlen}(\s+|$)/u";
926             $repl[] = ' ';
927         }
928
929         return array_filter(explode(" ", preg_replace($expr, $repl, $str)));
ceb5b5 930     }
TB 931
932     /**
933      * Normalize the given string for fulltext search.
49dad5 934      * Currently only optimized for ISO-8859-1 and ISO-8859-2 characters; to be extended
ceb5b5 935      *
TB 936      * @param string  Input string (UTF-8)
937      * @param boolean True to return list of words as array
e8b82c 938      * @param integer Minimum length of tokens
d19c0f 939      *
ceb5b5 940      * @return mixed  Normalized string or a list of normalized tokens
TB 941      */
e8b82c 942     public static function normalize_string($str, $as_array = false, $minlen = 2)
ceb5b5 943     {
d19c0f 944         // replace 4-byte unicode characters with '?' character,
AM 945         // these are not supported in default utf-8 charset on mysql,
946         // the chance we'd need them in searching is very low
947         $str = preg_replace('/('
948             . '\xF0[\x90-\xBF][\x80-\xBF]{2}'
949             . '|[\xF1-\xF3][\x80-\xBF]{3}'
950             . '|\xF4[\x80-\x8F][\x80-\xBF]{2}'
951             . ')/', '?', $str);
952
ceb5b5 953         // split by words
e8b82c 954         $arr = self::tokenize_string($str, $minlen);
ceb5b5 955
49dad5 956         // detect character set
AM 957         if (utf8_encode(utf8_decode($str)) == $str) {
958             // ISO-8859-1 (or ASCII)
959             preg_match_all('/./u', 'äâàåáãæçéêëèïîìíñöôòøõóüûùúýÿ', $keys);
960             preg_match_all('/./',  'aaaaaaaceeeeiiiinoooooouuuuyy', $values);
961
962             $mapping = array_combine($keys[0], $values[0]);
963             $mapping = array_merge($mapping, array('ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u'));
964         }
965         else if (rcube_charset::convert(rcube_charset::convert($str, 'UTF-8', 'ISO-8859-2'), 'ISO-8859-2', 'UTF-8') == $str) {
966             // ISO-8859-2
967             preg_match_all('/./u', 'ąáâäćçčéęëěíîłľĺńňóôöŕřśšşťţůúűüźžżý', $keys);
968             preg_match_all('/./',  'aaaaccceeeeiilllnnooorrsssttuuuuzzzy', $values);
969
970             $mapping = array_combine($keys[0], $values[0]);
971             $mapping = array_merge($mapping, array('ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u'));
972         }
973
ceb5b5 974         foreach ($arr as $i => $part) {
49dad5 975             $part = mb_strtolower($part);
AM 976
977             if (!empty($mapping)) {
978                 $part = strtr($part, $mapping);
ceb5b5 979             }
49dad5 980
AM 981             $arr[$i] = $part;
ceb5b5 982         }
TB 983
984         return $as_array ? $arr : join(" ", $arr);
985     }
986
929030 987     /**
4ba89c 988      * Compare two strings for matching words (order not relevant)
TB 989      *
990      * @param string Haystack
991      * @param string Needle
992      * @return boolen True if match, False otherwise
993      */
994     public static function words_match($haystack, $needle)
995     {
5143c4 996         $a_needle  = self::tokenize_string($needle, 1);
AM 997         $_haystack = join(" ", self::tokenize_string($haystack, 1));
998         $valid     = strlen($_haystack) > 0;
999         $hits      = 0;
4ba89c 1000
TB 1001         foreach ($a_needle as $w) {
5143c4 1002             if ($valid) {
AM 1003                 if (stripos($_haystack, $w) !== false) {
1004                     $hits++;
1005                 }
1006             }
1007             else if (stripos($haystack, $w) !== false) {
4ba89c 1008                 $hits++;
TB 1009             }
1010         }
1011
1012         return $hits >= count($a_needle);
1013     }
1014
1015     /**
929030 1016      * Parse commandline arguments into a hash array
AM 1017      *
1018      * @param array $aliases Argument alias names
1019      *
1020      * @return array Argument values hash
1021      */
1022     public static function get_opt($aliases = array())
1023     {
1024         $args = array();
1025
1026         for ($i=1; $i < count($_SERVER['argv']); $i++) {
1027             $arg   = $_SERVER['argv'][$i];
1028             $value = true;
1029             $key   = null;
1030
1031             if ($arg[0] == '-') {
1032                 $key = preg_replace('/^-+/', '', $arg);
1033                 $sp  = strpos($arg, '=');
1034                 if ($sp > 0) {
1035                     $key   = substr($key, 0, $sp - 2);
1036                     $value = substr($arg, $sp+1);
1037                 }
1038                 else if (strlen($_SERVER['argv'][$i+1]) && $_SERVER['argv'][$i+1][0] != '-') {
1039                     $value = $_SERVER['argv'][++$i];
1040                 }
1041
1042                 $args[$key] = is_string($value) ? preg_replace(array('/^["\']/', '/["\']$/'), '', $value) : $value;
1043             }
1044             else {
1045                 $args[] = $arg;
1046             }
1047
1048             if ($alias = $aliases[$key]) {
1049                 $args[$alias] = $args[$key];
1050             }
1051         }
1052
1053         return $args;
1054     }
1055
1056     /**
1057      * Safe password prompt for command line
1058      * from http://blogs.sitepoint.com/2009/05/01/interactive-cli-password-prompt-in-php/
1059      *
1060      * @return string Password
1061      */
1062     public static function prompt_silent($prompt = "Password:")
1063     {
1064         if (preg_match('/^win/i', PHP_OS)) {
1065             $vbscript  = sys_get_temp_dir() . 'prompt_password.vbs';
1066             $vbcontent = 'wscript.echo(InputBox("' . addslashes($prompt) . '", "", "password here"))';
1067             file_put_contents($vbscript, $vbcontent);
1068
1069             $command  = "cscript //nologo " . escapeshellarg($vbscript);
1070             $password = rtrim(shell_exec($command));
1071             unlink($vbscript);
1072
1073             return $password;
1074         }
1075         else {
1076             $command = "/usr/bin/env bash -c 'echo OK'";
1077             if (rtrim(shell_exec($command)) !== 'OK') {
1078                 echo $prompt;
1079                 $pass = trim(fgets(STDIN));
1080                 echo chr(8)."\r" . $prompt . str_repeat("*", strlen($pass))."\n";
1081                 return $pass;
1082             }
1083
1084             $command = "/usr/bin/env bash -c 'read -s -p \"" . addslashes($prompt) . "\" mypassword && echo \$mypassword'";
1085             $password = rtrim(shell_exec($command));
1086             echo "\n";
1087             return $password;
1088         }
1089     }
f707fe 1090
AM 1091     /**
1092      * Find out if the string content means true or false
1093      *
1094      * @param string $str Input value
1095      *
1096      * @return boolean Boolean value
1097      */
1098     public static function get_boolean($str)
1099     {
1100         $str = strtolower($str);
1101
1102         return !in_array($str, array('false', '0', 'no', 'off', 'nein', ''), true);
1103     }
1104
517c9f 1105     /**
AM 1106      * OS-dependent absolute path detection
1107      */
1108     public static function is_absolute_path($path)
1109     {
1110         if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
1111             return (bool) preg_match('!^[a-z]:[\\\\/]!i', $path);
1112         }
1113         else {
29c24e 1114             return $path[0] == '/';
517c9f 1115         }
AM 1116     }
5f5812 1117
AM 1118     /**
1119      * Resolve relative URL
1120      *
1121      * @param string $url Relative URL
1122      *
1123      * @return string Absolute URL
1124      */
1125     public static function resolve_url($url)
1126     {
1127         // prepend protocol://hostname:port
1128         if (!preg_match('|^https?://|', $url)) {
1129             $schema       = 'http';
1130             $default_port = 80;
1131
1132             if (self::https_check()) {
1133                 $schema       = 'https';
1134                 $default_port = 443;
1135             }
1136
1137             $prefix = $schema . '://' . preg_replace('/:\d+$/', '', $_SERVER['HTTP_HOST']);
1138             if ($_SERVER['SERVER_PORT'] != $default_port) {
1139                 $prefix .= ':' . $_SERVER['SERVER_PORT'];
1140             }
1141
1142             $url = $prefix . ($url[0] == '/' ? '' : '/') . $url;
1143         }
1144
1145         return $url;
1146     }
5529d9 1147
AM 1148     /**
1149      * Generate a ramdom string
1150      *
1151      * @param int String length
1152      *
1153      * @return string The generated random string
1154      */
1155     public static function random_bytes($length)
1156     {
1157         if (function_exists('openssl_random_pseudo_bytes')) {
1158             $random = openssl_random_pseudo_bytes(ceil($length / 2));
1159             $random = bin2hex($random);
1160
1161             // if the length wasn't even...
1162             if ($length < strlen($random)) {
1163                 $random = substr($random, 0, $length);
1164             }
1165         }
1166         else {
3f6fbd 1167             $alpha  = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789,*.:?!$-_=';
5529d9 1168             $random = '';
AM 1169
1170             for ($i = 0; $i < $length; $i++) {
1171                 $random .= $alpha[rand(0, strlen($alpha)-1)];
1172             }
1173         }
1174
1175         return $random;
1176     }
1aceb9 1177 }