Compare commits

...

2 commits

Author SHA1 Message Date
d3b5609515
⬆ Upgrade composer dependencies
All checks were successful
Deploy / deploy (push) Successful in 4s
2025-02-06 17:41:54 +01:00
250f072201
Add scribbles POST API 2025-02-06 17:40:34 +01:00
6 changed files with 122 additions and 10 deletions

4
.gitignore vendored
View file

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

14
composer.lock generated
View file

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

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;
}
}