public function query($postdata)
{
if ($this->user->getClass() < User::CLASS_ADMIN) {
throw new Exception(L::get("PERMISSION_DENIED"), 401);
}
$limit = (int) $postdata["limit"] ?: 25;
$index = (int) $postdata["index"] ?: 0;
$sth = $this->db->query("SELECT COUNT(*) FROM reports");
$res = $sth->fetch();
$totalCount = $res[0];
$sth = $this->db->prepare("SELECT reports.added AS added2, reports.targetid, reports.type, reports.id AS reportid, reports.reason, reports.handledBy, " . implode(',', User::getDefaultFields()) . " FROM reports LEFT JOIN users ON reports.userid = users.id ORDER BY reports.id DESC LIMIT ?, ?");
$sth->bindParam(1, $index, PDO::PARAM_INT);
$sth->bindParam(2, $limit, PDO::PARAM_INT);
$sth->execute();
$result = array();
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$r = array();
$r["id"] = $row["reportid"];
$r["added"] = $row["added2"];
$r["type"] = $row["type"];
$r["reason"] = $row["reason"];
$r["handledBy"] = $row["handledBy"] == 0 ? null : $this->user->get($row["handledBy"]);
$r["user"] = $this->user->generateUserObject($row);
switch ($row["type"]) {
case 'torrent':
try {
$r["torrent"] = $this->torrent->get($row["targetid"], true);
if ($r["torrent"]["imdbid"]) {
$r["relatedTorrents"] = $this->torrent->getRelated($r["torrent"]["imdbid"], $r["torrent"]["id"]);
} else {
$r["relatedTorrents"] = [];
}
} catch (Exception $e) {
$r["torrent"] = null;
$r["deleted"] = true;
}
break;
case 'post':
try {
$r["post"] = $this->forum->getPost($row["targetid"]);
$topic = $this->forum->getTopic($r["post"]["topicid"]);
$r["post"]["forumid"] = $topic["forumid"];
try {
$r["post"]["user"] = $this->user->get($r["post"]["userid"]);
} catch (Exception $e) {
$r["post"]["user"] = null;
}
} catch (Exception $e) {
$r["post"] = null;
$r["deleted"] = true;
}
break;
case 'pm':
try {
$r["pm"] = $this->mailbox->get($row["targetid"]);
try {
$r["pm"]["user"] = $this->user->get($r["pm"]["sender"]);
} catch (Exception $e) {
$r["pm"]["user"] = null;
}
} catch (Exception $e) {
$r["pm"] = null;
$r["deleted"] = true;
}
break;
case 'request':
try {
$r["request"] = $this->requests->get($row["targetid"]);
} catch (Exception $e) {
$r["request"] = null;
$r["deleted"] = true;
}
break;
case 'comment':
try {
$r["comment"] = $this->comments->get($row["targetid"]);
try {
$r["comment"]["user"] = $this->user->get($r["comment"]["user"]);
} catch (Exception $e) {
$r["comment"]["user"] = null;
}
} catch (Exception $e) {
$r["comment"] = null;
$r["deleted"] = true;
}
break;
case 'subtitle':
try {
$r["subtitle"] = $this->subtitles->get($row["targetid"]);
try {
$r["subtitle"]["user"] = $this->user->get($r["subtitle"]["userid"]);
} catch (Exception $e) {
$r["subtitle"]["user"] = null;
}
} catch (Exception $e) {
$r["subtitle"] = null;
$r["deleted"] = true;
}
break;
case 'user':
try {
$r["reportedUser"] = $this->user->get($row["targetid"]);
} catch (Exception $e) {
$r["reportedUser"] = null;
$r["deleted"] = true;
}
break;
}
array_push($result, $r);
}
return array($result, $totalCount);
}
httpResponse($result, $totalCount); break; case validateRoute('POST', 'reports'): $reports = new Reports($db, $user); httpResponse($reports->create($postdata)); break; case validateRoute('GET', 'reports'): $mailbox = new Mailbox($db, $user); $torrent = new Torrent($db, $user); $subtitles = new Subtitles($db, $user); $requests = new Requests($db, $user); $forum = new Forum($db, $user); $log = new Logs($db); $comments = new Comments($db, $user); $reports = new Reports($db, $user, $torrent, $subtitles, $requests, $forum, $mailbox, $comments, $log); list($result, $totalCount) = $reports->query(array("limit" => $_GET["limit"], "index" => $_GET["index"])); httpResponse($result, $totalCount); break; case validateRoute('PATCH', 'reports/\\d+'): $reports = new Reports($db, $user); httpResponse($reports->update((int) $params[1], $postdata)); break; case validateRoute('DELETE', 'reports/\\d+'): $reports = new Reports($db, $user); httpResponse($reports->delete((int) $params[1])); break; case validateRoute('GET', 'adminlogs'): $adminlogs = new AdminLogs($db, $user); list($result, $totalCount) = $adminlogs->query(array("limit" => $_GET["limit"], "index" => $_GET["index"], "search" => $_GET["searchText"])); httpResponse($result, $totalCount); break;