1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7
8//if (!defined('DOKU_INC')) die();
9
10//require_once (DOKU_INC.'inc/changelog.php');
11
12//if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13//if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14//if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16
17
18/**
19 * DokuWiki Plugin alertrocketchatgroup (Action Component)
20 *
21 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
22 * @author JPDroid <jpdroid.jpo@gmail.com>
23 */
24class action_plugin_alertrocketchatgroup extends ActionPlugin
25{
26    /** @inheritDoc */
27    public function register(EventHandler  $controller)
28    {
29        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActionActPreprocess');
30
31    }
32
33
34    /**
35     * Event handler for ACTION_ACT_PREPROCESS
36     *
37     * @see https://www.dokuwiki.org/devel:events:ACTION_ACT_PREPROCESS
38     * @param Event $event Event object
39     * @param mixed $param optional parameter passed when event was registered
40     * @return void
41     */
42    function handleActionActPreprocess(Event $event)
43    {
44		if ($event->data == 'save') {
45			$this->handle();
46		}
47		return;
48    }
49
50	private function handle() {
51		global $conf;
52		global $ID;
53		global $INFO;
54		global $SUM;
55
56		// filter by namespaces
57		$urlServer = $this->getConf('jurlserver');
58		$payloads = $this->getConf('jpayloads');
59		$grupos = $this->getConf('jgroups');
60		if (!empty($urlServer)) {
61		  $arrPayloads = explode(',', $payloads);
62		}
63		if (!empty($grupos)) {
64			$arrGrupos = explode(',', $grupos);
65		}
66
67		$fullname = $INFO['userinfo']['name'];
68    	$page     = $INFO['namespace'] . $INFO['id'];
69   		$title    = "{$fullname} atualizou a TRIDF-WIKI <{$this->urlize()}|{$INFO['id']}>";
70
71		//enviar mensagem para os grupos
72		foreach($arrGrupos as $chave =>$gpr){
73			if(explode(':',$INFO['id'])[0] == $gpr){
74				$pgIndex = $chave;
75				$pgNome = $gpr;
76				$pgPayload = $arrPayloads[$pgIndex];
77				if(!isset($pgPayload) || $pgPayload == '')
78				  return;
79
80
81				// text
82				$data = array(
83					"text"                  =>  $title
84				);
85
86				// attachments
87				// if (!empty($SUM)) {
88					// $data['attachments'] = array(array(
89					// "title_link"       => "{$this->urlize()}",
90					// "title"            => "Summary",
91					// "text"             => "{$SUM}\n- {$fullname}",
92					// "color"            => "#AB4531"
93					// ));
94				// }
95
96				// init curl
97				$json = json_encode($data);
98				$webhook = $urlServer;
99				$urlFullWebHook =  $webhook.'/hooks/'.$pgPayload;
100				$ch = curl_init($urlFullWebHook);
101				 // submit payload
102				 $pay = urlencode($json);
103				 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
104				 curl_setopt($ch, CURLOPT_POSTFIELDS, "payload={$pay}");
105				 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
106				 $result = curl_exec($ch);
107
108				 // ideally display only for Admin users and/or in debugging mode
109				 if ($result === false){
110				   echo 'cURL error when posting Wiki save notification to Rocket.Chat+: ' . curl_error($ch);
111				 }
112
113				 // close curl
114				 curl_close($ch);
115
116
117
118			}
119		}
120
121
122
123
124
125	}
126
127
128	private function urlize($diffRev=null) {
129
130		global $conf;
131		global $INFO;
132
133		switch($conf['userewrite']) {
134		case 0:
135		  if (!empty($diffRev)) {
136			$url = DOKU_URL . "doku.php?id={$INFO['id']}&rev={$diffRev}&do=diff";
137		  } else {
138			$url = DOKU_URL . "doku.php?id={$INFO['id']}";
139		  }
140		  break;
141		case 1:
142		  $id = $INFO['id'];
143		  if ($conf['useslash']) {
144			$id = str_replace(":", "/", $id);
145		  }
146		  if (!empty($diffRev)) {
147			$url = DOKU_URL . "{$id}?rev={$diffRev}&do=diff";
148		  } else {
149			$url = DOKU_URL . $id;
150		  }
151		  break;
152		case 2:
153		  $id = $INFO['id'];
154		  if ($conf['useslash']) {
155			$id = str_replace(":", "/", $id);
156		  }
157		  if (!empty($diffRev)) {
158			$url = DOKU_URL . "doku.php/{$id}?rev={$diffRev}&do=diff";
159		  } else {
160			$url = DOKU_URL . "doku.php/{$id}";
161		  }
162		  break;
163		}
164		return $url;
165	  }
166
167
168}
169