File: login.php

package info (click to toggle)
gollem 1.0.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,416 kB
  • ctags: 433
  • sloc: php: 2,088; xml: 359; makefile: 74; sh: 11
file content (310 lines) | stat: -rw-r--r-- 11,366 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
 * Login screen for Gollem.
 *
 * $Horde: gollem/login.php,v 1.94.2.12 2006/01/28 18:12:04 slusarz Exp $
 *
 * Copyright 1999-2006 Charles J. Hagenbuch <chuck@horde.org>
 * Copyright 1999-2006 Max Kalika <max@horde.org>
 *
 * See the enclosed file COPYING for license information (GPL).  If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 */

function _notificationOutput(&$template)
{
    ob_start();
    $GLOBALS['notification']->notify(array('listeners' => 'status'));
    $template->set('notification_output', ob_get_contents());
    ob_end_clean();
}

@define('AUTH_HANDLER', true);
@define('GOLLEM_BASE', dirname(__FILE__));
$authentication = 'none';
require_once GOLLEM_BASE . '/lib/base.php';

/* Get an Auth object. */
$gollem_auth = (Auth::getProvider() == 'gollem');
$auth = &Auth::singleton($conf['auth']['driver']);
$autologin = Util::getFormData('autologin', false);
$autologin_fail = Util::getFormData('autologin_fail', false);

$actionID = Util::getFormData('actionID');
$backend_key = Util::getFormData('backend_key');
$logout_reason = $auth->getLogoutReason();
$url_param = Util::getFormData('url');

/* Handle cases when we already have a session. */
if (!empty($_SESSION['gollem']) && is_array($_SESSION['gollem'])) {
    if ($logout_reason) {
        /* Log logout requests now. */
        if ($logout_reason == AUTH_REASON_LOGOUT) {
            $entry = sprintf('Logout for %s [%s]',
                             $GLOBALS['gollem_be']['params']['username'],
                             $_SERVER['REMOTE_ADDR']);
        } else {
            $entry = $_SERVER['REMOTE_ADDR'] . ' ' . $auth->getLogoutReasonString();
        }
        Horde::logMessage($entry, __FILE__, __LINE__, PEAR_LOG_NOTICE);

        $language = (isset($prefs)) ? $prefs->getValue('language') : NLS::select();

        unset($_SESSION['gollem']);

        if (isset($prefs)) {
            $prefs->cleanup($gollem_auth);
        }
        if ($gollem_auth) {
            Auth::clearAuth();
            @session_destroy();
            Horde::setupSessionHandler();
            @session_start();
        }

        NLS::setLang($language);

        /* Hook to preselect the correct language in the widget. */
        $_GET['new_lang'] = $language;

        $registry->loadPrefs('horde');
        $registry->loadPrefs();
    } elseif (!$autologin_fail) {
        /* Check to see if we have logged in to the backend yet. */
        if (isset($_SESSION['gollem']['backends'][$backend_key])) {
            require_once GOLLEM_BASE . '/lib/Session.php';
            Gollem_Session::changeBackend($backend_key);

            $url = $url_param;
            if (empty($url)) {
                /* If there is an existing session, redirect the user to the
                 * file manager. */
                $url = Horde::applicationUrl('manager.php', true);
                if ($actionID == 'login') {
                    $url = Util::addParameter($url, 'actionID', 'login', false);
                }
            }
            header('Location: ' . $url);
            exit;
        }
    }
}

/* Log session timeouts. */
if ($logout_reason == AUTH_REASON_SESSION) {
    $entry = sprintf('Session timeout for client [%s]', $_SERVER['REMOTE_ADDR']);
    Horde::logMessage($entry, __FILE__, __LINE__, PEAR_LOG_NOTICE);

    /* Make sure everything is really cleared. */
    Auth::clearAuth();
    unset($_SESSION['gollem']);
}

