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