summaryrefslogtreecommitdiff
path: root/pages/api/docs.php
blob: a60a6e685d5f84093588e4363cfa8a6cd447a006 (plain)
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
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/session.inc"; global $isLoggedIn; global $_PROFILE;
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/functions.inc";
if (!$isLoggedIn) header("Location: /-/login") and die();

$request_raw = file_get_contents('php://input');
$json_object = json_decode($request_raw, true);

$select = $_GET['id'] ?? null;

if (isset($select)) {
    if (ctype_alnum($select) && file_exists($_SERVER['DOCUMENT_ROOT'] . "/includes/data/docs/" . $select . ".json")) {
        $id = $select;
        $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/docs/" . $select . ".json"), true);
    } else {
        die("Not found");
    }
} else {
    die("Not set");
}

if ($json_object["content"])  $data["contents"] = $json_object["content"];
if ($json_object["name"])     $data["name"]     = $json_object["name"];
if ($json_object["explicit"]) $data["nsfw"]     = $json_object["explicit"];
if ($json_object["category"]) $data["category"] = trim($json_object["category"]) !== "" && trim($json_object["category"]) !== "Unsorted" && trim($json_object["category"]) !== "unsorted" && trim($json_object["category"]) !== "/no" && trim($json_object["category"]) !== "Unsort" && trim($json_object["category"]) !== "unsort" ? $json_object["category"] : null;

$data["last"]["date"] = time();
$data["last"]["author"] = $_PROFILE['login'];

file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/docs/" . $select . ".json", utf8_encode(json_encode($data)));