Fix queue deletion for The_legend_of_ice_soul
[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 $game = $_GET['game'];
24 $cleanGame = preg_replace('#[^a-zA-Z0-9._]#', '', $game);
25 if ($game != $cleanGame || $game == '') {
26     header('HTTP/1.0 400 Bad Request');
27     header('Content-type: text/plain');
28     echo 'Invalid game' . "\n";
29     exit(1);
30 }
31
32 try {
33     $db = new SQLite3($dbFile, SQLITE3_OPEN_READWRITE);
34 } catch (Exception $e) {
35     //db file not found
36     header('X-Fail-Reason: database file not found');
37     header('HTTP/1.0 204 No Content');
38     exit(1);
39 }
40
41 $rowId = $db->querySingle(
42     'SELECT id FROM pushes'
43     . ' WHERE ip = \'' . SQLite3::escapeString($ip) . '\''
44     . ' AND game =\'' . SQLite3::escapeString($game) . '\''
45 );
46 if ($rowId === null) {
47     header('HTTP/1.0 404 Not Found');
48     header('Content-type: text/plain');
49     echo 'Game not queued' . "\n";
50     exit(1);
51 }
52
53 $db->exec('DELETE FROM pushes WHERE id = ' . intval($rowId));
54 header('HTTP/1.0 204 No Content');
55 ?>