summaryrefslogtreecommitdiffabout
path: root/frontend/changepassword.php
blob: 69ebfb22975cf4330b65cef7eb9196c69d05993a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php

/*
   Cheetah News changepassword.php
   Copyright (C) 2005, 2006, 2010 Wojciech Polak.

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'lib/include.php';
require_once 'lib/d-sigs.php';

start_session (null, true);
$session->auth ('iflogged');

echo '<html xmlns="http://www.w3.org/1999/xhtml">';
?>
<head>
<title>Cheetah News</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<link rel="stylesheet" href="<?=dsp('css.cswindow')?>" type="text/css" />
<link rel="icon" href="images/favicon.png" type="image/png" />
</head>
<body>

<?php

getvars ('hash');
postvars ('submit,pass,rpass,opass');

$auth = 0;
$validPass = true;
$validPassLen = true;
$validOPass = true;
$message = '';
$email = '';

if ($session->status['afterlogged'] == 'yes')
{
  if ($session->email == 'guest') {
    echo '<h2>'._('You are using a guest account. You must register in order to do this.').'</h2>';
    echo "\n</body></html>\n";
    exit ();
  }

  $auth = 1;
  $email = $session->email;
  if (!empty ($submit))
  {
    if (empty ($pass))
      $validPassLen = false;
    if ($pass != $rpass)
      $validPass = false;

    if ($validPassLen && $validPass) {
      $success = $session->change_password ($opass, $pass, $rpass);
      if ($success)
	$message = _('Your password has been successfully changed.');
      else
	$validOPass = false;
    }
  }
}
else if (!empty ($hash))
{
  $auth = 2;
  $db = new Database ();
  $db->query ("SELECT email, UNIX_TIMESTAMP(date) AS forgot_date ".
	      "FROM forgotpassword WHERE hash='".$db->escape ($hash)."'");
  if ($db->next_record ())
  {
    $email = $db->f ('email');
    $forgot_date = $db->f ('forgot_date');
    $db->query ("SELECT email FROM user WHERE email='".$email."'");
    if ($db->next_record ())
    {
      $now = time ();
      $db->query ("SELECT UNIX_TIMESTAMP(UTC_TIMESTAMP()) AS now");
      if ($db->next_record ())
	$now = $db->f ('now');

      $diff = $now - $forgot_date;
      if ($diff > (3600 * 24)) { /* 24 hours */
	$message = _('Your password recovery session has expired.');
      }
      else if (!empty ($submit))
      {
	if (empty ($pass))
	  $validPassLen = false;
	if ($pass != $rpass)
	  $validPass = false;

	if ($validPassLen && $validPass) {
	  $db->query ("UPDATE user SET pass='".md5 ($pass).
		      "', failogCount=0 WHERE email='".$email."'");
	  $db->query ("DELETE FROM forgotpassword WHERE email='".$email."'");
	  $message = _('Your password has been successfully changed.');
	}
      }
    }
    else
      $message = printf (_("Account %s doesn't exist."), $email);
  }
  else
    $message = _('Your password recovery session has expired.');
}

if ($auth) {
  if ($message) {
?>
<div id="box">
  <h2><?php echo $message; ?></h2>
  <?php if ($auth == 2) { ?><p><a href="http://<?php echo $CONF['site']; ?>/"><?php echo _('Sign in'); ?></a></p><?php } ?>
</div>
<?php } else { ?>
<div id="box">
<h2>Cheetah: <?php echo _('Set New Password'); if ($auth == 2) { echo ' ('._('account').' <em>'.$email.'</em>)'; } ?></h2>
<form action="changepassword" method="post">
  <table>
    <?php if ($auth == 1) { ?>
    <tr>
      <td align="right"><?php echo _('Old password: '); ?></td>
      <td align="left">
	<input type="password" name="opass" size="20" maxlength="255" />
      </td>
    </tr>
    <?php } ?>
    <tr>
      <td align="right"><?php echo _('New password: '); ?></td>
      <td align="left">
	<?php if ($auth == 2) { ?><input type="hidden" name="hash" value="<?php echo htmlspecialchars ($hash); ?>" /><?php } ?>
	<input type="password" name="pass" size="20" maxlength="255" />
      </td>
    </tr>
    <tr>
      <td align="right"><?php echo _('Re-type password: '); ?></td>
      <td align="left"><input type="password" name="rpass" size="20" maxlength="255" /></td>
    </tr>
    <?php
       if (!$validPassLen)
	 echo '<tr><td colspan="2"><span class="warning">'._('Password cannot be an empty string.').'</span></td></tr>';
       else if (!$validPass)
	 echo '<tr><td colspan="2"><span class="warning">'._('Passwords do not match.').'</span></td></tr>';
       else if (!$validOPass)
	 echo '<tr><td colspan="2"><span class="warning">'._('Invalid old password.').'</span></td></tr>';
    ?>
    <tr>
      <td></td>
      <td align="left"><input type="submit" name="submit" value="<?php echo _('Change Password'); ?>" /></td>
    </tr>
    <tr><td></td></tr>
    <?php if ($auth == 2) { ?>
    <tr>
      <td colspan="2" align="left">
	<a href="http://<?php echo $CONF['site']; ?>/"><?php echo _('Sign in'); ?></a>
      </td>
    </tr>
    <?php } ?>
  </table>
</form>
</div>
<?php }
} ?>

</body>
</html>

Return to:

Send suggestions and report system problems to the System administrator.