Sfoglia il codice sorgente

Added support for verifying email addresses.

Alan Lam 8 anni fa
parent
commit
8ec6e09192
6 ha cambiato i file con 69 aggiunte e 4 eliminazioni
  1. 4 0
      controllers/log_in.php
  2. 17 0
      controllers/user_activation.php
  3. 2 2
      emails/registration.html
  4. 21 1
      engine/email.php
  5. 24 1
      engine/user.php
  6. 1 0
      routes.php

+ 4 - 0
controllers/log_in.php

@@ -23,6 +23,10 @@ if($submit) {
 
 		if(!$user) throw new Exception("Account not found.");
 		if(!password_verify($password, $user->password)) throw new Exception("Password is incorrect.");
+		if(!$user->activated) {
+			$user->sendActivationEmail();
+			throw new Exception("Your email address is not verified. An activation email has been sent to your email address.");
+		}
 
 		$_SESSION["user_id"] = $user->id;
 

+ 17 - 0
controllers/user_activation.php

@@ -0,0 +1,17 @@
+<?php
+
+$user = User::queryOne([
+	"verify_token" => $token
+]);
+
+if($user && $user->activated == 0) {
+	$user->activated = 1;
+	$success = $user->commit();
+	if(!$success) throw new Exception("Email verification failed. Please try again later.");
+	Core::addAlert("success", "Email address verified. You can now log in to your account.");
+}
+else {
+	Core::addAlert("danger", "Invalid verification token. The user account may have already been activated or the token has expired. Please try again.");
+}
+
+HTTP::redirect("/log-in");

+ 2 - 2
emails/registration.html

@@ -13,7 +13,7 @@
 						<tr>
 							<td align="center" style="padding: 40px 0 40px 0; border-bottom: 1px solid #cccccc;">
 								<a href="">
-									<img src="http://local.check-policy.co.uk/img/logo.png" width="386" height="80" style="display: block;" alt="Check Policy logo" />
+									<img src="../public/img/logo.png" width="386" height="80" style="display: block;" alt="Check Policy logo" />
 								</a>
 							</td>
 						</tr>
@@ -32,7 +32,7 @@
 									</tr>
 									<tr>
 										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
-											Welcome to Check Policy! Before you can take part in tests, we will need you to submit some ID. You can do so by going to the following link:
+											Welcome to Check Policy! Before you can log in, you will need to verify your email address. You can do so by tapping the following link:
 										</td>
 									</tr>
 									<tr>

+ 21 - 1
engine/email.php

@@ -32,7 +32,27 @@ class Email {
 		$path = "emails/$template_name.html";
 		if(!file_exists($path)) throw new Exception("Email HTML template \"$template_name\" cannot be found.");
 
-		$this->htmlContent = file_get_contents($path);
+		$content = file_get_contents($path);
+
+		// Embed images
+		$match_count = preg_match_all("/src\=\"(\.\.\/public\/img\/[^\"]*)\"/i", $content, $all_matches);
+		if($match_count > 0) {
+
+			$paths = $all_matches[1];
+
+			foreach($paths as $path) {
+				$filename = basename($path);
+				$cid = str_replace(".", "_", $filename);
+
+				$path = ltrim($path, "./");
+
+				$this->instance->addEmbeddedImage($path, $cid, $filename);
+				$content = str_replace("../$path", "cid:$cid", $content);
+
+			}
+		}
+
+		$this->htmlContent = $content;
 	}
 
 	public function setValues($values = []) {

+ 24 - 1
engine/user.php

@@ -4,7 +4,7 @@ class User extends Model {
 
 	protected static $table = "user";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["email", "old_id", "password", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "phone", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "verifier_id", "verifier_notes"];
+	protected static $fillable_fields = ["email", "old_id", "password", "activated", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "phone", "verify_token", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "dts_registered", "dts_last_seen", "dts_verified", "verifier_id", "verifier_notes"];
 
 	protected static $field_map = [
 	];
@@ -53,6 +53,29 @@ class User extends Model {
 	}
 
 	// Object methods
+	public function sendActivationEmail() {
+
+		$this->verify_token = bin2hex(random_bytes(16));
+		$this->commit();
+
+		$mail = new Email();
+
+		$mail->loadHtmlTemplate("registration");
+		$mail->setValues([
+			"name" => $this->first_name,
+			"link" => PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token
+		]);
+
+		$mail->setFrom("noreply@check-policy.co.uk", "Check Policy");
+		$mail->addAddress($this->email, $this->getDisplayName());
+
+		$mail->setSubject("Verify your email address");
+
+		$mail->send();
+
+		return true;
+	}
+
 	public function canAdmin() {
 		return $this->level >= User::LEVEL_ADMIN;
 	}

+ 1 - 0
routes.php

@@ -23,6 +23,7 @@ $routes = [
 	"/^log-out$/" => ["controller" => "log_out"],
 	"/^activate$/" => ["controller" => "activate"],
 	"/^privacy-policy$/" => ["controller" => "privacy_policy"],
+	"/^verify-email\/([a-f0-9]+)$/" => ["controller" => "user_activation", "params" => ["token"]],
 
 	// Marketing
 	"/^services$/" => ["controller" => "services"],