From db63b55a700996ddbd6c424bdd9fc182b199c77a Mon Sep 17 00:00:00 2001 From: Roman Hergenreder Date: Wed, 1 Jul 2020 23:07:00 +0200 Subject: [PATCH] Routing Fix + E-Mail Confirm frontend --- core/Api/Stats.class.php | 1 - core/Api/UserAPI.class.php | 24 +++++++++------- core/Views/Account/AccountView.class.php | 4 ++- core/Views/Account/ConfirmEmail.class.php | 35 +++++++++++++++++++---- core/Views/Account/Register.class.php | 23 +++++++++++---- index.php | 8 ++++++ 6 files changed, 73 insertions(+), 22 deletions(-) diff --git a/core/Api/Stats.class.php b/core/Api/Stats.class.php index 79b868d..052dcc1 100644 --- a/core/Api/Stats.class.php +++ b/core/Api/Stats.class.php @@ -2,7 +2,6 @@ namespace Api; -use Driver\SQL\Condition\Compare; use Driver\SQL\Condition\CondBool; class Stats extends Request { diff --git a/core/Api/UserAPI.class.php b/core/Api/UserAPI.class.php index 22bc3ec..00721da 100644 --- a/core/Api/UserAPI.class.php +++ b/core/Api/UserAPI.class.php @@ -544,17 +544,21 @@ namespace Api\User { $this->success = $req->execute(array("token" => $token)); $this->lastError = $req->getLastError(); - $result = $req->getResult(); - if (strcasecmp($result["token"]["type"], "invite") !== 0) { - return $this->createError("Invalid token type"); - } else if($result["user"]["confirmed"]) { - return $this->createError("Your email address is already confirmed."); - } else if (!$this->updateUser($result["user"]["uid"])) { - return false; - } else { - $this->invalidateToken($token); - return true; + if ($this->success) { + $result = $req->getResult(); + if (strcasecmp($result["token"]["type"], "email_confirm") !== 0) { + return $this->createError("Invalid token type"); + } else if($result["user"]["confirmed"]) { + return $this->createError("Your email address is already confirmed."); + } else if (!$this->updateUser($result["user"]["uid"])) { + return false; + } else { + $this->invalidateToken($token); + return true; + } } + + return $this->success; } } diff --git a/core/Views/Account/AccountView.class.php b/core/Views/Account/AccountView.class.php index 01d0263..b692b28 100644 --- a/core/Views/Account/AccountView.class.php +++ b/core/Views/Account/AccountView.class.php @@ -8,17 +8,19 @@ use Elements\View; abstract class AccountView extends View { protected string $description; + protected string $icon; public function __construct(Document $document, $loadView = true) { parent::__construct($document, $loadView); $this->description = ""; + $this->icon = "image"; } public function getCode() { $html = parent::getCode(); $content = $this->getAccountContent(); - $icon = $this->createIcon("user-plus", "fas", "fa-3x"); + $icon = $this->createIcon($this->icon, "fas", "fa-3x"); $html .= "
diff --git a/core/Views/Account/ConfirmEmail.class.php b/core/Views/Account/ConfirmEmail.class.php index 5ae50d4..47abe06 100644 --- a/core/Views/Account/ConfirmEmail.class.php +++ b/core/Views/Account/ConfirmEmail.class.php @@ -5,17 +5,42 @@ namespace Views\Account; use Elements\Document; -use Elements\View; -class ConfirmEmail extends View { +class ConfirmEmail extends AccountView { + + private bool $success; + private string $message; public function __construct(Document $document, $loadView = true) { parent::__construct($document, $loadView); + $this->title = "Confirm Email"; + $this->icon = "user-check"; + $this->success = false; + $this->message = "No content"; } - public function getCode() { - $html = parent::getCode(); + public function loadView() { + parent::loadView(); - return $html; + if (isset($_GET["token"]) && is_string($_GET["token"]) && !empty($_GET["token"])) { + $req = new \Api\User\ConfirmEmail($this->getDocument()->getUser()); + $this->success = $req->execute(array("token" => $_GET["token"])); + if ($this->success) { + $this->message = "Your e-mail address was successfully confirmed, you may now log in"; + } else { + $this->message = "Error confirming e-mail address: " . $req->getLastError(); + } + } else { + $this->success = false; + $this->message = "The link you visited is no longer valid"; + } + } + + protected function getAccountContent() { + if ($this->success) { + return $this->createSuccessText($this->message); + } else { + return $this->createErrorText($this->message); + } } } \ No newline at end of file diff --git a/core/Views/Account/Register.class.php b/core/Views/Account/Register.class.php index 10f589f..704c5d0 100644 --- a/core/Views/Account/Register.class.php +++ b/core/Views/Account/Register.class.php @@ -13,6 +13,7 @@ class Register extends AccountView { parent::__construct($document, $loadView); $this->title = "Registration"; $this->description = "Create a new account"; + $this->icon = "user-plus"; } public function loadView() { @@ -37,19 +38,31 @@ class Register extends AccountView { return "

Please fill with your details

-
+
+
+ +
-
+
+
+ +
-
+
+
+ +
-
+
+
+ +
-
+
"; diff --git a/index.php b/index.php index f6cf97a..6ab723a 100644 --- a/index.php +++ b/index.php @@ -77,6 +77,14 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) { } } else { $requestedUri = $_GET["site"] ?? $_SERVER["REQUEST_URI"]; + if (($index = strpos($requestedUri, "?")) !== false) { + $requestedUri = substr($requestedUri, 0, $index); + } + + if (($index = strpos($requestedUri, "#")) !== false) { + $requestedUri = substr($requestedUri, 0, $index); + } + if (startsWith($requestedUri, "/")) { $requestedUri = substr($requestedUri, 1); }