Dotclear

source: admin/auth.php @ 3418:f74c218a7b8e

Revision 3418:f74c218a7b8e, 13.2 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

IE 7, 8 and 9 not more supported (should not be problematic for IE9 — gracefully degraded, but it will for IE7 and IE8 users)

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

Sites map