-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
177 lines (152 loc) · 5.61 KB
/
action.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
<?php
/**
* DokuWiki Plugin Discord Notifier (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
*/
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
use dokuwiki\HTTP\DokuHTTPClient;
use dokuwiki\Logger;
use dokuwiki\plugin\fsdiscordnotifier\event\PageMoveEvent;
use dokuwiki\plugin\fsdiscordnotifier\event\PageSaveEvent;
use dokuwiki\plugin\fsdiscordnotifier\helper\Config;
use dokuwiki\plugin\fsdiscordnotifier\helper\Context;
use dokuwiki\plugin\fsdiscordnotifier\helper\Formatter;
class action_plugin_fsdiscordnotifier extends ActionPlugin
{
/** @var Event[] */
private $changes = [];
/** @var PageMoveEvent[] */
private $created = [];
/** @var PageMoveEvent[] */
private $deleted = [];
private $inRename = false;
/** @var Config */
private $config;
public function __construct() {
$this->config = new Config($this);
}
public function register(EventHandler $controller): void
{
if (!$this->config->webhook) {
return;
}
$controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handleSave');
$controller->register_hook('PLUGIN_MOVE_PAGE_RENAME', 'BEFORE', $this, 'handleRenameBefore', 'BEFORE');
$controller->register_hook('PLUGIN_MOVE_PAGE_RENAME', 'AFTER', $this, 'handleRenameAfter', 'AFTER');
}
public function handleRenameBefore(Event $rawEvent): void
{
$this->inRename = true;
$event = new PageMoveEvent($rawEvent);
$this->created[$event->dst_id] = $event;
$this->deleted[$event->src_id] = $event;
}
public function handleRenameAfter(): void
{
if (!$this->inRename) {
// Sanity check
throw new RuntimeException('Logic error: in rename is false after rename');
}
$this->inRename = false;
foreach ($this->getEvents() as $event) {
$this->processEvent($event);
}
}
public function handleSave(Event $event): void
{
if ($this->inRename) {
$this->changes[] = $event;
return;
}
$this->processEvent(new PageSaveEvent($event));
}
/**
* @return PageSaveEvent[]
*/
private function getEvents(): array
{
$events = [];
foreach ($this->changes as $rawEvent) {
$event = new PageSaveEvent($rawEvent);
$pageId = $event->id;
if ($event->isCreate() && isset($this->created[$pageId])) {
$moveEvent = $this->created[$pageId];
$moveEvent->setCreatedPageEvent($event);
unset($this->created[$pageId]);
} elseif ($event->isDelete() && isset($this->deleted[$pageId])) {
$moveEvent = $this->deleted[$pageId];
$createdEvent = $moveEvent->getCreatedPageEvent();
$createdEvent->convertToRename($event);
unset($this->deleted[$pageId]);
// Skip delete event itself
continue;
}
$events[] = $event;
}
$this->changes = [];
return $events;
}
private function processEvent(PageSaveEvent $event): void
{
if (!$this->config->isValidNamespace($event->getNamespace())) {
return;
}
if (!$this->isValidEvent($event->getEventType())) {
return;
}
$formatter = new Formatter($this->config);
$formatted = $formatter->format($event, new Context());
$this->submitPayload($this->config->webhook, $formatted);
}
private function isValidEvent(?string $eventType): bool
{
if ($eventType === 'create' && $this->config->notify_create) {
return true;
} elseif ($eventType === 'edit' && $this->config->notify_edit) {
return true;
} elseif ($eventType === 'edit minor' && $this->config->notify_edit && $this->config->notify_edit_minor) {
return true;
} elseif ($eventType === 'delete' && $this->config->notify_delete) {
return true;
} elseif ($eventType === 'rename' && $this->config->notify_create && $this->config->notify_delete) {
return true;
}
return false;
}
private function submitPayload(string $url, array $payload): void
{
global $conf;
$payload = json_encode($payload);
$ch = curl_init($url);
$proxy = $conf['proxy'];
if (!empty ($proxy['host'])) {
$proxyAddress = $proxy['host'] . ':' . $proxy['port'];
curl_setopt($ch, CURLOPT_PROXY, $proxyAddress );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($proxy['user']) && !empty ($proxy['pass'])) {
$proxyAuth = $proxy['user'] . ':' . conf_decodeString($proxy['port']);
curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-type: application/json',
'Content-length: ' . strlen($payload)
]);
$result = curl_exec($ch);
if ($result === false) {
Logger::error('Error posting to Discord', [
'resp_body' => curl_error($ch),
'result' => $payload,
'http_error' => curl_errno($ch),
], __FILE__, __LINE__);
}
curl_close($ch);
}
}