Completed
Push — develop ( 963445...8a3cb1 )
by Michael
01:36
created

Webhook::run()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 14
nop 0
dl 0
loc 34
ccs 0
cts 21
cp 0
crap 56
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\plugin\swarmzapierstructwebhook;
4
5
use dokuwiki\plugin\struct\meta\Schema;
6
7
class Webhook
8
{
9
    public function run()
10
    {
11
        global $conf, $INPUT;
12
13
        if ($conf['debug']) {
14
            dbglog($_SERVER);
15
        }
16
17
        /** @var null|\helper_plugin_swarmzapierstructwebhook $helper */
18
        $helper = plugin_load('helper', 'swarmzapierstructwebhook');
19
        if (!$helper) {
20
            http_status(422, 'swarmzapierstructwebhook plugin not active at this server');
21
            return;
22
        }
23
        $storedSecret = $helper->getConf('hook secret');
24
        if (!empty($storedSecret)) {
25
            $requestSecret = $INPUT->server->str('X_HOOK_SECRET');
26
            if (empty($requestSecret)) {
27
                http_status(401, 'Header X_HOOK_SECRET missing!');
28
                return;
29
            }
30
31
            if ($requestSecret !== $storedSecret) {
32
                http_status(403, 'Header X_HOOK_SECRET not identical with configured secret!');
33
                return;
34
            }
35
        }
36
37
        $body = file_get_contents('php://input');
38
39
        $ok = $this->handleWebhookPayload($body);
40
41
        if ($ok) {
42
            http_status(202);
43
        }
44
    }
45
46
    /**
47
     * Stores the webhook's payload to the struct table
48
     *
49
     * FIXME: don't set http status here
50
     *
51
     * @param string $json the original webhooks payload as json
52
     *
53
     * @return bool false if there was an error, http status has already been set, true if everything was ok
54
     */
55
    protected function handleWebhookPayload($json)
56
    {
57
        /** @var null|\helper_plugin_struct $struct */
58
        $struct = plugin_load('helper', 'struct');
59
        if (!$struct) {
60
            http_status(422, 'struct plugin not active at this server');
61
            return false;
62
        }
63
64
        /** @var \helper_plugin_swarmzapierstructwebhook $helper */
65
        $helper = plugin_load('helper', 'swarmzapierstructwebhook');
66
67
        $lookupData = $helper->extractDataFromPayload(json_decode($json, true));
68
        $lookupData['json'] = $json;
69
70
        try {
71
            $schemas = Schema::getAll('lookup');
72
            if (!in_array('swarm', $schemas)) {
73
                $helper->createNewSwarmSchema();
74
            }
75
76
            $helper->deleteCheckinFromLookup($lookupData['checkinid']);
77
            $helper->saveDataToLookup($lookupData);
78
        } catch (\Exception $e) { // FIXME: catch more specific exceptions!
79
            $errorMessage = $e->getMessage();
80
            dbglog($errorMessage);
81
            http_status(500, $errorMessage);
82
            return false;
83
        }
84
85
        return true;
86
    }
87
}
88