-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.php
202 lines (187 loc) · 7 KB
/
register.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
// Import util functions
require("util.php");
// Import library for backwards compatibility
require("lib/password.php");
// Import mailer
require("lib/PHPMailer/PHPMailerAutoload.php");
checkEmptySession();
dbConnect();
if (isset($_SESSION["errorMessage"])) {
$errorMessage = $_SESSION["errorMessage"];
unset($_SESSION["errorMessage"]);
}
// Initialize registration validity
$postValid = True;
if (empty($_POST)) {
$postValid = False;
} else {
// Get submitted information
$firstname = htmlspecialchars($_POST["firstname"]);
$lastname = htmlspecialchars($_POST["lastname"]);
$email = htmlspecialchars($_POST["email"]);
$password = htmlspecialchars($_POST["password"]);
$confirmpassword = htmlspecialchars($_POST["confirmpassword"]);
// Check for email in database
$stmt = $conn->prepare("SELECT * FROM users WHERE email = :email");
$stmt->bindParam(":email", $email);
$stmt->execute();
$result = $stmt->fetchAll();
// Validate name
if (empty($firstname) || empty($lastname) || ctype_space($firstname) || ctype_space($lastname)) {
$postValid = False;
$errorMessage = "<p id=\"error\">Name invalid.</p>";
// Validate email
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$postValid = False;
$errorMessage = "<p id=\"error\">Email invalid.</p>";
// Validate password
} else if (strlen($password) < 6) {
$postValid = False;
$errorMessage = "<p id=\"error\">Password too short.</p>";
// Validate password confirmation
} else if ($password != $confirmpassword) {
$postValid = False;
$errorMessage = "<p id=\"error\">Passwords do not match.</p>";
// Check for email availability
} else if (!empty($result)) {
$postValid = False;
$errorMessage = "<p id=\"error\">Email in use.</p>";
}
}
echoHeader(0);
?>
<div id="form">
<?php
if (isset($_SESSION["successMessage"])) {
echo($_SESSION["successMessage"]);
unset($_SESSION["successMessage"]);
} else {
// Display registration form if submitted details are invalid
if (!$postValid) { ?>
<form method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>>
<table style="margin: 0 auto;">
<?php if (!empty($errorMessage)) { echo("<tr><td colspan=\"2\">" . $errorMessage . "</td></tr>"); } ?>
<tr>
<td id="label" align="right">First Name: </td>
<td id="label"><input type="text" name="firstname" value="<?php if (!empty($firstname)) { echo($firstname); } ?>" /></td>
</tr>
<tr>
<td id="label" align="right">Last Name: </td>
<td id="label"><input type="text" name="lastname" value="<?php if (!empty($lastname)) { echo($lastname); } ?>" /></td>
</tr>
<tr>
<td id="label" align="right">Email: </td>
<td id="label"><input type="email" name="email" value="<?php if (!empty($email)) { echo($email); } ?>" /></td>
</tr>
<tr>
<td id="label" align="right">Password: </td>
<td id="label"><input type="password" name="password" /></td>
</tr>
<tr>
<td id="label" align="right">Confirm Password: </td>
<td id="label"><input type="password" name="confirmpassword" /></td>
</tr>
<tr>
<td id="label" align="right"><input type="submit" name="register" value="Register" /></td>
</tr>
</table>
</form>
<?php } else {
// Capitalize name
$firstname = ucfirst($firstname);
$lastname = ucfirst($lastname);
// Ensure id is unique
$id = uniqid("", true);
$stmt = $conn->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(":id", $id);
$stmt->execute();
$result = $stmt->fetchAll();
while (!empty($result)) {
$id = uniqid("", true);
$stmt->execute();
$result = $stmt->fetchAll();
}
// Ensure username is unique
$fn = strtolower(preg_replace("/[^a-z]/i", "", $firstname));
$ln = strtolower(preg_replace("/[^a-z]/i", "", $lastname));
$username = $fn . "." . $ln;
$stmt = $conn->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(":username", $username);
$stmt->execute();
$result = $stmt->fetchAll();
$i = 0;
while (!empty($result)) {
$username = $fn . "." . $ln . ++$i;
$stmt->execute();
$result = $stmt->fetchAll();
}
// Hash password
$passwordhash = password_hash($password, PASSWORD_DEFAULT);
// Create user
$stmt = $conn->prepare("INSERT INTO users (id, username, firstname, lastname, email, password) VALUES (:id, :username, :firstname, :lastname, :email, :password)");
$stmt->bindParam(":id", $id);
$stmt->bindParam(":username", $username);
$stmt->bindParam(":firstname", $firstname);
$stmt->bindParam(":lastname", $lastname);
$stmt->bindParam(":email", $email);
$stmt->bindParam(":password", $passwordhash);
$stmt->execute();
// Ensure key is unique
$key = uniqid();
$stmt = $conn->prepare("SELECT * FROM confirmations WHERE confirmkey = :key");
$stmt->bindParam(":key", $key);
$stmt->execute();
$result = $stmt->fetchAll();
while (!empty($result)) {
$key = uniqid();
$stmt->execute();
$result = $stmt->fetchAll();
}
// Create confirmation key
$stmt = $conn->prepare("INSERT INTO confirmations (id, confirmkey) VALUES (:id, :key)");
$stmt->bindParam(":id", $id);
$stmt->bindParam(":key", $key);
$stmt->execute();
// PRG
if (!empty($errorMessage)) {
$_SESSION["errorMessage"] = $errorMessage;
} else {
// Send confirmation email
$subject = "StructHub Account Confirmation";
$message = "
<html>
<body>
<p>Use this link to confirm your StructHub account.</p>
<p><a href=\"http://structhub.com/confirm.php?id=" . $key . "\">" . "http://structhub.com/confirm.php?id=" . $key . "</a></p>
</body>
</html>
";
$altmessage = "Use this link to confirm your StructHub account: http://structhub.com/confirm.php?id=" . $key;
$mail = new PHPMailer();
//$mail->SMTPAuth = false;
//$mail->SMTPSecure = "ssl";
//$mail->Port = 995;
$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->SetFrom('donotreply@structhub.com', 'StructHub');
$mail->AddReplyTo('donotreply@structhub.com', 'StructHub');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $altmessage;
if($mail->send()) {
$_SESSION["successMessage"] = "<p id=\"label\">Registration successful! A confirmation email has been sent to " . $email . ".</p>";
} else {
$_SESSION["successMessage"] = $mail->ErrorInfo;
}
}
// PRG
header("Location: " . $_SERVER['REQUEST_URI']);
die();
}
} ?>
</div>
</body>
</html>