Add IPv6 support for push-to-my-ouya
[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 == '' || strpos($ip, ':') !== false) {
16     //empty or IPv6
17     header('Content-type: application/json');
18     echo file_get_contents('queued_downloads');
19     exit(1);
20 }
21 $ip = mapIp($ip);
22
23 try {
24     $db = new SQLite3($dbFile, SQLITE3_OPEN_READONLY);
25 } catch (Exception $e) {
26     //db file not found
27     header('Content-type: application/json');
28     echo file_get_contents('queued_downloads');
29     exit(1);
30 }
31
32 $res = $db->query(
33     'SELECT * FROM pushes'
34     . ' WHERE ip = \'' . SQLite3::escapeString($ip) . '\''
35 );
36 $queue = [];
37 while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
38     $apiGameFile = $apiGameDir . $row['game'] . '.json';
39     if (!file_exists($apiGameFile)) {
40         //game deleted?
41         continue;
42     }
43     $json = json_decode(file_get_contents($apiGameFile));
44     $queue[] = [
45         'versionUuid' => '',
46         'title'       => $json->title,
47         'source'      => 'gamer',
48         'uuid'        => $row['game'],
49     ];
50 }
51
52 header('Content-type: application/json');
53 echo json_encode(['queue' => $queue]) . "\n";
54 ?>