No error in firmware update check when changelog is missing
[gamestick-pjgsapi.git] / www / check.php
1 <?php
2 /**
3  * Firmware update check
4  *
5  * Custom firmware versions need a folder in www/firmware/,
6  * e.g. www/firmware/0.9.2071/.
7  * Each folder needs to contain:
8  * - changelog.txt
9  * - update.img
10  *
11  * $GLOBALS['forceFirmwareVersion'] can be set to a custom version
12  */
13 header('HTTP/1.0 500 Internal Server Error');
14
15 $rootDir = dirname(__FILE__, 2);
16 require_once $rootDir . '/config.php';
17
18 if (!isset($_POST['v'])) {
19     header('HTTP/1.0 400 Bad Request');
20     header('Content-Type: text/plain');
21     echo "POST data missing\n";
22     exit(1);
23 }
24
25 $gsData = json_decode($_POST['v']);
26 if ($gsData === null) {
27     header('HTTP/1.0 400 Bad Request');
28     header('Content-Type: text/plain');
29     echo "POST data invalid\n";
30     exit(1);
31 }
32
33 if (!isset($gsData->hwid)
34     || !isset($gsData->major)
35     || !isset($gsData->minor)
36     || !isset($gsData->revision)
37     || !isset($gsData->platform)
38 ) {
39     header('HTTP/1.0 400 Bad Request');
40     header('Content-Type: text/plain');
41     echo "POST data incomplete\n";
42     exit(1);
43 }
44
45 $gsVersion = $gsData->major . '.' . $gsData->minor . '.' . $gsData->revision;
46
47 $requireUpdate = false;
48 if (isset($GLOBALS['firmwareVersion'])) {
49     $expectedVersion = $GLOBALS['firmwareVersion'];
50     if ($gsVersion != $expectedVersion) {
51         $requireUpdate = true;
52     }
53 } else {
54     $expectedVersion = '0.9.2071';
55     if (version_compare($expectedVersion, $gsVersion, '>')) {
56         $requireUpdate = true;
57     }
58 }
59 if (!$requireUpdate) {
60     header('HTTP/1.0 200 OK');
61     header('Content-type: application/json');
62     echo '{"available":false}' . "\n";
63     exit(0);
64 }
65
66 list($major, $minor, $revision) = explode('.', $expectedVersion);
67
68
69 $firmwareDir  = $rootDir . '/www/firmware/' . $expectedVersion;
70 $firmwareFile = $firmwareDir . '/update.img';
71
72 if (!file_exists($firmwareFile)) {
73     header('HTTP/1.0 200 OK');
74     header('Content-type: application/json');
75     header('X-Problem: Firmware file missing');
76     echo '{"available":false}' . "\n";
77     exit(0);
78 }
79
80 $changelogFile = $firmwareDir . '/changelog.txt';
81 $changelog = '';
82 if (file_exists($changelogFile)) {
83     $changelog = file_get_contents($changelogFile);
84 }
85
86 $data = [
87     'available'   => true,
88     'major'       => $major,
89     'minor'       => $minor,
90     'revision'    => $revision,
91     'forced'      => false,
92     'name'        => $expectedVersion,
93     'description' => $changelog,
94     'timestamp'   => filemtime($firmwareFile),
95     'url'         => 'http://update.gamestickservices.net/firmware/download?version=' . $expectedVersion,
96 ];
97
98 $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
99
100 header('HTTP/1.0 200 OK');
101 header('Content-Type: application/json');
102 echo $json . "\n";