RoundCube Webmail
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
11 KiB

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