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 foreach ( $validNamespacesArr as $namespace ) { 37 if ( strpos( $namespace, $INFO['namespace'] ) === 0 ) { 38 return true; 39 } 40 } 41 return false; 42 } else { 43 return true; 44 } 45 } 46 47 public function set_event ( $event ) { 48 $this -> _opt = print_r ( $event, true ); 49 $changeType = $event -> data['changeType']; 50 $event_type = $this -> _event_type[$changeType]; 51 $summary = $event -> data['summary']; 52 if ( !empty ( $summary ) ) { 53 $this -> _summary = $summary; 54 } 55 if ( $event_type == 'create' && $this -> getConf ( 'notify_create' ) == 1 ) { 56 $this -> _event = 'create'; 57 return true; 58 } elseif ( $event_type == 'edit' && $this -> getConf ( 'notify_edit' ) == 1 ) { 59 $this -> _event = 'edit'; 60 return true; 61 } elseif ( $event_type == 'edit minor' && ( $this -> getConf ( 'notify_edit' ) == 1 ) && ( $this -> getConf ( 'notify_edit_minor' ) == 1 ) ) { 62 $this -> _event = 'edit minor'; 63 return true; 64 } elseif ( $event_type == 'delete' && $this -> getConf ( 'notify_delete' ) == 1 ) { 65 $this -> _event = 'delete'; 66 return true; 67 } else { 68 return false; 69 } 70 } 71 72 public function set_payload_text ( $event ) { 73 global $conf; 74 global $lang; 75 global $INFO; 76 $event_name = ''; 77 $embed_color = hexdec ( "37474f" ); // default value 78 switch ( $this -> _event ) { 79 case 'create': 80 $title = $this -> getLang ( 't_created' ); 81 $event_name = $this -> getLang ( 'e_created' ); 82 $embed_color = hexdec ( "00cc00" ); 83 break; 84 case 'edit': 85 $title = $this -> getLang ( 't_updated' ); 86 $event_name = $this -> getLang ( 'e_updated' ); 87 $embed_color = hexdec ( "00cccc" ); 88 break; 89 case 'edit minor': 90 $title = $this -> getLang ( 't_minor_upd' ); 91 $event_name = $this -> getLang ( 'e_minor_upd' ); 92 $embed_color = hexdec ( "00cccc" ); 93 break; 94 case 'delete': 95 $title = $this -> getLang ( 't_removed' ); 96 $event_name = $this -> getLang ( 'e_removed' ); 97 $embed_color = hexdec ( "cc0000" ); 98 break; 99 } 100 101 if ( $this -> getConf ( 'notify_show_name' ) === 'real name' ) { 102 $user = $INFO['userinfo']['name']; 103 } elseif ( $this -> getConf ( 'notify_show_name' ) === 'username' ) { 104 $user = $_SERVER['REMOTE_USER']; 105 } else { 106 throw new Exception('invalid notify_show_name value'); 107 } 108 $link = $this -> _get_url ( $event, null ); 109 $page = $event -> data['id']; 110 $description = "{$user} {$event_name} [__{$page}__]({$link})"; 111 112 if ( $this -> _event != 'delete' ) { 113 $oldRev = $INFO['meta']['last_change']['date']; 114 if ( !empty ( $oldRev ) ) { 115 $diffURL = $this -> _get_url ( $event, $oldRev ); 116 $description .= " \([" . $this -> getLang ( 'compare' ) . "]({$diffURL})\)"; 117 } 118 } 119 120 $summary = $this -> _summary; 121 if ( ( strpos ( $this -> _event, 'edit' ) !== false ) && $this -> getConf ( 'notify_show_summary' ) ) { 122 if ( $summary ) $description .= "\n" . $lang['summary'] . ": " . $summary; 123 } 124 125 $footer = array ( "text" => "Dokuwiki DiscordNotifier v1.2.2" ); 126 $payload = array ( "embeds" => 127 array ( 128 ["title" => $title, "color" => $embed_color, "description" => $description, "footer" => $footer] 129 ), 130 ); 131 $this -> _payload = $payload; 132 } 133 134 private function _get_url ( $event = null, $Rev ) { 135 global $ID; 136 global $conf; 137 $oldRev = $event -> data['oldRevision']; 138 $page = $event -> data['id']; 139 if ( ( $conf['userewrite'] == 1 || $conf['userewrite'] == 2 ) && $conf['useslash'] == true ) 140 $page = str_replace ( ":", "/", $page ); 141 switch ( $conf['userewrite'] ) { 142 case 0: 143 $url = DOKU_URL . "doku.php?id={$page}"; 144 break; 145 case 1: 146 $url = DOKU_URL . $page; 147 break; 148 case 2: 149 $url = DOKU_URL . "doku.php/{$page}"; 150 break; 151 } 152 if ( $Rev != null ) { 153 switch ( $conf['userewrite'] ) { 154 case 0: 155 $url .= "&do=diff&rev={$Rev}"; 156 break; 157 case 1: 158 case 2: 159 $url .= "?do=diff&rev={$Rev}"; 160 break; 161 } 162 } 163 return $url; 164 } 165 166 public function submit_payload ( ) { 167 global $conf; 168 169 // extract separate webhook URLs 170 $webhooks_array = explode( ';', $this -> getConf ( 'webhook' ) ); 171 foreach ( $webhooks_array as $webhook ) { 172 173 // init curl 174 $ch = curl_init ( $webhook ); 175 176 // use proxy if defined 177 $proxy = $conf['proxy']; 178 if ( !empty ( $proxy['host'] ) ) { 179 // configure proxy address and port 180 $proxyAddress = $proxy['host'] . ':' . $proxy['port']; 181 curl_setopt ( $ch, CURLOPT_PROXY, $proxyAddress ); 182 curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 ); 183 curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false ); 184 185 // include username and password if defined 186 if ( !empty ( $proxy['user'] ) && !empty ( $proxy['pass'] ) ) { 187 $proxyAuth = $proxy['user'] . ':' . conf_decodeString ( $proxy['port'] ); 188 curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, $proxyAuth ); 189 } 190 191 } 192 193 // submit payload 194 curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" ); 195 $json_payload = json_encode ( $this->_payload ); 196 $payload_length = 'Content-length: ' . strlen ( $json_payload ); 197 curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ( 'Content-type: application/json', $payload_length ) ); 198 curl_setopt ( $ch, CURLOPT_POSTFIELDS, $json_payload ); 199 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); 200 curl_exec ( $ch ); 201 202 // close curl 203 Curl_close ( $ch ); 204 205 } 206 207 } 208 209} 210