aboutsummaryrefslogtreecommitdiff
path: root/categories/save.php
blob: 8b1ce1ea3fb811138a197cf984aa232362ade733 (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/session.php"; global $userName; global $id;
while (trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/saved.json")) === "") {}

$isSaved = false;

$saved = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/saved.json"), true);
$id = substr(bin2hex(random_bytes(32)), 0, 16);

if ($_GET["category"] === "new") $_GET["category"] = $id;
if (!isset($saved[$userName][$_GET["category"]])) $saved[$userName][$_GET["category"]] = [
    "name" => "New category " . $_GET["category"],
    "items" => []
];

if (!in_array($_GET["id"], $saved[$userName][$_GET["category"]]["items"])) {
    $saved[$userName][$_GET["category"]]["items"][] = $_GET["id"];
}

if (isset($saved[$userName][$_GET["category"]]["owner"]) && $saved[$userName][$_GET["category"]]["owner"] !== $id) {
    die();
}

file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/saved.json", json_encode($saved, JSON_PRETTY_PRINT));

while (trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/saved.json")) === "") {
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/saved.json", json_encode($saved, JSON_PRETTY_PRINT));
}

die("ok");