This repository has been archived by the owner on May 12, 2023. It is now read-only.
forked from littleiffel/dokuwiki-slack-integration
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaction.php
210 lines (178 loc) · 7.94 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
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
<?php
/**
* DokuWiki Plugin Slack Notifier (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
*
*/
if (!defined('DOKU_INC')) die();
//require_once (DOKU_INC.'inc/changelog.php');
if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
class action_plugin_slacknotifier extends DokuWiki_Action_Plugin {
var $_event = null;
var $_payload = null;
function register(Doku_Event_Handler $controller) {
$controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, '_handle');
}
function _handle(Doku_Event $event, $param) {
// filter writes to attic
if ($this->_attic_write($event)) return;
// filter namespace
if (!$this->_valid_namespace()) return;
// filer event
if (!$this->_set_event($event)) return;
// set payload text
$this->_set_payload_text();
// set payload attachments
$this->_set_payload_attachments();
// submit payload
$this->_submit_payload();
}
private function _attic_write($event) {
$filename = $event->data[0][0];
if (strpos($filename, 'data/attic') !== false) return true;
}
private function _valid_namespace() {
global $INFO;
$validNamespaces = $this->getConf('namespaces');
if (!empty($validNamespaces)) {
$validNamespacesArr = explode(',', $validNamespaces);
$thisNamespaceArr = explode(':', $INFO['namespace']);
return in_array($thisNamespaceArr[0], $validNamespacesArr);
} else {
return true;
}
}
private function _set_event($event) {
global $ID;
global $INFO;
$data = $event->data;
$contents = $data[0][1];
$newRev = $data[3];
$oldRev = $INFO['meta']['last_change']['date'];
if (!empty($contents) && empty($newRev) && empty($oldRev) && $this->getConf('notify_create') == 1) {
$this->_event = 'create';
return true;
} elseif (!empty($contents) && empty($newRev) && !empty($oldRev) && $this->getConf('notify_edit') == 1) {
$this->_event = 'edit';
return true;
} elseif (empty($contents) && empty($newRev) && $this->getConf('notify_delete') == 1) {
$this->_event = 'delete';
return true;
} else {
return false;
}
}
private function _set_payload_text() {
global $ID;
global $INFO;
switch ($this->_event) {
case 'create':
$event = "created";
break;
case 'edit':
$event = "updated";
break;
case 'delete':
$event = "removed";
break;
}
$user = $INFO['userinfo']['name'];
$link = $this->_get_url();
$page = $INFO['id'];
$title = "{$user} {$event} page <{$link}|{$page}>";
/* Searching changelogs yields previous revisions for
* created pages that had been deleted, however we'll
* use last_change which ignores these (so we won't
* show changes for created pages even if a previous
* revision exists)
*/
/*
$changelog = new PageChangeLog($ID);
$revArr = $changelog->getRevisions(0, 1);
$oldRev = (count($revArr) == 1) ? $revArr[0] : null;
*/
if ($this->_event != 'delete') {
$oldRev = $INFO['meta']['last_change']['date'];
if (!empty($oldRev)) {
$diffURL = $this->_get_url($oldRev);
$title .= " (<{$diffURL}|Compare changes>)";
}
}
$this->_payload = array("text" => $title);
}
private function _set_payload_attachments() {
global $INFO;
global $SUM;
$user = $INFO['userinfo']['name'];
if ($this->getConf('show_summary') == 1 && !empty($SUM)) {
$this->_payload['attachments'] = array(array(
"fallback" => "Change summary",
"title" => "Summary",
"text" => "{$SUM}\n- {$user}"
));
}
}
private function _get_url($oldRev=null) {
global $conf;
global $INFO;
$page = $INFO['id'];
if (($conf['userewrite'] == 1 || $conf['userewrite'] == 2) && $conf['useslash'] == true) {
return str_replace(":", "/", $page);
}
switch($conf['userewrite']) {
case 0:
$url = DOKU_URL . "doku.php?id={$page}";
break;
case 1:
$url = DOKU_URL . $page;
break;
case 2:
$url = DOKU_URL . "doku.php/{$page}";
break;
}
if (!empty($oldRev)) {
switch($conf['userewrite']) {
case 0:
$url .= "&do=diff&rev={$oldRev}";
break;
case 1:
case 2:
$url .= "?do=diff&rev={$oldRev}";
break;
}
}
return $url;
}
private function _submit_payload() {
global $conf;
// encode payload
$json = json_encode($this->_payload);
// init curl
$webhook = $this->getConf('webhook');
$ch = curl_init($webhook);
// use proxy if defined
$proxy = $conf['proxy'];
if (!empty($proxy['host'])) {
// configure proxy address and port
$proxyAddress = $proxy['host'] . ':' . $proxy['port'];
curl_setopt($ch, CURLOPT_PROXY, $proxyAddress);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// include username and password if defined
if (!empty($proxy['user']) && !empty($proxy['pass'])) {
$proxyAuth = $proxy['user'] . ':' . conf_decodeString($proxy['port']);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
}
}
// submit payload
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array('payload' => $json));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// close curl
Curl_close($ch);
}
}