-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·363 lines (359 loc) · 16.5 KB
/
index.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php namespace x\user__comment;
function route__comment($content, $path, $query, $hash) {
if ('GET' === $_SERVER['REQUEST_METHOD']) {
// Remove URL query string associated with this extension from the redirect link
\Hook::set('kick', function ($to) {
if (false === \strpos($to, '?')) {
return $to;
}
[$path, $query, $hash] = \preg_split('/[?#]/', $to);
$query = \To::query(\array_replace_recursive((array) \From::query($query), [
'name' => null,
'task' => null,
'token' => null
]));
return $path . $query . ("" !== $hash ? '#' . $hash : "");
});
$can_alert = \class_exists("\\Alert");
$error = 0;
$name = $_GET['name'] ?? null;
$task = $_GET['task'] ?? null;
$token = $_GET['token'] ?? null;
foreach (['name', 'task', 'token'] as $v) {
${$v} = $_GET[$v] ?? null;
if (empty(${$v})) {
$can_alert && \Alert::error('Missing %s query in URL.', [$v]);
++$error;
}
}
$folder = \LOT . \D . 'comment' . \strtr($path, '/', \D);
$file = $name ? \exist([
$folder . \D . $name . '.archive',
$folder . \D . $name . '.page'
], 1) : false;
if (!$file) {
$can_alert && \Alert::error('Comment does not exist.');
++$error;
}
if (!$token || !\check($token, 'comment')) {
$can_alert && \Alert::error('Invalid token.');
++$error;
}
// All passed the check(s)!
if (!$error) {
$has_comment_guard = null !== \State::get("x.comment\\.guard");
// Edit comment
if ('edit' === $task) {
$comment = new \Comment($file);
$data = \From::page(\file_get_contents($file));
// TODO: Add edit action
\kick($comment->url);
}
// Change comment status to `-1`
if ('hide' === $task) {
$comment = new \Comment($file);
$data = \From::page(\file_get_contents($file));
$data['status'] = -1;
if (false !== \file_put_contents($file, \To::page($data))) {
$can_alert && \Alert::success('Comment marked as spam.');
if ($has_comment_guard) {
if (isset($data['email']) && \is_file($rows = \LOT . \D . 'x' . \D . 'comment.guard' . \D . 'email.txt')) {
$exist = false;
$test = $data['email'];
foreach (\stream($rows) as $row) {
if ($test === \trim($row)) {
$exist = true;
break;
}
}
if (!$exist) {
$n = 0 === \filesize($rows) ? "" : "\n";
// Add email data to the black list
if (false !== \file_put_contents($rows, $n . $test, \FILE_APPEND | \LOCK_EX)) {
$can_alert && \Alert::info('Email address %s has been added to the black list.', ['<em>' . $test . '</em>']);
}
}
}
if (!empty($data['i-p']) && empty($data['ip'])) {
$data['ip'] = $data['i-p'];
}
if (isset($data['ip']) && \is_file($rows = \LOT . \D . 'x' . \D . 'comment.guard' . \D . 'i-p.txt')) {
$exist = false;
$test = $data['ip'];
foreach (\stream($rows) as $row) {
if ($test === \trim($row)) {
$exist = true;
break;
}
}
if (!$exist) {
$n = 0 === \filesize($rows) ? "" : "\n";
// Add IP data to the black list
if (false !== \file_put_contents($rows, $n . $test, \FILE_APPEND | \LOCK_EX)) {
$can_alert && \Alert::info('IP address %s has been added to the black list.', ['<em>' . $test . '</em>']);
}
}
}
if (isset($data['link']) && \is_file($rows = \LOT . \D . 'x' . \D . 'comment.guard' . \D . 'link.txt')) {
$exist = false;
$test = $data['link'];
foreach (\stream($rows) as $row) {
if ($test === \trim($row)) {
$exist = true;
break;
}
}
if (!$exist) {
$n = 0 === \filesize($rows) ? "" : "\n";
// Add link data to the black list
if (false !== \file_put_contents($rows, $n . $test, \FILE_APPEND | \LOCK_EX)) {
$can_alert && \Alert::info('Link address %s has been added to the black list.', ['<em>' . $test . '</em>']);
}
}
}
}
} else {
$can_alert && \Alert::error('Could not mark this comment as spam due to file system error.');
}
\kick($comment->url);
}
// Change comment status to `1` or `2`
if ('show' === $task) {
$comment = new \Comment($file);
$data = \From::page(\file_get_contents($file));
$data['status'] = \Is::user() === $comment['author'] ? 1 : 2;
if (false !== \file_put_contents($file, \To::page($data))) {
$can_alert && \Alert::success('Comment marked as not spam.');
if ($has_comment_guard) {
if (isset($data['email']) && \is_file($rows = \LOT . \D . 'x' . \D . 'comment.guard' . \D . 'email.txt')) {
if (\filesize($rows) > 0) {
$list = [];
$test = $data['email'];
foreach (\stream($rows) as $row) {
if ($test === ($row = \trim($row))) {
continue;
}
$list[] = $row;
}
if (false !== \file_put_contents($rows, \implode("\n", $list), \LOCK_EX)) {
$can_alert && \Alert::info('Email address %s has been removed from the black list.', ['<em>' . $test . '</em>']);
}
}
}
if (!empty($data['i-p']) && empty($data['ip'])) {
$data['ip'] = $data['i-p'];
}
if (isset($data['ip']) && \is_file($rows = \LOT . \D . 'x' . \D . 'comment.guard' . \D . 'i-p.txt')) {
if (\filesize($rows) > 0) {
$list = [];
$test = $data['ip'];
foreach (\stream($rows) as $row) {
if ($test === ($row = \trim($row))) {
continue;
}
$list[] = $row;
}
if (false !== \file_put_contents($rows, \implode("\n", $list), \LOCK_EX)) {
$can_alert && \Alert::info('IP address %s has been removed from the black list.', ['<em>' . $test . '</em>']);
}
}
}
if (isset($data['link']) && \is_file($rows = \LOT . \D . 'x' . \D . 'comment.guard' . \D . 'link.txt')) {
if (\filesize($rows) > 0) {
$list = [];
$test = $data['link'];
foreach (\stream($rows) as $row) {
if ($test === ($row = \trim($row))) {
continue;
}
$list[] = $row;
}
if (false !== \file_put_contents($rows, \implode("\n", $list), \LOCK_EX)) {
$can_alert && \Alert::info('Link address %s has been removed from the black list.', ['<em>' . $test . '</em>']);
}
}
}
}
} else {
$can_alert && \Alert::error('Could not mark this comment as not spam due to file system error.');
}
\kick($comment->url);
}
// Delete comment file permanently
if ('delete' === $task) {
if (\unlink($file)) {
foreach (\g(\dirname($file) . \D . \pathinfo($file, \PATHINFO_FILENAME), null, true) as $k => $v) {
if (1 === $v) {
\unlink($k);
} else {
\rmdir($k);
}
}
$can_alert && \Alert::success('Comment deleted.');
} else {
$can_alert && \Alert::error('Could not delete comment due to file system error.');
}
\kick($path . $query . '#comment');
}
// Change comment file extension to `.archive`
if ('remove' === $task) {
if (\rename($file, \dirname($file) . \D . \pathinfo($file, \PATHINFO_FILENAME) . '.archive')) {
$can_alert && \Alert::success('Comment removed.');
} else {
$can_alert && \Alert::error('Could not remove comment due to file system error.');
}
\kick($path . $query . '#comment');
}
$can_alert && \Alert::error('Invalid comment task.');
}
}
// Proceed to the default comment route from here. You should get an error message stating that method is not
// allowed. This is because the default comment route only accepts `POST` request.
return $content;
}
function y__comment($y) {
\extract(\lot(), \EXTR_SKIP);
$name = $this->name;
$route = \trim($state->x->comment->route ?? 'comment', '/');
$to = \strtr(\strtok($this->url, '?&#'), [
$url . '/' => $url . '/comment/'
]);
if (\Is::user(1)) {
if (isset($y[1]['footer'][1]['tasks'][1])) {
// TODO: Add `edit` task only to comment(s) that have no children
// $y[1]['footer'][1]['tasks'][1]['edit'] = [
// 0 => 'li',
// 1 => [
// 'link' => empty($this->comments->count()) ? [
// 0 => 'a',
// 1 => \i('Edit'),
// 2 => [
// 'href' => $to . $url->query([
// 'name' => $name,
// 'parent' => null,
// 'task' => 'edit',
// 'token' => \token('comment')
// ]),
// 'title' => \i('Edit this comment')
// ]
// ] : [
// 0 => 'span',
// 1 => \i('Edit'),
// 2 => [
// 'aria-disabled' => 'true'
// ]
// ]
// ]
// ];
$spam = -1 === $this->status;
$y[1]['footer'][1]['tasks'][1][$spam ? 'show' : 'hide'] = [
0 => 'li',
1 => [
'link' => [
0 => 'a',
1 => \i(($spam ? 'Not ' : "") . 'Spam'),
2 => [
'href' => $to . $url->query([
'name' => $name,
'parent' => null,
'task' => $spam ? 'show' : 'hide',
'token' => \token('comment')
]),
'title' => \i('Mark this comment as ' . ($spam ? 'not ' : "") . 'spam')
]
]
]
];
// Add `delete` and `remove` task(s) only to comment(s) that have no children
if (!empty($this->comments->count())) {
return $y;
}
$y[1]['footer'][1]['tasks'][1]['remove'] = [
0 => 'li',
1 => [
'link' => [
0 => 'a',
1 => \i('Remove'),
2 => [
'href' => $to . $url->query([
'name' => $name,
'parent' => null,
'task' => 'remove',
'token' => \token('comment')
]),
'title' => \i('Remove this comment')
]
]
]
];
$y[1]['footer'][1]['tasks'][1]['delete'] = [
0 => 'li',
1 => [
'link' => [
0 => 'a',
1 => \i('Delete'),
2 => [
'href' => $to . $url->query([
'name' => $name,
'parent' => null,
'task' => 'delete',
'token' => \token('comment')
]),
'title' => \i('Delete this comment permanently')
]
]
]
];
}
}
return $y;
}
function y__form__comment($y) {
\extract(\lot(), \EXTR_SKIP);
if (\Is::user()) {
unset($y[1]['author'], $y[1]['email'], $y[1]['link']);
$y[1] = [
'title' => [
0 => 'h4',
1 => \i('Comment as %s', ['<a href="' . $user->url . '" rel="nofollow" target="_blank">' . $user->author . '</a>'])
]
] + $y[1];
$y[1]['author'] = [
0 => 'input',
1 => false,
2 => [
'name' => 'comment[author]',
'type' => 'hidden',
'value' => $user->user
]
];
$y[1]['tasks'][1][2][1]['user'] = [
0 => 'a',
1 => $user->user,
2 => [
'href' => $url . '/' . \trim($state->x->user->guard->route ?? $state->x->user->route ?? 'user', '/') . '/' . $user->name . $url->query([
'exit' => $user->token,
'kick' => $url->path . $url->query . '#comment'
]),
'style' => 'padding: 0 0.5em;',
'title' => \i('Exit')
]
];
} else {
$y[1]['tasks'][1][2][1]['user'] = [
0 => 'a',
1 => \i('Log In'),
2 => [
'href' => $url . '/' . \trim($state->x->user->guard->route ?? $state->x->user->route ?? 'user', '/') . $url->query([
'kick' => $url->path . $url->query . '#comment'
]),
'style' => 'padding: 0 0.5em;',
'title' => \i('Enter')
]
];
}
return $y;
}
\Hook::set('route.comment', __NAMESPACE__ . "\\route__comment", 0);
\Hook::set('y.comment', __NAMESPACE__ . "\\y__comment", 10);
\Hook::set('y.form.comment', __NAMESPACE__ . "\\y__form__comment", 10);