From 250f072201532cd1fc29329df69c1a5ac5cbd3c2 Mon Sep 17 00:00:00 2001 From: Marcel Kapfer Date: Thu, 6 Feb 2025 17:40:34 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Add=20scribbles=20POST=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++ site/blueprints/pages/scribble.yml | 6 +- site/plugins/kirby-helpers/index.php | 15 ++++- .../src/Controller/ScribbleController.php | 57 +++++++++++++++++++ .../src/Middleware/ApiAuthentication.php | 36 ++++++++++++ 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 site/plugins/kirby-helpers/src/Controller/ScribbleController.php create mode 100644 site/plugins/kirby-helpers/src/Middleware/ApiAuthentication.php 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; + } +} From d3b5609515f45059cdc87e24561fd905bab5ec23 Mon Sep 17 00:00:00 2001 From: Marcel Kapfer Date: Thu, 6 Feb 2025 17:41:54 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=E2=AC=86=20Upgrade=20composer=20dependenci?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.lock | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/composer.lock b/composer.lock index a7c7598..b6c20fd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "22c9955fcf5f9ecd2075714cadc06760", + "content-hash": "fc199b109fe001629f126b05b4839dc6", "packages": [ { "name": "christian-riesen/base32", @@ -272,16 +272,16 @@ }, { "name": "getkirby/cms", - "version": "4.6.0", + "version": "4.6.1", "source": { "type": "git", "url": "https://github.com/getkirby/kirby.git", - "reference": "994556ca78eab3c11415702870b6388e8472addc" + "reference": "e29f216630ee1c2ab7e3f8f1bb3d335541121587" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/getkirby/kirby/zipball/994556ca78eab3c11415702870b6388e8472addc", - "reference": "994556ca78eab3c11415702870b6388e8472addc", + "url": "https://api.github.com/repos/getkirby/kirby/zipball/e29f216630ee1c2ab7e3f8f1bb3d335541121587", + "reference": "e29f216630ee1c2ab7e3f8f1bb3d335541121587", "shasum": "" }, "require": { @@ -371,7 +371,7 @@ "type": "custom" } ], - "time": "2025-01-30T11:02:53+00:00" + "time": "2025-02-06T15:39:59+00:00" }, { "name": "getkirby/composer-installer", @@ -1328,7 +1328,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "~8.1.0 || ~8.2.0 || ~8.3.0" + "php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0" }, "platform-dev": {}, "plugin-api-version": "2.6.0"