1<?php
2/**
3 * DokuWiki Plugin Rocket.Chat Notifier (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 *
7 */
8
9if (!defined('DOKU_INC')) die();
10
11require_once (DOKU_INC.'inc/changelog.php');
12
13if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
14if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
15if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16
17class action_plugin_rocketchatnotifier extends DokuWiki_Action_Plugin {
18
19  function register(Doku_Event_Handler $controller) {
20    $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess');
21  }
22
23  function handle_action_act_preprocess(Doku_Event $event, $param) {
24    if (isset($event->data['save'])) {
25      if ($event->data['save'] == $lang['btn_save']) {
26        $this->handle();
27      }
28    }
29    return;
30  }
31
32  private function handle() {
33
34    global $conf;
35    global $ID;
36    global $INFO;
37    global $SUM;
38
39    // filter by namespaces
40    $ns = $this->getConf('namespaces');
41    if (!empty($ns)) {
42      $namespaces = explode(',', $ns);
43      $current_namespace = explode(':', $INFO['namespace']);
44      if (!in_array($current_namespace[0], $namespaces)) {
45        return;
46      }
47    }
48
49    // title
50    $fullname = $INFO['userinfo']['name'];
51    $page     = $INFO['namespace'] . $INFO['id'];
52    $title    = "{$fullname} updated page <{$this->urlize()}|{$INFO['id']}>";
53
54    // compare changes
55    $changelog = new PageChangeLog($ID);
56    $revArr = $changelog->getRevisions(-1, 1);
57    if (count($revArr) == 1) {
58      $title .= " (<{$this->urlize($revArr[0])}|Compare changes>)";
59    }
60
61    // text
62    $data = array(
63      "text"                  =>  $title
64    );
65
66    // attachments
67    if (!empty($SUM)) {
68      $data['attachments'] = array(array(
69        "title_link"       => "{$this->urlize()}",
70        "title"            => "Summary",
71        "text"             => "{$SUM}\n- {$fullname}",
72        "color"            => "#AB4531"
73      ));
74    }
75
76    // encode data
77    $json = json_encode($data);
78
79    // init curl
80    $webhook = $this->getConf('webhook');
81    $ch = curl_init($webhook);
82
83    // use proxy if defined
84    $proxy = $conf['proxy'];
85    if (!empty($proxy['host'])) {
86
87      // configure proxy address and port
88      $proxyAddress = $proxy['host'] . ':' . $proxy['port'];
89
90      curl_setopt($ch, CURLOPT_PROXY, $proxyAddress);
91      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
92      // TODO: may be required internally but best to add a config flag/path to local certificate file
93      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
94
95      // include username and password if defined
96      if (!empty($proxy['user']) && !empty($proxy['pass'])) {
97        $proxyAuth = $proxy['user'] . ':' . conf_decodeString($proxy['port']);
98        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
99      }
100
101    }
102
103    // submit payload
104    $pay = urlencode($json);
105    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
106    curl_setopt($ch, CURLOPT_POSTFIELDS, "payload={$pay}");
107    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
108    $result = curl_exec($ch);
109
110    // ideally display only for Admin users and/or in debugging mode
111    if ($result === false){
112      echo 'cURL error when posting Wiki save notification to Rocket.Chat+: ' . curl_error($ch);
113    }
114
115    // close curl
116    curl_close($ch);
117
118  }
119
120  private function urlize($diffRev=null) {
121
122    global $conf;
123    global $INFO;
124
125    switch($conf['userewrite']) {
126    case 0:
127      if (!empty($diffRev)) {
128        $url = DOKU_URL . "doku.php?id={$INFO['id']}&rev={$diffRev}&do=diff";
129      } else {
130        $url = DOKU_URL . "doku.php?id={$INFO['id']}";
131      }
132      break;
133    case 1:
134      $id = $INFO['id'];
135      if ($conf['useslash']) {
136        $id = str_replace(":", "/", $id);
137      }
138      if (!empty($diffRev)) {
139        $url = DOKU_URL . "{$id}?rev={$diffRev}&do=diff";
140      } else {
141        $url = DOKU_URL . $id;
142      }
143      break;
144    case 2:
145      $id = $INFO['id'];
146      if ($conf['useslash']) {
147        $id = str_replace(":", "/", $id);
148      }
149      if (!empty($diffRev)) {
150        $url = DOKU_URL . "doku.php/{$id}?rev={$diffRev}&do=diff";
151      } else {
152        $url = DOKU_URL . "doku.php/{$id}";
153      }
154      break;
155    }
156    return $url;
157  }
158}
159
160// vim:ts=4:sw=4:et:enc=utf-8:
161