Dotclear

source: admin/auth.php @ 73:1214fac9906a

Revision 73:1214fac9906a, 12.0 KB checked in by Franck <carnet.franck.paul@…>, 13 years ago (diff)

Even more simple

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

Sites map