summaryrefslogtreecommitdiff
path: root/pages/api/rename.php
blob: 4e01b060419c8c4dc6b51c016aa9dfbdbf77231d (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
32
33
34
35
36
37
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/functions.inc";
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/util/session.inc"; global $isLoggedIn; global $isLowerLoggedIn;

header("Content-Type: application/json");

if (!$isLoggedIn || $isLowerLoggedIn) {
    header("Location: /-/login");
    die();
}

$obj = [
    "success" => true
];

global $token;
$data = json_decode(pf_utf8_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/sessions/" . str_replace("/", "", $token))), true);

$data["name"] = $_POST["name"] ?? $_GET["name"] ?? $data["name"];
$obj["pre_name"] = $data["name"];

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

if (json_last_error() === JSON_ERROR_NONE) {
    $obj["json_error"] = [ json_last_error(), json_last_error_msg() ];
    if (isset($json_object["name"])) {
        $data["name"] = $json_object["name"];
        $obj["json_name"] = $json_object["name"];
    }
}

$obj["new_name"] = $data["name"];

file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/sessions/" . str_replace("/", "", $token), pf_utf8_encode(json_encode($data)));
die(json_encode($obj));