commit | author | age
|
4e17e6
|
1 |
<?php |
T |
2 |
/* |
a6f90e
|
3 |
+-------------------------------------------------------------------------+ |
e019f2
|
4 |
| Roundcube Webmail IMAP Client | |
65f59f
|
5 |
| Version 1.1-git | |
a6f90e
|
6 |
| | |
65f59f
|
7 |
| Copyright (C) 2005-2014, The Roundcube Dev Team | |
a6f90e
|
8 |
| | |
7fe381
|
9 |
| This program is free software: you can redistribute it and/or modify | |
T |
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. | |
a6f90e
|
24 |
| | |
A |
25 |
| This program is distributed in the hope that it will be useful, | |
|
26 |
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |
7fe381
|
27 |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
a6f90e
|
28 |
| GNU General Public License for more details. | |
A |
29 |
| | |
7fe381
|
30 |
| You should have received a copy of the GNU General Public License | |
T |
31 |
| along with this program. If not, see http://www.gnu.org/licenses/. | |
a6f90e
|
32 |
| | |
A |
33 |
+-------------------------------------------------------------------------+ |
|
34 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
0301d9
|
35 |
| Author: Aleksander Machniak <alec@alec.pl> | |
a6f90e
|
36 |
+-------------------------------------------------------------------------+ |
4e17e6
|
37 |
*/ |
15a9d1
|
38 |
|
47124c
|
39 |
// include environment |
T |
40 |
require_once 'program/include/iniset.php'; |
15a9d1
|
41 |
|
48bc52
|
42 |
// init application, start session, init output class, etc. |
deb2b8
|
43 |
$RCMAIL = rcmail::get_instance($GLOBALS['env']); |
83a763
|
44 |
|
9e54e6
|
45 |
// Make the whole PHP output non-cacheable (#1487797) |
0c2596
|
46 |
$RCMAIL->output->nocacheing_headers(); |
9e54e6
|
47 |
|
d51c93
|
48 |
// turn on output buffering |
A |
49 |
ob_start(); |
8affba
|
50 |
|
8c72e3
|
51 |
// check if config files had errors |
T |
52 |
if ($err_str = $RCMAIL->config->get_error()) { |
0301d9
|
53 |
rcmail::raise_error(array( |
AM |
54 |
'code' => 601, |
|
55 |
'type' => 'php', |
|
56 |
'message' => $err_str), false, true); |
8c72e3
|
57 |
} |
T |
58 |
|
8affba
|
59 |
// check DB connections and exit on failure |
c321a9
|
60 |
if ($err_str = $RCMAIL->db->is_error()) { |
0301d9
|
61 |
rcmail::raise_error(array( |
AM |
62 |
'code' => 603, |
|
63 |
'type' => 'db', |
|
64 |
'message' => $err_str), FALSE, TRUE); |
f11541
|
65 |
} |
8affba
|
66 |
|
4e17e6
|
67 |
// error steps |
0c2596
|
68 |
if ($RCMAIL->action == 'error' && !empty($_GET['_code'])) { |
0301d9
|
69 |
rcmail::raise_error(array('code' => hexdec($_GET['_code'])), FALSE, TRUE); |
47124c
|
70 |
} |
570f0b
|
71 |
|
f5d61d
|
72 |
// check if https is required (for login) and redirect if necessary |
T |
73 |
if (empty($_SESSION['user_id']) && ($force_https = $RCMAIL->config->get('force_https', false))) { |
0301d9
|
74 |
$https_port = is_bool($force_https) ? 443 : $force_https; |
AM |
75 |
|
|
76 |
if (!rcube_utils::https_check($https_port)) { |
|
77 |
$host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']); |
|
78 |
$host .= ($https_port != 443 ? ':' . $https_port : ''); |
|
79 |
|
|
80 |
header('Location: https://' . $host . $_SERVER['REQUEST_URI']); |
|
81 |
exit; |
|
82 |
} |
f5d61d
|
83 |
} |
T |
84 |
|
cc97ea
|
85 |
// trigger startup plugin hook |
T |
86 |
$startup = $RCMAIL->plugins->exec_hook('startup', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action)); |
|
87 |
$RCMAIL->set_task($startup['task']); |
|
88 |
$RCMAIL->action = $startup['action']; |
|
89 |
|
4e17e6
|
90 |
// try to log in |
9b94eb
|
91 |
if ($RCMAIL->task == 'login' && $RCMAIL->action == 'login') { |
0301d9
|
92 |
$request_valid = $_SESSION['temp'] && $RCMAIL->check_request(rcube_utils::INPUT_POST, 'login'); |
784a42
|
93 |
|
0301d9
|
94 |
// purge the session in case of new login when a session already exists |
AM |
95 |
$RCMAIL->kill_session(); |
5f560e
|
96 |
|
0301d9
|
97 |
$auth = $RCMAIL->plugins->exec_hook('authenticate', array( |
AM |
98 |
'host' => $RCMAIL->autoselect_host(), |
|
99 |
'user' => trim(rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST)), |
|
100 |
'pass' => rcube_utils::get_input_value('_pass', rcube_utils::INPUT_POST, true, |
|
101 |
$RCMAIL->config->get('password_charset', 'ISO-8859-1')), |
|
102 |
'cookiecheck' => true, |
|
103 |
'valid' => $request_valid, |
|
104 |
)); |
cc97ea
|
105 |
|
0301d9
|
106 |
// Login |
AM |
107 |
if ($auth['valid'] && !$auth['abort'] |
|
108 |
&& $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'], $auth['cookiecheck']) |
|
109 |
) { |
|
110 |
// create new session ID, don't destroy the current session |
|
111 |
// it was destroyed already by $RCMAIL->kill_session() above |
|
112 |
$RCMAIL->session->remove('temp'); |
|
113 |
$RCMAIL->session->regenerate_id(false); |
aad6e2
|
114 |
|
0301d9
|
115 |
// send auth cookie if necessary |
AM |
116 |
$RCMAIL->session->set_auth_cookie(); |
aad6e2
|
117 |
|
0301d9
|
118 |
// log successful login |
AM |
119 |
$RCMAIL->log_login(); |
10eedb
|
120 |
|
0301d9
|
121 |
// restore original request parameters |
d2191c
|
122 |
$query = array(); |
0301d9
|
123 |
if ($url = rcube_utils::get_input_value('_url', rcube_utils::INPUT_POST)) { |
AM |
124 |
parse_str($url, $query); |
cc97ea
|
125 |
|
0301d9
|
126 |
// prevent endless looping on login page |
AM |
127 |
if ($query['_task'] == 'login') { |
|
128 |
unset($query['_task']); |
|
129 |
} |
5e0045
|
130 |
|
0301d9
|
131 |
// prevent redirect to compose with specified ID (#1488226) |
AM |
132 |
if ($query['_action'] == 'compose' && !empty($query['_id'])) { |
|
133 |
$query = array(); |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
// allow plugins to control the redirect url after login success |
|
138 |
$redir = $RCMAIL->plugins->exec_hook('login_after', $query + array('_task' => 'mail')); |
|
139 |
unset($redir['abort'], $redir['_err']); |
|
140 |
|
|
141 |
// send redirect |
|
142 |
$OUTPUT->redirect($redir); |
7c8fd8
|
143 |
} |
AM |
144 |
else { |
0301d9
|
145 |
if (!$auth['valid']) { |
AM |
146 |
$error_code = RCMAIL::ERROR_INVALID_REQUEST; |
|
147 |
} |
|
148 |
else { |
|
149 |
$error_code = $auth['error'] ? $auth['error'] : $RCMAIL->login_error(); |
|
150 |
} |
|
151 |
|
|
152 |
$error_labels = array( |
|
153 |
RCMAIL::ERROR_STORAGE => 'storageerror', |
|
154 |
RCMAIL::ERROR_COOKIES_DISABLED => 'cookiesdisabled', |
|
155 |
RCMAIL::ERROR_INVALID_REQUEST => 'invalidrequest', |
|
156 |
RCMAIL::ERROR_INVALID_HOST => 'invalidhost', |
|
157 |
); |
|
158 |
|
|
159 |
$error_message = $error_labels[$error_code] ? $error_labels[$error_code] : 'loginfailed'; |
|
160 |
|
|
161 |
$OUTPUT->show_message($error_message, 'warning'); |
|
162 |
|
|
163 |
// log failed login |
|
164 |
$RCMAIL->log_login($auth['user'], true, $error_code); |
|
165 |
|
|
166 |
$RCMAIL->plugins->exec_hook('login_failed', array( |
|
167 |
'code' => $error_code, 'host' => $auth['host'], 'user' => $auth['user'])); |
|
168 |
|
|
169 |
$RCMAIL->kill_session(); |
7c8fd8
|
170 |
} |
f11541
|
171 |
} |
4e17e6
|
172 |
|
de62f0
|
173 |
// end session (after optional referer check) |
0301d9
|
174 |
else if ($RCMAIL->task == 'logout' && isset($_SESSION['user_id']) |
AM |
175 |
&& (!$RCMAIL->config->get('referer_check') || rcube_utils::check_referer()) |
|
176 |
) { |
|
177 |
$userdata = array( |
|
178 |
'user' => $_SESSION['username'], |
|
179 |
'host' => $_SESSION['storage_host'], |
|
180 |
'lang' => $RCMAIL->user->language, |
|
181 |
); |
|
182 |
|
|
183 |
$OUTPUT->show_message('loggedout'); |
|
184 |
|
|
185 |
$RCMAIL->logout_actions(); |
|
186 |
$RCMAIL->kill_session(); |
|
187 |
$RCMAIL->plugins->exec_hook('logout_after', $userdata); |
f11541
|
188 |
} |
4e17e6
|
189 |
|
bac7d1
|
190 |
// check session and auth cookie |
9b94eb
|
191 |
else if ($RCMAIL->task != 'login' && $_SESSION['user_id'] && $RCMAIL->action != 'send') { |
0301d9
|
192 |
if (!$RCMAIL->session->check_auth()) { |
AM |
193 |
$RCMAIL->kill_session(); |
|
194 |
$session_error = true; |
|
195 |
} |
f11541
|
196 |
} |
4e17e6
|
197 |
|
T |
198 |
// not logged in -> show login page |
197601
|
199 |
if (empty($RCMAIL->user->ID)) { |
0301d9
|
200 |
// log session failures |
AM |
201 |
$task = rcube_utils::get_input_value('_task', rcube_utils::INPUT_GPC); |
fcc7f8
|
202 |
|
0301d9
|
203 |
if ($task && !in_array($task, array('login','logout')) |
AM |
204 |
&& !$session_error && ($sess_id = $_COOKIE[ini_get('session.name')]) |
|
205 |
) { |
|
206 |
$RCMAIL->session->log("Aborted session $sess_id; no valid session data found"); |
|
207 |
$session_error = true; |
|
208 |
} |
9e54e6
|
209 |
|
8d3d5b
|
210 |
if ($session_error || $_REQUEST['_err'] == 'session') { |
AM |
211 |
$OUTPUT->show_message('sessionerror', 'error', null, true, -1); |
|
212 |
} |
|
213 |
|
b360f7
|
214 |
if ($OUTPUT->ajax_call || $OUTPUT->framed) { |
8d3d5b
|
215 |
$OUTPUT->command('session_error', $RCMAIL->url(array('_err' => 'session'))); |
AM |
216 |
$OUTPUT->send('iframe'); |
|
217 |
} |
|
218 |
|
0301d9
|
219 |
// check if installer is still active |
AM |
220 |
if ($RCMAIL->config->get('enable_installer') && is_readable('./installer/index.php')) { |
|
221 |
$OUTPUT->add_footer(html::div(array('style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"), |
|
222 |
html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") . |
|
223 |
html::p(null, "The install script of your Roundcube installation is still stored in its default location!") . |
|
224 |
html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the Roundcube directory because . |
|
225 |
these files may expose sensitive configuration data like server passwords and encryption keys |
|
226 |
to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.") |
|
227 |
)); |
|
228 |
} |
1c0ce1
|
229 |
|
0301d9
|
230 |
$plugin = $RCMAIL->plugins->exec_hook('unauthenticated', array('task' => 'login', 'error' => $session_error)); |
AM |
231 |
|
|
232 |
$RCMAIL->set_task($plugin['task']); |
|
233 |
|
|
234 |
$OUTPUT->send($plugin['task']); |
f11541
|
235 |
} |
249db1
|
236 |
// CSRF prevention |
A |
237 |
else { |
0301d9
|
238 |
// don't check for valid request tokens in these actions |
AM |
239 |
$request_check_whitelist = array('login'=>1, 'spell'=>1, 'spell_html'=>1); |
4e17e6
|
240 |
|
0301d9
|
241 |
if (!$request_check_whitelist[$RCMAIL->action]) { |
AM |
242 |
// check client X-header to verify request origin |
|
243 |
if ($OUTPUT->ajax_call) { |
|
244 |
if (rcube_utils::request_header('X-Roundcube-Request') != $RCMAIL->get_request_token()) { |
|
245 |
header('HTTP/1.1 403 Forbidden'); |
|
246 |
die("Invalid Request"); |
|
247 |
} |
|
248 |
} |
|
249 |
// check request token in POST form submissions |
|
250 |
else if (!empty($_POST) && !$RCMAIL->check_request()) { |
|
251 |
$OUTPUT->show_message('invalidrequest', 'error'); |
|
252 |
$OUTPUT->send($RCMAIL->task); |
|
253 |
} |
a77cf2
|
254 |
|
0301d9
|
255 |
// check referer if configured |
AM |
256 |
if ($RCMAIL->config->get('referer_check') && !rcube_utils::check_referer()) { |
|
257 |
raise_error(array( |
|
258 |
'code' => 403, 'type' => 'php', |
|
259 |
'message' => "Referer check failed"), true, true); |
|
260 |
} |
b80708
|
261 |
} |
249db1
|
262 |
} |
4e17e6
|
263 |
|
370302
|
264 |
// we're ready, user is authenticated and the request is safe |
A |
265 |
$plugin = $RCMAIL->plugins->exec_hook('ready', array('task' => $RCMAIL->task, 'action' => $RCMAIL->action)); |
|
266 |
$RCMAIL->set_task($plugin['task']); |
|
267 |
$RCMAIL->action = $plugin['action']; |
|
268 |
|
249db1
|
269 |
// handle special actions |
48aff9
|
270 |
if ($RCMAIL->action == 'keep-alive') { |
0301d9
|
271 |
$OUTPUT->reset(); |
AM |
272 |
$RCMAIL->plugins->exec_hook('keep_alive', array()); |
|
273 |
$OUTPUT->send(); |
48aff9
|
274 |
} |
249db1
|
275 |
else if ($RCMAIL->action == 'save-pref') { |
0301d9
|
276 |
include INSTALL_PATH . 'program/steps/utils/save_pref.inc'; |
249db1
|
277 |
} |
4e17e6
|
278 |
|
6ea6c9
|
279 |
|
T |
280 |
// include task specific functions |
0301d9
|
281 |
if (is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/func.inc')) { |
AM |
282 |
include_once $incfile; |
|
283 |
} |
4e17e6
|
284 |
|
6ea6c9
|
285 |
// allow 5 "redirects" to another action |
T |
286 |
$redirects = 0; $incstep = null; |
|
287 |
while ($redirects < 5) { |
0301d9
|
288 |
// execute a plugin action |
AM |
289 |
if ($RCMAIL->plugins->is_plugin_task($RCMAIL->task)) { |
|
290 |
if (!$RCMAIL->action) $RCMAIL->action = 'index'; |
|
291 |
$RCMAIL->plugins->exec_action($RCMAIL->task.'.'.$RCMAIL->action); |
|
292 |
break; |
|
293 |
} |
|
294 |
else if (preg_match('/^plugin\./', $RCMAIL->action)) { |
|
295 |
$RCMAIL->plugins->exec_action($RCMAIL->action); |
|
296 |
break; |
|
297 |
} |
|
298 |
// try to include the step file |
|
299 |
else if (($stepfile = $RCMAIL->get_action_file()) |
|
300 |
&& is_file($incfile = INSTALL_PATH . 'program/steps/'.$RCMAIL->task.'/'.$stepfile) |
|
301 |
) { |
|
302 |
// include action file only once (in case it don't exit) |
|
303 |
include_once $incfile; |
|
304 |
$redirects++; |
|
305 |
} |
|
306 |
else { |
|
307 |
break; |
|
308 |
} |
6ea6c9
|
309 |
} |
4e17e6
|
310 |
|
a95687
|
311 |
if ($RCMAIL->action == 'refresh') { |
0301d9
|
312 |
$RCMAIL->plugins->exec_hook('refresh', array('last' => intval(rcube_utils::get_input_value('_last', rcube_utils::INPUT_GPC)))); |
a95687
|
313 |
} |
4e17e6
|
314 |
|
6ea6c9
|
315 |
// parse main template (default) |
197601
|
316 |
$OUTPUT->send($RCMAIL->task); |
539cd4
|
317 |
|
T |
318 |
// if we arrive here, something went wrong |
1aceb9
|
319 |
rcmail::raise_error(array( |
0301d9
|
320 |
'code' => 404, |
AM |
321 |
'type' => 'php', |
|
322 |
'line' => __LINE__, |
|
323 |
'file' => __FILE__, |
|
324 |
'message' => "Invalid request"), true, true); |