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.

index.php 12KB

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