3 * Click "push to my OUYA" in the browser, and the OUYA will install
4 * the game a few minutes later.
6 * Works without registration.
7 * We simply use the IP address as user identification.
8 * Pushed games are deleted after 24 hours.
9 * Maximal 30 games per IP to prevent flooding.
11 * @author Christian Weiske <cweiske@cweiske.de>
12 * @see api/v1/queued_downloads.php
13 * @see api/v1/queued_downloads_delete.php
15 $dbFile = __DIR__ . '/../data/push-to-my-ouya.sqlite3';
16 $apiGameDir = __DIR__ . '/api/v1/details-data/';
18 require_once __DIR__ . '/../src/push-to-my-ouya-helpers.php';
20 //support different ipv4-only domain
21 header('Access-Control-Allow-Origin: *');
23 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
24 header('HTTP/1.0 400 Bad Request');
25 header('Content-type: text/plain');
26 echo 'POST only, please' . "\n";
30 if (!isset($_GET['game'])) {
31 header('HTTP/1.0 400 Bad Request');
32 header('Content-type: text/plain');
33 echo '"game" parameter missing' . "\n";
37 $game = $_GET['game'];
38 $cleanGame = preg_replace('#[^a-zA-Z0-9._]#', '', $game);
39 if ($game != $cleanGame) {
40 header('HTTP/1.0 400 Bad Request');
41 header('Content-type: text/plain');
42 echo 'Invalid game' . "\n";
46 $apiGameFile = $apiGameDir . $game . '.json';
47 if (!file_exists($apiGameFile)) {
48 header('HTTP/1.0 404 Not Found');
49 header('Content-type: text/plain');
50 echo 'Game does not exist' . "\n";
54 $ip = $_SERVER['REMOTE_ADDR'];
56 header('HTTP/1.0 400 Bad Request');
57 header('Content-type: text/plain');
58 echo 'Cannot detect your IP address' . "\n";
64 $db = new SQLite3($dbFile, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
65 } catch (Exception $e) {
66 header('HTTP/1.0 500 Internal server error');
67 header('Content-type: text/plain');
68 echo 'Cannot open database' . "\n";
69 echo $e->getMessage() . "\n";
73 $res = $db->querySingle(
74 'SELECT name FROM sqlite_master WHERE type = "table" AND name = "pushes"'
77 //table does not exist yet
81 id INTEGER PRIMARY KEY AUTOINCREMENT,
84 created_at TEXT DEFAULT CURRENT_TIMESTAMP
93 . ' WHERE created_at < \'' . gmdate('Y-m-d H:i:s', time() - 86400) . '\''
96 //check if this IP already pushed this game
97 $numThisGame = $db->querySingle(
98 'SELECT COUNT(*) FROM pushes'
99 . ' WHERE ip = \'' . SQLite3::escapeString($ip) . '\''
100 . ' AND game = \'' . SQLite3::escapeString($game) . '\''
102 if ($numThisGame >= 1) {
103 header('HTTP/1.0 400 Bad Request');
104 header('Content-type: text/plain');
105 echo 'Already pushed.' . "\n";
109 //check number of pushes for this IP
110 $numPushes = $db->querySingle(
111 'SELECT COUNT(*) FROM pushes'
112 . ' WHERE ip = \'' . SQLite3::escapeString($ip) . '\''
114 if ($numPushes >= 30) {
115 header('HTTP/1.0 400 Bad Request');
116 header('Content-type: text/plain');
117 echo 'Too many pushes. Come back tomorrow.' . "\n";
122 $stmt = $db->prepare('INSERT INTO pushes (game, ip) VALUES(:game, :ip)');
123 $stmt->bindValue(':game', $game);
124 $stmt->bindValue(':ip', $ip);
125 $res = $stmt->execute();
126 if ($res === false) {
127 header('HTTP/1.0 500 Internal server error');
128 header('Content-type: text/plain');
129 echo 'Cannot store push' . "\n";
134 header('HTTP/1.0 200 OK');
135 header('Content-type: text/plain');
136 echo 'Push accepted' . "\n";