diff --git a/.gitignore b/.gitignore index d331e65..0a3fe88 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,10 @@ Icon /site/config/.license +# Env +# --------------- +/site/config/env.php + # Content # --------------- diff --git a/site/blueprints/pages/scribble.yml b/site/blueprints/pages/scribble.yml index 843a405..7384409 100644 --- a/site/blueprints/pages/scribble.yml +++ b/site/blueprints/pages/scribble.yml @@ -1,6 +1,6 @@ title: Scribble -num: '{{ page.date.toDate("YmdHis") }}' +num: '{{ page.date.toDate("YmdHi") }}' create: title: "{{ page.date.toDate('Y-m-d H:i') }}" @@ -17,7 +17,9 @@ sections: label: Date required: true time: - step: 1 + step: + unit: second + size: 1 default: now text: type: textarea diff --git a/site/plugins/kirby-helpers/index.php b/site/plugins/kirby-helpers/index.php index e1615e8..a82813b 100644 --- a/site/plugins/kirby-helpers/index.php +++ b/site/plugins/kirby-helpers/index.php @@ -1,7 +1,20 @@ 'src/RssFeed.php' + 'Mmk2410\\KirbyHelpers\\RssFeed' => 'src/RssFeed.php', + 'Mmk2410\\KirbyHelpers\\Middleware\\ApiAuthentication' => 'src/Middleware/ApiAuthentication.php', + 'Mmk2410\\KirbyHelpers\\Controller\\ScribbleController' => 'src/Controller/ScribbleController.php', ], __DIR__); + +Kirby::plugin('mmk2410/kirby-helpers', [ + 'routes' => [ + [ + 'pattern' => 'my/api/v1/scribble', + 'action' => fn () => (new Mmk2410\KirbyHelpers\Controller\ScribbleController())->addScribble(), + 'method' => 'POST' + ], + ] +]); diff --git a/site/plugins/kirby-helpers/src/Controller/ScribbleController.php b/site/plugins/kirby-helpers/src/Controller/ScribbleController.php new file mode 100644 index 0000000..08e4782 --- /dev/null +++ b/site/plugins/kirby-helpers/src/Controller/ScribbleController.php @@ -0,0 +1,57 @@ +auth(); + + $text = kirby()->request()->body()->get('text'); + + if ($text === null) { + throw new BadMethodCallException('Invalid request'); + } + + $date = new DateTimeImmutable(); + $num = (int) $date->format(self::NUM_DATE_FORMAT); + + kirby()->impersonate('kirby'); + $page = Page::create([ + 'parent' => page(self::SCRIBBLES_PAGE), + 'slug' => $date->format(self::SLUG_DATE_FORMAT), + 'draft' => false, + 'template' => self::SCRIBBLE_TEMPLATE, + 'content' => [ + 'title' => $date->format(self::TITLE_DATE_FORMAT), + 'text' => $text, + 'date' => $date->format(self::CONTENT_DATE_FORMAT), + ], + ]); + $page->changeStatus(self::STATUS_LISTED, $num); + kirby()->impersonate('nobody'); + + return new Response(code: 200); + } +} diff --git a/site/plugins/kirby-helpers/src/Middleware/ApiAuthentication.php b/site/plugins/kirby-helpers/src/Middleware/ApiAuthentication.php new file mode 100644 index 0000000..381d6c0 --- /dev/null +++ b/site/plugins/kirby-helpers/src/Middleware/ApiAuthentication.php @@ -0,0 +1,36 @@ +request(); + $apiKeyHeader = $request->header('Api-Key'); + + if ($apiKeyHeader === null) { + throw new PermissionException(); + } + + $apiKey = $this->getApiKey(); + + if ($apiKey !== $apiKeyHeader) { + throw new PermissionException(); + } + } + + private function getApiKey(): string + { + $apiKey = kirby()->option('mmk2410.kirby-helpers.api-key'); + + if ($apiKey === null) { + throw new Exception('No API key set!'); + } + + return $apiKey; + } +}