-
Notifications
You must be signed in to change notification settings - Fork 0
/
forgotPassword.php
140 lines (122 loc) · 5.32 KB
/
forgotPassword.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
<?php
//Connect to db
include_once "connect.php";
include_once "pictureResize.php";
$valid = true; // Start the sign up as valid
$validEmail = true; // Same for email
$email = "";
// Make sure the post variables are set
if (isset($_POST["email"]) && isset($_POST["fName"]) && isset($_POST["lName"])) {
// Get variables
$email = trim(strtolower($_POST["email"])); //strtolower makes all characters in the string lower case
$fName = trim(ucfirst(strip_tags($_POST["fName"])));
$lName = trim(ucfirst(strip_tags($_POST["lName"])));
$query = "SELECT gridid FROM grids WHERE email=? AND first_name=? AND last_name=?"; // Check if this person does exist in DB
if ($stmt = $mysqli->prepare($query)) {
// Bind the email and execute the statement
$stmt->bind_param("sss", $email, $fName, $lName);
$stmt->execute();
// Store the result
$stmt->store_result();
// If the number of rows is 0, than this person never signed up
if ($stmt->num_rows <= 0) {
$validEmail = false;
}
// close statement
$stmt->close();
}
// If adding a user (also check for valid email)
if ($validEmail) {
$newPassword = substr(md5(rand().rand()), 0, 7); //Generate a random set of nubers and letters.
$encryptedPassword = md5($newPassword);
$query = "UPDATE grids SET password=? WHERE email=? AND first_name=? ANd last_name=? "; // Insert query...
$stmt = $mysqli->prepare($query); // Prepare the query
$stmt->bind_param("ssss", $encryptedPassword, $email, $fName, $lName); // Bind the variables
$stmt->execute(); // Execute the query
$stmt->close(); // Close statement
//Send email of new password to the user
$messageStyle = '<style>
body{
background-color:whitesmoke;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
}
#message{
background-color:white;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
-webkit-border-radius: .5em;
-moz-border-radius: .5em;
border-radius: .5em;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.2);
box-shadow: 0 1px 2px rgba(0,0,0,.2);
padding: 0px 0px;
}
#message_logo{
-webkit-border-top-right-radius: .5em;
-webkit-border-top-left-radius: .5em;
padding:1% 1%;
-moz-border-top-right-radius: .5em;
-moz-border-top-left-radius: .5em;
border-top-right-radius: .5em;
border-top-left-radius: .5em;
width:98.2%;
}
#message_body{padding: 2% 20px;}
</style>';
$logo = 'http://campusgrids.com/Images/Campusgrids_mainLogo.png';
list($width, $height) = getimagesize("$logo");
list($newWidth, $newHeight) = picResize($width, $height, 200);
$subject = "Password Reset";
$message = "
<html>
<head>
$messageStyle
</head>
<body>
<div id='message'>
<!-- Message logo -->
<div id='message_logo'>
<img height='$newHeight' width='$newWidth' alt='CampusGrids' src='$logo' />
<br />
</div>
<!-- /message logo -->
<div id='message_body'>
<h3 style='display:inline;'>Hello $fName, your password has been reset.</h3>
<br />
<p>You can now log in using: <b>$newPassword</b>.</p>
<!-- Share Buttons -->
<a href='http://api.addthis.com/oexchange/0.8/forward/facebook/offer?pco=tbx32nj-1.0&url=http%3A%2F%2Fcampusgrids.com&pubid=ra-5067f30759971104' target='_blank' ><img src='http://cache.addthiscdn.com/icons/v1/thumbs/32x32/facebook.png' border='0' alt='Facebook' /></a>
<a href='http://api.addthis.com/oexchange/0.8/forward/twitter/offer?pco=tbx32nj-1.0&url=http%3A%2F%2Fcampusgrids.com&pubid=ra-5067f30759971104' target='_blank' ><img src='http://cache.addthiscdn.com/icons/v1/thumbs/32x32/twitter.png' border='0' alt='Twitter' /></a>
<!-- /share Buttons -->
<br />
<span id='END_SIGNATURE'><p>Thank you,<br />Campusgrids Team<br /><b>Contact us:</b> [email protected]</p></span>
</div> <!--end_message_body-->
</div> <!--end_message-->
</body>
</html>";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: CampusGrids";
mail($email,$subject,$message,$headers);
}
else {
$valid = false;
}
}
else {
$valid = false;
}
// If everything is valid(user does exist in DB), return the user's email for confirmation
if ($valid) {
// Create array of data
$arr = array('status' => 1, 'email' => $email);
}
else {
if (!$validEmail) // Person never signed up.
$arr = array('status' => -1); // create array for person never signing up.
else
$arr = array('status' => 0); // create array for other error
}
$mysqli->close(); // close the connection
echo json_encode($arr); // echo the output (JSON) which will be interpreted in javascript as an object
?>