-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.php
143 lines (128 loc) · 3.87 KB
/
endpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
$db=new PDO("sqlite:backing.db3");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$db->query("PRAGMA foreign_keys = ON");
$http_raw=file_get_contents("php://input");
$http_dec=json_decode($http_raw, true);
$retVal=array("status"=>"ok");
$endpoints=array(
"catfor",
"torrents",
"categories",
"category-add",
"category-del",
"torrent-del",
"torrent-rename",
"torrent-add"
);
foreach($endpoints as $endpoint){
if(isset($_GET[$endpoint])){
handleEndpoint($endpoint);
break;
}
}
function handleEndpoint($endpoint){
global $retVal;
global $http_dec;
global $db;
switch($endpoint){
case "torrent-add":
if (!isset($http_dec["hash"]) && empty($http_dec["hash"])) {
break;
}
if (!isset($http_dec["name"]) && empty($http_dec["name"])) {
break;
}
if (!isset($http_dec["size"]) && empty($http_dec["size"])) {
break;
}
$stmt=$db->prepare("INSERT INTO torrents(hash, name, size) VALUES(:hash, :name, :size)");
$done = $stmt->execute(array(
":hash" => $http_dec["hash"],
":name" => $http_dec["name"],
":size" => $http_dec["size"]
));
if ($done) {
$retVal["torrent-add"] = $db->lastInsertId();
} else {
$retVal["torrent-add"] = -1;
}
break;
case "torrents":
if(isset($http_dec["category"])&&!empty($http_dec["category"])){
if($http_dec["category"]=="new"){
//fetch new torrents
$stmt=$db->query("SELECT * FROM newtorrents");
$retVal["torrents"]=$stmt->fetchAll(PDO::FETCH_ASSOC);
}
else{
//fetch category
$stmt=$db->prepare("SELECT * FROM torrentcategories WHERE category=:cat");
$stmt->execute(array(":cat"=>$http_dec["category"]));
$retVal["torrents"]=$stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
else{
//fetch all
$stmt=$db->query("SELECT * FROM torrents JOIN peerstats ON peerstats.torrent=torrents.id");
$retVal["torrents"]=$stmt->fetchAll(PDO::FETCH_ASSOC);
}
break;
case "categories":
if(isset($http_dec["torrent"])&&!empty($http_dec["torrent"])){
$stmt=$db->prepare("SELECT * FROM torrentcategories WHERE id=:torrent");
$stmt->execute(array(":torrent"=>$http_dec["torrent"]));
$retVal["categories"]=$stmt->fetchAll(PDO::FETCH_ASSOC);
}
else{
$stmt=$db->query("SELECT * FROM categories");
$retVal["categories"]=$stmt->fetchAll(PDO::FETCH_ASSOC);
}
break;
case "category-add":
if(!isset($http_dec["torrent"])||empty($http_dec["torrent"])){
break;
}
if(!isset($http_dec["category"])||empty($http_dec["category"])){
break;
}
$stmt=$db->prepare("INSERT INTO categorymap (torrent, category) VALUES (:torrent, :cat)");
$stmt->execute(array(":torrent"=>$http_dec["torrent"], ":cat"=>$http_dec["category"]));
break;
case "category-del":
if(!isset($http_dec["torrent"])||empty($http_dec["torrent"])){
break;
}
if(!isset($http_dec["category"])||empty($http_dec["category"])){
break;
}
$stmt=$db->prepare("DELETE FROM categorymap WHERE torrent=:torrent AND category=:cat");
$stmt->execute(array(":torrent"=>$http_dec["torrent"], ":cat"=>$http_dec["category"]));
break;
case "torrent-del":
if(!isset($http_dec["torrent"])||empty($http_dec["torrent"])){
break;
}
$stmt=$db->prepare("DELETE FROM torrents WHERE id=:torrent");
$stmt->execute(array(":torrent"=>$http_dec["torrent"]));
break;
case "torrent-rename":
if(!isset($http_dec["torrent"])||empty($http_dec["torrent"])){
break;
}
if(!isset($http_dec["name"])||empty($http_dec["name"])){
break;
}
$stmt=$db->prepare("UPDATE torrents SET name=:name WHERE id=:torrent");
$stmt->execute(array(":torrent"=>$http_dec["torrent"], ":name"=>$http_dec["name"]));
break;
default:
$retVal["status"][2]="Unknown endpoint";
break;
}
}
$retVal["status"]=$db->errorInfo();
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
print(json_encode($retVal));
?>