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