2022-03-19 00:00:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace brain\api\v1;
|
|
|
|
|
|
|
|
use brain\data\Book;
|
|
|
|
use brain\data\Settings;
|
|
|
|
use brain\data\Session;
|
|
|
|
use brain\utility\StringTools;
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
use function _\filter;
|
|
|
|
|
|
|
|
class PagesAPI
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public static function getPageContent($request, $args)
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$task = $args['fourth'];
|
|
|
|
$pages = (new Book('../content/pages'))->getContents();
|
2022-03-19 00:00:51 +01:00
|
|
|
$content = [];
|
|
|
|
foreach ($pages as $page) {
|
|
|
|
$entry = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'id' => $page['id'],
|
|
|
|
'uuid' => $page['uuid'],
|
|
|
|
'title' => $page['title'],
|
|
|
|
'feature' => $page['feature'],
|
|
|
|
'path' => $page['path'],
|
|
|
|
'layout' => $page['layout'],
|
|
|
|
'tags' => $page['tags'],
|
|
|
|
'author' => $page['author'],
|
|
|
|
'created' => $page['created'],
|
|
|
|
'updated' => $page['updated'],
|
|
|
|
'deleted' => $page['deleted'],
|
|
|
|
'menu' => $page['menu'],
|
|
|
|
'featured' => $page['featured'],
|
|
|
|
'published' => $page['published'],
|
|
|
|
'slug' => $page['slug'],
|
|
|
|
'content' => StringTools::sanitizeContent($page['content']),
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
array_push($content, $entry);
|
|
|
|
}
|
|
|
|
switch ($task) {
|
2022-05-17 02:41:15 +02:00
|
|
|
case 'published':
|
2022-03-19 00:00:51 +01:00
|
|
|
$published = filter($content, function ($item) {
|
2022-05-17 02:41:15 +02:00
|
|
|
return $item['published'] == true && $item['deleted'] == false;
|
2022-03-19 00:00:51 +01:00
|
|
|
});
|
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
$result = ['pages' => $published, 'totalItems' => count($published)];
|
2022-03-19 00:00:51 +01:00
|
|
|
break;
|
2022-05-17 02:41:15 +02:00
|
|
|
case 'featured':
|
2022-03-19 00:00:51 +01:00
|
|
|
$featured = filter($content, function ($item) {
|
2022-05-17 02:41:15 +02:00
|
|
|
return $item['featured'] == true && $item['deleted'] == false;
|
2022-03-19 00:00:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'pages' => $featured,
|
|
|
|
'totalItems' => count($featured),
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
break;
|
2022-05-17 02:41:15 +02:00
|
|
|
case 'menu':
|
2022-03-19 00:00:51 +01:00
|
|
|
$menu = filter($content, function ($item) {
|
2022-05-17 02:41:15 +02:00
|
|
|
return $item['menu'] == true && $item['deleted'] == false;
|
2022-03-19 00:00:51 +01:00
|
|
|
});
|
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
$result = ['pages' => $menu, 'totalItems' => count($menu)];
|
2022-03-19 00:00:51 +01:00
|
|
|
break;
|
2022-05-17 02:41:15 +02:00
|
|
|
case 'single':
|
|
|
|
$uuid = $args['fifth'];
|
|
|
|
$page = (new Book('../content/pages'))->findPageById($uuid);
|
2022-03-19 00:00:51 +01:00
|
|
|
|
|
|
|
$entry = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'id' => $page['id'],
|
|
|
|
'uuid' => $page['uuid'],
|
|
|
|
'title' => $page['title'],
|
|
|
|
'feature' => $page['feature'],
|
|
|
|
'path' => $page['path'],
|
|
|
|
'layout' => $page['layout'],
|
|
|
|
'tags' => $page['tags'],
|
|
|
|
'author' => $page['author'],
|
|
|
|
'created' => $page['created'],
|
|
|
|
'updated' => $page['updated'],
|
|
|
|
'deleted' => $page['deleted'],
|
|
|
|
'menu' => $page['menu'],
|
|
|
|
'featured' => $page['featured'],
|
|
|
|
'published' => $page['published'],
|
|
|
|
'slug' => $page['slug'],
|
|
|
|
'content' => StringTools::sanitizeContent($page['content']),
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
$result = $entry;
|
|
|
|
break;
|
2022-05-17 02:41:15 +02:00
|
|
|
case 'tags':
|
2022-03-19 00:00:51 +01:00
|
|
|
$result = Settings::getTags();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => "Hm, no task. That's unfortunate",
|
|
|
|
'type' => 'TASK_NONE',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public static function handlePageTask($request, $args)
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$task = $args['fourth'];
|
2022-03-19 00:00:51 +01:00
|
|
|
switch ($task) {
|
2022-05-17 02:41:15 +02:00
|
|
|
case 'delete':
|
|
|
|
case 'create':
|
|
|
|
case 'write':
|
|
|
|
$body = $request->getParsedBody();
|
2022-03-19 00:00:51 +01:00
|
|
|
$passed = true;
|
2022-05-17 02:41:15 +02:00
|
|
|
if (!isset($body['form_token'])) {
|
2022-03-19 00:00:51 +01:00
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => 'No form token. Not good, sport.',
|
|
|
|
'type' => 'TASK_FORM_AUTH',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
if ($body['form_token'] == Session::get('form_token')) {
|
2022-03-19 00:00:51 +01:00
|
|
|
$keys = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'id',
|
|
|
|
'uuid',
|
|
|
|
'layout',
|
|
|
|
'current_title',
|
|
|
|
'content',
|
|
|
|
'title',
|
|
|
|
'created',
|
|
|
|
'slug',
|
|
|
|
'tags',
|
|
|
|
'menu',
|
|
|
|
'featured',
|
|
|
|
'published',
|
|
|
|
'form_token',
|
|
|
|
'feature_image',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($body as $key => $item) {
|
|
|
|
if (!in_array($key, $keys)) {
|
|
|
|
//found unnecessary key, so reject submission
|
|
|
|
$passed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($passed) {
|
|
|
|
$result = (new Book())->editPage($task, $request);
|
|
|
|
} else {
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => 'Unneccessary key found. Post not authorized, slick.',
|
|
|
|
'type' => 'TASK_FORM_AUTH',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => 'Form token, auth failed. Uh oh.',
|
|
|
|
'type' => 'TASK_FORM_AUTH',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2022-05-17 02:41:15 +02:00
|
|
|
case 'add-entry-image':
|
2022-03-19 00:00:51 +01:00
|
|
|
$result = ImagesAPI::uploadImage($request);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$result = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'message' => "Hm, no task. That's unfortunate",
|
|
|
|
'type' => 'TASK_NONE',
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|