Aleksander Machniak
2013-10-17 037af6890fe6fdb84a08d3c86083e847c90ec0ad
commit | author | age
4e17e6 1 <?php
T 2 /*
a6f90e 3  +-------------------------------------------------------------------------+
e019f2 4  | Roundcube Webmail IMAP Client                                           |
183c7f 5  | Version 0.9.4                                                           |
a6f90e 6  |                                                                         |
2c6aea 7  | Copyright (C) 2005-2013, The Roundcube Dev Team                         |
a6f90e 8  |                                                                         |
7fe381 9  | This program is free software: you can redistribute it and/or modify    |
T 10  | it under the terms of the GNU General Public License (with exceptions   |
11  | for skins & plugins) as published by the Free Software Foundation,      |
12  | either version 3 of the License, or (at your option) any later version. |
13  |                                                                         |
14  | This file forms part of the Roundcube Webmail Software for which the    |
15  | following exception is added: Plugins and Skins which merely make       |
16  | function calls to the Roundcube Webmail Software, and for that purpose  |
17  | include it by reference shall not be considered modifications of        |
18  | the software.                                                           |
19  |                                                                         |
20  | If you wish to use this file in another project or create a modified    |
21  | version that will not be part of the Roundcube Webmail Software, you    |
22  | may remove the exception above and use this source code under the       |
23  | original version of the license.                                        |
a6f90e 24  |                                                                         |
A 25  | This program is distributed in the hope that it will be useful,         |
26  | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
7fe381 27  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            |
a6f90e 28  | GNU General Public License for more details.                            |
A 29  |                                                                         |
7fe381 30  | You should have received a copy of the GNU General Public License       |
T 31  | along with this program.  If not, see http://www.gnu.org/licenses/.     |
a6f90e 32  |                                                                         |
A 33  +-------------------------------------------------------------------------+
34  | Author: Thomas Bruederli <roundcube@gmail.com>                          |
35  +-------------------------------------------------------------------------+
4e17e6 36 */
15a9d1 37
47124c 38 // include environment
T 39 require_once 'program/include/iniset.php';
15a9d1 40
48bc52 41 // init application, start session, init output class, etc.
83a763 42 $RCMAIL = rcmail::get_instance();
T 43
9e54e6 44 // Make the whole PHP output non-cacheable (#1487797)
0c2596 45 $RCMAIL->output->nocacheing_headers();
9e54e6 46
d51c93 47 // turn on output buffering
A 48 ob_start();
8affba 49
8c72e3 50 // check if config files had errors
T 51 if ($err_str = $RCMAIL->config->get_error()) {
1aceb9 52   rcmail::raise_error(array(
8c72e3 53     'code' => 601,
T 54     'type' => 'php',
55     'message' => $err_str), false, true);
56 }
57
8affba 58 // check DB connections and exit on failure
c321a9 59 if ($err_str = $RCMAIL->db->is_error()) {
1aceb9 60   rcmail::raise_error(array(
f11541 61     'code' => 603,
T 62     'type' => 'db',
63     'message' => $err_str), FALSE, TRUE);
64 }
8affba 65
4e17e6 66 // error steps
0c2596 67 if ($RCMAIL->action == 'error' && !empty($_GET['_code'])) {
1aceb9 68   rcmail::raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
47124c 69 }
570f0b 70
f5d61d 71 // check if https is required (for login) and redirect if necessary
T 72 if (empty($_SESSION['user_id']) && ($force_https = $RCMAIL->config->get('force_https', false))) {
73   $https_port = is_bool($force_https) ? 443 : $force_https;
1aceb9 74   if (!rcube_utils::https_check($https_port)) {
76c94b 75     $host  = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
A 76     $host .= ($https_port != 443 ? ':' . $https_port : '');
77     header('Location: https://' . $host . $_SERVER['REQUEST_URI']);
f5d61d 78     exit;
T 79   }
80 }
81
cc97ea 82 // trigger startup plugin hook
T 83 $startup = $RCMAIL->plugins->exec_hook('startup', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
84 $RCMAIL->set_task($startup['task']);
85 $RCMAIL->action = $startup['action'];
86
4e17e6 87 // try to log in
9b94eb 88 if ($RCMAIL->task == 'login' && $RCMAIL->action == 'login') {
1aceb9 89   $request_valid = $_SESSION['temp'] && $RCMAIL->check_request(rcube_utils::INPUT_POST, 'login');
784a42 90
0129d7 91   // purge the session in case of new login when a session already exists 
cc97ea 92   $RCMAIL->kill_session();
5f560e 93
cc97ea 94   $auth = $RCMAIL->plugins->exec_hook('authenticate', array(
T 95     'host' => $RCMAIL->autoselect_host(),
1aceb9 96     'user' => trim(rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST)),
A 97     'pass' => rcube_utils::get_input_value('_pass', rcube_utils::INPUT_POST, true,
5f560e 98        $RCMAIL->config->get('password_charset', 'ISO-8859-1')),
446364 99     'cookiecheck' => true,
784a42 100     'valid' => $request_valid,
64608b 101   ));
cc97ea 102
7c8fd8 103   // Login
AM 104   if ($auth['valid'] && !$auth['abort'] &&
105     $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'], $auth['cookiecheck'])
4cfe66 106   ) {
A 107     // create new session ID, don't destroy the current session
c294ea 108     // it was destroyed already by $RCMAIL->kill_session() above
4cfe66 109     $RCMAIL->session->remove('temp');
c294ea 110     $RCMAIL->session->regenerate_id(false);
aad6e2 111
T 112     // send auth cookie if necessary
cf2da2 113     $RCMAIL->session->set_auth_cookie();
aad6e2 114
5e0045 115     // log successful login
0c2596 116     $RCMAIL->log_login();
10eedb 117
cc97ea 118     // restore original request parameters
88007c 119     $query = array();
1aceb9 120     if ($url = rcube_utils::get_input_value('_url', rcube_utils::INPUT_POST)) {
cc97ea 121       parse_str($url, $query);
c294ea 122
32234d 123       // prevent endless looping on login page
T 124       if ($query['_task'] == 'login')
125         unset($query['_task']);
d2191c 126
A 127       // prevent redirect to compose with specified ID (#1488226)
128       if ($query['_action'] == 'compose' && !empty($query['_id']))
129         $query = array();
32234d 130     }
cc97ea 131
T 132     // allow plugins to control the redirect url after login success
32234d 133     $redir = $RCMAIL->plugins->exec_hook('login_after', $query + array('_task' => 'mail'));
fcc7f8 134     unset($redir['abort'], $redir['_err']);
5e0045 135
4e17e6 136     // send redirect
cc97ea 137     $OUTPUT->redirect($redir);
4e17e6 138   }
47124c 139   else {
7c8fd8 140     if (!$auth['valid']) {
AM 141       $error_code  = RCMAIL::ERROR_INVALID_REQUEST;
142     }
143     else {
144       $error_code = $auth['error'] ? $auth['error'] : $RCMAIL->login_error();
145     }
6d99f9 146
7c8fd8 147     $error_labels = array(
AM 148       RCMAIL::ERROR_STORAGE          => 'storageerror',
149       RCMAIL::ERROR_COOKIES_DISABLED => 'cookiesdisabled',
150       RCMAIL::ERROR_INVALID_REQUEST  => 'invalidrequest',
151       RCMAIL::ERROR_INVALID_HOST     => 'invalidhost',
152     );
153
154     $error_message = $error_labels[$error_code] ? $error_labels[$error_code] : 'loginfailed';
155
156     $OUTPUT->show_message($error_message, 'warning');
8fcc3e 157     $RCMAIL->plugins->exec_hook('login_failed', array(
6d99f9 158       'code' => $error_code, 'host' => $auth['host'], 'user' => $auth['user']));
1854c4 159     $RCMAIL->kill_session();
f11541 160   }
T 161 }
4e17e6 162
de62f0 163 // end session (after optional referer check)
457130 164 else if ($RCMAIL->task == 'logout' && isset($_SESSION['user_id']) && (!$RCMAIL->config->get('referer_check') || rcube_utils::check_referer())) {
c321a9 165   $userdata = array(
T 166     'user' => $_SESSION['username'],
167     'host' => $_SESSION['storage_host'],
168     'lang' => $RCMAIL->user->language,
169   );
f11541 170   $OUTPUT->show_message('loggedout');
1854c4 171   $RCMAIL->logout_actions();
T 172   $RCMAIL->kill_session();
7ef47e 173   $RCMAIL->plugins->exec_hook('logout_after', $userdata);
f11541 174 }
4e17e6 175
bac7d1 176 // check session and auth cookie
9b94eb 177 else if ($RCMAIL->task != 'login' && $_SESSION['user_id'] && $RCMAIL->action != 'send') {
cf2da2 178   if (!$RCMAIL->session->check_auth()) {
1854c4 179     $RCMAIL->kill_session();
fcc7f8 180     $session_error = true;
4e17e6 181   }
f11541 182 }
4e17e6 183
T 184 // not logged in -> show login page
197601 185 if (empty($RCMAIL->user->ID)) {
fcc7f8 186   // log session failures
1aceb9 187   $task = rcube_utils::get_input_value('_task', rcube_utils::INPUT_GPC);
0c2596 188   if ($task && !in_array($task, array('login','logout')) && !$session_error && ($sess_id = $_COOKIE[ini_get('session.name')])) {
fcc7f8 189     $RCMAIL->session->log("Aborted session " . $sess_id . "; no valid session data found");
T 190     $session_error = true;
191   }
192
ec045b 193   if ($OUTPUT->ajax_call)
fcc7f8 194     $OUTPUT->redirect(array('_err' => 'session'), 2000);
9b94eb 195
ccc80d 196   if (!empty($_REQUEST['_framed']))
fcc7f8 197     $OUTPUT->command('redirect', $RCMAIL->url(array('_err' => 'session')));
ccc80d 198
330127 199   // check if installer is still active
83a763 200   if ($RCMAIL->config->get('enable_installer') && is_readable('./installer/index.php')) {
47124c 201     $OUTPUT->add_footer(html::div(array('style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"),
T 202       html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") .
e019f2 203       html::p(null, "The install script of your Roundcube installation is still stored in its default location!") .
A 204       html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the Roundcube directory because .
47124c 205         these files may expose sensitive configuration data like server passwords and encryption keys
T 206         to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.")
207       )
208     );
209   }
9e54e6 210
fcc7f8 211   if ($session_error || $_REQUEST['_err'] == 'session')
T 212     $OUTPUT->show_message('sessionerror', 'error', null, true, -1);
249db1 213
1c0ce1 214   $plugin = $RCMAIL->plugins->exec_hook('unauthenticated', array('task' => 'login', 'error' => $session_error));
AM 215
216   $RCMAIL->set_task($plugin['task']);
217   $OUTPUT->send($plugin['task']);
f11541 218 }
249db1 219 // CSRF prevention
A 220 else {
221   // don't check for valid request tokens in these actions
b80708 222   $request_check_whitelist = array('login'=>1, 'spell'=>1, 'spell_html'=>1);
4e17e6 223
b80708 224   if (!$request_check_whitelist[$RCMAIL->action]) {
AM 225     // check client X-header to verify request origin
226     if ($OUTPUT->ajax_call) {
227       if (rcube_utils::request_header('X-Roundcube-Request') != $RCMAIL->get_request_token()) {
228         header('HTTP/1.1 403 Forbidden');
229         die("Invalid Request");
230       }
249db1 231     }
b80708 232     // check request token in POST form submissions
AM 233     else if (!empty($_POST) && !$RCMAIL->check_request()) {
234       $OUTPUT->show_message('invalidrequest', 'error');
235       $OUTPUT->send($RCMAIL->task);
236     }
a77cf2 237
b80708 238     // check referer if configured
457130 239     if ($RCMAIL->config->get('referer_check') && !rcube_utils::check_referer()) {
b80708 240       raise_error(array(
AM 241         'code' => 403, 'type' => 'php',
242         'message' => "Referer check failed"), true, true);
243     }
a77cf2 244   }
249db1 245 }
4e17e6 246
370302 247 // we're ready, user is authenticated and the request is safe
A 248 $plugin = $RCMAIL->plugins->exec_hook('ready', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
249 $RCMAIL->set_task($plugin['task']);
250 $RCMAIL->action = $plugin['action'];
251
249db1 252 // handle special actions
48aff9 253 if ($RCMAIL->action == 'keep-alive') {
T 254   $OUTPUT->reset();
28ac5c 255   $RCMAIL->plugins->exec_hook('keep_alive', array());
48aff9 256   $OUTPUT->send();
T 257 }
249db1 258 else if ($RCMAIL->action == 'save-pref') {
4351f7 259   include INSTALL_PATH . 'program/steps/utils/save_pref.inc';
249db1 260 }
4e17e6 261
6ea6c9 262
T 263 // include task specific functions
4351f7 264 if (is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/func.inc'))
A 265   include_once $incfile;
4e17e6 266
6ea6c9 267 // allow 5 "redirects" to another action
T 268 $redirects = 0; $incstep = null;
269 while ($redirects < 5) {
cc97ea 270   // execute a plugin action
05a631 271   if ($RCMAIL->plugins->is_plugin_task($RCMAIL->task)) {
87e58c 272     if (!$RCMAIL->action) $RCMAIL->action = 'index';
05a631 273     $RCMAIL->plugins->exec_action($RCMAIL->task.'.'.$RCMAIL->action);
T 274     break;
275   }
276   else if (preg_match('/^plugin\./', $RCMAIL->action)) {
cc97ea 277     $RCMAIL->plugins->exec_action($RCMAIL->action);
T 278     break;
279   }
6ea6c9 280   // try to include the step file
68d2d5 281   else if (($stepfile = $RCMAIL->get_action_file())
4351f7 282     && is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/'.$stepfile)
68d2d5 283   ) {
77de23 284     // include action file only once (in case it don't exit)
AM 285     include_once $incfile;
6ea6c9 286     $redirects++;
T 287   }
288   else {
289     break;
290   }
291 }
4e17e6 292
a95687 293 if ($RCMAIL->action == 'refresh') {
AM 294   $RCMAIL->plugins->exec_hook('refresh', array());
295 }
4e17e6 296
6ea6c9 297 // parse main template (default)
197601 298 $OUTPUT->send($RCMAIL->task);
539cd4 299
T 300
301 // if we arrive here, something went wrong
1aceb9 302 rcmail::raise_error(array(
f11541 303   'code' => 404,
T 304   'type' => 'php',
305   'line' => __LINE__,
306   'file' => __FILE__,
47124c 307   'message' => "Invalid request"), true, true);
b25dfd 308