Dotclear

source: admin/auth.php @ 1949:ee535dc74914

Revision 1949:ee535dc74914, 12.5 KB checked in by Denis Jean-Chirstian <contact@…>, 12 years ago (diff)

Send lang code to l10n for plural form at stratégic places

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

Sites map