Passed
Push — develop ( fcfb7f...7a8492 )
by Michael
01:17
created

Webhook.php (8 issues)

1
<?php
2
3
namespace dokuwiki\plugin\swarmzapierstructwebhook;
4
5
use dokuwiki\plugin\struct\meta\Schema;
1 ignored issue
show
The type dokuwiki\plugin\struct\meta\Schema 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...
6
7
class Webhook
8
{
9
    public function run()
10
    {
11
        global $conf, $INPUT;
12
13
        if ($conf['debug']) {
14
            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

14
            /** @scrutinizer ignore-call */ 
15
            dbglog($_SERVER);
Loading history...
15
        }
16
17
        /** @var \helper_plugin_swarmzapierstructwebhook $helper */
18
        $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

18
        $helper = /** @scrutinizer ignore-call */ plugin_load('helper', 'swarmzapierstructwebhook');
Loading history...
19
        $storedSecret = $helper->getConf('hook secret');
20
        if (!empty($storedSecret)) {
21
            $requestSecret = $INPUT->server->str('X_HOOK_SECRET');
22
            if (empty($requestSecret)) {
23
                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

23
                /** @scrutinizer ignore-call */ 
24
                http_status(401, 'Header X_HOOK_SECRET missing!');
Loading history...
24
                return;
25
            }
26
27
            if ($requestSecret !== $storedSecret) {
28
                http_status(403, 'Header X_HOOK_SECRET not identical with configured secret!');
29
                return;
30
            }
31
        }
32
33
        $body = file_get_contents('php://input');
34
35
        $ok = $this->handleWebhookPayload($body);
36
37
        if ($ok) {
38
            http_status(202);
39
        }
40
    }
41
42
    /**
43
     * Stores the webhook's payload to the struct table
44
     *
45
     * FIXME: don't set http status here
46
     *
47
     * @param string $json the original webhooks payload as json
48
     *
49
     * @return bool false if there was an error, http status has already been set, true if everything was ok
50
     */
51
    protected function handleWebhookPayload($json)
52
    {
53
        /** @var \helper_plugin_struct $struct */
54
        $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

54
        $struct = /** @scrutinizer ignore-call */ plugin_load('helper', 'struct');
Loading history...
55
        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:53
Loading history...
56
            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

56
            /** @scrutinizer ignore-call */ 
57
            http_status(422, 'struct plugin not active at this server');
Loading history...
57
            return false;
58
        }
59
60
        /** @var \helper_plugin_swarmzapierstructwebhook $helper */
61
        $helper = plugin_load('helper', 'swarmzapierstructwebhook');
62
63
        $lookupData = $helper->extractDataFromPayload(json_decode($json, true));
64
        $lookupData['json'] = $json;
65
66
        try {
67
            $schemas = Schema::getAll('lookup');
68
            if (!in_array('swarm', $schemas)) {
69
                $helper->createNewSwarmSchema();
70
            }
71
72
            $helper->deleteCheckinFromLookup($lookupData['checkinid']);
73
            $helper->saveDataToLookup($lookupData);
74
        } catch (\Exception $e) { // FIXME: catch more specific exceptions!
75
            $errorMessage = $e->getMessage();
76
            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

76
            /** @scrutinizer ignore-call */ 
77
            dbglog($errorMessage);
Loading history...
77
            http_status(500, $errorMessage);
78
            return false;
79
        }
80
81
        return true;
82
    }
83
}
84