source: subversion/trunk/roundcubemail/index.php

Last change on this file was 6128, checked in by alec, 13 months ago
  • Fix redirect to mail/compose on re-login (1488226)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1<?php
2/*
3 +-------------------------------------------------------------------------+
4 | Roundcube Webmail IMAP Client                                           |
5 | Version 0.9-svn                                                         |
6 |                                                                         |
7 | Copyright (C) 2005-2012, The Roundcube Dev Team                         |
8 |                                                                         |
9 | This program is free software: you can redistribute it and/or modify    |
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.                                        |
24 |                                                                         |
25 | This program is distributed in the hope that it will be useful,         |
26 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
27 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            |
28 | GNU General Public License for more details.                            |
29 |                                                                         |
30 | You should have received a copy of the GNU General Public License       |
31 | along with this program.  If not, see http://www.gnu.org/licenses/.     |
32 |                                                                         |
33 +-------------------------------------------------------------------------+
34 | Author: Thomas Bruederli <roundcube@gmail.com>                          |
35 +-------------------------------------------------------------------------+
36
37 $Id$
38
39*/
40
41// include environment
42require_once 'program/include/iniset.php';
43
44// init application, start session, init output class, etc.
45$RCMAIL = rcmail::get_instance();
46
47// Make the whole PHP output non-cacheable (#1487797)
48$RCMAIL->output->nocacheing_headers();
49
50// turn on output buffering
51ob_start();
52
53// check if config files had errors
54if ($err_str = $RCMAIL->config->get_error()) {
55  rcmail::raise_error(array(
56    'code' => 601,
57    'type' => 'php',
58    'message' => $err_str), false, true);
59}
60
61// check DB connections and exit on failure
62if ($err_str = $RCMAIL->db->is_error()) {
63  rcmail::raise_error(array(
64    'code' => 603,
65    'type' => 'db',
66    'message' => $err_str), FALSE, TRUE);
67}
68
69// error steps
70if ($RCMAIL->action == 'error' && !empty($_GET['_code'])) {
71  rcmail::raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
72}
73
74// check if https is required (for login) and redirect if necessary
75if (empty($_SESSION['user_id']) && ($force_https = $RCMAIL->config->get('force_https', false))) {
76  $https_port = is_bool($force_https) ? 443 : $force_https;
77  if (!rcube_utils::https_check($https_port)) {
78    $host  = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
79    $host .= ($https_port != 443 ? ':' . $https_port : '');
80    header('Location: https://' . $host . $_SERVER['REQUEST_URI']);
81    exit;
82  }
83}
84
85// trigger startup plugin hook
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
90// try to log in
91if ($RCMAIL->task == 'login' && $RCMAIL->action == 'login') {
92  $request_valid = $_SESSION['temp'] && $RCMAIL->check_request(rcube_utils::INPUT_POST, 'login');
93
94  // purge the session in case of new login when a session already exists
95  $RCMAIL->kill_session();
96
97  $auth = $RCMAIL->plugins->exec_hook('authenticate', array(
98    'host' => $RCMAIL->autoselect_host(),
99    'user' => trim(rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST)),
100    'pass' => rcube_utils::get_input_value('_pass', rcube_utils::INPUT_POST, true,
101       $RCMAIL->config->get('password_charset', 'ISO-8859-1')),
102    'cookiecheck' => true,
103    'valid' => $request_valid,
104  ));
105
106  // check if client supports cookies
107  if ($auth['cookiecheck'] && empty($_COOKIE)) {
108    $OUTPUT->show_message("cookiesdisabled", 'warning');
109  }
110  else if ($auth['valid'] && !$auth['abort'] &&
111    $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'])
112  ) {
113    // create new session ID, don't destroy the current session
114    // it was destroyed already by $RCMAIL->kill_session() above
115    $RCMAIL->session->remove('temp');
116    $RCMAIL->session->regenerate_id(false);
117
118    // send auth cookie if necessary
119    $RCMAIL->session->set_auth_cookie();
120
121    // log successful login
122    $RCMAIL->log_login();
123
124    // restore original request parameters
125    $query = array();
126    if ($url = rcube_utils::get_input_value('_url', rcube_utils::INPUT_POST)) {
127      parse_str($url, $query);
128
129      // prevent endless looping on login page
130      if ($query['_task'] == 'login')
131        unset($query['_task']);
132
133      // prevent redirect to compose with specified ID (#1488226)
134      if ($query['_action'] == 'compose' && !empty($query['_id']))
135        $query = array();
136    }
137
138    // allow plugins to control the redirect url after login success
139    $redir = $RCMAIL->plugins->exec_hook('login_after', $query + array('_task' => 'mail'));
140    unset($redir['abort'], $redir['_err']);
141
142    // send redirect
143    $OUTPUT->redirect($redir);
144  }
145  else {
146    $error_code = is_object($RCMAIL->storage) ? $RCMAIL->storage->get_error_code() : 1;
147
148    $OUTPUT->show_message($error_code < -1 ? 'storageerror' : (!$auth['valid'] ? 'invalidrequest' : 'loginfailed'), 'warning');
149    $RCMAIL->plugins->exec_hook('login_failed', array(
150      'code' => $error_code, 'host' => $auth['host'], 'user' => $auth['user']));
151    $RCMAIL->kill_session();
152  }
153}
154
155// end session (after optional referer check)
156else if ($RCMAIL->task == 'logout' && isset($_SESSION['user_id']) && (!$RCMAIL->config->get('referer_check') || rcmail::check_referer())) {
157  $userdata = array(
158    'user' => $_SESSION['username'],
159    'host' => $_SESSION['storage_host'],
160    'lang' => $RCMAIL->user->language,
161  );
162  $OUTPUT->show_message('loggedout');
163  $RCMAIL->logout_actions();
164  $RCMAIL->kill_session();
165  $RCMAIL->plugins->exec_hook('logout_after', $userdata);
166}
167
168// check session and auth cookie
169else if ($RCMAIL->task != 'login' && $_SESSION['user_id'] && $RCMAIL->action != 'send') {
170  if (!$RCMAIL->session->check_auth()) {
171    $RCMAIL->kill_session();
172    $session_error = true;
173  }
174}
175
176// not logged in -> show login page
177if (empty($RCMAIL->user->ID)) {
178  // log session failures
179  $task = rcube_utils::get_input_value('_task', rcube_utils::INPUT_GPC);
180  if ($task && !in_array($task, array('login','logout')) && !$session_error && ($sess_id = $_COOKIE[ini_get('session.name')])) {
181    $RCMAIL->session->log("Aborted session " . $sess_id . "; no valid session data found");
182    $session_error = true;
183  }
184
185  if ($OUTPUT->ajax_call)
186    $OUTPUT->redirect(array('_err' => 'session'), 2000);
187
188  if (!empty($_REQUEST['_framed']))
189    $OUTPUT->command('redirect', $RCMAIL->url(array('_err' => 'session')));
190
191  // check if installer is still active
192  if ($RCMAIL->config->get('enable_installer') && is_readable('./installer/index.php')) {
193    $OUTPUT->add_footer(html::div(array('style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"),
194      html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") .
195      html::p(null, "The install script of your Roundcube installation is still stored in its default location!") .
196      html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the Roundcube directory because .
197        these files may expose sensitive configuration data like server passwords and encryption keys
198        to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.")
199      )
200    );
201  }
202
203  if ($session_error || $_REQUEST['_err'] == 'session')
204    $OUTPUT->show_message('sessionerror', 'error', null, true, -1);
205
206  $RCMAIL->set_task('login');
207  $OUTPUT->send('login');
208}
209// CSRF prevention
210else {
211  // don't check for valid request tokens in these actions
212  $request_check_whitelist = array('login'=>1, 'spell'=>1);
213
214  // check client X-header to verify request origin
215  if ($OUTPUT->ajax_call) {
216    if (rcube_utils::request_header('X-Roundcube-Request') != $RCMAIL->get_request_token() && !$RCMAIL->config->get('devel_mode')) {
217      header('HTTP/1.1 403 Forbidden');
218      die("Invalid Request");
219    }
220  }
221  // check request token in POST form submissions
222  else if (!empty($_POST) && !$request_check_whitelist[$RCMAIL->action] && !$RCMAIL->check_request()) {
223    $OUTPUT->show_message('invalidrequest', 'error');
224    $OUTPUT->send($RCMAIL->task);
225  }
226
227  // check referer if configured
228  if (!$request_check_whitelist[$RCMAIL->action] && $RCMAIL->config->get('referer_check') && !rcmail::check_referer()) {
229    raise_error(array(
230      'code' => 403,
231      'type' => 'php',
232      'message' => "Referer check failed"), true, true);
233  }
234}
235
236// we're ready, user is authenticated and the request is safe
237$plugin = $RCMAIL->plugins->exec_hook('ready', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
238$RCMAIL->set_task($plugin['task']);
239$RCMAIL->action = $plugin['action'];
240
241
242// handle special actions
243if ($RCMAIL->action == 'keep-alive') {
244  $OUTPUT->reset();
245  $RCMAIL->plugins->exec_hook('keep_alive', array());
246  $OUTPUT->send();
247}
248else if ($RCMAIL->action == 'save-pref') {
249  include INSTALL_PATH . 'program/steps/utils/save_pref.inc';
250}
251
252
253// include task specific functions
254if (is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/func.inc'))
255  include_once $incfile;
256
257// allow 5 "redirects" to another action
258$redirects = 0; $incstep = null;
259while ($redirects < 5) {
260  // execute a plugin action
261  if ($RCMAIL->plugins->is_plugin_task($RCMAIL->task)) {
262    if (!$RCMAIL->action) $RCMAIL->action = 'index';
263    $RCMAIL->plugins->exec_action($RCMAIL->task.'.'.$RCMAIL->action);
264    break;
265  }
266  else if (preg_match('/^plugin\./', $RCMAIL->action)) {
267    $RCMAIL->plugins->exec_action($RCMAIL->action);
268    break;
269  }
270  // try to include the step file
271  else if (($stepfile = $RCMAIL->get_action_file())
272    && is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/'.$stepfile)
273  ) {
274    include $incfile;
275    $redirects++;
276  }
277  else {
278    break;
279  }
280}
281
282
283// parse main template (default)
284$OUTPUT->send($RCMAIL->task);
285
286
287// if we arrive here, something went wrong
288rcmail::raise_error(array(
289  'code' => 404,
290  'type' => 'php',
291  'line' => __LINE__,
292  'file' => __FILE__,
293  'message' => "Invalid request"), true, true);
294
Note: See TracBrowser for help on using the repository browser.