-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.php
98 lines (81 loc) · 4.08 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
<?php
namespace mauricerenck\Komments;
use mauricerenck\Komments\KommentModeration;
use mauricerenck\Komments\KommentReceiver;
use Kirby;
use Kirby\Http\Response;
use Kirby\Toolkit\I18n;
@include_once __DIR__ . '/vendor/autoload.php';
Kirby::plugin('mauricerenck/komments', [
'areas' => require_once(__DIR__ . '/components/areas.php'),
'options' => require_once(__DIR__ . '/config/options.php'),
'snippets' => require_once(__DIR__ . '/components/snippets.php'),
'templates' => [
'emails/newcomments' => __DIR__ . '/templates/emails/newComments.php'
],
'blueprints' => [
'sections/komments' => __DIR__ . '/blueprints/sections/komments.yml'
],
'pageMethods' => require_once(__DIR__ . '/components/page-methods.php'),
'siteMethods' => require_once(__DIR__ . '/components/site-methods.php'),
'fields' => require_once(__DIR__ . '/components/fields.php'),
'translations' => require_once(__DIR__ . '/config/translations.php'),
'api' => require_once(__DIR__ . '/components/api.php'),
'hooks' => require_once(__DIR__ . '/components/hooks.php'),
'routes' => [
[
'pattern' => 'komments/send',
'method' => 'POST',
'action' => function () {
$headers = kirby()->request()->headers();
$formData = kirby()->request()->data();
$shouldReturnJson = ($headers['X-Return-Type'] === 'json');
$kommentReceiver = new KommentReceiver();
$kommentModeration = new KommentModeration();
$targetPage = $kommentReceiver->getPageFromUrl($formData['wmTarget']);
$spamlevel = 0;
if (is_null($targetPage)) {
return $kommentReceiver->sendReponseToClient('mauricerenck.komments.error', 'mauricerenck.komments.pagenotfound', 404, $shouldReturnJson);
}
if ($kommentReceiver->isSpam($formData)) {
if (option('mauricerenck.komments.auto-delete-spam') === true) {
return $kommentReceiver->sendReponseToClient('mauricerenck.komments.error', 'mauricerenck.komments.lookslikespam', 403, $shouldReturnJson);
} else {
$spamlevel = 100;
}
}
$webmention = $kommentReceiver->convertToWebmention($formData, $targetPage);
$isVerified = $kommentReceiver->isVerified($formData['email']);
$autoPublish = $kommentReceiver->autoPublish($formData['email']);
if (!$kommentReceiver->requiredFieldsAreValid($webmention)) {
return $kommentReceiver->sendReponseToClient('mauricerenck.komments.error', 'mauricerenck.komments.invalidfieldvalues', 412, $shouldReturnJson);
}
$newEntry = $kommentReceiver->createKomment($webmention, $spamlevel, $isVerified, $autoPublish);
$kommentReceiver->storeData($newEntry, $targetPage);
kirby()->trigger('komments.comment.received', []);
if ($shouldReturnJson) {
$response = [
'status' => 'success',
'pending' => true,
'message' => I18n::translate('mauricerenck.komments.thankyou', null , kirby()->languageCode()),
'data' => $webmention
];
return new Response(json_encode($response), 'application/json');
}
go($targetPage . '#inModeration');
}
],
[
'pattern' => 'komments/cron/notification/(:any)',
'method' => 'GET',
'action' => function ($secret) {
if (option('mauricerenck.komments.notifications.cronSecret', '') === $secret) {
$notifications = new KommentNotificationUtils();
$notifications->sendNotifications();
return new Response('sent', 'text/plain');
}
return new Response('Forbidden', 'text/plain', 401);
}
],
]
]);