-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
executable file
·149 lines (126 loc) · 6.48 KB
/
api.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
144
145
146
147
148
149
<?php
/* Adrien 'Adriweb' Bertrand
* v2.0 - Feb. 27th, 2015
* tiplanet.org
*/
/* @var $pdo PDO */
global $pdo;
include_once "/data/web/vhosts/tiplanet.org/ROOT/archivesv2/ArchiveManager.php";
include_once "functions.php";
/* @var $arcMan ArcMan_v1 */
$arcMan = ArchiveManager::create(AM_Mode::Read, AM_User::$DummyUserData, AM_Version::v1);
define('TIME_START', microtime());
$results = array("Status" => NULL, "Message" => NULL, "Results" => 0);
$output_type = "json";
if (@ishere($_REQUEST["output"])) {
$output_type = strtolower($_REQUEST["output"]);
}
if (@ishere($_REQUEST["key"])) {
$apiKey = $_REQUEST["key"];
$debug = false;
if (checkApiKEY($pdo, $apiKey)) {
$reqType = "none";
$resCount = 0;
if (@ishere($_REQUEST['req'])) {
$reqType = $_REQUEST['req'];
switch ($reqType)
{
case "arc":
case "info":
if (@ishere($_REQUEST['arcID']) || @ishere($_REQUEST['arcid']))
{
$arcID = @ishere($_REQUEST['arcID']) ? (int)$_REQUEST['arcID'] : (int)$_REQUEST['arcid'];
if ($arcID > 0)
{
$arcMan->select_archive($arcID);
$arc = $arcMan->get_archive_info();
if ($arc === null) {
output_status(31, "This archive does not exist !");
} else {
if ($arc->deleted !== false) {
output_status(32, "This archive has been deleted.");
} elseif ($arc->private === 1 && $arc->name === null) { // private and not allowed (user is not uploader, for instance)
output_status(32, "This archive is private.");
} else {
output_resultsNumber(1);
output_status(0, "Request successful (" . abs(round((microtime() - TIME_START), 4)) . " s.)");
output(AM_to_API($arc));
}
}
} else {
output_status(30, "Invalid archive id ('arcID') given !");
}
} else {
output_status(30, "No archive id ('arcID') given !");
}
break;
case "list":
$arcMan->useNonGenView(true); // speeds things up by looking at uploads only, not generations.
$archives = $arcMan->search([["private","=","0"], ["deleted","IS","NULL"]], ["id", "name", "categories"], ["hitsD"], 99999);
foreach ($archives as $arc) {
output(AM_to_API($arc, true));
}
output_resultsNumber(count($archives));
output_status(0, "Request successful (listing all public uploads with their categories). " . ($debug ? " (" . abs(round((microtime() - TIME_START), 4)) . " s.)" : ""));
break;
case "search":
if (@ishere($_REQUEST['name']) || @ishere($_REQUEST['platform']) || @ishere($_REQUEST['author']) || @ishere($_REQUEST['category'])) {
$arcMan->useNonGenView(true); // speeds things up by looking at uploads only, not generations.
$filters = [ ["private","=","0"], ["deleted","IS","NULL"] ];
if (@ishere($_REQUEST['platform'])) {
$platform = $_REQUEST['platform'];
array_push($filters, ["platform", "REGEXP", ".*{$platform}.*"]);
}
if (@ishere($_REQUEST['category'])) {
$category = $_REQUEST['category'];
array_push($filters, ["category", "REGEXP", ".*{$category}.*"]);
}
if (@ishere($_REQUEST['name'])) {
$name = $_REQUEST['name'];
array_push($filters, (strlen($name) <= 5) ? ["name", "=", $name] : ["name", "REGEXP", ".*{$name}.*"]);
}
if (@ishere($_REQUEST['author'])) {
$author = $_REQUEST['author'];
array_push($filters, (strlen($author) <= 5) ? ["author", "=", $author] : ["author", "REGEXP", ".*{$author}.*"]);
}
$archives = $arcMan->search($filters, ["id", "name", "categories"], ["hitsD"], 99999);
foreach ($archives as $arc) {
output(AM_to_API($arc, true));
}
output_resultsNumber(count($archives));
output_status(0, "Request successful" . ($debug ? " (" . abs(round((microtime() - TIME_START), 4)) . " s.)" : ""));
} else {
output_status(20, "At least 1 search filter ('name', 'author', 'category', 'platform') has to be given !");
}
break;
default:
output_status(11, "Unrecognized request type : '" . $reqType . "' !");
break;
}
} else {
output_status(10, "No request type given !");
}
logQueryMetadata($pdo, $apiKey, $reqType, $resCount);
} else {
output_status(2, "Invalid API key ! ");
}
} else {
output_status(1, "No API key given !");
}
if ($output_type == "xml") {
$xml = new SimpleXMLElement('<APIResponse/>');
array_to_xml($results, $xml);
finalOutput($xml->asXML(), 'Content-Type: application/xml; charset=utf-8');
} elseif ($output_type == "php") {
finalOutput(serialize($results), 'Content-Type: application/vnd.php.serialized; charset=utf-8');
} elseif ($output_type == "phpdebug") {
header('Content-Type: text/plain; charset=utf-8');
print_r($results);
} else {
if ($output_type != "json" && $output_type != "prettyjson")
$results = array("Alert" => "Unrecognized output type '" . $output_type . "' ; defaulting to json.") + $results;
finalOutput(json_encode($results, $output_type == "prettyjson" ? JSON_PRETTY_PRINT : 0), 'Content-Type: application/json; charset=utf-8');
}
error_reporting(0);
ini_set('display_errors', '0');
?>