71d82739172009c54dfe4a34d5788938fab30471
[gamestick-pjgsapi.git] / www / api / rest / connect.php
1 <?php
2 /**
3  * Generate the apps list + other information available at
4  * http://l2.gamestickservices.net/api/rest/connect/stick/stick/xxx/view.json
5  */
6 header('HTTP/1.0 500 Internal Server Error');
7
8 $rootDir = dirname(__FILE__, 4);
9 require_once $rootDir . '/config.php';
10
11 $cacheDir = $rootDir . '/cache/';
12
13 $nowMilli = time() * 1000;
14
15 if (!isset($_GET['hwid'])) {
16     header('HTTP/1.0 400 Bad Request');
17     header('Content-Type: text/plain');
18     echo "Hardware ID is missing\n";
19     exit(1);
20 }
21 $hwId = strtolower($_GET['hwid']);
22
23 if (count($GLOBALS['whitelistedHardwareIds'])
24     && array_search($hwId, $GLOBALS['whitelistedHardwareIds']) === false
25 ) {
26     header('HTTP/1.0 403 Forbidden');
27     header('Content-Type: text/plain');
28     echo "Gamestick with this hardware ID may not use this server\n";
29     exit(1);
30 }
31
32 $sessionId = null;
33 if (isset($_GET['jsessionid'])) {
34     $sessionId = $_GET['jsessionid'];
35 } else if (isset($_COOKIE['JSESSIONID'])) {
36     $sessionId = $_COOKIE['JSESSIONID'];
37 }
38
39 require $rootDir . '/src/ProfileDb.php';
40 $profileDb = new ProfileDb();
41 $profile = $profileDb->getProfileByHardwareId($hwId);
42
43 if ($profile === null || !$profile->complete()) {
44     //unregistered gamestick
45     if ($profile === null) {
46         $profile = $profileDb->createProfile($hwId);
47     }
48
49     $data = [
50         'sid'  => $profile->sessionId,
51         'time' => (string) $nowMilli,
52         'body' => [
53             'status'           => 'CONNECTION_IN_PROGRESS',
54             'registrationCode' => ($GLOBALS['verificationCodePrefix'] ?? '')
55                 . $profile->verificationCode,
56         ]
57     ];
58     $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
59     header('HTTP/1.0 200 OK');
60     header('Content-Type: application/json');
61     echo $json . "\n";
62     exit(0);
63 }
64
65
66 $data = [
67     'sid'             => $profile->sessionId,
68     'time'            => (string) $nowMilli,
69     'lastaccessed'    => $nowMilli,
70     'x-forwarded-for' => null,
71     'created'         => $nowMilli,
72     'accessCount'     => 0,
73     'addr'            => '1.2.3.4',
74     'remoteaddr'      => '1.2.3.4',
75
76     'body' => [
77         'status' => 'CONNECTED',
78         'config' => [
79             'apps'   => 'FIXME_APPS',
80             'global' => [
81                 'defaults' => [
82                     'images'   => [],
83                     'currency' => null,
84                     'social'   => [],
85                 ],
86                 'uitranslation' => [
87                     'version' => 0,
88                     'country' => [],
89                 ],
90                 'newfeatured' => [
91                     'ages' => 'FIXME_AGES',
92                 ],
93             ],
94         ]
95     ]
96 ];
97
98 $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
99
100
101 //inject apps
102 $appsCacheFile = $cacheDir . 'connect-apps.min.json';
103 if (!file_exists($appsCacheFile)) {
104     header('Content-Type: text/plain');
105     echo "Cache file missing: connect-apps.min.json\n";
106     exit(1);
107 }
108 $json = str_replace('"FIXME_APPS"', file_get_contents($appsCacheFile), $json);
109
110
111 //inject featured apps
112 $featuredAgesCacheFile = $cacheDir . 'connect-featured-ages.min.json';
113 if (!file_exists($featuredAgesCacheFile)) {
114     header('Content-Type: text/plain');
115     echo "Cache file missing: connect-featured-ages.min.json\n";
116     exit(1);
117 }
118 $json = str_replace('"FIXME_AGES"', file_get_contents($featuredAgesCacheFile), $json);
119
120
121 header('HTTP/1.0 200 OK');
122 header('Content-Type: application/json');
123 echo $json . "\n";
124 ?>