Handle minimum age rating changes
[gamestick-pjgsapi.git] / www / api / rest / parentcontroll / change / agerating.php
1 <?php
2 /**
3  * Change the minimum age in the profile
4  * GET http://l2.gamestickservices.net/api/rest/parentcontroll/change/agerating/17/7694f4a66316e53c8cdd9d9954bd611d/view.json;jsessionid=zz
5  */
6 header('HTTP/1.0 500 Internal Server Error');
7
8 $rootDir = dirname(__FILE__, 6);
9 require_once $rootDir . '/config.php';
10 require_once $rootDir . '/src/ProfileDb.php';
11
12 if (!isset($_GET['jsessionid'])) {
13     header('HTTP/1.0 400 Bad Request');
14     header('Content-Type: text/plain');
15     echo "Session ID missing\n";
16     exit(1);
17 }
18 $sessionId = $_GET['jsessionid'];
19
20 if (!isset($_GET['age'])) {
21     header('HTTP/1.0 400 Bad Request');
22     header('Content-Type: text/plain');
23     echo "age missing\n";
24     exit(1);
25 }
26 $age = $_GET['age'];
27 if ($age != 3 && $age != 7 && $age != 12 && $age != 17) {
28     header('HTTP/1.0 400 Bad Request');
29     header('Content-Type: text/plain');
30     echo "Invalid age (only 3, 7, 12 and 17 allowed)\n";
31     exit(1);
32 }
33
34 if (!isset($_GET['pwhash'])) {
35     header('HTTP/1.0 400 Bad Request');
36     header('Content-Type: text/plain');
37     echo "Password hash missing\n";
38     exit(1);
39 }
40 $passwordHash = $_GET['pwhash'];
41 if (strlen($passwordHash) != 32) {
42     header('HTTP/1.0 400 Bad Request');
43     header('Content-Type: text/plain');
44     echo "Password hash must be 32 characters long\n";
45     exit(1);
46 }
47
48 $profileDb = new ProfileDb();
49 $profile = $profileDb->getProfileBySessionId($sessionId);
50 if ($profile === null) {
51     header('HTTP/1.0 404 Not Found');
52     header('Content-Type: text/plain');
53     echo "Unknown session ID\n";
54     exit(1);
55 }
56
57 //we do not verify the actual password
58 // the hash is calculated md5($password)
59 $profileDb->updateProfile(
60     $profile->hwId,
61     [
62         'minAge' => $age,
63     ]
64 );
65
66
67 $data = [
68     'body' => [
69         'success' => true,
70     ],
71 ];
72 $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
73
74 header('HTTP/1.0 200 OK');
75 header('Content-Type: application/json');
76 echo $json . "\n";