-
Notifications
You must be signed in to change notification settings - Fork 0
/
changePassword.php
145 lines (137 loc) · 3.23 KB
/
changePassword.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
<?php
require 'dbConnect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Project Transparency - Change Password</title>
</head>
<body>
<?php
$con;
function checkCode($confCode)
{
$con = makeCon();
$sql = "SELECT user_id FROM tb_password_recover WHERE conf_code ='" .
$con->escape_string($confCode) . "'";
if($result = $con->query($sql))
{
$row = $result->fetch_row();
if((int)$result->num_rows == 1)
{
return $row[0];
}
else
return false;
}
else
echo $con->error;
}
function makeCon()
{
if(!isset($con) || $con == null)
{
$con = makeSQLI();
if($con === false)
die('Error ' . mysqli_connect_error);
}
return $con;
}
$pass = false;
if(((isset($_POST['emailTB']) && isset($_POST['oldPassTB'])) || isset($_GET['c'])) &&
isset($_POST['passwordTB']) &&
isset($_POST['passwordConfTB']))
{
if(isset($_POST['emailTB']) && strlen($_POST['emailTB']) == 0)
$error = "You must enter an email address";
else if($_POST['passwordTB'] != $_POST['passwordConfTB'])
$error = "Passwords did not match";
else if(strlen($_POST['passwordTB'])<5)
$error = "Password must be at least 5 characters";
// Valid fields
else
{
if(isset($_POST['emailTB']))
{
$con = makeCon();
$cleanEmail = $con->escape_string($_POST['emailTB']);
$existingPass = md5($_POST['oldPassTB']);
$newPass = (md5($_POST['passwordTB']));
$sql = "UPDATE tb_users " .
"SET password = '$newPass' " .
"WHERE email_address = '$cleanEmail' " .
"AND password = '$existingPass'";
if($result = $con->query($sql))
{
if($con->affected_rows == 1)
{
$pass = true;
}
else
{
$error = "Invalid Email/Password Combo.";
}
}
else
{
$error = "Error updating user password.";
}
}
else
{
$con = makeCon();
$userID = checkCode((string)$_GET['c']);
$newPass = md5($_POST['passwordTB']);
if($userID)
{
$sql = "UPDATE tb_users " .
"SET password = '$newPass' " .
"WHERE id = $userID";
if(!$result = $con->query($sql))
{
$error = "Error updating user password.";
}
else
{
$sql = "DELETE FROM tb_password_recover " .
"WHERE conf_code = '" . $_GET['c'] ."'";
$pass = $con->query($sql);
}
}
else
$error = "Invalid confirmation code.";
}
}
}
if(!$pass)
{
?>
<h1>Change Your Password</h1>
<?php
if(isset($error))
echo "<h3 style='color: red;'>$error</h3>\n";
?>
<form method="post">
<?php if(!isset($_GET['c']))
{
?>
<label for="emailTB">Email Address:</label>
<input type="email" name="emailTB" id="emailTB" value="<?php
if(isset($_POST['emailTB'])) echo $_POST['emailTB'];
?>"/><br />
<label for="oldPassTB">Current Password:</label>
<input type="password" name="oldPassTB" id="oldPassTB"/><br />
<?php } ?>
<label for="passwordTB">Password:</label>
<input type="password" name="passwordTB" id="passwordTB" /><br />
<label for="passwordConfTB">Confirm Password:</label>
<input type="password" name="passwordConfTB" id="passwordConfTB" /><br />
<input type="submit" value="Submit"/>
<?php
}
else
echo "Successfully changed password."
?>
</form>
</body>
</html>