Compare commits

..

No commits in common. "d3b5609515f45059cdc87e24561fd905bab5ec23" and "8d84fda8afae9097861fbb5c16e9261e14b69b40" have entirely different histories.

6 changed files with 10 additions and 122 deletions

4
.gitignore vendored
View file

@ -49,10 +49,6 @@ Icon
/site/config/.license /site/config/.license
# Env
# ---------------
/site/config/env.php
# Content # 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", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "fc199b109fe001629f126b05b4839dc6", "content-hash": "22c9955fcf5f9ecd2075714cadc06760",
"packages": [ "packages": [
{ {
"name": "christian-riesen/base32", "name": "christian-riesen/base32",
@ -272,16 +272,16 @@
}, },
{ {
"name": "getkirby/cms", "name": "getkirby/cms",
"version": "4.6.1", "version": "4.6.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/getkirby/kirby.git", "url": "https://github.com/getkirby/kirby.git",
"reference": "e29f216630ee1c2ab7e3f8f1bb3d335541121587" "reference": "994556ca78eab3c11415702870b6388e8472addc"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/getkirby/kirby/zipball/e29f216630ee1c2ab7e3f8f1bb3d335541121587", "url": "https://api.github.com/repos/getkirby/kirby/zipball/994556ca78eab3c11415702870b6388e8472addc",
"reference": "e29f216630ee1c2ab7e3f8f1bb3d335541121587", "reference": "994556ca78eab3c11415702870b6388e8472addc",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -371,7 +371,7 @@
"type": "custom" "type": "custom"
} }
], ],
"time": "2025-02-06T15:39:59+00:00" "time": "2025-01-30T11:02:53+00:00"
}, },
{ {
"name": "getkirby/composer-installer", "name": "getkirby/composer-installer",
@ -1328,7 +1328,7 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0" "php": "~8.1.0 || ~8.2.0 || ~8.3.0"
}, },
"platform-dev": {}, "platform-dev": {},
"plugin-api-version": "2.6.0" "plugin-api-version": "2.6.0"

View file

@ -1,6 +1,6 @@
title: Scribble title: Scribble
num: '{{ page.date.toDate("YmdHi") }}' num: '{{ page.date.toDate("YmdHis") }}'
create: create:
title: "{{ page.date.toDate('Y-m-d H:i') }}" title: "{{ page.date.toDate('Y-m-d H:i') }}"
@ -17,9 +17,7 @@ sections:
label: Date label: Date
required: true required: true
time: time:
step: step: 1
unit: second
size: 1
default: now default: now
text: text:
type: textarea type: textarea

View file

@ -1,20 +1,7 @@
<?php <?php
use Kirby\Cms\App as Kirby;
use Kirby\Filesystem\F; use Kirby\Filesystem\F;
F::loadClasses([ 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__); ], __DIR__);
Kirby::plugin('mmk2410/kirby-helpers', [
'routes' => [
[
'pattern' => 'my/api/v1/scribble',
'action' => fn () => (new Mmk2410\KirbyHelpers\Controller\ScribbleController())->addScribble(),
'method' => 'POST'
],
]
]);

View file

@ -1,57 +0,0 @@
<?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

@ -1,36 +0,0 @@
<?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;
}
}