forked from mallchin/dokuwiki-slack-notifier
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelper.php
executable file
·209 lines (186 loc) · 7.08 KB
/
helper.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
<?php
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
class helper_plugin_discordnotifier extends DokuWiki_Plugin {
var $_event = null;
var $_event_type = array (
"E" => "edit",
"e" => "edit minor",
"C" => "create",
"D" => "delete",
"R" => "revert"
);
var $_summary = null;
var $_payload = null;
public function setPayload($payload){
$this->_payload = $payload;
}
public function attic_write ( $filename ) {
if ( strpos ( $filename, 'data/attic' ) !== false ) {
return true;
}
else {
return false;
}
}
public function valid_namespace ( ) {
global $INFO;
$validNamespaces = $this -> getConf ( 'namespaces' );
if ( !empty ( $validNamespaces ) ) {
$validNamespacesArr = explode ( ',', $validNamespaces );
foreach ( $validNamespacesArr as $namespace ) {
if ( strpos( $namespace, $INFO['namespace'] ) === 0 ) {
return true;
}
}
return false;
} else {
return true;
}
}
public function set_event ( $event ) {
$this -> _opt = print_r ( $event, true );
$changeType = $event -> data['changeType'];
$event_type = $this -> _event_type[$changeType];
$summary = $event -> data['summary'];
if ( !empty ( $summary ) ) {
$this -> _summary = $summary;
}
if ( $event_type == 'create' && $this -> getConf ( 'notify_create' ) == 1 ) {
$this -> _event = 'create';
return true;
} elseif ( $event_type == 'edit' && $this -> getConf ( 'notify_edit' ) == 1 ) {
$this -> _event = 'edit';
return true;
} elseif ( $event_type == 'edit minor' && ( $this -> getConf ( 'notify_edit' ) == 1 ) && ( $this -> getConf ( 'notify_edit_minor' ) == 1 ) ) {
$this -> _event = 'edit minor';
return true;
} elseif ( $event_type == 'delete' && $this -> getConf ( 'notify_delete' ) == 1 ) {
$this -> _event = 'delete';
return true;
} else {
return false;
}
}
public function set_payload_text ( $event ) {
global $conf;
global $lang;
global $INFO;
$event_name = '';
$embed_color = hexdec ( "37474f" ); // default value
switch ( $this -> _event ) {
case 'create':
$title = $this -> getLang ( 't_created' );
$event_name = $this -> getLang ( 'e_created' );
$embed_color = hexdec ( "00cc00" );
break;
case 'edit':
$title = $this -> getLang ( 't_updated' );
$event_name = $this -> getLang ( 'e_updated' );
$embed_color = hexdec ( "00cccc" );
break;
case 'edit minor':
$title = $this -> getLang ( 't_minor_upd' );
$event_name = $this -> getLang ( 'e_minor_upd' );
$embed_color = hexdec ( "00cccc" );
break;
case 'delete':
$title = $this -> getLang ( 't_removed' );
$event_name = $this -> getLang ( 'e_removed' );
$embed_color = hexdec ( "cc0000" );
break;
}
if ( $this -> getConf ( 'notify_show_name' ) === 'real name' ) {
$user = $INFO['userinfo']['name'];
} elseif ( $this -> getConf ( 'notify_show_name' ) === 'username' ) {
$user = $_SERVER['REMOTE_USER'];
} else {
throw new Exception('invalid notify_show_name value');
}
$link = $this -> _get_url ( $event, null );
$page = $event -> data['id'];
$description = "{$user} {$event_name} [__{$page}__]({$link})";
if ( $this -> _event != 'delete' ) {
if ( isset ( $INFO['meta']['last_change'] ) ) {
$oldRev = $INFO['meta']['last_change']['date'];
$diffURL = $this -> _get_url ( $event, $oldRev );
$description .= " \([" . $this -> getLang ( 'compare' ) . "]({$diffURL})\)";
}
}
$summary = $this -> _summary;
if ( ( strpos ( $this -> _event, 'edit' ) !== false ) && $this -> getConf ( 'notify_show_summary' ) ) {
if ( $summary ) $description .= "\n" . $lang['summary'] . ": " . $summary;
}
$footer = array ( "text" => "Dokuwiki DiscordNotifier v1.2.2" );
$payload = array ( "embeds" =>
array (
["title" => $title, "color" => $embed_color, "description" => $description, "footer" => $footer]
),
);
$this -> _payload = $payload;
}
private function _get_url ( $event = null, $Rev ) {
global $ID;
global $conf;
$oldRev = $event -> data['oldRevision'];
$page = $event -> data['id'];
if ( ( $conf['userewrite'] == 1 || $conf['userewrite'] == 2 ) && $conf['useslash'] == true )
$page = 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 ( $Rev != null ) {
switch ( $conf['userewrite'] ) {
case 0:
$url .= "&do=diff&rev={$Rev}";
break;
case 1:
case 2:
$url .= "?do=diff&rev={$Rev}";
break;
}
}
return $url;
}
public function submit_payload ( ) {
global $conf;
// extract separate webhook URLs
$webhooks_array = explode( ';', $this -> getConf ( 'webhook' ) );
foreach ( $webhooks_array as $webhook ) {
// init curl
$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" );
$json_payload = json_encode ( $this->_payload );
$payload_length = 'Content-length: ' . strlen ( $json_payload );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ( 'Content-type: application/json', $payload_length ) );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $json_payload );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_exec ( $ch );
// close curl
Curl_close ( $ch );
}
}
}