Allow direct access to success page
[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     if ($code === 'success') {
21         require $tplDir . '/activate-success.phtml';
22         exit();
23     }
24 }
25
26 if ($profile === null) {
27     require $tplDir . '/activate-code.phtml';
28     exit();
29 }
30
31 $input = [
32     'gamerTag'    => $_POST['gamerTag'] ?? null,
33     'founderFlag' => (bool) ($_POST['founderFlag'] ?? false),
34     'founderName' => $_POST['founderName'] ?? null,
35     'minAge'      => $_POST['minAge'] ?? 3,
36     'avatar'      => $_POST['avatar'] ?? 'avatar_1',
37     'submit'      => $_POST['submit'] ?? false,
38 ];
39
40 $avatars = [];
41 foreach (glob(__DIR__ . '/../www/resources/avatars/*.small.jpg') as $smallImage) {
42     $key = basename($smallImage, '.small.jpg');
43     $avatars[$key] = '/resources/avatars/' . basename($smallImage);
44 }
45
46 //input validation
47 $errors = [];
48 if (!preg_match('#^[A-Za-z0-9 ]+$#', $input['gamerTag'])) {
49     $errors['gamerTag'] = 'Invalid gamer tag';
50 }
51 if ($input['founderFlag']) {
52     if ($input['founderName'] === '') {
53         $errors['founderName'] = 'Founder name missing';
54     } else if (!preg_match('#^[A-Za-z0-9 ]+$#', $input['founderName'])) {
55         $errors['founderName'] = 'Invalid founder name';
56     }
57 }
58 if (!in_array($input['minAge'], [3, 7, 12, 17])) {
59     $errors['minAge'] = 'Invalid age';
60 }
61 if (!in_array($input['avatar'], array_keys($avatars))) {
62     $errors['avatar'] = 'Invalid avatar image';
63 }
64
65 if (!$input['submit'] || count($errors)) {
66     require $tplDir . '/activate-profile.phtml';
67     exit();
68 }
69
70 //validation successful, store the profile
71 //$input['verificationCode'] => null;
72 unset($input['submit']);
73 $profile = $profileDb->updateProfile($profile->hwId, $input);
74
75 require $tplDir . '/activate-success.phtml';