1<?php 2/** 3 * DokuWiki Plugin Telegram Notifier (Action Component) 4 * 5 * @license Apache 2.0 http://www.apache.org/licenses/ 6 * 7 */ 8 9if (!defined('DOKU_INC')) die(); 10if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 11 12 13class action_plugin_tgnotify extends \dokuwiki\Extension\ActionPlugin 14{ 15 const __PLUGIN_VERSION__ = '1.0.9'; 16 17 /** 18 * plugin should use this method to register its handlers with the DokuWiki's event controller 19 * 20 * @param Doku_Event_Handler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER 21 * 22 * @return not required 23 */ 24 public function register(Doku_Event_Handler $controller) 25 { 26 $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, '_handle'); 27 } 28 29 30 /** 31 * custom event handler 32 * 33 * @param Doku_Event $event event object by reference 34 * @param mixed $param the parameters passed to register_hook when this 35 * handler was registered 36 * 37 * @return not required 38 */ 39 public function _handle(Doku_Event $event, $param) 40 { 41 if ( $this->getConf('enable') ) { 42 $this->transmitMessage($this->prepareMessage($event)); 43 } 44 } 45 46 47 /** 48 * Prepare message text 49 * 50 * @param Doku_Event $event event object by reference 51 * 52 * @return string 53 */ 54 private function prepareMessage(Doku_Event $event) 55 { 56 $message = ''; 57 58 // Page change type 59 switch ($event->data['changeType']) { 60 case 'C': 61 $message .= $this->getLang('page-added') . PHP_EOL; 62 break; 63 case 'D': 64 $message .= $this->getLang('page-deleted') . PHP_EOL; 65 break; 66 case 'E': 67 $message .= $this->getLang('page-modified') . PHP_EOL; 68 break; 69 case 'e': 70 $message .= $this->getLang('page-modified-minor') . PHP_EOL; 71 break; 72 case 'R': 73 $message .= $this->getLang('page-reverted') . PHP_EOL; 74 break; 75 } 76 77 // Add row with page name and url 78 $pagename = $event->data['id']; 79 $pageurl = rtrim(DOKU_URL, '/') . '/' . $pagename; 80 $message .= sprintf($this->getLang('pagename'), $pagename, $pageurl) . PHP_EOL; 81 82 // Add row with page size diff (in bytes) 83 $message .= sprintf($this->getLang('sizechange'), $event->data['sizechange']) . PHP_EOL; 84 85 // Add row with username (if logged in) or IP address 86 global $USERINFO; 87 if ( $this->getConf('showuser') && isset($USERINFO['name']) ) { 88 $username = $USERINFO['name']; 89 $message .= sprintf($this->getLang('username'), $username) . PHP_EOL; 90 } 91 92 // Add row with IP address 93 if ( $this->getConf('showaddr') ) { 94 $useraddr = $_SERVER['REMOTE_ADDR'] ?? $this->getLang('unknown'); 95 $message .= sprintf($this->getLang('useraddr'), $useraddr) . PHP_EOL; 96 } 97 98 return $message; 99 } 100 101 /** 102 * Send telegram message 103 * 104 * @param string $text message text 105 * 106 * @return not required 107 */ 108 private function transmitMessage($text) 109 { 110 $token = $this->getConf('token'); 111 $chatid = $this->getConf('chatid'); 112 $url = 'https://api.telegram.org/bot' . $token . '/sendMessage'; 113 114 $data = array( 115 'chat_id' => $chatid, 116 'text' => $text, 117 'parse_mode' => 'MarkdownV2', 118 ); 119 120 $opts = array( 121 'http' => array( 122 'method' => "POST", 123 'header' => "Content-type: application/json", 124 'timeout' => 10, 125 'content' => json_encode($data), 126 ), 127 ); 128 129 $content = file_get_contents($url, false, stream_context_create($opts)); 130 $json = json_decode($content); 131 if ( $this->getConf('debug') ) { 132 error_log("[tgnotify] Request headers: " . var_dump($opts)); 133 error_log("[tgnotify] Request content: " . var_dump($data)); 134 error_log("[tgnotify] Response headers: " . var_dump($http_response_header)); 135 error_log("[tgnotify] Response content: " . var_dump($json)); 136 } 137 } 138 139} 140