Fix png images
[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'] ?? 'rocket',
37     'submit'      => $_POST['submit'] ?? false,
38 ];
39
40 $avatars = [
41     $input['avatar'] => null,//have active one first, especially for mobile
42 ];
43 $avatarFiles = glob(__DIR__ . '/../www/resources/avatars/*.small.{jpg,png}', GLOB_BRACE);
44 foreach ($avatarFiles as $smallImage) {
45     $key = basename($smallImage, '.small.jpg');
46     $key = basename($key, '.small.png');
47     $avatars[$key] = '/resources/avatars/' . basename($smallImage);
48 }
49 $avatars = array_filter($avatars);
50
51 //input validation
52 $errors = [];
53 if (!preg_match('#^[A-Za-z0-9 ]+$#', $input['gamerTag'])) {
54     $errors['gamerTag'] = 'Invalid gamer tag';
55 }
56 if ($input['founderFlag']) {
57     if ($input['founderName'] === '') {
58         $errors['founderName'] = 'Founder name missing';
59     } else if (!preg_match('#^[A-Za-z0-9 ]+$#', $input['founderName'])) {
60         $errors['founderName'] = 'Invalid founder name';
61     }
62 }
63 if (!in_array($input['minAge'], [3, 7, 12, 17])) {
64     $errors['minAge'] = 'Invalid age';
65 }
66 if (!in_array($input['avatar'], array_keys($avatars))) {
67     $errors['avatar'] = 'Invalid avatar image';
68 }
69
70 if (!$input['submit'] || count($errors)) {
71     require $tplDir . '/activate-profile.phtml';
72     exit();
73 }
74
75 //validation successful, store the profile
76 //$input['verificationCode'] => null;
77 unset($input['submit']);
78 $profile = $profileDb->updateProfile($profile->hwId, $input);
79
80 require $tplDir . '/activate-success.phtml';