From c0dc531ebefd8912819f3b6c8bda1fed3c7e750c Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 Jan 2024 06:11:00 +0300 Subject: make it simple, but not simpler --- handler/admin/AdminRequestHandler.php | 20 ------ handler/admin/AutoAddOrEdit.php | 99 -------------------------- handler/admin/AutoDelete.php | 34 --------- handler/admin/AutoEdit.php | 130 ---------------------------------- handler/admin/Index.php | 13 ---- handler/admin/Login.php | 31 -------- handler/admin/Logout.php | 17 ----- handler/admin/MarkdownPreview.php | 22 ------ handler/admin/PageAdd.php | 66 ----------------- handler/admin/PostAdd.php | 68 ------------------ handler/admin/UploadDelete.php | 25 ------- handler/admin/UploadEditNote.php | 25 ------- handler/admin/Uploads.php | 73 ------------------- 13 files changed, 623 deletions(-) delete mode 100644 handler/admin/AdminRequestHandler.php delete mode 100644 handler/admin/AutoAddOrEdit.php delete mode 100644 handler/admin/AutoDelete.php delete mode 100644 handler/admin/AutoEdit.php delete mode 100644 handler/admin/Index.php delete mode 100644 handler/admin/Login.php delete mode 100644 handler/admin/Logout.php delete mode 100644 handler/admin/MarkdownPreview.php delete mode 100644 handler/admin/PageAdd.php delete mode 100644 handler/admin/PostAdd.php delete mode 100644 handler/admin/UploadDelete.php delete mode 100644 handler/admin/UploadEditNote.php delete mode 100644 handler/admin/Uploads.php (limited to 'handler/admin') 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -_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 @@ -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 @@ -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 @@ -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 -- cgit v1.2.3