| 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 \helper_plugin_swarmzapierstructwebhook $helper */ |
||||||
| 18 | $helper = plugin_load('helper', 'swarmzapierstructwebhook'); |
||||||
| 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
Bug
introduced
by
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
Loading history...
|
|||||||
| 55 | if (!$struct) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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
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
Loading history...
|
|||||||
| 77 | http_status(500, $errorMessage); |
||||||
| 78 | return false; |
||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | return true; |
||||||
| 82 | } |
||||||
| 83 | } |
||||||
| 84 |