This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
forked from brendonrapp/dokuwiki-hipchat
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaction.php
156 lines (128 loc) · 4.13 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
<?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 {
function register(Doku_Event_Handler $controller) {
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess');
}
function handle_action_act_preprocess(Doku_Event $event, $param) {
if (isset($event->data['save'])) {
$this->handle();
}
return;
}
private function handle() {
global $conf;
global $ID;
global $INFO;
global $SUM;
// filter by namespaces
$ns = $this->getConf('namespaces');
if (!empty($ns)) {
$namespaces = explode(',', $ns);
$current_namespace = explode(':', $INFO['namespace']);
if (!in_array($current_namespace[0], $namespaces)) {
return;
}
}
// title
$fullname = $INFO['userinfo']['name'];
$page = $INFO['namespace'] . $INFO['id'];
$title = "{$fullname} updated page <{$this->urlize()}|{$INFO['id']}>";
// compare changes
$changelog = new PageChangeLog($ID);
$revArr = $changelog->getRevisions(-1, 1);
if (count($revArr) == 1) {
$title .= " (<{$this->urlize($revArr[0])}|Compare changes>)";
}
// text
$data = array(
"text" => $title
);
// attachments
if (!empty($SUM)) {
$data['attachments'] = array(array(
"fallback" => "Change summary",
"title" => "Summary",
"text" => "{$SUM}\n- {$fullname}"
));
}
// encode data
$json = json_encode($data);
// 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);
// TODO: may be required internally but best to add a config flag/path to local certificate file
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);
// ideally display only for Admin users and/or in debugging mode
if ($result === false){
echo 'cURL error when posting Wiki save notification to Slack: ' . curl_error($ch);
}
// close curl
curl_close($ch);
}
private function urlize($diffRev=null) {
global $conf;
global $INFO;
switch($conf['userewrite']) {
case 0:
if (!empty($diffRev)) {
$url = DOKU_URL . "doku.php?id={$INFO['id']}&rev={$diffRev}&do=diff";
} else {
$url = DOKU_URL . "doku.php?id={$INFO['id']}";
}
break;
case 1:
$id = $INFO['id'];
if ($conf['useslash']) {
$id = str_replace(":", "/", $id);
}
if (!empty($diffRev)) {
$url = DOKU_URL . "{$id}?rev={$diffRev}&do=diff";
} else {
$url = DOKU_URL . $id;
}
break;
case 2:
$id = $INFO['id'];
if ($conf['useslash']) {
$id = str_replace(":", "/", $id);
}
if (!empty($diffRev)) {
$url = DOKU_URL . "doku.php/{$id}?rev={$diffRev}&do=diff";
} else {
$url = DOKU_URL . "doku.php/{$id}";
}
break;
}
return $url;
}
}
// vim:ts=4:sw=4:et:enc=utf-8: