1<?php
2
3namespace dokuwiki\plugin\issuelinks;
4
5class Webhook extends \DokuWiki_Plugin
6{
7
8    public function run()
9    {
10        /** @var \helper_plugin_issuelinks_util $util */
11        $util = plugin_load('helper', 'issuelinks_util');
12        if (!$util) {
13            http_status(424);
14            echo 'Plugin is deactived at server. Aborting.';
15            return;
16        }
17        $body = file_get_contents('php://input');
18
19        global $INPUT;
20        $userAgent = $INPUT->server->str('HTTP_USER_AGENT');
21        dbglog($userAgent);
22        dbglog($INPUT->server);
23
24        $serviceProvider = classes\ServiceProvider::getInstance();
25        $services = $serviceProvider->getServices();
26        $handlingService = null;
27        foreach ($services as $service) {
28            if (!$service::isOurWebhook()) {
29                continue;
30            }
31            $handlingService = $service::getInstance();
32            break;
33        }
34
35
36        if ($handlingService === null) {
37            dbglog('webhook could not be indentified', __FILE__ . ': ' . __LINE__);
38            dbglog('user agent: ' . $userAgent);
39            dbglog(json_decode($body, true));
40            $util->sendResponse(400, 'unknown webhook');
41            return;
42        }
43
44        try {
45            $validationResult = $handlingService->validateWebhook($body);
46            if ($validationResult !== true) {
47                $util->sendResponse($validationResult->code, $validationResult->body);
48                return;
49            }
50            $result = $handlingService->handleWebhook($body);
51        } catch (\Throwable $e) {
52            $util->sendResponse(500, $e->getMessage());
53            return;
54        }
55
56        $util->sendResponse($result->code, $result->body);
57    }
58}
59