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