thomascube
2012-04-04 c04b23adfd3945fe2f177cc4b86f4cbcc325bd96
commit | author | age
4e17e6 1 <?php
T 2 /*
a6f90e 3  +-------------------------------------------------------------------------+
e019f2 4  | Roundcube Webmail IMAP Client                                           |
80216d 5  | Version 0.8-svn                                                         |
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
4e17e6 106   // check if client supports cookies
446364 107   if ($auth['cookiecheck'] && empty($_COOKIE)) {
f11541 108     $OUTPUT->show_message("cookiesdisabled", 'warning');
T 109   }
784a42 110   else if ($auth['valid'] && !$auth['abort'] &&
fdff34 111     $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'])
4cfe66 112   ) {
A 113     // create new session ID, don't destroy the current session
c294ea 114     // it was destroyed already by $RCMAIL->kill_session() above
4cfe66 115     $RCMAIL->session->remove('temp');
c294ea 116     $RCMAIL->session->regenerate_id(false);
aad6e2 117
T 118     // send auth cookie if necessary
cf2da2 119     $RCMAIL->session->set_auth_cookie();
aad6e2 120
5e0045 121     // log successful login
354455 122     rcmail_log_login();
10eedb 123
cc97ea 124     // restore original request parameters
88007c 125     $query = array();
32234d 126     if ($url = get_input_value('_url', RCUBE_INPUT_POST)) {
cc97ea 127       parse_str($url, $query);
c294ea 128
32234d 129       // prevent endless looping on login page
T 130       if ($query['_task'] == 'login')
131         unset($query['_task']);
132     }
cc97ea 133
T 134     // allow plugins to control the redirect url after login success
32234d 135     $redir = $RCMAIL->plugins->exec_hook('login_after', $query + array('_task' => 'mail'));
fcc7f8 136     unset($redir['abort'], $redir['_err']);
5e0045 137
4e17e6 138     // send redirect
cc97ea 139     $OUTPUT->redirect($redir);
4e17e6 140   }
47124c 141   else {
c321a9 142     $error_code = is_object($RCMAIL->storage) ? $RCMAIL->storage->get_error_code() : 1;
6d99f9 143
c321a9 144     $OUTPUT->show_message($error_code < -1 ? 'storageerror' : (!$auth['valid'] ? 'invalidrequest' : 'loginfailed'), 'warning');
8fcc3e 145     $RCMAIL->plugins->exec_hook('login_failed', array(
6d99f9 146       'code' => $error_code, 'host' => $auth['host'], 'user' => $auth['user']));
1854c4 147     $RCMAIL->kill_session();
f11541 148   }
T 149 }
4e17e6 150
de62f0 151 // end session (after optional referer check)
T 152 else if ($RCMAIL->task == 'logout' && isset($_SESSION['user_id']) && (!$RCMAIL->config->get('referer_check') || rcube_check_referer())) {
c321a9 153   $userdata = array(
T 154     'user' => $_SESSION['username'],
155     'host' => $_SESSION['storage_host'],
156     'lang' => $RCMAIL->user->language,
157   );
f11541 158   $OUTPUT->show_message('loggedout');
1854c4 159   $RCMAIL->logout_actions();
T 160   $RCMAIL->kill_session();
7ef47e 161   $RCMAIL->plugins->exec_hook('logout_after', $userdata);
f11541 162 }
4e17e6 163
bac7d1 164 // check session and auth cookie
9b94eb 165 else if ($RCMAIL->task != 'login' && $_SESSION['user_id'] && $RCMAIL->action != 'send') {
cf2da2 166   if (!$RCMAIL->session->check_auth()) {
1854c4 167     $RCMAIL->kill_session();
fcc7f8 168     $session_error = true;
4e17e6 169   }
f11541 170 }
4e17e6 171
T 172 // not logged in -> show login page
197601 173 if (empty($RCMAIL->user->ID)) {
fcc7f8 174   // log session failures
6354da 175   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 176     $RCMAIL->session->log("Aborted session " . $sess_id . "; no valid session data found");
T 177     $session_error = true;
178   }
179
ec045b 180   if ($OUTPUT->ajax_call)
fcc7f8 181     $OUTPUT->redirect(array('_err' => 'session'), 2000);
9b94eb 182
ccc80d 183   if (!empty($_REQUEST['_framed']))
fcc7f8 184     $OUTPUT->command('redirect', $RCMAIL->url(array('_err' => 'session')));
ccc80d 185
330127 186   // check if installer is still active
83a763 187   if ($RCMAIL->config->get('enable_installer') && is_readable('./installer/index.php')) {
47124c 188     $OUTPUT->add_footer(html::div(array('style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"),
T 189       html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") .
e019f2 190       html::p(null, "The install script of your Roundcube installation is still stored in its default location!") .
A 191       html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the Roundcube directory because .
47124c 192         these files may expose sensitive configuration data like server passwords and encryption keys
T 193         to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.")
194       )
195     );
196   }
9e54e6 197
fcc7f8 198   if ($session_error || $_REQUEST['_err'] == 'session')
T 199     $OUTPUT->show_message('sessionerror', 'error', null, true, -1);
249db1 200
784a42 201   $RCMAIL->set_task('login');
f11541 202   $OUTPUT->send('login');
T 203 }
249db1 204 // CSRF prevention
A 205 else {
206   // don't check for valid request tokens in these actions
207   $request_check_whitelist = array('login'=>1, 'spell'=>1);
4e17e6 208
249db1 209   // check client X-header to verify request origin
A 210   if ($OUTPUT->ajax_call) {
ec045b 211     if (rc_request_header('X-Roundcube-Request') != $RCMAIL->get_request_token() && !$RCMAIL->config->get('devel_mode')) {
abdf31 212       header('HTTP/1.1 403 Forbidden');
249db1 213       die("Invalid Request");
A 214     }
215   }
216   // check request token in POST form submissions
217   else if (!empty($_POST) && !$request_check_whitelist[$RCMAIL->action] && !$RCMAIL->check_request()) {
218     $OUTPUT->show_message('invalidrequest', 'error');
219     $OUTPUT->send($RCMAIL->task);
220   }
a77cf2 221
T 222   // check referer if configured
223   if (!$request_check_whitelist[$RCMAIL->action] && $RCMAIL->config->get('referer_check') && !rcube_check_referer()) {
224     raise_error(array(
225       'code' => 403,
226       'type' => 'php',
227       'message' => "Referer check failed"), true, true);
228   }
249db1 229 }
4e17e6 230
370302 231 // we're ready, user is authenticated and the request is safe
A 232 $plugin = $RCMAIL->plugins->exec_hook('ready', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
233 $RCMAIL->set_task($plugin['task']);
234 $RCMAIL->action = $plugin['action'];
235
236
249db1 237 // handle special actions
48aff9 238 if ($RCMAIL->action == 'keep-alive') {
T 239   $OUTPUT->reset();
28ac5c 240   $RCMAIL->plugins->exec_hook('keep_alive', array());
48aff9 241   $OUTPUT->send();
T 242 }
249db1 243 else if ($RCMAIL->action == 'save-pref') {
4351f7 244   include INSTALL_PATH . 'program/steps/utils/save_pref.inc';
249db1 245 }
4e17e6 246
6ea6c9 247
T 248 // include task specific functions
4351f7 249 if (is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/func.inc'))
A 250   include_once $incfile;
4e17e6 251
6ea6c9 252 // allow 5 "redirects" to another action
T 253 $redirects = 0; $incstep = null;
254 while ($redirects < 5) {
cc97ea 255   // execute a plugin action
05a631 256   if ($RCMAIL->plugins->is_plugin_task($RCMAIL->task)) {
87e58c 257     if (!$RCMAIL->action) $RCMAIL->action = 'index';
05a631 258     $RCMAIL->plugins->exec_action($RCMAIL->task.'.'.$RCMAIL->action);
T 259     break;
260   }
261   else if (preg_match('/^plugin\./', $RCMAIL->action)) {
cc97ea 262     $RCMAIL->plugins->exec_action($RCMAIL->action);
T 263     break;
264   }
6ea6c9 265   // try to include the step file
68d2d5 266   else if (($stepfile = $RCMAIL->get_action_file())
4351f7 267     && is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/'.$stepfile)
68d2d5 268   ) {
4351f7 269     include $incfile;
6ea6c9 270     $redirects++;
T 271   }
272   else {
273     break;
274   }
275 }
4e17e6 276
T 277
6ea6c9 278 // parse main template (default)
197601 279 $OUTPUT->send($RCMAIL->task);
539cd4 280
T 281
282 // if we arrive here, something went wrong
f11541 283 raise_error(array(
T 284   'code' => 404,
285   'type' => 'php',
286   'line' => __LINE__,
287   'file' => __FILE__,
47124c 288   'message' => "Invalid request"), true, true);
b25dfd 289