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