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

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