/* Redirect the user on logout if redirection is enabled. */
if (($logout_reason == AUTH_REASON_LOGOUT) &&
    ($conf['user']['redirect_on_logout'] ||
     !empty($conf['auth']['redirect_on_logout']))) {
    $url = Auth::addLogoutParameters((!empty($conf['auth']['redirect_on_logout'])) ? $conf['auth']['redirect_on_logout'] : $conf['user']['redirect_on_logout'], AUTH_REASON_LOGOUT);
    if (!isset($_COOKIE[session_name()])) {
        $url = Util::addParameter($url, session_name(), session_id());
    }
    header('Location: ' . $url);
    exit;
}

/* Redirect the user if an alternate login page has been specified. */
if (!empty($conf['auth']['alternate_login']) ||
    !empty($conf['user']['alternate_login'])) {
    $url = Auth::addLogoutParameters(!empty($conf['auth']['alternate_login']) ? $conf['auth']['alternate_login'] : $conf['user']['alternate_login']);
    if (!isset($_COOKIE[session_name()])) {
        $url = Util::addParameter($url, session_name(), session_id());
    }
    if ($url_param) {
        $url = Util::addParameter($url, 'url', $url_param, false);
    }
    header('Location: ' . $url);
    exit;
}

/* Initialize the password key(s). If we are doing Horde auth as well,
 * make sure that the Horde auth key gets set. */
Secret::setKey('gollem');
if ($gollem_auth) {
    Secret::setKey('auth');
}
/* Prepare the login template. */
require_once 'Horde/Template.php';
$template = &new Horde_Template();
$template->setOption('gettext', true);

require_once 'Horde/Menu.php';
$menu = &new Menu(HORDE_MENU_MASK_NONE);
$template->set('menu', $menu->render(), true);
$template->set('title', sprintf(_("%s Login"), $registry->get('name')));

if (!$backend_key) {
    $backend_key = Gollem::getPreferredBackend();
    /* If there is no backend key defined, there is no available backends for
     * the current user. */
    if (is_null($backend_key)) {
        $reason = _("There are no backends available for the current user.");
        require GOLLEM_TEMPLATES . '/common-header.inc';
        _notificationOutput($template);
        $template->set('allowlogin', false, true);
        echo $template->fetch(GOLLEM_TEMPLATES . '/login/login.html');
        if (@is_readable(GOLLEM_BASE . '/config/motd.php')) {
            require GOLLEM_BASE . '/config/motd.php';
        }
        require $registry->get('templates', 'horde') . '/common-footer.inc';
        exit;
    }
}

$login_vfs = array();
foreach ($GLOBALS['gollem_backends'] as $key => $curBackend) {
    $login_vfs[$key] = &VFS::singleton($curBackend['driver'], array_merge($curBackend['params'], array('user' => Auth::getAuth())));
    if (is_a($login_vfs[$key], 'PEAR_Error')) {
        Horde::fatal($login_vfs[$key]);
    }
}
$GLOBALS['gollem_vfs'] = $login_vfs[$backend_key];

/* If we only have one backend, and it doesn't require authentication, skip
 * all further steps and login. */
$redirect_params = array();
if (!$logout_reason && !$autologin_fail) {
    if (count($login_vfs) == 1) {
        if (!$GLOBALS['gollem_vfs']->getRequiredCredentials()) {
            $redirect_params['nocredentials'] = 1;
        } elseif (Gollem::canAutoLogin($backend_key, $autologin)) {
            $redirect_params['autologin'] = 1;
        }
    } elseif (Util::getFormData('change_backend') &&
              empty($GLOBALS['gollem_backends'][$backend_key]['loginparams'])) {
        $redirect_params['autologin'] = 1;
    }
}

if (!empty($redirect_params)) {
    $redirect_params += array('actionID' => 'login', 'backend_key' => $backend_key);
    $url = Util::addParameter(Horde::applicationUrl('redirect.php', true), $redirect_params, null, false);
    header('Location: ' . $url);
    exit;
}

$title = sprintf(_("Welcome to %s"), $registry->get('name', ($gollem_auth) ? 'horde' : null));

if ($logout_reason && $gollem_auth && $conf['menu']['always']) {
    $notification->push('setFocus();if (window.parent.frames.horde_menu) window.parent.frames.horde_menu.location.reload();', 'javascript');
} else {
    $notification->push('setFocus()', 'javascript');
}

$reason = $auth->getLogoutReasonString();

