-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
executable file
·203 lines (168 loc) · 5.42 KB
/
update.php
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php session_start();
include "core.php";
include "config.php";
// fields
$Date = "";
$Endpoint = "";
$File = "";
$id = -1;
$noHist = false;
$Request = "false";
$Redirect = "";
$AuthorizedCreation = 0;
if (isset($_REQUEST['redir'])) {
$Redirect = htmlspecialchars($_REQUEST['redir']);
}
if (isset($_REQUEST['request'])) {
$Request = htmlspecialchars($_REQUEST['request']);
}
if (isset($_REQUEST['id'])) {
$id = htmlspecialchars($_REQUEST['id']);
} else {
if ($Redirect == "admin") {
header("Location: admin.php?e=endpoint");
} else if ($Redirect == "edit") {
header("Location: edit.php?e=endpoint&action=articles");
} else {
header("Location: /");
}
die();
}
$Database = createTables($sqlDB);
$DatabaseQuery = $Database->query('SELECT * FROM users');
if (!isset($_SESSION['username']) || !isset($_SESSION['password']) || !isset($_SESSION['type'])) {
header('Location: login.php?redir=admin');
die();
} else if (htmlspecialchars($_SESSION['type']) != 2) { // not allowed
header('Location: /');
die();
}
$DatabaseQuery = $Database->query('SELECT * FROM users');
while ($line = $DatabaseQuery->fetchArray()) {
if ($line['username'] == htmlspecialchars($_SESSION['username']) && htmlspecialchars($_SESSION['username']) != "" && $line['password'] == htmlspecialchars($_SESSION['password']) && $line['usertype'] == 2) {
$AuthorizedCreation = 1;
break;
}
}
$Username = htmlspecialchars($_SESSION['username']);
// not authorized
if ($AuthorizedCreation != 1) {
header('Location: /');
die();
}
// date must be specified
if (isset($_REQUEST['date']) && (htmlspecialchars($_REQUEST['date']) != "")) {
$Date = htmlspecialchars($_REQUEST['date']);
} else {
$Date = date($dateFormat);
}
// endpoint must be specified
if (isset($_REQUEST['endpoint']) && htmlspecialchars($_REQUEST['endpoint']) != "") {
$Endpoint = htmlspecialchars($_REQUEST['endpoint']);
} else {
if ($Redirect == "admin") {
header("Location: admin.php?e=endpoint");
} else if ($Redirect == "edit") {
header("Location: edit.php?e=endpoint&action=write");
} else {
header("Location: /");
}
die();
}
$DatabaseQuery = $Database->query('SELECT * FROM pages');
while ($line = $DatabaseQuery->fetchArray()) {
if ($line['id'] == $id && $id != -1) {
$File = $line['file'];
}
if ($line['endpoint'] == $Endpoint && $line['id'] != $id) {
if ($Redirect == "admin") {
header("Location: admin.php?e=exists");
} else if ($Redirect == "edit") {
header("Location: edit.php?e=exists&action=write");
} else {
header("Location: /");
}
die();
}
}
$OldFile = "";
if (isset($_REQUEST['body']) && htmlspecialchars($_REQUEST['body']) != "") {
$Body = htmlspecialchars($_REQUEST['body']);
if ($File == '') {
print("File parameter cannot be empty. $id");
die();
}
// back up the old file first
$OldFileContents = file_get_contents($File);
$Hash = hash('sha256', $OldFileContents);
$OldFile = "$historyLocation/$Hash/$Hash.md";
if (file_exists($OldFile)) {
$noHist = true;
} else {
if (!is_dir("$historyLocation/$Hash")) {
mkdir("$historyLocation/$Hash", 0777, true);
}
if (!file_put_contents($OldFile, $OldFileContents)) {
if ($Redirect == "admin") {
header("Location: admin.php?e=ofile");
} else if ($Redirect == "edit") {
header("Location: edit.php?e=ofile&action=articles");
} else {
header("Location: /");
}
die();
}
}
// now write to the new file
if (!file_put_contents($File, $Body)) {
if ($Redirect == "admin") {
header("Location: admin.php?e=file");
} else if ($Redirect == "edit") {
header("Location: edit.php?e=file&action=articles");
} else {
header("Location: /");
}
die();
}
// also delete requests
if ($Request == "true") {
$ModDatabaseQuery = $Database->query('SELECT * FROM requests');
while ($mline = $ModDatabaseQuery->fetchArray()) {
if ($mline['pageid'] == $id && $id != -1) {
$File = $mline['file'];
$Directory = dirname($File);
if (is_file($File)) {
unlink($File);
if (is_dir($Directory)) {
rmdir($Directory);
}
}
$Database->exec("DELETE FROM requests WHERE pageid='$id'");
break;
}
}
}
} else {
if ($Redirect == "admin") {
header("Location: admin.php?e=file");
} else if ($Redirect == "edit") {
header("Location: edit.php?e=file&action=articles");
} else {
header("Location: /");
}
die();
}
$Database->exec("UPDATE pages SET date='$Date' WHERE id='$id'");
$Database->exec("UPDATE pages SET endpoint='$Endpoint' WHERE id='$id'");
if ($noHist == false) {
$Database->exec("INSERT INTO history(username, pageid, date, endpoint, file) VALUES('$Username', '$id', '$Date', '$Endpoint', '$OldFile')");
}
if ($Redirect == "admin") {
header("Location: admin.php?action=users");
} else if ($Redirect == "edit") {
header("Location: edit.php?action=write&id=$id&e=saved");
} else {
header("Location: /");
}
die();
?>