Script to generate the games menu with all infos
[gamestick-pjgsapi.git] / www / api / rest / connect / stick / stick / generate.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 require_once dirname(__FILE__, 7) . '/config.php';
9
10 $cacheDir = dirname(__FILE__, 7) . '/cache/';
11
12 $nowMilli = time() * 1000;
13 $data = [
14     'sid'             => 'FIXME_SID',
15     'time'            => (string) $nowMilli,
16     'lastaccessed'    => $nowMilli,
17     'x-forwarded-for' => null,
18     'created'         => $nowMilli,
19     'accessCount'     => 0,
20     'addr'            => '1.2.3.4',
21     'remoteaddr'      => '1.2.3.4',
22
23     'body' => [
24         'status' => 'CONNECTED',
25         'config' => [
26             'apps'   => 'FIXME_APPS',
27             'global' => [
28                 'defaults' => [
29                     'images'   => [],
30                     'currency' => null,
31                     'social'   => [],
32                 ],
33                 'uitranslation' => [
34                     'version' => 0,
35                     'country' => [],
36                 ],
37                 'newfeatured' => [
38                     'ages' => 'FIXME_AGES',
39                 ],
40             ],
41         ]
42     ]
43 ];
44
45 if (isset($_GET['jsessionid'])) {
46     $data['sid'] = $_GET['jsessionid'];
47 } else if (isset($_COOKIE['JSESSIONID'])) {
48     $data['sid'] = $_COOKIE['JSESSIONID'];
49 } else {
50     //FIXME: map hwid to user sid
51     $data['sid'] = 'dummy';
52     // header('HTTP/1.0 400 Bad Request');
53     // header('Content-Type: text/plain');
54     // echo "jsessionid GET parameter or JSESSIONID cookie missing\n";
55     // exit(1);
56 }
57
58 if (!isset($_GET['hwid'])) {
59     header('HTTP/1.0 400 Bad Request');
60     header('Content-Type: text/plain');
61     echo "Hardware ID is missing\n";
62     exit(1);
63 }
64 if (count($GLOBALS['whitelistedHardwareIds'])
65     && array_search($_GET['hwid'], $GLOBALS['whitelistedHardwareIds']) === false
66 ) {
67     header('HTTP/1.0 403 Forbidden');
68     header('Content-Type: text/plain');
69     echo "Gamestick with this hardware ID may not use this server\n";
70     exit(1);
71 }
72
73
74 $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
75
76
77 //inject apps
78 $appsCacheFile = $cacheDir . 'connect-apps.min.json';
79 if (!file_exists($appsCacheFile)) {
80     header('Content-Type: text/plain');
81     echo "Cache file missing: connect-apps.min.json\n";
82     exit(1);
83 }
84 $json = str_replace('"FIXME_APPS"', file_get_contents($appsCacheFile), $json);
85
86
87 //inject featured apps
88 $featuredAgesCacheFile = $cacheDir . 'connect-featured-ages.min.json';
89 if (!file_exists($featuredAgesCacheFile)) {
90     header('Content-Type: text/plain');
91     echo "Cache file missing: connect-featured-ages.min.json\n";
92     exit(1);
93 }
94 $json = str_replace('"FIXME_AGES"', file_get_contents($featuredAgesCacheFile), $json);
95
96
97 header('HTTP/1.0 200 OK');
98 header('Content-Type: application/json');
99 echo $json . "\n";
100 ?>