Passed
Push — master ( cf401d...252787 )
by Michael
02:49
created

webhook.php (1 issue)

Severity
1
<?php
2
3
if (!defined('DOKU_INC')) {
4
    define('DOKU_INC', realpath(dirname(__FILE__) . '/../../../') . '/');
5
}
6
define('NOSESSION', 1);
7
require_once(DOKU_INC . 'inc/init.php');
8
9
class webhook_plugin_issuelinks extends DokuWiki_Plugin
10
{
11
12
    public function run()
13
    {
14
15
        /** @var helper_plugin_issuelinks_util $util */
16
        $util = plugin_load('helper', 'issuelinks_util');
17
        if (!$util) {
0 ignored issues
show
$util is of type helper_plugin_issuelinks_util, thus it always evaluated to true.
Loading history...
18
            http_status(424);
19
            echo 'Plugin is deactived at server. Aborting.';
20
            return;
21
        }
22
        $body = file_get_contents('php://input');
23
24
        global $INPUT;
25
        $userAgent = $INPUT->server->str('HTTP_USER_AGENT');
26
        dbglog($userAgent);
27
        dbglog($INPUT->server);
28
29
        $serviceProvider = dokuwiki\plugin\issuelinks\classes\ServiceProvider::getInstance();
30
        $services = $serviceProvider->getServices();
31
        $handlingService = null;
32
        foreach ($services as $service) {
33
            if (!$service::isOurWebhook()) {
34
                continue;
35
            }
36
            $handlingService = $service::getInstance();
37
            break;
38
        }
39
40
41
        if ($handlingService === null) {
42
            dbglog('webhook could not be indentified', __FILE__ . ': ' . __LINE__);
43
            dbglog('user agent: ' . $userAgent);
44
            dbglog(json_decode($body, true));
45
            $util->sendResponse(400, 'unknown webhook');
46
            return;
47
        }
48
49
        try {
50
            $validationResult = $handlingService->validateWebhook($body);
51
            if ($validationResult !== true) {
52
                $util->sendResponse($validationResult->code, $validationResult->body);
53
                return;
54
            }
55
            $result = $handlingService->handleWebhook($body);
56
        } catch (\Throwable $e) {
57
            $util->sendResponse(500, $e->getMessage());
58
            return;
59
        }
60
61
        $util->sendResponse($result->code, $result->body);
62
    }
63
}
64
65
if (!defined('DOKU_TESTING')) {
66
    // Main
67
    $hook = new webhook_plugin_issuelinks();
68
    $hook->run();
69
}
70
71