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>
13 $dbFile = __DIR__ . '/../data/push-to-my-ouya.sqlite3';
14 $apiGameDir = __DIR__ . '/api/v1/details-data/';
16 require_once __DIR__ . '/../src/push-to-my-ouya-helpers.php';
18 //support different ipv4-only domain
19 header('Access-Control-Allow-Origin: *');
21 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
22 header('HTTP/1.0 400 Bad Request');
23 header('Content-type: text/plain');
24 echo 'POST only, please' . "\n";
28 if (!isset($_GET['game'])) {
29 header('HTTP/1.0 400 Bad Request');
30 header('Content-type: text/plain');
31 echo '"game" parameter missing' . "\n";
35 $game = $_GET['game'];
36 $cleanGame = preg_replace('#[^a-zA-Z0-9.]#', '', $game);
37 if ($game != $cleanGame) {
38 header('HTTP/1.0 400 Bad Request');
39 header('Content-type: text/plain');
40 echo 'Invalid game' . "\n";
44 $apiGameFile = $apiGameDir . $game . '.json';
45 if (!file_exists($apiGameFile)) {
46 header('HTTP/1.0 404 Not Found');
47 header('Content-type: text/plain');
48 echo 'Game does not exist' . "\n";
52 $ip = $_SERVER['REMOTE_ADDR'];
54 header('HTTP/1.0 400 Bad Request');
55 header('Content-type: text/plain');
56 echo 'Cannot detect your IP address' . "\n";
59 if (strpos($ip, ':') !== false) {
60 header('HTTP/1.0 400 Bad Request');
61 header('Content-type: text/plain');
62 echo 'Sorry, IPv6 is not supported' . "\n";
63 echo 'This here only works if the OUYA and your PC have the same IP address,'
65 echo 'and this is definitely not the case when using IPv6' . "\n";
71 $db = new SQLite3($dbFile, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
72 } catch (Exception $e) {
73 header('HTTP/1.0 500 Internal server error');
74 header('Content-type: text/plain');
75 echo 'Cannot open database' . "\n";
76 echo $e->getMessage() . "\n";
80 $res = $db->querySingle(
81 'SELECT name FROM sqlite_master WHERE type = "table" AND name = "pushes"'
84 //table does not exist yet
88 id INTEGER PRIMARY KEY AUTOINCREMENT,
91 created_at TEXT DEFAULT CURRENT_TIMESTAMP
100 . ' WHERE created_at < \'' . gmdate('Y-m-d H:i:s', time() - 86400) . '\''
103 //check if this IP already pushed this game
104 $numThisGame = $db->querySingle(
105 'SELECT COUNT(*) FROM pushes'
106 . ' WHERE ip = \'' . SQLite3::escapeString($ip) . '\''
107 . ' AND game = \'' . SQLite3::escapeString($game) . '\''
109 if ($numThisGame >= 1) {
110 header('HTTP/1.0 400 Bad Request');
111 header('Content-type: text/plain');
112 echo 'Already pushed.' . "\n";
116 //check number of pushes for this IP
117 $numPushes = $db->querySingle(
118 'SELECT COUNT(*) FROM pushes'
119 . ' WHERE ip = \'' . SQLite3::escapeString($ip) . '\''
121 if ($numPushes >= 30) {
122 header('HTTP/1.0 400 Bad Request');
123 header('Content-type: text/plain');
124 echo 'Too many pushes. Come back tomorrow.' . "\n";
129 $stmt = $db->prepare('INSERT INTO pushes (game, ip) VALUES(:game, :ip)');
130 $stmt->bindValue(':game', $game);
131 $stmt->bindValue(':ip', $ip);
132 $res = $stmt->execute();
133 if ($res === false) {
134 header('HTTP/1.0 500 Internal server error');
135 header('Content-type: text/plain');
136 echo 'Cannot store push' . "\n";
141 header('HTTP/1.0 200 OK');
142 header('Content-type: text/plain');
143 echo 'Push accepted' . "\n";