prepare("UPDATE posts SET views = views + 1 WHERE id = ?"); $s->bind_param("s", $id); $s->execute(); $d = $s->get_result()->fetch_assoc(); if (!(bool)$d) { require_once("../_json.php"); ReturnJSONError($Err_Int_Unexpected, "failed to increment number of views"); } } // Get single publication by ID function Post_GetByID ($id) { global $db; $result = array(); $s = $db->prepare("SELECT * FROM posts WHERE id = ?"); $s->bind_param("s", $id); $s->execute(); $d = $s->get_result()->fetch_assoc(); if (!(bool)$d) { return null; } $result["id"] = $d["id"]; $result["author_id"] = $d["author_id"]; if ($d["comments_enabled"]) $result["comment_section_id"] = $d["comment_section_id"]; $result["created_at"] = $d["created_at"]; $result["tags"] = $d["tags"]; $result["title"] = $d["title"]; $result["votes_up"] = $d["votes_up"]; $result["votes_down"] = $d["votes_down"]; $result["views"] = $d["views"] + 1; $result["pic_path"] = $d["pic_path"]; $result["preview_path"] = $d["preview_path"]; $result["edit_lock"] = $d["edit_lock"]; Post_AddView($id); // TODO: add rate-limit or completely rework return $result; } if (ThisFileIsRequested(__FILE__)) { require_once("../_json.php"); if (isset($_REQUEST["id"])) { if (!ctype_digit($_REQUEST["id"])) ReturnJSONError($Err_RDP_InvalidID, "id must be numeric"); $UserID = intval($_REQUEST["id"]); } else { ReturnJSONError($Err_RDP_InvalidID, "id must be specified"); } $ResponseData = Post_GetByID($_REQUEST["id"]); if ($ResponseData) ReturnJSONData($ResponseData); else ReturnJSONError($Err_DP_IDNotFound, "wrong id"); } ?>