Add scribbles POST API

This commit is contained in:
Marcel Kapfer 2025-02-06 17:40:34 +01:00
parent 8d84fda8af
commit 250f072201
Signed by: mmk2410
GPG key ID: CADE6F0C09F21B09
5 changed files with 115 additions and 3 deletions

4
.gitignore vendored
View file

@ -49,6 +49,10 @@ Icon
/site/config/.license
# Env
# ---------------
/site/config/env.php
# Content
# ---------------

View file

@ -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

View file

@ -1,7 +1,20 @@
<?php
use Kirby\Cms\App as Kirby;
use Kirby\Filesystem\F;
F::loadClasses([
'Mmk2410\\KirbyHelpers\\RssFeed' => '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'
],
]
]);

View file

@ -0,0 +1,57 @@
<?php
namespace Mmk2410\KirbyHelpers\Controller;
use DateTimeImmutable;
use Kirby\Cms\Page;
use Kirby\Cms\Response;
use Kirby\Exception\BadMethodCallException;
use Mmk2410\KirbyHelpers\Middleware\ApiAuthentication;
class ScribbleController
{
private const SCRIBBLES_PAGE = 'scribbles';
private const SCRIBBLE_TEMPLATE = 'scribble';
private const STATUS_LISTED = 'listed';
private const SLUG_DATE_FORMAT = 'Ymd-Hi';
private const TITLE_DATE_FORMAT = 'Y-m-d H:i';
private const NUM_DATE_FORMAT = 'YmdHi';
private const CONTENT_DATE_FORMAT = 'Y-m-d H:i';
public function addScribble(): Response
{
(new ApiAuthentication())->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);
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace Mmk2410\KirbyHelpers\Middleware;
use Kirby\Exception\Exception;
use Kirby\Exception\PermissionException;
class ApiAuthentication
{
public function auth(): void
{
$request = kirby()->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;
}
}