PageRenderTime 24ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/roundcubemail-0.7.2-dep/index.php

#
PHP | 275 lines | 169 code | 41 blank | 65 comment | 75 complexity | 78c8c7fae12c8e8acf95725b878fa37c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. +-------------------------------------------------------------------------+
  4. | Roundcube Webmail IMAP Client |
  5. | Version 0.7.2 |
  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 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. +-------------------------------------------------------------------------+
  25. $Id: index.php 5995 2012-03-11 16:22:50Z thomasb $
  26. */
  27. // include environment
  28. require_once 'program/include/iniset.php';
  29. // init application, start session, init output class, etc.
  30. $RCMAIL = rcmail::get_instance();
  31. // Make the whole PHP output non-cacheable (#1487797)
  32. send_nocacheing_headers();
  33. // turn on output buffering
  34. ob_start();
  35. // check if config files had errors
  36. if ($err_str = $RCMAIL->config->get_error()) {
  37. raise_error(array(
  38. 'code' => 601,
  39. 'type' => 'php',
  40. 'message' => $err_str), false, true);
  41. }
  42. // check DB connections and exit on failure
  43. if ($err_str = $DB->is_error()) {
  44. raise_error(array(
  45. 'code' => 603,
  46. 'type' => 'db',
  47. 'message' => $err_str), FALSE, TRUE);
  48. }
  49. // error steps
  50. if ($RCMAIL->action=='error' && !empty($_GET['_code'])) {
  51. raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
  52. }
  53. // check if https is required (for login) and redirect if necessary
  54. if (empty($_SESSION['user_id']) && ($force_https = $RCMAIL->config->get('force_https', false))) {
  55. $https_port = is_bool($force_https) ? 443 : $force_https;
  56. if (!rcube_https_check($https_port)) {
  57. $host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
  58. $host .= ($https_port != 443 ? ':' . $https_port : '');
  59. header('Location: https://' . $host . $_SERVER['REQUEST_URI']);
  60. exit;
  61. }
  62. }
  63. // trigger startup plugin hook
  64. $startup = $RCMAIL->plugins->exec_hook('startup', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
  65. $RCMAIL->set_task($startup['task']);
  66. $RCMAIL->action = $startup['action'];
  67. // try to log in
  68. if ($RCMAIL->task == 'login' && $RCMAIL->action == 'login') {
  69. $request_valid = $_SESSION['temp'] && $RCMAIL->check_request(RCUBE_INPUT_POST, 'login');
  70. // purge the session in case of new login when a session already exists
  71. $RCMAIL->kill_session();
  72. $auth = $RCMAIL->plugins->exec_hook('authenticate', array(
  73. 'host' => $RCMAIL->autoselect_host(),
  74. 'user' => trim(get_input_value('_user', RCUBE_INPUT_POST)),
  75. 'pass' => get_input_value('_pass', RCUBE_INPUT_POST, true,
  76. $RCMAIL->config->get('password_charset', 'ISO-8859-1')),
  77. 'cookiecheck' => true,
  78. 'valid' => $request_valid,
  79. ));
  80. // check if client supports cookies
  81. if ($auth['cookiecheck'] && empty($_COOKIE)) {
  82. $OUTPUT->show_message("cookiesdisabled", 'warning');
  83. }
  84. else if ($auth['valid'] && !$auth['abort'] &&
  85. !empty($auth['host']) && !empty($auth['user']) &&
  86. $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'])
  87. ) {
  88. // create new session ID, don't destroy the current session
  89. // it was destroyed already by $RCMAIL->kill_session() above
  90. $RCMAIL->session->remove('temp');
  91. $RCMAIL->session->regenerate_id(false);
  92. // send auth cookie if necessary
  93. $RCMAIL->session->set_auth_cookie();
  94. // log successful login
  95. rcmail_log_login();
  96. // restore original request parameters
  97. $query = array();
  98. if ($url = get_input_value('_url', RCUBE_INPUT_POST)) {
  99. parse_str($url, $query);
  100. // prevent endless looping on login page
  101. if ($query['_task'] == 'login')
  102. unset($query['_task']);
  103. }
  104. // allow plugins to control the redirect url after login success
  105. $redir = $RCMAIL->plugins->exec_hook('login_after', $query + array('_task' => 'mail'));
  106. unset($redir['abort'], $redir['_err']);
  107. // send redirect
  108. $OUTPUT->redirect($redir);
  109. }
  110. else {
  111. $error_code = is_object($IMAP) ? $IMAP->get_error_code() : -1;
  112. $OUTPUT->show_message($error_code < -1 ? 'imaperror' : (!$auth['valid'] ? 'invalidrequest' : 'loginfailed'), 'warning');
  113. $RCMAIL->plugins->exec_hook('login_failed', array(
  114. 'code' => $error_code, 'host' => $auth['host'], 'user' => $auth['user']));
  115. $RCMAIL->kill_session();
  116. }
  117. }
  118. // end session (after optional referer check)
  119. else if ($RCMAIL->task == 'logout' && isset($_SESSION['user_id']) && (!$RCMAIL->config->get('referer_check') || rcube_check_referer())) {
  120. $userdata = array('user' => $_SESSION['username'], 'host' => $_SESSION['imap_host'], 'lang' => $RCMAIL->user->language);
  121. $OUTPUT->show_message('loggedout');
  122. $RCMAIL->logout_actions();
  123. $RCMAIL->kill_session();
  124. $RCMAIL->plugins->exec_hook('logout_after', $userdata);
  125. }
  126. // check session and auth cookie
  127. else if ($RCMAIL->task != 'login' && $_SESSION['user_id'] && $RCMAIL->action != 'send') {
  128. if (!$RCMAIL->session->check_auth()) {
  129. $RCMAIL->kill_session();
  130. $session_error = true;
  131. }
  132. }
  133. // not logged in -> show login page
  134. if (empty($RCMAIL->user->ID)) {
  135. // log session failures
  136. if (($task = get_input_value('_task', RCUBE_INPUT_GPC)) && !in_array($task, array('login','logout')) && !$session_error && ($sess_id = $_COOKIE[ini_get('session.name')])) {
  137. $RCMAIL->session->log("Aborted session " . $sess_id . "; no valid session data found");
  138. $session_error = true;
  139. }
  140. if ($OUTPUT->ajax_call)
  141. $OUTPUT->redirect(array('_err' => 'session'), 2000);
  142. if (!empty($_REQUEST['_framed']))
  143. $OUTPUT->command('redirect', $RCMAIL->url(array('_err' => 'session')));
  144. // check if installer is still active
  145. if ($RCMAIL->config->get('enable_installer') && is_readable('./installer/index.php')) {
  146. $OUTPUT->add_footer(html::div(array('style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"),
  147. html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") .
  148. html::p(null, "The install script of your Roundcube installation is still stored in its default location!") .
  149. html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the Roundcube directory because .
  150. these files may expose sensitive configuration data like server passwords and encryption keys
  151. to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.")
  152. )
  153. );
  154. }
  155. if ($session_error || $_REQUEST['_err'] == 'session')
  156. $OUTPUT->show_message('sessionerror', 'error', null, true, -1);
  157. $RCMAIL->set_task('login');
  158. $OUTPUT->send('login');
  159. }
  160. // CSRF prevention
  161. else {
  162. // don't check for valid request tokens in these actions
  163. $request_check_whitelist = array('login'=>1, 'spell'=>1);
  164. // check client X-header to verify request origin
  165. if ($OUTPUT->ajax_call) {
  166. if (rc_request_header('X-Roundcube-Request') != $RCMAIL->get_request_token() && !$RCMAIL->config->get('devel_mode')) {
  167. header('HTTP/1.1 403 Forbidden');
  168. die("Invalid Request");
  169. }
  170. }
  171. // check request token in POST form submissions
  172. else if (!empty($_POST) && !$request_check_whitelist[$RCMAIL->action] && !$RCMAIL->check_request()) {
  173. $OUTPUT->show_message('invalidrequest', 'error');
  174. $OUTPUT->send($RCMAIL->task);
  175. }
  176. // check referer if configured
  177. if (!$request_check_whitelist[$RCMAIL->action] && $RCMAIL->config->get('referer_check') && !rcube_check_referer()) {
  178. raise_error(array(
  179. 'code' => 403,
  180. 'type' => 'php',
  181. 'message' => "Referer check failed"), true, true);
  182. }
  183. }
  184. // we're ready, user is authenticated and the request is safe
  185. $plugin = $RCMAIL->plugins->exec_hook('ready', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
  186. $RCMAIL->set_task($plugin['task']);
  187. $RCMAIL->action = $plugin['action'];
  188. // handle special actions
  189. if ($RCMAIL->action == 'keep-alive') {
  190. $OUTPUT->reset();
  191. $RCMAIL->plugins->exec_hook('keep_alive', array());
  192. $OUTPUT->send();
  193. }
  194. else if ($RCMAIL->action == 'save-pref') {
  195. include INSTALL_PATH . 'program/steps/utils/save_pref.inc';
  196. }
  197. // include task specific functions
  198. if (is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/func.inc'))
  199. include_once $incfile;
  200. // allow 5 "redirects" to another action
  201. $redirects = 0; $incstep = null;
  202. while ($redirects < 5) {
  203. // execute a plugin action
  204. if ($RCMAIL->plugins->is_plugin_task($RCMAIL->task)) {
  205. if (!$RCMAIL->action) $RCMAIL->action = 'index';
  206. $RCMAIL->plugins->exec_action($RCMAIL->task.'.'.$RCMAIL->action);
  207. break;
  208. }
  209. else if (preg_match('/^plugin\./', $RCMAIL->action)) {
  210. $RCMAIL->plugins->exec_action($RCMAIL->action);
  211. break;
  212. }
  213. // try to include the step file
  214. else if (($stepfile = $RCMAIL->get_action_file())
  215. && is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/'.$stepfile)
  216. ) {
  217. include $incfile;
  218. $redirects++;
  219. }
  220. else {
  221. break;
  222. }
  223. }
  224. // parse main template (default)
  225. $OUTPUT->send($RCMAIL->task);
  226. // if we arrive here, something went wrong
  227. raise_error(array(
  228. 'code' => 404,
  229. 'type' => 'php',
  230. 'line' => __LINE__,
  231. 'file' => __FILE__,
  232. 'message' => "Invalid request"), true, true);