1<?php
2
3// must be run within Dokuwiki
4if (!defined('DOKU_INC')) die();
5class helper_plugin_discordnotifier extends DokuWiki_Plugin {
6
7    var $_event = null;
8    var $_event_type = array (
9        "E" => "edit",
10        "e" => "edit minor",
11        "C" => "create",
12        "D" => "delete",
13        "R" => "revert"
14    );
15    var $_summary = null;
16    var $_payload = null;
17
18    public function setPayload($payload){
19        $this->_payload = $payload;
20    }
21
22    public function attic_write ( $filename ) {
23        if ( strpos ( $filename, 'data/attic' ) !== false ) {
24            return true;
25        }
26        else {
27            return false;
28        }
29    }
30
31    public function valid_namespace ( ) {
32        global $INFO;
33        $validNamespaces = $this -> getConf ( 'namespaces' );
34        if ( !empty ( $validNamespaces ) ) {
35            $validNamespacesArr = explode ( ',', $validNamespaces );
36            $thisNamespaceArr = explode ( ':', $INFO['namespace'] );
37            return in_array ( $thisNamespaceArr[0], $validNamespacesArr );
38        } else {
39            return true;
40        }
41    }
42
43    public function set_event ( $event ) {
44        $this -> _opt = print_r ( $event, true );
45        $changeType = $event -> data['changeType'];
46        $event_type = $this -> _event_type[$changeType];
47        $summary = $event -> data['summary'];
48        if ( !empty ( $summary ) ) {
49            $this -> _summary = $summary;
50        }
51        if ( $event_type == 'create' && $this -> getConf ( 'notify_create' ) == 1 ) {
52            $this -> _event = 'create';
53            return true;
54        } elseif ( $event_type == 'edit' && $this -> getConf ( 'notify_edit' ) == 1 ) {
55            $this -> _event = 'edit';
56            return true;
57        } elseif ( $event_type == 'edit minor' && ( $this -> getConf ( 'notify_edit' ) == 1 ) && ( $this -> getConf ( 'notify_edit_minor' ) == 1 ) ) {
58            $this -> _event = 'edit minor';
59            return true;
60        } elseif ( $event_type == 'delete' && $this -> getConf ( 'notify_delete' ) == 1 ) {
61            $this -> _event = 'delete';
62            return true;
63        } else {
64            return false;
65        }
66    }
67
68    public function set_payload_text ( $event ) {
69        global $conf;
70        global $lang;
71        global $INFO;
72        $event_name = '';
73        $embed_color = hexdec ( "37474f" ); // default value
74        switch ( $this -> _event ) {
75            case 'create':
76                $title = $this -> getLang ( 't_created' );
77                $event_name = $this -> getLang ( 'e_created' );
78                $embed_color = hexdec ( "00cc00" );
79                break;
80            case 'edit':
81                $title = $this -> getLang ( 't_updated' );
82                $event_name = $this -> getLang ( 'e_updated' );
83                $embed_color = hexdec ( "00cccc" );
84                break;
85            case 'edit minor':
86                $title = $this -> getLang ( 't_minor_upd' );
87                $event_name = $this -> getLang ( 'e_minor_upd' );
88                $embed_color = hexdec ( "00cccc" );
89                break;
90            case 'delete':
91                $title = $this -> getLang ( 't_removed' );
92                $event_name = $this -> getLang ( 'e_removed' );
93                $embed_color = hexdec ( "cc0000" );
94                break;
95        }
96
97        $user = $INFO['userinfo']['name'];
98        $link = $this -> _get_url ( $event, null );
99        $page = $event -> data['id'];
100        $description = "{$user} {$event_name} [__{$page}__]({$link})";
101
102        if ( $this -> _event != 'delete' ) {
103            $oldRev = $INFO['meta']['last_change']['date'];
104            if ( !empty ( $oldRev ) ) {
105                $diffURL = $this -> _get_url ( $event, $oldRev );
106                $description .= " \([" . $this -> getLang ( 'compare' ) . "]({$diffURL})\)";
107            }
108        }
109
110        $summary = $this -> _summary;
111        if ( ( strpos ( $this -> _event, 'edit' ) !== false ) && $this -> getConf ( 'notify_show_summary' ) ) {
112            if ( $summary ) $description .= "\n" . $lang['summary'] . ": " . $summary;
113        }
114
115        $footer = array ( "text" => "Dokuwiki DiscordNotifier v. 1.1.1" );
116        $payload = array ( "embeds" =>
117            array (
118                ["title" => $title, "color" => $embed_color, "description" => $description, "footer" => $footer]
119            ),
120        );
121        $this -> _payload = $payload;
122    }
123
124    private function _get_url ( $event = null, $Rev ) {
125        global $ID;
126        global $conf;
127        $oldRev = $event -> data['oldRevision'];
128		$page = $event -> data['id'];
129		if ( ( $conf['userewrite'] == 1 || $conf['userewrite'] == 2 ) && $conf['useslash'] == true )
130			$page = str_replace ( ":", "/", $page );
131        switch ( $conf['userewrite'] ) {
132            case 0:
133                $url = DOKU_URL . "doku.php?id={$page}";
134                break;
135            case 1:
136                $url = DOKU_URL . $page;
137                break;
138            case 2:
139                $url = DOKU_URL . "doku.php/{$page}";
140                break;
141        }
142        if ( $Rev != null  ) {
143            switch ( $conf['userewrite'] ) {
144                case 0:
145                    $url .= "&do=diff&rev={$Rev}";
146                    break;
147                case 1:
148                case 2:
149                    $url .= "?do=diff&rev={$Rev}";
150                    break;
151            }
152        }
153        return $url;
154    }
155
156    public function submit_payload ( ) {
157        global $conf;
158
159        // init curl
160        $ch = curl_init ( $this -> getConf ( 'webhook' ) );
161
162        // use proxy if defined
163        $proxy = $conf['proxy'];
164        if ( !empty ( $proxy['host'] ) ) {
165            // configure proxy address and port
166            $proxyAddress = $proxy['host'] . ':' . $proxy['port'];
167            curl_setopt ( $ch, CURLOPT_PROXY, $proxyAddress );
168            curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
169            curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
170
171            // include username and password if defined
172            if ( !empty ( $proxy['user'] ) && !empty ( $proxy['pass'] ) ) {
173                $proxyAuth = $proxy['user'] . ':' . conf_decodeString ( $proxy['port'] );
174                curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, $proxyAuth );
175            }
176
177        }
178
179        // submit payload
180        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
181        $json_payload = json_encode ( $this->_payload );
182        $payload_length = 'Content-length: ' . mb_strlen ( $json_payload );
183        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ( 'Content-type: application/json', $payload_length ) );
184        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $json_payload );
185        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
186        curl_exec ( $ch );
187
188        // close curl
189        Curl_close ( $ch );
190
191    }
192
193    public function shouldBeSend($filename){
194        $send = true;
195        if($this -> attic_write ( $filename )) $send = false;
196        if(!$this -> valid_namespace ( )) $send = false;
197        return $send;
198    }
199
200    public function sendUpdate($event) {
201
202        if(!($this->shouldBeSend($event -> data['file']) && !$this -> set_event ( $event ))) return ;
203        // set payload text
204        $this -> set_payload_text ( $event );
205
206        // submit payload
207        $helper -> submit_payload ( );
208    }
209}
210