|
1 | 1 | <?php |
2 | | -// Parts of this file are based on index.php (Roundcube version 1.4.8). |
3 | | -// TODO Reduce amount of duplicate code from index.php. We may be able to do that by: |
4 | | -// * removing authenticate hook logic using $_POST. |
5 | | -// * moving login logic to a function provided by base Roundcube |
6 | | - |
7 | | -// include environment |
8 | | -require_once __DIR__ . '/../../program/include/iniset.php'; |
9 | | - |
10 | | -// init application, start session, init output class, etc. |
11 | | -$RCMAIL = rcmail::get_instance(0, $GLOBALS['env']); |
12 | | - |
13 | | -/// Auth hack BEGIN |
14 | | -// TODO authenticate hook may actually be removed. Unclear if this is required for cPanel auth. |
15 | | -// Set some global POST vars that would be usually set via HTML <input> tags are: |
16 | | -// _task, _action, _timezone, _user, _pass, _token . We set all except for token. |
17 | | -// Token should only be required for an existing session. Also disregarding Timezone for now |
18 | | -$_POST['_user'] = $_SERVER['PHP_AUTH_USER']; |
19 | | -$_POST['_pass'] = $_SERVER['PHP_AUTH_PW']; |
20 | | -$_POST['_action'] = 'login'; |
21 | | -$_POST['_task'] = 'login'; |
| 2 | +// Assuming we are inside RC's plugins/jmap dir |
| 3 | +define('INSTALL_PATH', realpath('../../') . '/'); |
| 4 | + |
| 5 | +// load the whole Roundcube Webmail code with its autoloader |
| 6 | +require_once INSTALL_PATH . '/program/include/iniset.php'; |
| 7 | +$RCMAIL = rcmail::get_instance(rcube::INIT_WITH_DB | rcube::INIT_WITH_PLUGINS); |
| 8 | + |
| 9 | +$user = $_SERVER['PHP_AUTH_USER']; |
| 10 | +$pass = $_SERVER['PHP_AUTH_PW']; |
22 | 11 |
|
23 | 12 | /// Impersonation / admin auth BEGIN |
24 | 13 | // An array to store the admin user, as well the user-to-impersonate |
|
28 | 17 | // Check if we're dealing with admin auth credentials |
29 | 18 | // and if yes, then take the first part as the admin username |
30 | 19 | // to use for login |
31 | | -if (mb_strpos($_POST['_user'], "*")) { |
32 | | - $users = explode("*", $_POST['_user']); |
33 | | - $_POST['_user'] = $users[0]; |
| 20 | +if (mb_strpos($user, "*")) { |
| 21 | + $users = explode("*", $user); |
| 22 | + $user = $users[0]; |
34 | 23 | } |
35 | | -/// Impersonation / admin auth END |
36 | 24 |
|
| 25 | +/// Authenticate hook |
37 | 26 | $pass_charset = $RCMAIL->config->get('password_charset', 'UTF-8'); |
38 | 27 |
|
39 | 28 | $auth = $RCMAIL->plugins->exec_hook('authenticate', array( |
40 | 29 | 'host' => $RCMAIL->autoselect_host(), |
41 | | - 'user' => trim(rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST)), |
42 | | - 'pass' => rcube_utils::get_input_value('_pass', rcube_utils::INPUT_POST, true, $pass_charset), |
| 30 | + 'user' => trim(rcube_utils::parse_input_value($user)), |
| 31 | + 'pass' => rcube_utils::parse_input_value($pass, true, $pass_charset), |
43 | 32 | 'valid' => true, // It is always valid in Karlsruhe! |
44 | 33 | 'cookiecheck' => false, // No cookies for you in Karlsruhe! |
45 | 34 | )); |
46 | | -/// Auth hack END |
47 | | - |
48 | | -// Login |
49 | | -// TODO The following contains quite a lot of duplicate code from RC's index.php. |
50 | | -// It may be moved to an own function (except for returning errors via API)? |
51 | | -if ( |
52 | | - $auth['valid'] && !$auth['abort'] |
53 | | - && $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'], $auth['cookiecheck']) |
54 | | -) { |
55 | | - $logger->info("Successfully logged in as " . $auth['user']); |
56 | | - |
57 | | - // log successful login |
58 | | - $RCMAIL->log_login(); |
59 | | -} else { |
| 35 | + |
| 36 | +// IMAP Login |
| 37 | +$login_success = false; |
| 38 | +if ($auth['valid'] && !$auth['abort']){ |
| 39 | + if($RCMAIL->login($auth['user'], $auth['pass'], $auth['host'], false, true)) { |
| 40 | + $logger->info("Successfully logged in as " . $auth['user']); |
| 41 | + $login_success = true; |
| 42 | + } |
| 43 | +} |
| 44 | +if (!$auth['valid'] || $auth['abort'] || !$login_success){ |
60 | 45 | if (!$auth['valid']) { |
61 | 46 | $error_code = rcmail::ERROR_INVALID_REQUEST; |
62 | 47 | } else { |
63 | 48 | $error_code = is_numeric($auth['error']) ? $auth['error'] : $RCMAIL->login_error(); |
64 | 49 | } |
65 | | - |
66 | 50 | $error_labels = array( |
67 | 51 | rcmail::ERROR_STORAGE => 'storageerror', |
68 | 52 | rcmail::ERROR_COOKIES_DISABLED => 'cookiesdisabled', |
|
83 | 67 | $loginError = null; |
84 | 68 |
|
85 | 69 | switch ($error_code) { |
86 | | - case rcmail::ERROR_RATE_LIMIT: |
87 | | - $loginError = 'urn:ietf:params:jmap:error:limit'; |
88 | | - header('HTTP/1.0 429 Too Many Requests'); |
89 | | - break; |
90 | | - case rcmail::ERROR_INVALID_REQUEST: |
91 | | - $loginError = 'urn:ietf:params:jmap:error:notRequest'; |
92 | | - header('HTTP/1.0 400 Bad Request'); |
93 | | - break; |
94 | | - default: |
95 | | - $loginError = '401 Unauthorized'; |
96 | | - header('HTTP/1.0 401 Unauthorized'); |
| 70 | + case rcmail::ERROR_RATE_LIMIT: |
| 71 | + $loginError = 'urn:ietf:params:jmap:error:limit'; |
| 72 | + header('HTTP/1.0 429 Too Many Requests'); |
| 73 | + break; |
| 74 | + case rcmail::ERROR_INVALID_REQUEST: |
| 75 | + $loginError = 'urn:ietf:params:jmap:error:notRequest'; |
| 76 | + header('HTTP/1.0 400 Bad Request'); |
| 77 | + break; |
| 78 | + default: |
| 79 | + $loginError = '401 Unauthorized'; |
| 80 | + header('HTTP/1.0 401 Unauthorized'); |
97 | 81 | } |
98 | 82 |
|
99 | 83 | die($loginError); |
|
0 commit comments