aboutsummaryrefslogtreecommitdiff
path: root/handler/admin
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2024-01-31 06:11:00 +0300
committerEvgeny Zinoviev <me@ch1p.io>2024-01-31 20:45:40 +0300
commitc0dc531ebefd8912819f3b6c8bda1fed3c7e750c (patch)
tree2c75aa9df182260aef09faf4befd81a4c2b9c5e2 /handler/admin
parent48d688cdf7f9eae1bf11b8a6f0e5b98687c604cb (diff)
make it simple, but not simpler
Diffstat (limited to 'handler/admin')
-rw-r--r--handler/admin/AdminRequestHandler.php20
-rw-r--r--handler/admin/AutoAddOrEdit.php99
-rw-r--r--handler/admin/AutoDelete.php34
-rw-r--r--handler/admin/AutoEdit.php130
-rw-r--r--handler/admin/Index.php13
-rw-r--r--handler/admin/Login.php31
-rw-r--r--handler/admin/Logout.php17
-rw-r--r--handler/admin/MarkdownPreview.php22
-rw-r--r--handler/admin/PageAdd.php66
-rw-r--r--handler/admin/PostAdd.php68
-rw-r--r--handler/admin/UploadDelete.php25
-rw-r--r--handler/admin/UploadEditNote.php25
-rw-r--r--handler/admin/Uploads.php73
13 files changed, 0 insertions, 623 deletions
diff --git a/handler/admin/AdminRequestHandler.php b/handler/admin/AdminRequestHandler.php
deleted file mode 100644
index 5a6bd12..0000000
--- a/handler/admin/AdminRequestHandler.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use admin;
-use Response;
-
-class AdminRequestHandler extends \RequestHandler {
-
- public function beforeDispatch(): ?Response {
- $this->skin->static[] = 'css/admin.css';
- $this->skin->static[] = 'js/admin.js';
-
- if (!($this instanceof Login) && !admin::isAdmin())
- throw new \ForbiddenException('looks like you are not admin');
-
- return null;
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/AutoAddOrEdit.php b/handler/admin/AutoAddOrEdit.php
deleted file mode 100644
index 1627642..0000000
--- a/handler/admin/AutoAddOrEdit.php
+++ /dev/null
@@ -1,99 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use Page;
-use Post;
-use Response;
-
-abstract class AutoAddOrEdit extends AdminRequestHandler {
-
- public function beforeDispatch(): ?Response {
- $this->skin->setOptions([
- 'full_width' => true,
- 'no_footer' => true
- ]);
- return parent::beforeDispatch();
- }
-
- protected function _get_postAdd(
- string $title = '',
- string $text = '',
- ?array $tags = null,
- string $short_name = '',
- ?string $error_code = null
- ): Response {
- $this->skin->addLangKeys($this->lang->search('/^(err_)?blog_/'));
- $this->skin->title = $this->lang['blog_write'];
- return $this->skin->renderPage('admin/postForm',
- title: $title,
- text: $text,
- tags: $tags ? implode(', ', $tags) : '',
- short_name: $short_name,
- error_code: $error_code);
- }
-
- protected function _get_postEdit(
- Post $post,
- string $title = '',
- string $text = '',
- ?array $tags = null,
- bool $visible = false,
- bool $toc = false,
- string $short_name = '',
- ?string $error_code = null,
- bool $saved = false,
- ): Response {
- $this->skin->addLangKeys($this->lang->search('/^(err_)?blog_/'));
- $this->skin->title = ($this->lang)('blog_post_edit_title', $post->title);
- return $this->skin->renderPage('admin/postForm',
- is_edit: true,
- post_id: $post->id,
- post_url: $post->getUrl(),
- title: $title,
- text: $text,
- tags: $tags ? implode(', ', $tags) : '',
- visible: $visible,
- toc: $toc,
- saved: $saved,
- short_name: $short_name,
- error_code: $error_code
- );
- }
-
- protected function _get_pageAdd(
- string $name,
- string $title = '',
- string $text = '',
- ?string $error_code = null
- ): Response {
- $this->skin->addLangKeys($this->lang->search('/^(err_)?pages_/'));
- $this->skin->title = ($this->lang)('pages_create_title', $name);
- return $this->skin->renderPage('admin/pageForm',
- short_name: $name,
- title: $title,
- text: $text,
- error_code: $error_code);
- }
-
- protected function _get_pageEdit(
- Page $page,
- string $title = '',
- string $text = '',
- bool $saved = false,
- bool $visible = false,
- ?string $error_code = null
- ): Response {
- $this->skin->addLangKeys($this->lang->search('/^(err_)?pages_/'));
- $this->skin->title = ($this->lang)('pages_page_edit_title', $page->shortName.'.html');
- return $this->skin->renderPage('admin/pageForm',
- is_edit: true,
- short_name: $page->shortName,
- title: $title,
- text: $text,
- visible: $visible,
- saved: $saved,
- error_code: $error_code);
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/AutoDelete.php b/handler/admin/AutoDelete.php
deleted file mode 100644
index 80c8eef..0000000
--- a/handler/admin/AutoDelete.php
+++ /dev/null
@@ -1,34 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use csrf;
-use NotFoundException;
-use pages;
-use posts;
-use RedirectResponse;
-use Response;
-
-class AutoDelete extends AdminRequestHandler {
-
- public function get(): Response {
- list($name) = $this->input('short_name');
-
- $post = posts::getPostByName($name);
- if ($post) {
- csrf::check('delpost'.$post->id);
- posts::delete($post);
- return new RedirectResponse('/');
- }
-
- $page = pages::getPageByName($name);
- if ($page) {
- csrf::check('delpage'.$page->shortName);
- pages::delete($page);
- return new RedirectResponse('/');
- }
-
- throw new NotFoundException();
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/AutoEdit.php b/handler/admin/AutoEdit.php
deleted file mode 100644
index ba6a7d8..0000000
--- a/handler/admin/AutoEdit.php
+++ /dev/null
@@ -1,130 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use csrf;
-use pages;
-use posts;
-use Response;
-
-class AutoEdit extends AutoAddOrEdit {
-
- public function get(): Response {
- list($short_name, $saved) = $this->input('short_name, b:saved');
-
- $post = posts::getPostByName($short_name);
- if ($post) {
- $tags = $post->getTags();
- return $this->_get_postEdit($post,
- title: $post->title,
- text: $post->md,
- tags: $post->getTags(),
- visible: $post->visible,
- toc: $post->toc,
- short_name: $post->shortName,
- saved: $saved,
- );
- }
-
- $page = pages::getPageByName($short_name);
- if ($page) {
- return $this->_get_pageEdit($page,
- title: $page->title,
- text: $page->md,
- saved: $saved,
- visible: $page->visible,
- );
- }
-
- throw new \NotFoundException();
- }
-
- public function post(): Response {
- list($short_name) = $this->input('short_name');
-
- $post = posts::getPostByName($short_name);
- if ($post) {
- csrf::check('editpost'.$post->id);
-
- list($text, $title, $tags, $visible, $toc, $short_name)
- = $this->input('text, title, tags, b:visible, b:toc, new_short_name');
-
- $tags = posts::splitStringToTags($tags);
- $error_code = null;
-
- if (!$title) {
- $error_code = 'no_title';
- } else if (!$text) {
- $error_code = 'no_text';
- } else if (empty($tags)) {
- $error_code = 'no_tags';
- } else if (empty($short_name)) {
- $error_code = 'no_short_name';
- }
-
- if ($error_code)
- $this->_get_postEdit($post,
- title: $title,
- text: $text,
- tags: $tags,
- visible: $visible,
- toc: $toc,
- short_name: $short_name,
- error_code: $error_code
- );
-
- $post->edit([
- 'title' => $title,
- 'md' => $text,
- 'visible' => (int)$visible,
- 'toc' => (int)$toc,
- 'short_name' => $short_name
- ]);
- $tag_ids = posts::getTagIds($tags);
- $post->setTagIds($tag_ids);
-
- return new \RedirectResponse($post->getUrl().'edit/?saved=1');
- }
-
- $page = pages::getPageByName($short_name);
- if ($page) {
- csrf::check('editpage'.$page->shortName);
-
- list($text, $title, $visible, $short_name)
- = $this->input('text, title, b:visible, new_short_name');
-
- $text = trim($text);
- $title = trim($title);
- $error_code = null;
-
- if (!$title) {
- $error_code = 'no_title';
- } else if (!$text) {
- $error_code = 'no_text';
- } else if (!$short_name) {
- $error_code = 'no_short_name';
- }
-
- if ($error_code) {
- return $this->_get_pageEdit($page,
- title: $title,
- text: $text,
- visible: $visible,
- error_code: $error_code
- );
- }
-
- $page->edit([
- 'title' => $title,
- 'md' => $text,
- 'visible' => (int)$visible,
- 'short_name' => $short_name,
- ]);
-
- return new \RedirectResponse($page->getUrl().'edit/?saved=1');
- }
-
- throw new \NotFoundException();
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/Index.php b/handler/admin/Index.php
deleted file mode 100644
index e829913..0000000
--- a/handler/admin/Index.php
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use Response;
-
-class Index extends AdminRequestHandler {
-
- public function get(): Response {
- return $this->skin->renderPage('admin/index');
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/Login.php b/handler/admin/Login.php
deleted file mode 100644
index cade137..0000000
--- a/handler/admin/Login.php
+++ /dev/null
@@ -1,31 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use admin;
-use csrf;
-use RedirectResponse;
-use Response;
-use UnauthorizedException;
-
-class Login extends AdminRequestHandler {
-
- public function get(): Response {
- if (admin::isAdmin())
- return new RedirectResponse('/admin/');
- return $this->skin->renderPage('admin/login');
- }
-
- public function post(): Response {
- csrf::check('adminlogin');
- $password = $_POST['password'] ?? '';
- $valid = admin::checkPassword($password);
- if ($valid) {
- admin::logAuth();
- admin::setCookie();
- return new RedirectResponse('/admin/');
- }
- throw new UnauthorizedException('nice try');
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/Logout.php b/handler/admin/Logout.php
deleted file mode 100644
index bb11e43..0000000
--- a/handler/admin/Logout.php
+++ /dev/null
@@ -1,17 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use admin;
-use csrf;
-use Response;
-
-class Logout extends AdminRequestHandler {
-
- public function get(): Response {
- csrf::check('logout');
- admin::unsetCookie();
- return new \RedirectResponse('/admin/login/');
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/MarkdownPreview.php b/handler/admin/MarkdownPreview.php
deleted file mode 100644
index e513709..0000000
--- a/handler/admin/MarkdownPreview.php
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use Response;
-
-class MarkdownPreview extends AdminRequestHandler {
-
- public function post(): Response {
- list($md, $title, $use_image_previews) = $this->input('md, title, b:use_image_previews');
-
- $html = \markup::markdownToHtml($md, $use_image_previews);
-
- $ctx = new \SkinContext('\\skin\\admin');
- $html = $ctx->markdownPreview(
- unsafe_html: $html,
- title: $title
- );
- return new \AjaxOkResponse(['html' => $html]);
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/PageAdd.php b/handler/admin/PageAdd.php
deleted file mode 100644
index 42a9911..0000000
--- a/handler/admin/PageAdd.php
+++ /dev/null
@@ -1,66 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use csrf;
-use NotFoundException;
-use pages;
-use RedirectResponse;
-use Response;
-
-class PageAdd extends AutoAddOrEdit {
-
- public function get(): Response {
- list($name) = $this->input('short_name');
- $page = pages::getPageByName($name);
- if ($page)
- throw new NotFoundException();
-
- return $this->_get_pageAdd($name);
- }
-
- public function post(): Response {
- csrf::check('addpage');
-
- list($name) = $this->input('short_name');
- $page = pages::getPageByName($name);
- if ($page)
- throw new NotFoundException();
-
- $text = trim($_POST['text'] ?? '');
- $title = trim($_POST['title'] ?? '');
- $error_code = null;
-
- if (!$title) {
- $error_code = 'no_title';
- } else if (!$text) {
- $error_code = 'no_text';
- }
-
- if ($error_code) {
- return $this->_get_pageAdd(
- name: $name,
- title: $title,
- text: $text,
- error_code: $error_code
- );
- }
-
- if (!pages::add([
- 'short_name' => $name,
- 'title' => $title,
- 'md' => $text
- ])) {
- return $this->_get_pageAdd(
- name: $name,
- title: $title,
- text: $text,
- error_code: 'db_err'
- );
- }
-
- $page = pages::getPageByName($name);
- return new RedirectResponse($page->getUrl());
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/PostAdd.php b/handler/admin/PostAdd.php
deleted file mode 100644
index c21a239..0000000
--- a/handler/admin/PostAdd.php
+++ /dev/null
@@ -1,68 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use csrf;
-use posts;
-use RedirectResponse;
-use Response;
-
-class PostAdd extends AutoAddOrEdit {
-
- public function get(): Response {
- return $this->_get_postAdd();
- }
-
- public function post(): Response {
- csrf::check('addpost');
-
- list($text, $title, $tags, $visible, $short_name)
- = $this->input('text, title, tags, b:visible, short_name');
- $tags = posts::splitStringToTags($tags);
-
- $error_code = null;
-
- if (!$title) {
- $error_code = 'no_title';
- } else if (!$text) {
- $error_code = 'no_text';
- } else if (empty($tags)) {
- $error_code = 'no_tags';
- } else if (empty($short_name)) {
- $error_code = 'no_short_name';
- }
-
- if ($error_code)
- return $this->_get_postAdd(
- text: $text,
- title: $title,
- tags: $tags,
- short_name: $short_name,
- error_code: $error_code
- );
-
- $id = posts::add([
- 'title' => $title,
- 'md' => $text,
- 'visible' => (int)$visible,
- 'short_name' => $short_name,
- ]);
-
- if (!$id)
- $this->_get_postAdd(
- text: $text,
- title: $title,
- tags: $tags,
- short_name: $short_name,
- error_code: 'db_err'
- );
-
- // set tags
- $post = posts::get($id);
- $tag_ids = posts::getTagIds($tags);
- $post->setTagIds($tag_ids);
-
- return new RedirectResponse($post->getUrl());
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/UploadDelete.php b/handler/admin/UploadDelete.php
deleted file mode 100644
index 26b58b7..0000000
--- a/handler/admin/UploadDelete.php
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use csrf;
-use RedirectResponse;
-use Response;
-
-class UploadDelete extends AdminRequestHandler {
-
- public function get(): Response {
- list($id) = $this->input('i:id');
-
- $upload = \uploads::get($id);
- if (!$upload)
- return new RedirectResponse('/uploads/?error='.urlencode('upload not found'));
-
- csrf::check('delupl'.$id);
-
- \uploads::delete($id);
-
- return new RedirectResponse('/uploads/');
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/UploadEditNote.php b/handler/admin/UploadEditNote.php
deleted file mode 100644
index e7cdbb2..0000000
--- a/handler/admin/UploadEditNote.php
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use csrf;
-use Response;
-
-class UploadEditNote extends AdminRequestHandler {
-
- public function post(): Response {
- list($id) = $this->input('i:id');
-
- $upload = \uploads::get($id);
- if (!$upload)
- return new \RedirectResponse('/uploads/?error='.urlencode('upload not found'));
-
- csrf::check('editupl'.$id);
-
- $note = $_POST['note'] ?? '';
- $upload->setNote($note);
-
- return new \RedirectResponse('/uploads/');
- }
-
-} \ No newline at end of file
diff --git a/handler/admin/Uploads.php b/handler/admin/Uploads.php
deleted file mode 100644
index 0cbb2f6..0000000
--- a/handler/admin/Uploads.php
+++ /dev/null
@@ -1,73 +0,0 @@
-<?php
-
-namespace handler\admin;
-
-use csrf;
-use RedirectResponse;
-use Response;
-
-// So it's 2022 outside, and it's PHP 8.1 already, which is actually so cool comparing to 5.x and even 7.4, but...
-// ...class names are still case-insensitive?!! And I can't import \uploads because it's the same as Uploads?!!
-//
-// PHP, what the fuck is wrong with you?!
-
-class Uploads extends AdminRequestHandler {
-
- public function get(): Response {
- list($error) = $this->input('error');
- $uploads = \uploads::getAll();
-
- $this->skin->title = ($this->lang)('blog_upload');
- return $this->skin->renderPage('admin/uploads',
- error: $error,
- uploads: $uploads);
- }
-
- public function post(): Response {
- csrf::check('addupl');
-
- list($custom_name, $note) = $this->input('name, note');
-
- if (!isset($_FILES['files']))
- return new RedirectResponse('/uploads/?error='.urlencode('no file'));
-
- $files = [];
- for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
- $files[] = [
- 'name' => $_FILES['files']['name'][$i],
- 'type' => $_FILES['files']['type'][$i],
- 'tmp_name' => $_FILES['files']['tmp_name'][$i],
- 'error' => $_FILES['files']['error'][$i],
- 'size' => $_FILES['files']['size'][$i],
- ];
- }
-
- if (count($files) > 1) {
- $note = '';
- $custom_name = '';
- }
-
- foreach ($files as $f) {
- if ($f['error'])
- return new RedirectResponse('/uploads/?error='.urlencode('error code '.$f['error']));
-
- if (!$f['size'])
- return new RedirectResponse('/uploads/?error='.urlencode('received empty file'));
-
- $ext = extension($f['name']);
- if (!\uploads::isExtensionAllowed($ext))
- return new RedirectResponse('/uploads/?error='.urlencode('extension not allowed'));
-
- $upload_id = \uploads::add(
- $f['tmp_name'],
- $custom_name ?: $f['name'],
- $note);
-
- if (!$upload_id)
- return new RedirectResponse('/uploads/?error='.urlencode('failed to create upload'));
- }
-
- return new RedirectResponse('/uploads/');
- }
-
-} \ No newline at end of file