1<?php 2 3// must be run within Dokuwiki 4if (!defined('DOKU_INC')) die(); 5 6require_once __DIR__ . '/vendor/autoload.php'; 7use MatrixPhp\MatrixClient; 8 9class helper_plugin_matrixnotifierwas extends DokuWiki_Plugin { 10 11 var $_event = null; 12 var $_event_type = array ( 13 "E" => "edit", 14 "e" => "edit minor", 15 "C" => "create", 16 "D" => "delete", 17 "R" => "revert" 18 ); 19 var $_summary = null; 20 var $_payload = null; 21 22 public function setPayload($payload){ 23 $this->_payload = $payload; 24 } 25 26 public function attic_write ( $filename ) { 27 if ( strpos ( $filename, 'data/attic' ) !== false ) { 28 return true; 29 } 30 else { 31 return false; 32 } 33 } 34 35 public function valid_namespace ( ) { 36 global $INFO; 37 $validNamespaces = $this -> getConf ( 'namespaces' ); 38 if ( !empty ( $validNamespaces ) ) { 39 $validNamespacesArr = explode ( ',', $validNamespaces ); 40 foreach ( $validNamespacesArr as $namespace ) { 41 if ( strpos( $namespace, $INFO['namespace'] ) === 0 ) { 42 return true; 43 } 44 } 45 return false; 46 } else { 47 return true; 48 } 49 } 50 51 public function set_event ( $event ) { 52 $this -> _opt = print_r ( $event, true ); 53 $changeType = $event -> data['changeType']; 54 $event_type = $this -> _event_type[$changeType]; 55 $summary = $event -> data['summary']; 56 if ( !empty ( $summary ) ) { 57 $this -> _summary = $summary; 58 } 59 if ( $event_type == 'create' && $this -> getConf ( 'notify_create' ) == 1 ) { 60 $this -> _event = 'create'; 61 return true; 62 } elseif ( $event_type == 'edit' && $this -> getConf ( 'notify_edit' ) == 1 ) { 63 $this -> _event = 'edit'; 64 return true; 65 } elseif ( $event_type == 'edit minor' && ( $this -> getConf ( 'notify_edit' ) == 1 ) && ( $this -> getConf ( 'notify_edit_minor' ) == 1 ) ) { 66 $this -> _event = 'edit minor'; 67 return true; 68 } elseif ( $event_type == 'delete' && $this -> getConf ( 'notify_delete' ) == 1 ) { 69 $this -> _event = 'delete'; 70 return true; 71 } else { 72 return false; 73 } 74 } 75 76 public function set_payload_text ( $event ) { 77 global $conf; 78 global $lang; 79 global $INFO; 80 $event_name = ''; 81 $embed_color = hexdec ( "37474f" ); // default value 82 switch ( $this -> _event ) { 83 case 'create': 84 $title = $this -> getLang ( 't_created' ); 85 $event_name = $this -> getLang ( 'e_created' ); 86 $embed_color = hexdec ( "00cc00" ); 87 break; 88 case 'edit': 89 $title = $this -> getLang ( 't_updated' ); 90 $event_name = $this -> getLang ( 'e_updated' ); 91 $embed_color = hexdec ( "00cccc" ); 92 break; 93 case 'edit minor': 94 $title = $this -> getLang ( 't_minor_upd' ); 95 $event_name = $this -> getLang ( 'e_minor_upd' ); 96 $embed_color = hexdec ( "00cccc" ); 97 break; 98 case 'delete': 99 $title = $this -> getLang ( 't_removed' ); 100 $event_name = $this -> getLang ( 'e_removed' ); 101 $embed_color = hexdec ( "cc0000" ); 102 break; 103 } 104 105 if ( $this -> getConf ( 'notify_show_name' ) === 'real name' ) { 106 $user = $INFO['userinfo']['name']; 107 } elseif ( $this -> getConf ( 'notify_show_name' ) === 'username' ) { 108 $user = $_SERVER['REMOTE_USER']; 109 } else { 110 throw new Exception('invalid notify_show_name value'); 111 } 112 $link = $this -> _get_url ( $event, null ); 113 $page = $event -> data['id']; 114 //$description = "{$user} {$event_name} [__{$page}__]({$link})"; 115 $description = $user.' <b>'.$event_name.'</b> <a href="'.$page.'">'.$link.'</a><br/>'; 116 117 if ( $this -> _event != 'delete' ) { 118 $oldRev = $INFO['meta']['last_change']['date']; 119 if ( !empty ( $oldRev ) ) { 120 $diffURL = $this -> _get_url ( $event, $oldRev ); 121 //$description .= " \([" . $this -> getLang ( 'compare' ) . "]({$diffURL})\)"; 122 $description .= '<a href="'.$diffURL.'">'.$this -> getLang ( 'compare' ).'</a><br/>'; 123 } 124 } 125 126 $summary = $this -> _summary; 127 if ( ( strpos ( $this -> _event, 'edit' ) !== false ) && $this -> getConf ( 'notify_show_summary' ) ) { 128 if ( $summary ) { 129 //$description .= "\n" . $lang['summary'] . ": " . $summary; 130 $description .= $lang['summary'] . ": " . $summary; 131 } 132 } 133 134 $footer = array ( "text" => "Dokuwiki MatrixNotifier v1.2.2" ); 135 $payload = array ( "embeds" => 136 array ( 137 ["title" => $title, "color" => $embed_color, "description" => $description, "footer" => $footer] 138 ), 139 ); 140 $this -> _payload = $payload; 141 } 142 143 private function _get_url ( $event = null, $Rev ) { 144 global $ID; 145 global $conf; 146 $oldRev = $event -> data['oldRevision']; 147 $page = $event -> data['id']; 148 if ( ( $conf['userewrite'] == 1 || $conf['userewrite'] == 2 ) && $conf['useslash'] == true ) 149 $page = str_replace ( ":", "/", $page ); 150 switch ( $conf['userewrite'] ) { 151 case 0: 152 $url = DOKU_URL . "doku.php?id={$page}"; 153 break; 154 case 1: 155 $url = DOKU_URL . $page; 156 break; 157 case 2: 158 $url = DOKU_URL . "doku.php/{$page}"; 159 break; 160 } 161 if ( $Rev != null ) { 162 switch ( $conf['userewrite'] ) { 163 case 0: 164 $url .= "&do=diff&rev={$Rev}"; 165 break; 166 case 1: 167 case 2: 168 $url .= "?do=diff&rev={$Rev}"; 169 break; 170 } 171 } 172 return $url; 173 } 174 175 public function submit_payload ( ) { 176 global $conf; 177 178 $client = new MatrixClient( $this -> getConf ( 'server' ) ); 179 $token = $client->login($this -> getConf ( 'username' ), $this -> getConf ( 'password' ) ); 180 $room = $client -> joinRoom( $this -> getConf ( 'room' ) ); 181 $response = $room->sendHtml('<b>'.$this -> _payload['embeds'][0]['title'].'</b><hr/>'.$this -> _payload['embeds'][0]['description'] ); 182 } 183 184} 185