From 64f8dd8b217d8582fe1c2c45a9f35d6907ee72c7 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 24 May 2023 21:11:13 +0200 Subject: [PATCH] Unstyled browser-based activation process --- src/ProfileDb.php | 29 +++++++++ templates/activate-code.phtml | 26 ++++++++ templates/activate-profile.phtml | 106 +++++++++++++++++++++++++++++++ templates/activate-success.phtml | 13 ++++ www/.htaccess | 6 +- www/activate.php | 70 ++++++++++++++++++++ 6 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 templates/activate-code.phtml create mode 100644 templates/activate-profile.phtml create mode 100644 templates/activate-success.phtml create mode 100644 www/activate.php diff --git a/src/ProfileDb.php b/src/ProfileDb.php index 7f661b7..5eed726 100644 --- a/src/ProfileDb.php +++ b/src/ProfileDb.php @@ -40,6 +40,15 @@ class ProfileDb return $row === false ? null : $row; } + public function getProfileByVerificationCode(string $code): ?Profile + { + $stmt = $this->db->prepare('SELECT * FROM gamesticks WHERE verificationCode = :code'); + $stmt->setFetchMode(PDO::FETCH_CLASS, 'Profile'); + $stmt->execute([':code' => $code]); + $row = $stmt->fetch(); + return $row === false ? null : $row; + } + public function createProfile(string $hwId): Profile { $stmt = $this->db->prepare( @@ -59,6 +68,26 @@ SQL return $this->getProfileByHardwareId($hwId); } + public function updateProfile(string $hwId, array $values): ?Profile + { + $params = [ + 'hwId' => $hwId, + ]; + + $sql = 'UPDATE gamesticks SET'; + $sqlParts = []; + foreach ($values as $column => $value) { + $sqlParts[] = ' ' . $column . '= :' . $column; + $params[':' . $column] = $value; + } + $sql .= implode(', ', $sqlParts) . ' WHERE hwId = :hwId'; + + $stmt = $this->db->prepare($sql); + $stmt->execute($params); + + return $this->getProfileByHardwareId($hwId); + } + protected function createTablesIfNeeded() { $res = $this->db->query( diff --git a/templates/activate-code.phtml b/templates/activate-code.phtml new file mode 100644 index 0000000..16da967 --- /dev/null +++ b/templates/activate-code.phtml @@ -0,0 +1,26 @@ + + + + GameStick activation: Code + + + +

PlayJam GameStick activation

+
+
+ + +
+ +
+

+
+ + +
+ + diff --git a/templates/activate-profile.phtml b/templates/activate-profile.phtml new file mode 100644 index 0000000..c455af6 --- /dev/null +++ b/templates/activate-profile.phtml @@ -0,0 +1,106 @@ + + + + GameStick activation: Profile + + + +

PlayJam GameStick activation: Profile

+
+ + + +
+ Errors: +
    + +
  • + +
+
+ + +
+ + +
+ +
+ +
+ +
+ + +
+ +
+ +
+ +
+ +
+ +
+
+ +
+ + $smallImagePath): ?> +
+ + +
+ + +
+ + diff --git a/templates/activate-success.phtml b/templates/activate-success.phtml new file mode 100644 index 0000000..cc7f858 --- /dev/null +++ b/templates/activate-success.phtml @@ -0,0 +1,13 @@ + + + + GameStick activation: Success + + +

PlayJam GameStick activation complete

+

+ Your profile has been updated. + You can continue with your GameStick setup. +

+ + diff --git a/www/.htaccess b/www/.htaccess index 5c1b8be..2e40d0b 100644 --- a/www/.htaccess +++ b/www/.htaccess @@ -1,8 +1,7 @@ RewriteEngine on RewriteBase / -RewriteRule ^connect_check.php$ - [R=204,L] -RewriteRule ^generate_204 - [R=204,L] +RewriteRule ^activate$ /activate.php [END] RewriteRule ^api/rest/analytics/application-event/analytics/event/view.json(.*)$ /api/rest/analytics/application-event/analytics/event/view.json [END] RewriteRule ^api/rest/analytics/game/(.*)/event/(.*)/view.json(.*)$ /api/rest/analytics/application-event/analytics/event/view.json [END] @@ -12,3 +11,6 @@ RewriteRule ^api/rest/connect/stick/stick/(.*)/view.json;jsessionid=(.*)$ /api/r RewriteRule ^api/rest/player/profile/view.json$ /api/rest/player/profile.php [END] RewriteRule ^api/rest/player/profile/view.json;jsessionid=(.*)$ /api/rest/player/profile.php?jsessionid=$1 [END] + +RewriteRule ^connect_check.php$ - [R=204,L] +RewriteRule ^generate_204 - [R=204,L] diff --git a/www/activate.php b/www/activate.php new file mode 100644 index 0000000..454c7e0 --- /dev/null +++ b/www/activate.php @@ -0,0 +1,70 @@ +getProfileByVerificationCode($code); + if ($profile === null) { + $error = 'Invalid code'; + } +} + +if ($profile === null) { + require $tplDir . '/activate-code.phtml'; + exit(); +} + +$input = [ + 'gamerTag' => $_POST['gamerTag'] ?? null, + 'founderFlag' => (bool) ($_POST['founderFlag'] ?? false), + 'founderName' => $_POST['founderName'] ?? null, + 'minAge' => $_POST['minAge'] ?? 3, + 'avatar' => $_POST['avatar'] ?? 'avatar_1', + 'submit' => $_POST['submit'] ?? false, +]; + +$avatars = []; +foreach (glob(__DIR__ . '/../www/resources/avatars/*.small.jpg') as $smallImage) { + $key = basename($smallImage, '.small.jpg'); + $avatars[$key] = '/resources/avatars/' . basename($smallImage); +} + +//input validation +$errors = []; +if (!preg_match('#^[A-Za-z0-9 ]+$#', $input['gamerTag'])) { + $errors['gamerTag'] = 'Invalid gamer tag'; +} +if ($input['founderFlag']) { + if ($input['founderName'] === '') { + $errors['founderName'] = 'Founder name missing'; + } else if (!preg_match('#^[A-Za-z0-9 ]+$#', $input['founderName'])) { + $errors['founderName'] = 'Invalid founder name'; + } +} +if (!in_array($input['minAge'], [3, 7, 12, 17])) { + $errors['minAge'] = 'Invalid age'; +} +if (!in_array($input['avatar'], array_keys($avatars))) { + $errors['avatar'] = 'Invalid avatar image'; +} + +if (!$input['submit'] || count($errors)) { + require $tplDir . '/activate-profile.phtml'; + exit(); +} + +//validation successful, store the profile +//$input['verificationCode'] => null; +unset($input['submit']); +$profile = $profileDb->updateProfile($profile->hwId, $input); + +require $tplDir . '/activate-success.phtml'; -- 2.30.2