Unify domain names in README
[stouyapi.git] / www / api / v1 / queued_downloads_delete.php
1 <?php
2 /**
3  * Delete a game 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('HTTP/1.0 204 No Content');
19     exit(1);
20 }
21 $ip = mapIp($ip);
22
23 if (!isset($_GET['game'])) {
24     header('HTTP/1.0 400 Bad Request');
25     header('Content-type: text/plain');
26     echo 'Game parameter missing' . "\n";
27     exit(1);
28 }
29
30 $game = $_GET['game'];
31 $cleanGame = preg_replace('#[^a-zA-Z0-9._]#', '', $game);
32 if ($game != $cleanGame || $game == '') {
33     header('HTTP/1.0 400 Bad Request');
34     header('Content-type: text/plain');
35     echo 'Invalid game' . "\n";
36     exit(1);
37 }
38
39 try {
40     $db = new SQLite3($dbFile, SQLITE3_OPEN_READWRITE);
41 } catch (Exception $e) {
42     //db file not found
43     header('X-Fail-Reason: database file not found');
44     header('HTTP/1.0 204 No Content');
45     exit(1);
46 }
47
48 $rowId = $db->querySingle(
49     'SELECT id FROM pushes'
50     . ' WHERE ip = \'' . SQLite3::escapeString($ip) . '\''
51     . ' AND game =\'' . SQLite3::escapeString($game) . '\''
52 );
53 if ($rowId === null) {
54     header('HTTP/1.0 404 Not Found');
55     header('Content-type: text/plain');
56     echo 'Game not queued' . "\n";
57     exit(1);
58 }
59
60 $db->exec('DELETE FROM pushes WHERE id = ' . intval($rowId));
61 header('HTTP/1.0 204 No Content');
62 ?>