Thomas Bruederli
2012-11-25 9be2f43d02cd54340fc64c9b2cee9cfa94b49034
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
f707fe 5  | program/include/bootstrap.php                                         |
4e17e6 6  |                                                                       |
e019f2 7  | This file is part of the Roundcube PHP suite                          |
0c2596 8  | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
7fe381 9  |                                                                       |
T 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.                     |
4e17e6 13  |                                                                       |
T 14  | CONTENTS:                                                             |
f707fe 15  |   Roundcube Framework Initialization                                  |
4e17e6 16  |                                                                       |
T 17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
f707fe 19  | Author: Aleksander Machniak <alec@alec.pl>                            |
4e17e6 20  +-----------------------------------------------------------------------+
T 21 */
22
23
6d969b 24 /**
f707fe 25  * Roundcube Framework Initialization
9e54e6 26  *
9ab346 27  * @package    Framework
AM 28  * @subpackage Core
6d969b 29  */
f707fe 30
AM 31 $config = array(
32     'error_reporting'         => E_ALL &~ (E_NOTICE | E_STRICT),
33     // Some users are not using Installer, so we'll check some
34     // critical PHP settings here. Only these, which doesn't provide
35     // an error/warning in the logs later. See (#1486307).
36     'mbstring.func_overload'  => 0,
37     'suhosin.session.encrypt' => 0,
38     'session.auto_start'      => 0,
39     'file_uploads'            => 1,
40     'magic_quotes_runtime'    => 0,
41     'magic_quotes_sybase'     => 0, // #1488506
42 );
43 foreach ($config as $optname => $optval) {
44     if ($optval != ini_get($optname) && @ini_set($optname, $optval) === false) {
45         die("ERROR: Wrong '$optname' option value and it wasn't possible to set it to required value ($optval).\n"
46             ."Check your PHP configuration (including php_admin_flag).");
47     }
48 }
49
50 // application constants
51 define('RCMAIL_VERSION', '0.9-git');
52 define('RCMAIL_CHARSET', 'UTF-8');
53 define('RCMAIL_START', microtime(true));
54
9be2f4 55 if (!defined('RCUBE_LIB_DIR')) {
TB 56     define('RCUBE_LIB_DIR', dirname(__FILE__).'/');
f707fe 57 }
AM 58
9be2f4 59 if (!defined('RCUBE_INSTALL_PATH')) {
TB 60     define('RCUBE_INSTALL_PATH', RCUBE_LIB_DIR);
61 }
62
63 if (!defined('RCUBE_CONFIG_DIR')) {
64     define('RCUBE_CONFIG_DIR', RCUBE_INSTALL_PATH . 'config');
65 }
66
67 if (!defined('RCUBE_PLUGINS_DIR')) {
68     define('RCUBE_PLUGINS_DIR', RCUBE_INSTALL_PATH . 'plugins/');
69 }
70
71 if (!defined('RCUBE_LOCALIZATION_DIR')) {
72     define('RCUBE_LOCALIZATION_DIR', RCUBE_INSTALL_PATH . 'localization/');
f707fe 73 }
AM 74
75 // set internal encoding for mbstring extension
76 if (extension_loaded('mbstring')) {
77     mb_internal_encoding(RCMAIL_CHARSET);
78     @mb_regex_encoding(RCMAIL_CHARSET);
79 }
80
81 // Register autoloader
82 spl_autoload_register('rcube_autoload');
83
84 // set PEAR error handling (will also load the PEAR main class)
85 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'rcube_pear_error');
86
4e17e6 87
a0109c 88
6d969b 89 /**
f11541 90  * Similar function as in_array() but case-insensitive
6d969b 91  *
0c2596 92  * @param string $needle    Needle value
A 93  * @param array  $heystack  Array to search in
94  *
6d969b 95  * @return boolean True if found, False if not
f11541 96  */
4e17e6 97 function in_array_nocase($needle, $haystack)
6d969b 98 {
0c2596 99     $needle = mb_strtolower($needle);
be9840 100     foreach ((array)$haystack as $value) {
0c2596 101         if ($needle === mb_strtolower($value)) {
A 102             return true;
103         }
104     }
9e54e6 105
0c2596 106     return false;
6d969b 107 }
4e17e6 108
T 109
6d969b 110 /**
0c2596 111  * Parse a human readable string for a number of bytes.
6d969b 112  *
0c2596 113  * @param string $str  Input string
A 114  *
47f072 115  * @return float Number of bytes
6d969b 116  */
86df15 117 function parse_bytes($str)
6d969b 118 {
0c2596 119     if (is_numeric($str)) {
A 120         return floatval($str);
86df15 121     }
T 122
0c2596 123     if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs)) {
A 124         $bytes = floatval($regs[1]);
125         switch (strtolower($regs[2])) {
126         case 'g':
127         case 'gb':
128             $bytes *= 1073741824;
129             break;
130         case 'm':
131         case 'mb':
132             $bytes *= 1048576;
133             break;
134         case 'k':
135         case 'kb':
136             $bytes *= 1024;
137             break;
7145e0 138         }
A 139     }
0c2596 140
A 141     return floatval($bytes);
7145e0 142 }
86df15 143
0c2596 144
6d969b 145 /**
T 146  * Make sure the string ends with a slash
147  */
bac7d1 148 function slashify($str)
6d969b 149 {
bac7d1 150   return unslashify($str).'/';
6d969b 151 }
bac7d1 152
T 153
6d969b 154 /**
e8be30 155  * Remove slashes at the end of the string
6d969b 156  */
bac7d1 157 function unslashify($str)
6d969b 158 {
e8be30 159   return preg_replace('/\/+$/', '', $str);
6d969b 160 }
9fee0e 161
T 162
6d969b 163 /**
5d66a4 164  * Returns number of seconds for a specified offset string.
6d969b 165  *
5d66a4 166  * @param string $str  String representation of the offset (e.g. 20min, 5h, 2days, 1week)
0c2596 167  *
5d66a4 168  * @return int Number of seconds
6d969b 169  */
5d66a4 170 function get_offset_sec($str)
9e54e6 171 {
5d66a4 172     if (preg_match('/^([0-9]+)\s*([smhdw])/i', $str, $regs)) {
A 173         $amount = (int) $regs[1];
0c2596 174         $unit   = strtolower($regs[2]);
734584 175     }
bf13ba 176     else {
5d66a4 177         $amount = (int) $str;
0c2596 178         $unit   = 's';
bf13ba 179     }
734584 180
0c2596 181     switch ($unit) {
A 182     case 'w':
183         $amount *= 7;
184     case 'd':
185         $amount *= 24;
186     case 'h':
187         $amount *= 60;
188     case 'm':
189         $amount *= 60;
190     }
191
5d66a4 192     return $amount;
A 193 }
194
195
196 /**
197  * Create a unix timestamp with a specified offset from now.
198  *
199  * @param string $offset_str  String representation of the offset (e.g. 20min, 5h, 2days)
200  * @param int    $factor      Factor to multiply with the offset
201  *
202  * @return int Unix timestamp
203  */
204 function get_offset_time($offset_str, $factor=1)
205 {
206     return time() + get_offset_sec($offset_str) * $factor;
734584 207 }
T 208
0501b6 209
T 210 /**
0c2596 211  * Truncate string if it is longer than the allowed length.
A 212  * Replace the middle or the ending part of a string with a placeholder.
0501b6 213  *
0c2596 214  * @param string $str         Input string
A 215  * @param int    $maxlength   Max. length
216  * @param string $placeholder Replace removed chars with this
217  * @param bool   $ending      Set to True if string should be truncated from the end
218  *
219  * @return string Abbreviated string
0501b6 220  */
0c2596 221 function abbreviate_string($str, $maxlength, $placeholder='...', $ending=false)
0501b6 222 {
0c2596 223     $length = mb_strlen($str);
0501b6 224
0c2596 225     if ($length > $maxlength) {
A 226         if ($ending) {
227             return mb_substr($str, 0, $maxlength) . $placeholder;
228         }
229
230         $placeholder_length = mb_strlen($placeholder);
231         $first_part_length  = floor(($maxlength - $placeholder_length)/2);
232         $second_starting_location = $length - $maxlength + $first_part_length + $placeholder_length;
233
234         $str = mb_substr($str, 0, $first_part_length) . $placeholder . mb_substr($str, $second_starting_location);
235     }
236
237     return $str;
0501b6 238 }
T 239
240
b54121 241 /**
0c2596 242  * Get all keys from array (recursive).
A 243  *
244  * @param array $array  Input array
245  *
246  * @return array List of array keys
f52c93 247  */
T 248 function array_keys_recursive($array)
249 {
0c2596 250     $keys = array();
9e54e6 251
e8be30 252     if (!empty($array) && is_array($array)) {
0c2596 253         foreach ($array as $key => $child) {
A 254             $keys[] = $key;
255             foreach (array_keys_recursive($child) as $val) {
256                 $keys[] = $val;
257             }
258         }
f52c93 259     }
0c2596 260
A 261     return $keys;
262 }
263
264
265 /**
266  * Remove all non-ascii and non-word chars except ., -, _
267  */
268 function asciiwords($str, $css_id = false, $replace_with = '')
269 {
270     $allowed = 'a-z0-9\_\-' . (!$css_id ? '\.' : '');
271     return preg_replace("/[^$allowed]/i", $replace_with, $str);
272 }
273
274
275 /**
a04a74 276  * Check if a string contains only ascii characters
AM 277  *
278  * @param string $str           String to check
279  * @param bool   $control_chars Includes control characters
280  *
281  * @return bool
282  */
283 function is_ascii($str, $control_chars = true)
284 {
285     $regexp = $control_chars ? '/[^\x00-\x7F]/' : '/[^\x20-\x7E]/';
286     return preg_match($regexp, $str) ? false : true;
287 }
288
289
290 /**
e8be30 291  * Remove single and double quotes from a given string
0c2596 292  *
A 293  * @param string Input value
294  *
295  * @return string Dequoted string
296  */
297 function strip_quotes($str)
298 {
299     return str_replace(array("'", '"'), '', $str);
300 }
301
302
303 /**
304  * Remove new lines characters from given string
305  *
306  * @param string $str  Input value
307  *
308  * @return string Stripped string
309  */
310 function strip_newlines($str)
311 {
312     return preg_replace('/[\r\n]/', '', $str);
313 }
314
315
316 /**
317  * Compose a valid representation of name and e-mail address
318  *
319  * @param string $email  E-mail address
320  * @param string $name   Person name
321  *
322  * @return string Formatted string
323  */
324 function format_email_recipient($email, $name = '')
325 {
326     $email = trim($email);
327
328     if ($name && $name != $email) {
329         // Special chars as defined by RFC 822 need to in quoted string (or escaped).
330         if (preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name)) {
331             $name = '"'.addcslashes($name, '"').'"';
332         }
333
334         return "$name <$email>";
335     }
336
337     return $email;
f52c93 338 }
T 339
340
341 /**
6ab936 342  * Format e-mail address
AM 343  *
344  * @param string $email E-mail address
345  *
346  * @return string Formatted e-mail address
347  */
348 function format_email($email)
349 {
350     $email = trim($email);
351     $parts = explode('@', $email);
352     $count = count($parts);
353
354     if ($count > 1) {
355         $parts[$count-1] = mb_strtolower($parts[$count-1]);
356
357         $email = implode('@', $parts);
358     }
359
360     return $email;
361 }
362
363
364 /**
ee258c 365  * mbstring replacement functions
A 366  */
8f6a46 367 if (!extension_loaded('mbstring'))
A 368 {
ee258c 369     function mb_strlen($str)
A 370     {
0c2596 371         return strlen($str);
ee258c 372     }
A 373
374     function mb_strtolower($str)
375     {
376         return strtolower($str);
377     }
378
379     function mb_strtoupper($str)
380     {
381         return strtoupper($str);
382     }
383
384     function mb_substr($str, $start, $len=null)
385     {
386         return substr($str, $start, $len);
387     }
388
389     function mb_strpos($haystack, $needle, $offset=0)
390     {
391         return strpos($haystack, $needle, $offset);
392     }
393
394     function mb_strrpos($haystack, $needle, $offset=0)
395     {
396         return strrpos($haystack, $needle, $offset);
397     }
398 }
399
e99991 400 /**
A 401  * intl replacement functions
402  */
403
404 if (!function_exists('idn_to_utf8'))
405 {
406     function idn_to_utf8($domain, $flags=null)
407     {
408         static $idn, $loaded;
409
410         if (!$loaded) {
411             $idn = new Net_IDNA2();
412             $loaded = true;
413         }
414
e8d5bd 415         if ($idn && $domain && preg_match('/(^|\.)xn--/i', $domain)) {
e99991 416             try {
A 417                 $domain = $idn->decode($domain);
418             }
419             catch (Exception $e) {
420             }
421         }
422         return $domain;
423     }
424 }
425
426 if (!function_exists('idn_to_ascii'))
427 {
428     function idn_to_ascii($domain, $flags=null)
429     {
430         static $idn, $loaded;
431
432         if (!$loaded) {
433             $idn = new Net_IDNA2();
434             $loaded = true;
435         }
436
437         if ($idn && $domain && preg_match('/[^\x20-\x7E]/', $domain)) {
438             try {
439                 $domain = $idn->encode($domain);
440             }
441             catch (Exception $e) {
442             }
443         }
444         return $domain;
445     }
446 }
447
0c2596 448 /**
A 449  * Use PHP5 autoload for dynamic class loading
450  *
451  * @todo Make Zend, PEAR etc play with this
452  * @todo Make our classes conform to a more straight forward CS.
453  */
454 function rcube_autoload($classname)
455 {
456     $filename = preg_replace(
457         array(
458             '/Mail_(.+)/',
459             '/Net_(.+)/',
460             '/Auth_(.+)/',
461             '/^html_.+/',
ba6f21 462             '/^rcube(.*)/',
0c2596 463             '/^utf8$/',
A 464         ),
465         array(
466             'Mail/\\1',
467             'Net/\\1',
468             'Auth/\\1',
ba6f21 469             'Roundcube/html',
AM 470             'Roundcube/rcube\\1',
0c2596 471             'utf8.class',
A 472         ),
473         $classname
474     );
475
476     if ($fp = @fopen("$filename.php", 'r', true)) {
477         fclose($fp);
1aceb9 478         include_once "$filename.php";
0c2596 479         return true;
A 480     }
481
482     return false;
483 }
484
485 /**
486  * Local callback function for PEAR errors
487  */
488 function rcube_pear_error($err)
489 {
490     error_log(sprintf("%s (%s): %s",
491         $err->getMessage(),
492         $err->getCode(),
493         $err->getUserinfo()), 0);
494 }