Dotclear

source: admin/auth.php @ 3725:b47f38c701ee

Revision 3725:b47f38c701ee, 14.6 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Use specialized input fields (color, email, url, number, …) where is relevant

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12
13require dirname(__FILE__) . '/../inc/admin/prepend.php';
14
15# If we have a session cookie, go to index.php
16if (isset($_SESSION['sess_user_id'])) {
17    $core->adminurl->redirect('admin.home');
18}
19
20# Loading locales for detected language
21# That's a tricky hack but it works ;)
22$dlang = http::getAcceptLanguage();
23$dlang = ($dlang == '' ? 'en' : $dlang);
24if ($dlang != 'en' && preg_match('/^[a-z]{2}(-[a-z]{2})?$/', $dlang)) {
25    l10n::lang($dlang);
26    l10n::set(dirname(__FILE__) . '/../locales/' . $dlang . '/main');
27}
28
29if (defined('DC_ADMIN_URL')) {
30    $page_url = DC_ADMIN_URL . $core->adminurl->get('admin.auth');
31} else {
32    $page_url = http::getHost() . $_SERVER['REQUEST_URI'];
33}
34
35$change_pwd = $core->auth->allowPassChange() && isset($_POST['new_pwd']) && isset($_POST['new_pwd_c']) && isset($_POST['login_data']);
36$login_data = !empty($_POST['login_data']) ? html::escapeHTML($_POST['login_data']) : null;
37$recover    = $core->auth->allowPassChange() && !empty($_REQUEST['recover']);
38$safe_mode  = !empty($_REQUEST['safe_mode']);
39$akey       = $core->auth->allowPassChange() && !empty($_GET['akey']) ? $_GET['akey'] : null;
40$user_id    = $user_pwd    = $user_key    = $user_email    = null;
41$err        = $msg        = null;
42
43# Auto upgrade
44if (empty($_GET) && empty($_POST)) {
45    require dirname(__FILE__) . '/../inc/dbschema/upgrade.php';
46    try {
47        if (($changes = dcUpgrade::dotclearUpgrade($core)) !== false) {
48            $msg = __('Dotclear has been upgraded.') . '<!-- ' . $changes . ' -->';
49        }
50    } catch (Exception $e) {
51        $err = $e->getMessage();
52    }
53}
54
55# If we have POST login informations, go throug auth process
56if (!empty($_POST['user_id']) && !empty($_POST['user_pwd'])) {
57    $user_id  = !empty($_POST['user_id']) ? $_POST['user_id'] : null;
58    $user_pwd = !empty($_POST['user_pwd']) ? $_POST['user_pwd'] : null;
59}
60# If we have COOKIE login informations, go throug auth process
61elseif (isset($_COOKIE['dc_admin']) && strlen($_COOKIE['dc_admin']) == 104) {
62    # If we have a remember cookie, go through auth process with user_key
63    $user_id = substr($_COOKIE['dc_admin'], 40);
64    $user_id = @unpack('a32', @pack('H*', $user_id));
65    if (is_array($user_id)) {
66        $user_id  = trim($user_id[1]);
67        $user_key = substr($_COOKIE['dc_admin'], 0, 40);
68        $user_pwd = null;
69    } else {
70        $user_id = null;
71    }
72}
73
74# Recover password
75if ($recover && !empty($_POST['user_id']) && !empty($_POST['user_email'])) {
76    $user_id    = !empty($_POST['user_id']) ? $_POST['user_id'] : null;
77    $user_email = !empty($_POST['user_email']) ? html::escapeHTML($_POST['user_email']) : '';
78    try
79    {
80        $recover_key = $core->auth->setRecoverKey($user_id, $user_email);
81
82        $subject = mail::B64Header('Dotclear ' . __('Password reset'));
83        $message =
84        __('Someone has requested to reset the password for the following site and username.') . "\n\n" .
85        $page_url . "\n" . __('Username:') . ' ' . $user_id . "\n\n" .
86        __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\n" .
87            $page_url . '?akey=' . $recover_key;
88
89        $headers[] = 'From: ' . (defined('DC_ADMIN_MAILFROM') && DC_ADMIN_MAILFROM ? DC_ADMIN_MAILFROM : 'dotclear@local');
90        $headers[] = 'Content-Type: text/plain; charset=UTF-8;';
91
92        mail::sendMail($user_email, $subject, $message, $headers);
93        $msg = sprintf(__('The e-mail was sent successfully to %s.'), $user_email);
94    } catch (Exception $e) {
95        $err = $e->getMessage();
96    }
97}
98# Send new password
99elseif ($akey) {
100    try
101    {
102        $recover_res = $core->auth->recoverUserPassword($akey);
103
104        $subject = mb_encode_mimeheader('Dotclear ' . __('Your new password'), 'UTF-8', 'B');
105        $message =
106        __('Username:') . ' ' . $recover_res['user_id'] . "\n" .
107        __('Password:') . ' ' . $recover_res['new_pass'] . "\n\n" .
108        preg_replace('/\?(.*)$/', '', $page_url);
109
110        $headers[] = 'From: ' . (defined('DC_ADMIN_MAILFROM') && DC_ADMIN_MAILFROM ? DC_ADMIN_MAILFROM : 'dotclear@local');
111        $headers[] = 'Content-Type: text/plain; charset=UTF-8;';
112
113        mail::sendMail($recover_res['user_email'], $subject, $message, $headers);
114        $msg = __('Your new password is in your mailbox.');
115    } catch (Exception $e) {
116        $err = $e->getMessage();
117    }
118}
119# Change password and retry to log
120elseif ($change_pwd) {
121    try
122    {
123        $tmp_data = explode('/', $_POST['login_data']);
124        if (count($tmp_data) != 3) {
125            throw new Exception();
126        }
127        $data = array(
128            'user_id'       => base64_decode($tmp_data[0]),
129            'cookie_admin'  => $tmp_data[1],
130            'user_remember' => $tmp_data[2] == '1'
131        );
132        if ($data['user_id'] === false) {
133            throw new Exception();
134        }
135
136        # Check login informations
137        $check_user = false;
138        if (isset($data['cookie_admin']) && strlen($data['cookie_admin']) == 104) {
139            $user_id = substr($data['cookie_admin'], 40);
140            $user_id = @unpack('a32', @pack('H*', $user_id));
141            if (is_array($user_id)) {
142                $user_id    = trim($data['user_id']);
143                $user_key   = substr($data['cookie_admin'], 0, 40);
144                $check_user = $core->auth->checkUser($user_id, null, $user_key) === true;
145            } else {
146                $user_id = trim($user_id);
147            }
148        }
149
150        if (!$core->auth->allowPassChange() || !$check_user) {
151            $change_pwd = false;
152            throw new Exception();
153        }
154
155        if ($_POST['new_pwd'] != $_POST['new_pwd_c']) {
156            throw new Exception(__("Passwords don't match"));
157        }
158
159        if ($core->auth->checkUser($user_id, $_POST['new_pwd']) === true) {
160            throw new Exception(__("You didn't change your password."));
161        }
162
163        $cur                  = $core->con->openCursor($core->prefix . 'user');
164        $cur->user_change_pwd = 0;
165        $cur->user_pwd        = $_POST['new_pwd'];
166        $core->updUser($core->auth->userID(), $cur);
167
168        $core->session->start();
169        $_SESSION['sess_user_id']     = $user_id;
170        $_SESSION['sess_browser_uid'] = http::browserUID(DC_MASTER_KEY);
171
172        if ($data['user_remember']) {
173            setcookie('dc_admin', $data['cookie_admin'], strtotime('+15 days'), '', '', DC_ADMIN_SSL);
174        }
175
176        $core->adminurl->redirect('admin.home');
177    } catch (Exception $e) {
178        $err = $e->getMessage();
179    }
180}
181# Try to log
182elseif ($user_id !== null && ($user_pwd !== null || $user_key !== null)) {
183    # We check the user
184    $check_user = $core->auth->checkUser($user_id, $user_pwd, $user_key, false) === true;
185    if ($check_user) {
186        $check_perms = $core->auth->findUserBlog() !== false;
187    } else {
188        $check_perms = false;
189    }
190
191    $cookie_admin = http::browserUID(DC_MASTER_KEY . $user_id .
192        $core->auth->cryptLegacy($user_id)) . bin2hex(pack('a32', $user_id));
193
194    if ($check_perms && $core->auth->mustChangePassword()) {
195        $login_data = join('/', array(
196            base64_encode($user_id),
197            $cookie_admin,
198            empty($_POST['user_remember']) ? '0' : '1'
199        ));
200
201        if (!$core->auth->allowPassChange()) {
202            $err = __('You have to change your password before you can login.');
203        } else {
204            $err        = __('In order to login, you have to change your password now.');
205            $change_pwd = true;
206        }
207    } elseif ($check_perms && !empty($_POST['safe_mode']) && !$core->auth->isSuperAdmin()) {
208        $err = __('Safe Mode can only be used for super administrators.');
209    } elseif ($check_perms) {
210        $core->session->start();
211        $_SESSION['sess_user_id']     = $user_id;
212        $_SESSION['sess_browser_uid'] = http::browserUID(DC_MASTER_KEY);
213
214        if (!empty($_POST['blog'])) {
215            $_SESSION['sess_blog_id'] = $_POST['blog'];
216        }
217
218        if (!empty($_POST['safe_mode']) && $core->auth->isSuperAdmin()) {
219            $_SESSION['sess_safe_mode'] = true;
220        }
221
222        if (!empty($_POST['user_remember'])) {
223            setcookie('dc_admin', $cookie_admin, strtotime('+15 days'), '', '', DC_ADMIN_SSL);
224        }
225
226        $core->adminurl->redirect('admin.home');
227    } else {
228        if (isset($_COOKIE['dc_admin'])) {
229            unset($_COOKIE['dc_admin']);
230            setcookie('dc_admin', false, -600, '', '', DC_ADMIN_SSL);
231        }
232        if ($check_user) {
233            $err = __('Insufficient permissions');
234        } else {
235            $err = __('Wrong username or password');
236        }
237    }
238}
239
240if (isset($_GET['user'])) {
241    $user_id = $_GET['user'];
242}
243
244header('Content-Type: text/html; charset=UTF-8');
245
246// Prevents Clickjacking as far as possible
247header('X-Frame-Options: SAMEORIGIN'); // FF 3.6.9+ Chrome 4.1+ IE 8+ Safari 4+ Opera 10.5+
248
249?>
250<!DOCTYPE html>
251<html lang="<?php echo $dlang; ?>">
252<head>
253  <meta charset="UTF-8" />
254  <meta http-equiv="Content-Script-Type" content="text/javascript" />
255  <meta http-equiv="Content-Style-Type" content="text/css" />
256  <meta http-equiv="Content-Language" content="<?php echo $dlang; ?>" />
257  <meta name="ROBOTS" content="NOARCHIVE,NOINDEX,NOFOLLOW" />
258  <meta name="GOOGLEBOT" content="NOSNIPPET" />
259  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
260  <title><?php echo html::escapeHTML(DC_VENDOR_NAME); ?></title>
261  <link rel="icon" type="image/png" href="images/favicon96-logout.png" />
262  <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
263
264
265<?php
266echo dcPage::jsCommon();
267?>
268
269    <link rel="stylesheet" href="style/default.css" type="text/css" media="screen" />
270
271<?php
272# --BEHAVIOR-- loginPageHTMLHead
273$core->callBehavior('loginPageHTMLHead');
274
275echo dcPage::jsLoad('js/_auth.js');
276?>
277</head>
278
279<body id="dotclear-admin" class="auth">
280
281<form action="<?php echo $core->adminurl->get('admin.auth'); ?>" method="post" id="login-screen">
282<h1 role="banner"><?php echo html::escapeHTML(DC_VENDOR_NAME); ?></h1>
283
284<?php
285if ($err) {
286    echo '<div class="error" role="alert">' . $err . '</div>';
287}
288if ($msg) {
289    echo '<p class="success" role="alert">' . $msg . '</p>';
290}
291
292if ($akey) {
293    echo '<p><a href="' . $core->adminurl->get('admin.auth') . '">' . __('Back to login screen') . '</a></p>';
294} elseif ($recover) {
295    echo
296    '<div class="fieldset" role="main"><h2>' . __('Request a new password') . '</h2>' .
297    '<p><label for="user_id">' . __('Username:') . '</label> ' .
298    form::field('user_id', 20, 32,
299        array(
300            'default'      => html::escapeHTML($user_id),
301            'autocomplete' => 'username'
302        )
303    ) .
304    '</p>' .
305
306    '<p><label for="user_email">' . __('Email:') . '</label> ' .
307    form::email('user_email',
308        array(
309            'default'      => html::escapeHTML($user_email),
310            'autocomplete' => 'email'
311        )
312    ) .
313    '</p>' .
314
315    '<p><input type="submit" value="' . __('recover') . '" />' .
316    form::hidden('recover', 1) . '</p>' .
317    '</div>' .
318
319    '<div id="issue">' .
320    '<p><a href="' . $core->adminurl->get('admin.auth') . '">' . __('Back to login screen') . '</a></p>' .
321        '</div>';
322} elseif ($change_pwd) {
323    echo
324    '<div class="fieldset"><h2>' . __('Change your password') . '</h2>' .
325    '<p><label for="new_pwd">' . __('New password:') . '</label> ' .
326    form::password('new_pwd', 20, 255,
327        array(
328            'autocomplete' => 'new-password'
329        )
330    ) . '</p>' .
331
332    '<p><label for="new_pwd_c">' . __('Confirm password:') . '</label> ' .
333    form::password('new_pwd_c', 20, 255,
334        array(
335            'autocomplete' => 'new-password'
336        )
337    ) . '</p>' .
338    '</div>' .
339
340    '<p><input type="submit" value="' . __('change') . '" />' .
341    form::hidden('login_data', $login_data) . '</p>';
342} else {
343    if (is_callable(array($core->auth, 'authForm'))) {
344        echo $core->auth->authForm($user_id);
345    } else {
346        if ($safe_mode) {
347            echo '<div class="fieldset" role="main">';
348            echo '<h2>' . __('Safe mode login') . '</h2>';
349            echo
350            '<p class="form-note">' .
351            __('This mode allows you to login without activating any of your plugins. This may be useful to solve compatibility problems') . '&nbsp;</p>' .
352            '<p class="form-note">' . __('Disable or delete any plugin suspected to cause trouble, then log out and log back in normally.') .
353                '</p>';
354        } else {
355            echo '<div class="fieldset" role="main">';
356        }
357
358        echo
359        '<p><label for="user_id">' . __('Username:') . '</label> ' .
360        form::field('user_id', 20, 32,
361            array(
362                'default'      => html::escapeHTML($user_id),
363                'autocomplete' => 'username'
364            )
365        ) . '</p>' .
366
367        '<p><label for="user_pwd">' . __('Password:') . '</label> ' .
368        form::password('user_pwd', 20, 255,
369            array(
370                'autocomplete' => 'current-password'
371            )
372        ) . '</p>' .
373
374        '<p>' .
375        form::checkbox('user_remember', 1) .
376        '<label for="user_remember" class="classic">' .
377        __('Remember my ID on this device') . '</label></p>' .
378
379        '<p><input type="submit" value="' . __('log in') . '" class="login" /></p>';
380
381        if (!empty($_REQUEST['blog'])) {
382            echo form::hidden('blog', html::escapeHTML($_REQUEST['blog']));
383        }
384        if ($safe_mode) {
385            echo
386            form::hidden('safe_mode', 1) .
387                '</div>';
388        } else {
389            echo '</div>';
390        }
391        echo
392        '<p id="cookie_help" class="error">' . __('You must accept cookies in order to use the private area.') . '</p>';
393
394        echo '<div id="issue">';
395
396        if ($safe_mode) {
397            echo
398            '<p><a href="' . $core->adminurl->get('admin.auth') . '" id="normal_mode_link">' . __('Get back to normal authentication') . '</a></p>';
399        } else {
400            echo '<p id="more"><strong>' . __('Connection issue?') . '</strong></p>';
401            if ($core->auth->allowPassChange()) {
402                echo '<p><a href="' . $core->adminurl->get('admin.auth', array('recover' => 1)) . '">' . __('I forgot my password') . '</a></p>';
403            }
404            echo '<p><a href="' . $core->adminurl->get('admin.auth', array('safe_mode' => 1)) . '" id="safe_mode_link">' . __('I want to log in in safe mode') . '</a></p>';
405        }
406
407        echo '</div>';
408    }
409}
410?>
411</form>
412</body>
413</html>
Note: See TracBrowser for help on using the repository browser.

Sites map