-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfirm.php
139 lines (125 loc) · 4.04 KB
/
confirm.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
<?php
// Import util functions
require("util.php");
// Import mailer
require("lib/PHPMailer/PHPMailerAutoload.php");
checkEmptySession();
dbConnect();
if (isset($_SESSION["errorMessage"])) {
$errorMessage = $_SESSION["errorMessage"];
unset($_SESSION["errorMessage"]);
}
if (empty($_SESSION["successMessage"]) && !empty($id)) {
// Confirm account
$stmt = $conn->prepare("UPDATE users SET confirmed = 1 WHERE id = :id");
$stmt->bindParam(":id", $id);
$stmt->execute();
// PRG
$_SESSION["successMessage"] = "<p id=\"label\">Confirmation successful. You can now log in to your account.</p>";
header("Location: " . $_SERVER["PHP_SELF"]);
die();
}
// Check if email set
if (isset($_POST["email"])) {
// Get email
$email = htmlspecialchars($_POST["email"]);
$result = userFromEmail($email);
// Check if email exists
if (empty($result)) {
$errorMessage = "<p id=\"error\">Email not found.</p>";
} else if ($result["confirmed"] == 1) {
$errorMessage = "<p id=\"error\">Account already confirmed.</p>";
} else {
$id = $result["id"];
$stmt = $conn->prepare("SELECT * FROM confirmations WHERE id = :id");
$stmt->bindParam(":id", $id);
$stmt->execute();
$result = $stmt->fetchAll();
$key;
if (!empty($result)) {
$key = $result["confirmkey"];
} else {
// 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();
}
// 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('[email protected]', 'StructHub');
$mail->AddReplyTo('[email protected]', 'StructHub');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $altmessage;
if($mail->send()) {
$_SESSION["successMessage"] = "<p id=\"label\">A confirmation email has been sent to " . $email . ".</p>";
} else {
$_SESSION["successMessage"] = $mail->ErrorInfo;
}
}
}
// Check if confirmation key set
else if (isset($_GET["id"])) {
$key = htmlspecialchars($_GET["id"]);
$stmt = $conn->prepare("SELECT * FROM confirmations WHERE confirmkey = :key");
$stmt->bindParam(":key", $key);
$stmt->execute();
$result = $stmt->fetch();
// Check if id valid
if (!empty($result)) {
$id = htmlspecialchars($result["id"]);
}
}
echoHeader(0);
?>
<div id="form">
<?php
if (isset($_SESSION["successMessage"])) {
echo($_SESSION["successMessage"]);
unset($_SESSION["successMessage"]);
} else if (empty($id)) { ?>
<form method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>>
<table style="margin: 0 auto;">
<?php if (!empty($errorMessage)) { echo("<tr><td colspan=\"3\">" . $errorMessage . "</td></tr>"); } ?>
<tr>
<td id="label" align="right">Email: </td>
<td id="label"><input type="email" name="email" /></td>
<td id="label" align="right"><input type="submit" name="resend" value="Resend Confirmation Link" /></td>
</tr>
</table>
</form>
<?php
}
?>
</div>
</body>
</html>