Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

bootstrap.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube PHP suite |
  5. | Copyright (C) 2005-2015, The Roundcube Dev Team |
  6. | |
  7. | Licensed under the GNU General Public License version 3 or |
  8. | any later version with exceptions for skins & plugins. |
  9. | See the README file for a full license statement. |
  10. | |
  11. | CONTENTS: |
  12. | Roundcube Framework Initialization |
  13. +-----------------------------------------------------------------------+
  14. | Author: Thomas Bruederli <roundcube@gmail.com> |
  15. | Author: Aleksander Machniak <alec@alec.pl> |
  16. +-----------------------------------------------------------------------+
  17. */
  18. /**
  19. * Roundcube Framework Initialization
  20. *
  21. * @package Framework
  22. * @subpackage Core
  23. */
  24. $config = array(
  25. 'error_reporting' => E_ALL & ~E_NOTICE & ~E_STRICT,
  26. // Some users are not using Installer, so we'll check some
  27. // critical PHP settings here. Only these, which doesn't provide
  28. // an error/warning in the logs later. See (#1486307).
  29. 'mbstring.func_overload' => 0,
  30. 'magic_quotes_runtime' => false,
  31. 'magic_quotes_sybase' => false, // #1488506
  32. );
  33. // check these additional ini settings if not called via CLI
  34. if (php_sapi_name() != 'cli') {
  35. $config += array(
  36. 'suhosin.session.encrypt' => false,
  37. 'file_uploads' => true,
  38. );
  39. }
  40. foreach ($config as $optname => $optval) {
  41. $ini_optval = filter_var(ini_get($optname), is_bool($optval) ? FILTER_VALIDATE_BOOLEAN : FILTER_VALIDATE_INT);
  42. if ($optval != $ini_optval && @ini_set($optname, $optval) === false) {
  43. $optval = !is_bool($optval) ? $optval : ($optval ? 'On' : 'Off');
  44. $error = "ERROR: Wrong '$optname' option value and it wasn't possible to set it to required value ($optval).\n"
  45. . "Check your PHP configuration (including php_admin_flag).";
  46. if (defined('STDERR')) fwrite(STDERR, $error); else echo $error;
  47. exit(1);
  48. }
  49. }
  50. // framework constants
  51. define('RCUBE_VERSION', '1.2.2');
  52. define('RCUBE_CHARSET', 'UTF-8');
  53. if (!defined('RCUBE_LIB_DIR')) {
  54. define('RCUBE_LIB_DIR', __DIR__ . '/');
  55. }
  56. if (!defined('RCUBE_INSTALL_PATH')) {
  57. define('RCUBE_INSTALL_PATH', RCUBE_LIB_DIR);
  58. }
  59. if (!defined('RCUBE_CONFIG_DIR')) {
  60. define('RCUBE_CONFIG_DIR', RCUBE_INSTALL_PATH . 'config/');
  61. }
  62. if (!defined('RCUBE_PLUGINS_DIR')) {
  63. define('RCUBE_PLUGINS_DIR', RCUBE_INSTALL_PATH . 'plugins/');
  64. }
  65. if (!defined('RCUBE_LOCALIZATION_DIR')) {
  66. define('RCUBE_LOCALIZATION_DIR', RCUBE_INSTALL_PATH . 'localization/');
  67. }
  68. // set internal encoding for mbstring extension
  69. if (function_exists('mb_internal_encoding')) {
  70. mb_internal_encoding(RCUBE_CHARSET);
  71. }
  72. if (function_exists('mb_regex_encoding')) {
  73. mb_regex_encoding(RCUBE_CHARSET);
  74. }
  75. // make sure the Roundcube lib directory is in the include_path
  76. $rcube_path = realpath(RCUBE_LIB_DIR . '..');
  77. $sep = PATH_SEPARATOR;
  78. $regexp = "!(^|$sep)" . preg_quote($rcube_path, '!') . "($sep|\$)!";
  79. $path = ini_get('include_path');
  80. if (!preg_match($regexp, $path)) {
  81. set_include_path($path . PATH_SEPARATOR . $rcube_path);
  82. }
  83. // Register autoloader
  84. spl_autoload_register('rcube_autoload');
  85. // set PEAR error handling (will also load the PEAR main class)
  86. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'rcube_pear_error');
  87. /**
  88. * Similar function as in_array() but case-insensitive with multibyte support.
  89. *
  90. * @param string $needle Needle value
  91. * @param array $heystack Array to search in
  92. *
  93. * @return boolean True if found, False if not
  94. */
  95. function in_array_nocase($needle, $haystack)
  96. {
  97. // use much faster method for ascii
  98. if (is_ascii($needle)) {
  99. foreach ((array) $haystack as $value) {
  100. if (strcasecmp($value, $needle) === 0) {
  101. return true;
  102. }
  103. }
  104. }
  105. else {
  106. $needle = mb_strtolower($needle);
  107. foreach ((array) $haystack as $value) {
  108. if ($needle === mb_strtolower($value)) {
  109. return true;
  110. }
  111. }
  112. }
  113. return false;
  114. }
  115. /**
  116. * Parse a human readable string for a number of bytes.
  117. *
  118. * @param string $str Input string
  119. *
  120. * @return float Number of bytes
  121. */
  122. function parse_bytes($str)
  123. {
  124. if (is_numeric($str)) {
  125. return floatval($str);
  126. }
  127. if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs)) {
  128. $bytes = floatval($regs[1]);
  129. switch (strtolower($regs[2])) {
  130. case 'g':
  131. case 'gb':
  132. $bytes *= 1073741824;
  133. break;
  134. case 'm':
  135. case 'mb':
  136. $bytes *= 1048576;
  137. break;
  138. case 'k':
  139. case 'kb':
  140. $bytes *= 1024;
  141. break;
  142. }
  143. }
  144. return floatval($bytes);
  145. }
  146. /**
  147. * Make sure the string ends with a slash
  148. */
  149. function slashify($str)
  150. {
  151. return unslashify($str).'/';
  152. }
  153. /**
  154. * Remove slashes at the end of the string
  155. */
  156. function unslashify($str)
  157. {
  158. return preg_replace('/\/+$/', '', $str);
  159. }
  160. /**
  161. * Returns number of seconds for a specified offset string.
  162. *
  163. * @param string $str String representation of the offset (e.g. 20min, 5h, 2days, 1week)
  164. *
  165. * @return int Number of seconds
  166. */
  167. function get_offset_sec($str)
  168. {
  169. if (preg_match('/^([0-9]+)\s*([smhdw])/i', $str, $regs)) {
  170. $amount = (int) $regs[1];
  171. $unit = strtolower($regs[2]);
  172. }
  173. else {
  174. $amount = (int) $str;
  175. $unit = 's';
  176. }
  177. switch ($unit) {
  178. case 'w':
  179. $amount *= 7;
  180. case 'd':
  181. $amount *= 24;
  182. case 'h':
  183. $amount *= 60;
  184. case 'm':
  185. $amount *= 60;
  186. }
  187. return $amount;
  188. }
  189. /**
  190. * Create a unix timestamp with a specified offset from now.
  191. *
  192. * @param string $offset_str String representation of the offset (e.g. 20min, 5h, 2days)
  193. * @param int $factor Factor to multiply with the offset
  194. *
  195. * @return int Unix timestamp
  196. */
  197. function get_offset_time($offset_str, $factor = 1)
  198. {
  199. return time() + get_offset_sec($offset_str) * $factor;
  200. }
  201. /**
  202. * Truncate string if it is longer than the allowed length.
  203. * Replace the middle or the ending part of a string with a placeholder.
  204. *
  205. * @param string $str Input string
  206. * @param int $maxlength Max. length
  207. * @param string $placeholder Replace removed chars with this
  208. * @param bool $ending Set to True if string should be truncated from the end
  209. *
  210. * @return string Abbreviated string
  211. */
  212. function abbreviate_string($str, $maxlength, $placeholder = '...', $ending = false)
  213. {
  214. $length = mb_strlen($str);
  215. if ($length > $maxlength) {
  216. if ($ending) {
  217. return mb_substr($str, 0, $maxlength) . $placeholder;
  218. }
  219. $placeholder_length = mb_strlen($placeholder);
  220. $first_part_length = floor(($maxlength - $placeholder_length)/2);
  221. $second_starting_location = $length - $maxlength + $first_part_length + $placeholder_length;
  222. $prefix = mb_substr($str, 0, $first_part_length);
  223. $suffix = mb_substr($str, $second_starting_location);
  224. $str = $prefix . $placeholder . $suffix;
  225. }
  226. return $str;
  227. }
  228. /**
  229. * Get all keys from array (recursive).
  230. *
  231. * @param array $array Input array
  232. *
  233. * @return array List of array keys
  234. */
  235. function array_keys_recursive($array)
  236. {
  237. $keys = array();
  238. if (!empty($array) && is_array($array)) {
  239. foreach ($array as $key => $child) {
  240. $keys[] = $key;
  241. foreach (array_keys_recursive($child) as $val) {
  242. $keys[] = $val;
  243. }
  244. }
  245. }
  246. return $keys;
  247. }
  248. /**
  249. * Remove all non-ascii and non-word chars except ., -, _
  250. */
  251. function asciiwords($str, $css_id = false, $replace_with = '')
  252. {
  253. $allowed = 'a-z0-9\_\-' . (!$css_id ? '\.' : '');
  254. return preg_replace("/[^$allowed]/i", $replace_with, $str);
  255. }
  256. /**
  257. * Check if a string contains only ascii characters
  258. *
  259. * @param string $str String to check
  260. * @param bool $control_chars Includes control characters
  261. *
  262. * @return bool
  263. */
  264. function is_ascii($str, $control_chars = true)
  265. {
  266. $regexp = $control_chars ? '/[^\x00-\x7F]/' : '/[^\x20-\x7E]/';
  267. return preg_match($regexp, $str) ? false : true;
  268. }
  269. /**
  270. * Compose a valid representation of name and e-mail address
  271. *
  272. * @param string $email E-mail address
  273. * @param string $name Person name
  274. *
  275. * @return string Formatted string
  276. */
  277. function format_email_recipient($email, $name = '')
  278. {
  279. $email = trim($email);
  280. if ($name && $name != $email) {
  281. // Special chars as defined by RFC 822 need to in quoted string (or escaped).
  282. if (preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name)) {
  283. $name = '"'.addcslashes($name, '"').'"';
  284. }
  285. return "$name <$email>";
  286. }
  287. return $email;
  288. }
  289. /**
  290. * Format e-mail address
  291. *
  292. * @param string $email E-mail address
  293. *
  294. * @return string Formatted e-mail address
  295. */
  296. function format_email($email)
  297. {
  298. $email = trim($email);
  299. $parts = explode('@', $email);
  300. $count = count($parts);
  301. if ($count > 1) {
  302. $parts[$count-1] = mb_strtolower($parts[$count-1]);
  303. $email = implode('@', $parts);
  304. }
  305. return $email;
  306. }
  307. /**
  308. * Fix version number so it can be used correctly in version_compare()
  309. *
  310. * @param string $version Version number string
  311. *
  312. * @param return Version number string
  313. */
  314. function version_parse($version)
  315. {
  316. return str_replace(
  317. array('-stable', '-git'),
  318. array('.0', '.99'),
  319. $version
  320. );
  321. }
  322. /**
  323. * intl replacement functions
  324. */
  325. if (!function_exists('idn_to_utf8'))
  326. {
  327. function idn_to_utf8($domain)
  328. {
  329. static $idn, $loaded;
  330. if (!$loaded) {
  331. $idn = new Net_IDNA2();
  332. $loaded = true;
  333. }
  334. if ($idn && $domain && preg_match('/(^|\.)xn--/i', $domain)) {
  335. try {
  336. $domain = $idn->decode($domain);
  337. }
  338. catch (Exception $e) {
  339. }
  340. }
  341. return $domain;
  342. }
  343. }
  344. if (!function_exists('idn_to_ascii'))
  345. {
  346. function idn_to_ascii($domain)
  347. {
  348. static $idn, $loaded;
  349. if (!$loaded) {
  350. $idn = new Net_IDNA2();
  351. $loaded = true;
  352. }
  353. if ($idn && $domain && preg_match('/[^\x20-\x7E]/', $domain)) {
  354. try {
  355. $domain = $idn->encode($domain);
  356. }
  357. catch (Exception $e) {
  358. }
  359. }
  360. return $domain;
  361. }
  362. }
  363. /**
  364. * Use PHP5 autoload for dynamic class loading
  365. *
  366. * @todo Make Zend, PEAR etc play with this
  367. * @todo Make our classes conform to a more straight forward CS.
  368. */
  369. function rcube_autoload($classname)
  370. {
  371. if (strpos($classname, 'rcube') === 0) {
  372. $classname = 'Roundcube/' . $classname;
  373. }
  374. else if (strpos($classname, 'html_') === 0 || $classname === 'html') {
  375. $classname = 'Roundcube/html';
  376. }
  377. else if (strpos($classname, 'Mail_') === 0) {
  378. $classname = 'Mail/' . substr($classname, 5);
  379. }
  380. else if (strpos($classname, 'Net_') === 0) {
  381. $classname = 'Net/' . substr($classname, 4);
  382. }
  383. else if (strpos($classname, 'Auth_') === 0) {
  384. $classname = 'Auth/' . substr($classname, 5);
  385. }
  386. if ($fp = @fopen("$classname.php", 'r', true)) {
  387. fclose($fp);
  388. include_once "$classname.php";
  389. return true;
  390. }
  391. return false;
  392. }
  393. /**
  394. * Local callback function for PEAR errors
  395. */
  396. function rcube_pear_error($err)
  397. {
  398. $msg = sprintf("ERROR: %s (%s)", $err->getMessage(), $err->getCode());
  399. if ($info = $err->getUserinfo()) {
  400. $msg .= ': ' . $info;
  401. }
  402. error_log($msg, 0);
  403. }