1<?php
2/**
3 * DokuWiki Plugin Webex Teams 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_webexteamsnotifier 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 ($event->data == 'save') {
25      $this->handle();
26    }
27    return;
28  }
29
30  private function handle() {
31
32    global $conf;
33    global $ID;
34    global $INFO;
35    global $SUM;
36
37    // if there is at least one namespace specified in the configuration section, only take action if the triggered
38    // namespace is in the list
39    $ns = $this->getConf('namespaces');
40    if (!empty($ns)) {
41      $namespaces = explode(',', $ns);
42      $current_namespace = explode(':', $INFO['namespace']);
43      if (!in_array($current_namespace[0], $namespaces)) {
44        return;
45      }
46    }
47
48    // title
49    $fullname = $INFO['userinfo']['name'];
50    $page     = $INFO['namespace'] . $INFO['id'];
51    $title    = "{$fullname} updated page [{$INFO['id']}]({$this->urlize()})";
52
53    // compare changes
54    $changelog = new PageChangeLog($ID);
55    $revArr = $changelog->getRevisions(-1, 1);
56    if (count($revArr) == 1) {
57      $title .= " ([Compare changes]({$this->urlize($revArr[0])}))";
58    }
59
60    // markdown
61    $data = array(
62      "markdown"                  =>  $title
63    );
64
65    if (!empty($SUM)) {
66      $data = array("markdown" => "{$title}\n\n{$SUM}");
67    }
68
69    // encode data
70    $json = json_encode($data);
71
72    // init curl
73    $webhook = $this->getConf('webhook');
74    $ch = curl_init($webhook);
75
76    // use proxy if defined
77    $proxy = $conf['proxy'];
78    if (!empty($proxy['host'])) {
79
80      // configure proxy address and port
81      $proxyAddress = $proxy['host'] . ':' . $proxy['port'];
82
83      curl_setopt($ch, CURLOPT_PROXY, $proxyAddress);
84      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
85      // TODO: may be required internally but best to add a config flag/path to local certificate file
86      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
87
88      // include username and password if defined
89      if (!empty($proxy['user']) && !empty($proxy['pass'])) {
90        $proxyAuth = $proxy['user'] . ':' . conf_decodeString($proxy['port']);
91        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
92      }
93
94    }
95
96    // Header Content-Type: application/json
97    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
98
99    // submit payload
100    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
101    curl_setopt($ch, CURLOPT_POSTFIELDS, "{$json}");
102    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
103    $result = curl_exec($ch);
104
105    // ideally display only for Admin users and/or in debugging mode
106    if ($result === false){
107      echo 'cURL error when posting Wiki save notification to Webex Teams: ' . curl_error($ch);
108    }
109
110    // close curl
111    curl_close($ch);
112
113  }
114
115  private function urlize($diffRev=null) {
116
117    global $conf;
118    global $INFO;
119
120    // Evaluating the userrewrite and useslash configuration to create proper URL,
121    // for details see: https://www.dokuwiki.org/config:userewrite and https://www.dokuwiki.org/config:useslash
122    switch($conf['userewrite']) {
123    case 0:
124      if (!empty($diffRev)) {
125        $url = DOKU_URL . "doku.php?id={$INFO['id']}&rev={$diffRev}&do=diff";
126      } else {
127        $url = DOKU_URL . "doku.php?id={$INFO['id']}";
128      }
129      break;
130    case 1:
131      $id = $INFO['id'];
132      if ($conf['useslash']) {
133        $id = str_replace(":", "/", $id);
134      }
135      if (!empty($diffRev)) {
136        $url = DOKU_URL . "{$id}?rev={$diffRev}&do=diff";
137      } else {
138        $url = DOKU_URL . $id;
139      }
140      break;
141    case 2:
142      $id = $INFO['id'];
143      if ($conf['useslash']) {
144        $id = str_replace(":", "/", $id);
145      }
146      if (!empty($diffRev)) {
147        $url = DOKU_URL . "doku.php/{$id}?rev={$diffRev}&do=diff";
148      } else {
149        $url = DOKU_URL . "doku.php/{$id}";
150      }
151      break;
152    }
153    return $url;
154  }
155}
156
157