Test Setup Failed
Push — develop ( feb2ef...fcfb7f )
by Michael
01:02
created

Webhook.php (9 issues)

1
<?php
2
3
namespace dokuwiki\plugin\swarmzapierstructwebhook;
4
5
class Webhook
6
{
7
    public function run()
8
    {
9
        global $conf, $INPUT;
10
11
        if ($conf['debug']) {
12
            dbglog($_SERVER);
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

12
            /** @scrutinizer ignore-call */ 
13
            dbglog($_SERVER);
Loading history...
13
        }
14
15
        /** @var \helper_plugin_swarmzapierstructwebhook $helper */
16
        $helper = plugin_load('helper', 'swarmzapierstructwebhook');
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
        $helper = /** @scrutinizer ignore-call */ plugin_load('helper', 'swarmzapierstructwebhook');
Loading history...
17
        $storedSecret = $helper->getConf('hook secret');
18
        if (!empty($storedSecret)) {
19
            $requestSecret = $INPUT->server->str('X_HOOK_SECRET');
20
            if (empty($requestSecret)) {
21
                http_status(401, 'Header X_HOOK_SECRET missing!');
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

21
                /** @scrutinizer ignore-call */ 
22
                http_status(401, 'Header X_HOOK_SECRET missing!');
Loading history...
22
                return;
23
            }
24
25
            if ($requestSecret !== $storedSecret) {
26
                http_status(403, 'Header X_HOOK_SECRET not identical with configured secret!');
27
                return;
28
            }
29
        }
30
31
        $body = file_get_contents('php://input');
32
33
        $this->handleWebhookPayload($body);
34
35
        http_status(202);
36
    }
37
38
    /**
39
     * Stores the webhook's payload to the struct table
40
     *
41
     * @param string $json the original webhooks payload as json
42
     */
43
    protected function handleWebhookPayload($json)
44
    {
45
        /** @var \helper_plugin_struct $struct */
46
        $struct = plugin_load('helper', 'struct');
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

46
        $struct = /** @scrutinizer ignore-call */ plugin_load('helper', 'struct');
Loading history...
47
        if (!$struct) {
0 ignored issues
show
$struct is of type helper_plugin_struct, thus it always evaluated to true. If $struct can have other possible types, add them to Webhook.php:45
Loading history...
48
            http_status(422, 'struct plugin not active at this server');
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

48
            /** @scrutinizer ignore-call */ 
49
            http_status(422, 'struct plugin not active at this server');
Loading history...
49
            exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
50
        }
51
52
        /** @var \helper_plugin_swarmzapierstructwebhook $helper */
53
        $helper = plugin_load('helper', 'swarmzapierstructwebhook');
54
55
        $lookupData = $helper->extractDataFromPayload(json_decode($json, true));
56
        $lookupData['json'] = $json;
57
58
        try {
59
            $helper->deleteCheckinFromLookup($lookupData['checkinid']);
60
            $helper->saveDataToLookup($lookupData);
61
        } catch (\Exception $e) {
62
            $errorMessage = $e->getMessage();
63
            dbglog($errorMessage);
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

63
            /** @scrutinizer ignore-call */ 
64
            dbglog($errorMessage);
Loading history...
64
            http_status(500, $errorMessage);
65
            exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
66
        }
67
    }
68
}
69