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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/session.inc"; global $isLoggedIn; global $isLowerLoggedIn;
if (!$isLoggedIn || $isLowerLoggedIn) {
header("Location: /-/login");
die();
}
$system = $_GET['s'] ?? null;
$member = $_GET['m'] ?? null;
$index = (int)$_GET['i'] ?? null;
$type = $_GET['t'] ?? null;
$date = $_GET['d'] ?? null;
if (!isset($system) || trim($system) === "" || strlen($system) !== 5 || !preg_match("/[a-z]/i", $system) || ($system !== "gdapd" && $system !== "ynmuc"))
peh_error("System not found", 404);
if (!isset($type) || trim($type) === "")
peh_error("Type not found", 404);
if (!isset($date) || trim($date) === "" || strlen($date) !== 10 || !preg_match("/[\d-]/i", $date))
peh_error("Date not found", 404);
$list = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/planner/$system.json"), true);
function moveElement(&$array, $a, $b) {
$out = array_splice($array, $a, 1);
array_splice($array, $b, 0, $out);
}
switch ($type) {
case "add":
if (!isset($member) || trim($member) === "" || strlen($member) !== 5 || !preg_match("/[a-z]/i", $member))
if ($member !== null && $member !== "null") peh_error("System member not found", 404);
if (!isset($list[$date])) $list[$date] = [];
$list[$date][] = [$member, null];
break;
case "cofront":
if (!isset($index) || trim($index) === "" || is_integer($index))
if ($index !== null && $index !== "null") peh_error("Invalid index", 400);
$day = $list[$date];
if (!isset($day[$index]))
if ($index !== null && $index !== "null") peh_error("Index not found", 404);
if (!isset($member) || trim($member) === "" || strlen($member) !== 5 || !preg_match("/[a-z]/i", $member))
if ($member !== null && $member !== "null") peh_error("System member not found", 404);
$list[$date][$index][1] = $member;
break;
case "delete":
if (!isset($index) || trim($index) === "" || is_integer($index))
if ($index !== null && $index !== "null") peh_error("Invalid index", 400);
$day = $list[$date];
if (!isset($day[$index]))
if ($index !== null && $index !== "null") peh_error("Index not found", 404);
unset($day[$index]);
$list[$date] = array_values($day);
break;
case "codelete":
if (!isset($index) || trim($index) === "" || is_integer($index))
if ($index !== null && $index !== "null") peh_error("Invalid index", 400);
$day = $list[$date];
if (!isset($day[$index]))
if ($index !== null && $index !== "null") peh_error("Index not found", 404);
$list[$date][$index][1] = null;
break;
case "down":
if (!isset($index) || trim($index) === "" || is_integer($index))
if ($index !== null && $index !== "null") peh_error("Invalid index", 400);
if (!isset($day[$index]))
if ($index !== null && $index !== "null") peh_error("Index not found", 404);
$day = $list[$date];
moveElement($list[$date], $index, $index + 1 < count($list[$date]) ? $index + 1 : $index);
break;
case "up":
if (!isset($index) || trim($index) === "" || is_integer($index))
if ($index !== null && $index !== "null") peh_error("Invalid index", 400);
if (!isset($day[$index]))
if ($index !== null && $index !== "null") peh_error("Index not found", 404);
$day = $list[$date];
moveElement($list[$date], $index, $index - 1 > -1 ? $index - 1 : $index);
break;
default:
peh_error("Invalid type name", 400);
break;
}
file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/planner/$system.json", json_encode($list));
die();
|