1<?php
2
3/**
4 +-----------------------------------------------------------------------+
5 | This file is part of the Roundcube Webmail client                     |
6 |                                                                       |
7 | Copyright (C) The Roundcube Dev Team                                  |
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 |   Setup the application environment required to process               |
15 |   any request.                                                        |
16 +-----------------------------------------------------------------------+
17 | Author: Till Klampaeckel <till@php.net>                               |
18 |         Thomas Bruederli <roundcube@gmail.com>                        |
19 +-----------------------------------------------------------------------+
20*/
21
22// application constants
23define('RCMAIL_VERSION', '1.5.1');
24define('RCMAIL_START', microtime(true));
25
26if (!defined('INSTALL_PATH')) {
27    define('INSTALL_PATH', dirname($_SERVER['SCRIPT_FILENAME']).'/');
28}
29
30if (!defined('RCMAIL_CONFIG_DIR')) {
31    define('RCMAIL_CONFIG_DIR', getenv('ROUNDCUBE_CONFIG_DIR') ?: (INSTALL_PATH . 'config'));
32}
33
34if (!defined('RCUBE_LOCALIZATION_DIR')) {
35    define('RCUBE_LOCALIZATION_DIR', INSTALL_PATH . 'program/localization/');
36}
37
38define('RCUBE_INSTALL_PATH', INSTALL_PATH);
39define('RCUBE_CONFIG_DIR',  RCMAIL_CONFIG_DIR.'/');
40
41// Show basic error message on fatal PHP error
42register_shutdown_function('rcmail_fatal_error');
43
44// RC include folders MUST be included FIRST to avoid other
45// possible not compatible libraries (i.e PEAR) to be included
46// instead the ones provided by RC
47$include_path = INSTALL_PATH . 'program/lib' . PATH_SEPARATOR;
48$include_path.= ini_get('include_path');
49
50if (set_include_path($include_path) === false) {
51    die("Fatal error: ini_set/set_include_path does not work.");
52}
53
54// increase maximum execution time for php scripts
55// (does not work in safe mode)
56@set_time_limit(120);
57
58// include composer autoloader (if available)
59if (@file_exists(INSTALL_PATH . 'vendor/autoload.php')) {
60    require INSTALL_PATH . 'vendor/autoload.php';
61}
62
63// translate PATH_INFO to _task and _action GET parameters
64if (!empty($_SERVER['PATH_INFO']) && preg_match('!^/([a-z]+)/([a-z]+)$!', $_SERVER['PATH_INFO'], $m)) {
65    if (!isset($_GET['_task'])) {
66        $_GET['_task'] = $m[1];
67    }
68    if (!isset($_GET['_action'])) {
69        $_GET['_action'] = $m[2];
70    }
71}
72
73// include Roundcube Framework
74require_once 'Roundcube/bootstrap.php';
75
76// register autoloader for rcmail app classes
77spl_autoload_register('rcmail_autoload');
78
79/**
80 * PHP5 autoloader routine for dynamic class loading
81 */
82function rcmail_autoload($classname)
83{
84    if (strpos($classname, 'rcmail') === 0) {
85        if (preg_match('/^rcmail_action_([^_]+)_(.*)$/', $classname, $matches)) {
86            $filepath = INSTALL_PATH . "program/actions/{$matches[1]}/{$matches[2]}.php";
87        }
88        else {
89            $filepath = INSTALL_PATH . "program/include/$classname.php";
90        }
91
92        if (is_readable($filepath)) {
93            include_once $filepath;
94            return true;
95        }
96    }
97
98    return false;
99}
100
101/**
102 * Show basic error message on fatal PHP error
103 */
104function rcmail_fatal_error()
105{
106    $error = error_get_last();
107
108    if ($error && ($error['type'] === E_ERROR || $error['type'] === E_PARSE)) {
109        if (php_sapi_name() === 'cli') {
110            echo "Fatal error: Please check the Roundcube error log and/or server error logs for more information.\n";
111        }
112        elseif (!empty($_REQUEST['_remote'])) {
113            // Ajax request from UI
114            header('Content-Type: application/json; charset=UTF-8');
115            echo json_encode(['code' => 500, 'message' => "Internal Server Error"]);
116        }
117        else {
118            if (!defined('RCUBE_FATAL_ERROR_MSG')) {
119                define('RCUBE_FATAL_ERROR_MSG', INSTALL_PATH . 'program/resources/error.html');
120            }
121
122            echo file_get_contents(RCUBE_FATAL_ERROR_MSG);
123        }
124    }
125}
126