Dotclear

source: admin/auth.php @ 365:374950e550f7

Revision 365:374950e550f7, 12.3 KB checked in by Dsls <dsls@…>, 13 years ago (diff)

Simplified password change fields handling.

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

Sites map