/* Add some javascript. */
Horde::addScriptFile('enter_key_trap.js', 'horde', true);

/* Do we need to do IE version detection? */
if (!Auth::getAuth() &&
    ($browser->getBrowser() == 'msie') &&
    ($browser->getMajor() >= 5)) {
    $ie_clientcaps = true;
}

$choose_language = ($gollem_auth && !$prefs->isLocked('language'));
if ($choose_language) {
    $lang_url = (!empty($url_param)) ? urlencode($url_param) : null;
}

require GOLLEM_TEMPLATES . '/common-header.inc';
require_once GOLLEM_TEMPLATES . '/login/javascript.inc';

$tabindex = 0;
$template->set('allowlogin', true, true);

$template->set('action', Horde::url('redirect.php', false, -1, true));
$template->set('gollem_auth', $gollem_auth, true);
$template->set('form_input', Util::formInput());
$template->set('actionid', $actionID);
$template->set('load_frameset', ($gollem_auth) ? 1 : 0);
$template->set('url', htmlspecialchars($url_param));
$template->set('autologin', ($autologin || Gollem::canAutoLogin($backend_key, true)) ? 1 : 0);
$template->set('clientcaps', !empty($ie_clientcaps), true);
$template->set('backend_key', ($conf['backend']['backend_list'] != 'shown') ? $backend_key : null, true);

_notificationOutput($template);

$template->set('backend_shown', ($conf['backend']['backend_list'] == 'shown'), true);
if ($template->get('backend_shown')) {
    $template->set('shown_tabindex', ++$tabindex);
    $shown_array = array();
    foreach ($GLOBALS['gollem_backends'] as $key => $curBackend) {
        $shown_array[] = array(
            'sel' => ($key == $backend_key),
            'name' => $curBackend['name'],
            'val' => $key
        );
    }
    $template->set('shown_array', $shown_array, true);
}

$template->set('backend_hidden', ($conf['backend']['backend_list'] == 'hidden'), true);
if ($template->get('backend_hidden')) {
    $template->set('hidden_text', sprintf(_("Connect to: %s"), $GLOBALS['gollem_backends'][$backend_key]['name']));
}

$loginparams = array();
if (!empty($GLOBALS['gollem_backends'][$backend_key]['loginparams'])) {
    foreach ($GLOBALS['gollem_backends'][$backend_key]['loginparams'] as $key => $val) {
        $loginparams[] = array(
            'label' => $val,
            'tabindex' => ++$tabindex,
            'name' => $key,
            'val' => $GLOBALS['gollem_backends'][$backend_key]['params'][$key]
        );
    }
}
$template->set('loginparams', $loginparams);

$log_creds = array();
require GOLLEM_BASE . '/config/credentials.php';
foreach ($login_vfs[$backend_key]->getRequiredCredentials() as $credential) {
    if ((($credential == 'username') ||
         ($credential == 'password')) &&
        empty($GLOBALS['gollem_backends'][$backend_key]['hordeauth'])) {
        $log_creds[] = array(
            'label' => $credentials[$credential]['desc'],
            'tabindex' => ++$tabindex,
            'type' => $credentials[$credential]['type'],
            'name' => $credential,
            'val' => htmlspecialchars(Util::getFormData($credential))
        );
    }
}
$template->set('log_creds', $log_creds);

$template->set('choose_language', $choose_language, true);
if ($choose_language) {
    $template->set('langs_tabindex', ++$tabindex);
    $_SESSION['horde_language'] = NLS::select();
    $langs = array();
    foreach ($nls['languages'] as $key => $val) {
        $langs[] = array(
            'sel' => ($key == $_SESSION['horde_language']),
            'val' => $key,
            'name' => $val
        );
    }
    $template->set('langs', $langs, true);
}

$template->set('login_tabindex', ++$tabindex);
$template->set('login', _("Login"));

echo $template->fetch(GOLLEM_TEMPLATES . '/login/login.html');

if (@is_readable(GOLLEM_BASE . '/config/motd.php')) {
    require GOLLEM_BASE . '/config/motd.php';
}
require $registry->get('templates', 'horde') . '/common-footer.inc';