e949/api/comments/index.php

140 lines
3.4 KiB
PHP

<?php
// Get all comments from comment section by ID and base methods for managing comment sections
// Includes
if ($IS_FRONTEND) {
require_once("api/_auth.php");
require_once("api/_utils.php");
require_once("api/_errorslist.php");
require_once("api/_types.php");
require_once("api/user/index.php");
} else {
require_once("../_auth.php");
require_once("../_utils.php");
require_once("../_errorslist.php");
require_once("../_types.php");
require_once("../user/index.php");
}
// Functions
/*
* FUNCTION
* Get comments from range of selected comment section
*/
function Comments_GetSectionRange (int $sec_id, int $ts_from = 0, int $ts_to = 0xffffffff): ReturnT {
global $db, $LOGGED_IN, $THIS_USER;
$result = array();
$s = $db->prepare("SELECT * FROM comments WHERE comment_section_id=? AND created_at>=? AND created_at<=? ORDER BY created_at");
$s->bind_param("sss", $sec_id, date("Y-m-d H:i:s", $ts_from), date("Y-m-d H:i:s", $ts_to));
$s->execute();
$d = $s->get_result();
if (!(bool)$d)
return new ReturnT(data: $result);
$isAdmin = false;
if ($LOGGED_IN && User_HasRole($THIS_USER, "admin")->GetData())
$isAdmin = true;
while ($row = $d->fetch_array()) {
if (!$isAdmin && $row["needs_check"])
continue;
$newResultRow = array(
"id" => $row["id"],
"author_id" => $row["author_id"],
"created_at" => $row["created_at"],
"contents" => $row["contents"]
);
if ($isAdmin)
$newResultRow["needs_check"] = (bool)$row["needs_check"];
$result[] = $newResultRow;
}
return new ReturnT(data: $result);
}
// Methods
/*
* METHOD
* Get comments from range of selected comment section
*/
function Comments_GetSectionRange_Method (array $req): ReturnT {
// Input sanity checks
$SectionID = null;
$TSFrom = 0;
$TSTo = 0xffffffff;
if (isset($req["id"])) {
if (!ctype_digit($req["id"]))
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "id must be numeric");
$SectionID = intval($req["id"]);
} else {
return new ReturnT(err_code: E_UIN_INSUFARGS, err_desc: "id must be specified");
}
if (isset($req["ts_from"])) {
$TSFrom = $req["ts_from"];
if (strlen($TSFrom) > 24)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be this long");
if (!ctype_digit($TSFrom))
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp must be numeric");
$TSFrom = intval($TSFrom);
if ($TSFrom > 0xffffffff)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be bigger than INT32_MAX");
}
if (isset($req["ts_to"])) {
$TSTo = $req["ts_to"];
if (strlen($TSTo) > 24)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be this long");
if (!ctype_digit($TSTo))
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp must be numeric");
$TSTo = intval($TSTo);
if ($TSTo > 0xffffffff)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be bigger than INT32_MAX");
}
if ($TSTo < $TSFrom)
return new ReturnT(err_code: E_UIN_BADARGS, err_desc: "start timestamp cant be bigger than end timestamp");
// Actions
return Comments_GetSectionRange($SectionID, $TSFrom, $TSTo);
}
if (Utils_ThisFileIsRequested(__FILE__)) {
require_once("../_json.php");
$result = Comments_GetSectionRange_Method($_REQUEST);
if ($result->IsError())
$result->ThrowJSONError();
else
JSON_ReturnData($result->GetData());
}
?>