Passed
Pull Request — master (#1)
by Michael
02:10
created

webhook.php (5 issues)

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
1 ignored issue
show
The type DokuWiki_Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
{
11
12
    public function run()
13
    {
14
15
        /** @var helper_plugin_issuelinks_util $util */
16
        $util = plugin_load('helper', 'issuelinks_util');
1 ignored issue
show
The function plugin_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
        $util = /** @scrutinizer ignore-call */ plugin_load('helper', 'issuelinks_util');
Loading history...
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);
1 ignored issue
show
The function http_status was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
            /** @scrutinizer ignore-call */ 
19
            http_status(424);
Loading history...
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);
1 ignored issue
show
The function dbglog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        /** @scrutinizer ignore-call */ 
27
        dbglog($userAgent);
Loading history...
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