Fix indentation and close tags in html templates and css
[gamestick-pjgsapi.git] / www / activate.php
1 <?php
2 $rootDir = dirname(__FILE__, 2);
3 $tplDir = $rootDir . '/templates';
4
5 require_once $rootDir . '/config.php';
6 require $rootDir . '/src/ProfileDb.php';
7 $profileDb = new ProfileDb();
8
9 $error   = null;
10 $code    = '';
11 $profile = null;
12 if (isset($_REQUEST['code']) && trim($_REQUEST['code']) !== '') {
13     $code = $_REQUEST['code'];
14
15     $profile = $profileDb->getProfileByVerificationCode($code);
16     if ($profile === null) {
17         $error = 'Invalid code';
18     }
19 }
20
21 if ($profile === null) {
22     require $tplDir . '/activate-code.phtml';
23     exit();
24 }
25
26 $input = [
27     'gamerTag'    => $_POST['gamerTag'] ?? null,
28     'founderFlag' => (bool) ($_POST['founderFlag'] ?? false),
29     'founderName' => $_POST['founderName'] ?? null,
30     'minAge'      => $_POST['minAge'] ?? 3,
31     'avatar'      => $_POST['avatar'] ?? 'avatar_1',
32     'submit'      => $_POST['submit'] ?? false,
33 ];
34
35 $avatars = [];
36 foreach (glob(__DIR__ . '/../www/resources/avatars/*.small.jpg') as $smallImage) {
37     $key = basename($smallImage, '.small.jpg');
38     $avatars[$key] = '/resources/avatars/' . basename($smallImage);
39 }
40
41 //input validation
42 $errors = [];
43 if (!preg_match('#^[A-Za-z0-9 ]+$#', $input['gamerTag'])) {
44     $errors['gamerTag'] = 'Invalid gamer tag';
45 }
46 if ($input['founderFlag']) {
47     if ($input['founderName'] === '') {
48         $errors['founderName'] = 'Founder name missing';
49     } else if (!preg_match('#^[A-Za-z0-9 ]+$#', $input['founderName'])) {
50         $errors['founderName'] = 'Invalid founder name';
51     }
52 }
53 if (!in_array($input['minAge'], [3, 7, 12, 17])) {
54     $errors['minAge'] = 'Invalid age';
55 }
56 if (!in_array($input['avatar'], array_keys($avatars))) {
57     $errors['avatar'] = 'Invalid avatar image';
58 }
59
60 if (!$input['submit'] || count($errors)) {
61     require $tplDir . '/activate-profile.phtml';
62     exit();
63 }
64
65 //validation successful, store the profile
66 //$input['verificationCode'] => null;
67 unset($input['submit']);
68 $profile = $profileDb->updateProfile($profile->hwId, $input);
69
70 require $tplDir . '/activate-success.phtml';