Fix HTML background image size
[stouyapi.git] / www / api / v1 / queued_downloads.php
1 <?php
2 /**
3  * List games from the "push to my OUYA" list
4  *
5  * Pushes are stored in the sqlite3 database in push-to-my-ouya.php
6  *
7  * @author Christian Weiske <cweiske@cweiske.de>
8  */
9 $dbFile     = __DIR__ . '/../../../data/push-to-my-ouya.sqlite3';
10 $apiGameDir = __DIR__ . '/details-data/';
11
12 require_once __DIR__ . '/../../../src/push-to-my-ouya-helpers.php';
13
14 $ip = $_SERVER['REMOTE_ADDR'];
15 if ($ip == '') {
16     //empty ip
17     header('X-Fail-Reason: empty ip address');
18     header('Content-type: application/json');
19     echo file_get_contents('queued_downloads');
20     exit(1);
21 }
22 $ip = mapIp($ip);
23
24 try {
25     $db = new SQLite3($dbFile, SQLITE3_OPEN_READONLY);
26 } catch (Exception $e) {
27     //db file not found
28     header('X-Fail-Reason: database file not found');
29     header('Content-type: application/json');
30     echo file_get_contents('queued_downloads');
31     exit(1);
32 }
33
34 $res = $db->query(
35     'SELECT * FROM pushes'
36     . ' WHERE ip = \'' . SQLite3::escapeString($ip) . '\''
37 );
38 $queue = [];
39 while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
40     $apiGameFile = $apiGameDir . $row['game'] . '.json';
41     if (!file_exists($apiGameFile)) {
42         //game deleted?
43         continue;
44     }
45     $json = json_decode(file_get_contents($apiGameFile));
46     $queue[] = [
47         'versionUuid' => '',
48         'title'       => $json->title,
49         'source'      => 'gamer',
50         'uuid'        => $row['game'],
51     ];
52 }
53
54 header('Content-type: application/json');
55 echo json_encode(['queue' => $queue]) . "\n";
56 ?>