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.

main.php 12KB

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