Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /*
  3. +-------------------------------------------------------------------------+
  4. | Roundcube Webmail IMAP Client |
  5. | Version 0.9.5 |
  6. | |
  7. | Copyright (C) 2005-2013, 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. // include environment
  38. require_once 'program/include/iniset.php';
  39. // init application, start session, init output class, etc.
  40. $RCMAIL = rcmail::get_instance();
  41. // Make the whole PHP output non-cacheable (#1487797)
  42. $RCMAIL->output->nocacheing_headers();
  43. // turn on output buffering
  44. ob_start();
  45. // check if config files had errors
  46. if ($err_str = $RCMAIL->config->get_error()) {
  47. rcmail::raise_error(array(
  48. 'code' => 601,
  49. 'type' => 'php',
  50. 'message' => $err_str), false, true);
  51. }
  52. // check DB connections and exit on failure
  53. if ($err_str = $RCMAIL->db->is_error()) {
  54. rcmail::raise_error(array(
  55. 'code' => 603,
  56. 'type' => 'db',
  57. 'message' => $err_str), FALSE, TRUE);
  58. }
  59. // error steps
  60. if ($RCMAIL->action == 'error' && !empty($_GET['_code'])) {
  61. rcmail::raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE);
  62. }
  63. // check if https is required (for login) and redirect if necessary
  64. if (empty($_SESSION['user_id']) && ($force_https = $RCMAIL->config->get('force_https', false))) {
  65. $https_port = is_bool($force_https) ? 443 : $force_https;
  66. if (!rcube_utils::https_check($https_port)) {
  67. $host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
  68. $host .= ($https_port != 443 ? ':' . $https_port : '');
  69. header('Location: https://' . $host . $_SERVER['REQUEST_URI']);
  70. exit;
  71. }
  72. }
  73. // trigger startup plugin hook
  74. $startup = $RCMAIL->plugins->exec_hook('startup', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
  75. $RCMAIL->set_task($startup['task']);
  76. $RCMAIL->action = $startup['action'];
  77. // try to log in
  78. if ($RCMAIL->task == 'login' && $RCMAIL->action == 'login') {
  79. $request_valid = $_SESSION['temp'] && $RCMAIL->check_request(rcube_utils::INPUT_POST, 'login');
  80. // purge the session in case of new login when a session already exists
  81. $RCMAIL->kill_session();
  82. $auth = $RCMAIL->plugins->exec_hook('authenticate', array(
  83. 'host' => $RCMAIL->autoselect_host(),
  84. 'user' => trim(rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST)),
  85. 'pass' => rcube_utils::get_input_value('_pass', rcube_utils::INPUT_POST, true,
  86. $RCMAIL->config->get('password_charset', 'ISO-8859-1')),
  87. 'cookiecheck' => true,
  88. 'valid' => $request_valid,
  89. ));
  90. // Login
  91. if ($auth['valid'] && !$auth['abort'] &&
  92. $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'], $auth['cookiecheck'])
  93. ) {
  94. // create new session ID, don't destroy the current session
  95. // it was destroyed already by $RCMAIL->kill_session() above
  96. $RCMAIL->session->remove('temp');
  97. $RCMAIL->session->regenerate_id(false);
  98. // send auth cookie if necessary
  99. $RCMAIL->session->set_auth_cookie();
  100. // log successful login
  101. $RCMAIL->log_login();
  102. // restore original request parameters
  103. $query = array();
  104. if ($url = rcube_utils::get_input_value('_url', rcube_utils::INPUT_POST)) {
  105. parse_str($url, $query);
  106. // prevent endless looping on login page
  107. if ($query['_task'] == 'login')
  108. unset($query['_task']);
  109. // prevent redirect to compose with specified ID (#1488226)
  110. if ($query['_action'] == 'compose' && !empty($query['_id']))
  111. $query = array();
  112. }
  113. // allow plugins to control the redirect url after login success
  114. $redir = $RCMAIL->plugins->exec_hook('login_after', $query + array('_task' => 'mail'));
  115. unset($redir['abort'], $redir['_err']);
  116. // send redirect
  117. $OUTPUT->redirect($redir);
  118. }
  119. else {
  120. if (!$auth['valid']) {
  121. $error_code = RCMAIL::ERROR_INVALID_REQUEST;
  122. }
  123. else {
  124. $error_code = $auth['error'] ? $auth['error'] : $RCMAIL->login_error();
  125. }
  126. $error_labels = array(
  127. RCMAIL::ERROR_STORAGE => 'storageerror',
  128. RCMAIL::ERROR_COOKIES_DISABLED => 'cookiesdisabled',
  129. RCMAIL::ERROR_INVALID_REQUEST => 'invalidrequest',
  130. RCMAIL::ERROR_INVALID_HOST => 'invalidhost',
  131. );
  132. $error_message = $error_labels[$error_code] ? $error_labels[$error_code] : 'loginfailed';
  133. $OUTPUT->show_message($error_message, 'warning');
  134. $RCMAIL->plugins->exec_hook('login_failed', array(
  135. 'code' => $error_code, 'host' => $auth['host'], 'user' => $auth['user']));
  136. $RCMAIL->kill_session();
  137. }
  138. }
  139. // end session (after optional referer check)
  140. else if ($RCMAIL->task == 'logout' && isset($_SESSION['user_id']) && (!$RCMAIL->config->get('referer_check') || rcube_utils::check_referer())) {
  141. $userdata = array(
  142. 'user' => $_SESSION['username'],
  143. 'host' => $_SESSION['storage_host'],
  144. 'lang' => $RCMAIL->user->language,
  145. );
  146. $OUTPUT->show_message('loggedout');
  147. $RCMAIL->logout_actions();
  148. $RCMAIL->kill_session();
  149. $RCMAIL->plugins->exec_hook('logout_after', $userdata);
  150. }
  151. // check session and auth cookie
  152. else if ($RCMAIL->task != 'login' && $_SESSION['user_id'] && $RCMAIL->action != 'send') {
  153. if (!$RCMAIL->session->check_auth()) {
  154. $RCMAIL->kill_session();
  155. $session_error = true;
  156. }
  157. }
  158. // not logged in -> show login page
  159. if (empty($RCMAIL->user->ID)) {
  160. // log session failures
  161. $task = rcube_utils::get_input_value('_task', rcube_utils::INPUT_GPC);
  162. if ($task && !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. // don't check for valid request tokens in these actions
  190. $request_check_whitelist = array('login'=>1, 'spell'=>1, 'spell_html'=>1);
  191. if (!$request_check_whitelist[$RCMAIL->action]) {
  192. // check client X-header to verify request origin
  193. if ($OUTPUT->ajax_call) {
  194. if (rcube_utils::request_header('X-Roundcube-Request') != $RCMAIL->get_request_token()) {
  195. header('HTTP/1.1 403 Forbidden');
  196. die("Invalid Request");
  197. }
  198. }
  199. // check request token in POST form submissions
  200. else if (!empty($_POST) && !$RCMAIL->check_request()) {
  201. $OUTPUT->show_message('invalidrequest', 'error');
  202. $OUTPUT->send($RCMAIL->task);
  203. }
  204. // check referer if configured
  205. if ($RCMAIL->config->get('referer_check') && !rcube_utils::check_referer()) {
  206. raise_error(array(
  207. 'code' => 403, 'type' => 'php',
  208. 'message' => "Referer check failed"), true, true);
  209. }
  210. }
  211. }
  212. // we're ready, user is authenticated and the request is safe
  213. $plugin = $RCMAIL->plugins->exec_hook('ready', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action));
  214. $RCMAIL->set_task($plugin['task']);
  215. $RCMAIL->action = $plugin['action'];
  216. // handle special actions
  217. if ($RCMAIL->action == 'keep-alive') {
  218. $OUTPUT->reset();
  219. $RCMAIL->plugins->exec_hook('keep_alive', array());
  220. $OUTPUT->send();
  221. }
  222. else if ($RCMAIL->action == 'save-pref') {
  223. include INSTALL_PATH . 'program/steps/utils/save_pref.inc';
  224. }
  225. // include task specific functions
  226. if (is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/func.inc'))
  227. include_once $incfile;
  228. // allow 5 "redirects" to another action
  229. $redirects = 0; $incstep = null;
  230. while ($redirects < 5) {
  231. // execute a plugin action
  232. if ($RCMAIL->plugins->is_plugin_task($RCMAIL->task)) {
  233. if (!$RCMAIL->action) $RCMAIL->action = 'index';
  234. $RCMAIL->plugins->exec_action($RCMAIL->task.'.'.$RCMAIL->action);
  235. break;
  236. }
  237. else if (preg_match('/^plugin\./', $RCMAIL->action)) {
  238. $RCMAIL->plugins->exec_action($RCMAIL->action);
  239. break;
  240. }
  241. // try to include the step file
  242. else if (($stepfile = $RCMAIL->get_action_file())
  243. && is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/'.$stepfile)
  244. ) {
  245. // include action file only once (in case it don't exit)
  246. include_once $incfile;
  247. $redirects++;
  248. }
  249. else {
  250. break;
  251. }
  252. }
  253. if ($RCMAIL->action == 'refresh') {
  254. $RCMAIL->plugins->exec_hook('refresh', array());
  255. }
  256. // parse main template (default)
  257. $OUTPUT->send($RCMAIL->task);
  258. // if we arrive here, something went wrong
  259. rcmail::raise_error(array(
  260. 'code' => 404,
  261. 'type' => 'php',
  262. 'line' => __LINE__,
  263. 'file' => __FILE__,
  264. 'message' => "Invalid request"), true, true);