-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.php
185 lines (173 loc) · 4.83 KB
/
password.php
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
181
182
183
184
185
<?php
/*
* PSO password change form.
* Created by gatchi (github.com/gatchi) (christen.got@gmail.com)
* Inspired by Soly's PSO registration form. (github.com/Solybum)
*/
$error = false;
$dberror = false;
$passChanged = false;
$errorString = $dberrorString = $username = $newpass = $oldpass = "";
$regtime = 0;
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"]))
{
if(empty($_POST["username"]))
{
$error = true;
$errorString .= "<br />Enter your username.";
}
if(empty($_POST["oldpass"]))
{
$error = true;
$errorSttring .= "<br />Enter your old password.";
}
else if(empty($_POST["newpass"]))
{
$error = true;
$errorString .= "<br />Enter a new password.";
}
else if(empty($_POST["passcheck"]))
{
$error = true;
$errorString .= "<br />Retype your new password.";
}
else if($_POST["newpass"] !== $_POST["passcheck"])
{
$error = true;
$errorString .= "<br />New passwords do not match.";
}
if($error == false)
{
require_once("mysql.php");
$username = db_escape($_POST["username"]);
$oldpass = db_escape($_POST["oldpass"]);
$newpass = db_escape($_POST["newpass"]);
// Obtain regtime for hash
$query = sprintf("SELECT regtime FROM account_data WHERE username='%s'", $username);
$result = db_query($query);
if($result == false)
{
$dberror = true;
$dberrorString .= "<br />There was an error connecting to the database.";
echo db_error();
}
else
{
$row = mysqli_fetch_assoc($result);
$regtime = $row["regtime"];
$md5str = "%s_%u_salt";
$md5password = md5(sprintf($md5str, $newpass, $regtime));
}
// Obtain the account hash and compare it with a hash of the submitted old password
$query = sprintf("SELECT password FROM account_data WHERE username='%s'", $username);
$result = db_query($query);
if($result == false)
{
$dberror = true;
$dberrorString .= "<br />There was an error connecting to the database.";
echo db_error();
}
else
{
$row = mysqli_fetch_assoc($result);
$accountmd5 = $row["password"];
}
$oldmd5 = md5(sprintf($md5str, $oldpass, $regtime));
if($dberror == false && $oldmd5 !== $accountmd5)
{
$error = true;
$errorString .= "<br />Account verification failed -- wrong password.";
}
// change password
if($error == false && $dberror == false)
{
$query = sprintf("UPDATE account_data SET password = '%s' WHERE username = '%s'", $md5password, $username);
$result = db_query($query);
if($result == false)
{
$dberror = true;
$dberrorString .= "<br />Password change failed. Contact the administrator.";
echo db_error();
}
else
{
$passChanged = true;
}
}
}
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Change Password</title>
<meta name="description" content="Change your player account password here." />
<meta name="keywords" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style/style.css" />
</head>
<body>
<div id="main">
<div id="site_content">
<div id="content">
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" && $error == false && $dberror == false && $passChanged == true)
{
?>
<h2>Change your password</h2>
<p>Password changed.</p>
<?php
}
else
{
?>
<h2>Change your password</h2>
<p>If you know your password, you can supply it below to change it.</p>
<?php
if($dberror == true)
{
?>
<p>Please contact the administrators with the following message:
<?php echo $dberrorString; ?>
</p>
<?php
}
else if($error == true)
{
?>
<p>Please solve the following errors:
<?php echo $errorString; ?>
</p>
<?php
}
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<div class="form_settings">
<p>
<span>Username</span>
<input type="text" name="username" title="username" placeholder="" value="<?php echo $username;?>" />
</p>
<p>
<span>Old password</span>
<input type="password" name="oldpass" title="password" placeholder="" value="" />
</p>
<p>
<span>New password</span>
<input type="password" name="newpass" title="password" placeholder="" value="" />
</p>
<p>
<span>Re-type new password</span>
<input type="password" name="passcheck" title="password" placeholder="" value="" />
</p>
<p>
<span> </span>
<input class="submit" type="submit" name="submit" title="submit" value="Submit"/>
</p>
</div>
</form>
</div>
</div>
</div>
</body>
</html>