aboutsummaryrefslogtreecommitdiff
path: root/handler
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
parent48d688cdf7f9eae1bf11b8a6f0e5b98687c604cb (diff)
make it simple, but not simpler
Diffstat (limited to 'handler')
-rw-r--r--handler/AdminHandler.php470
-rw-r--r--handler/Auto.php110
-rw-r--r--handler/Contacts.php16
-rw-r--r--handler/Index.php20
-rw-r--r--handler/MainHandler.php147
-rw-r--r--handler/ProjectsHtml.php11
-rw-r--r--handler/RSS.php32
-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
20 files changed, 617 insertions, 812 deletions
diff --git a/handler/AdminHandler.php b/handler/AdminHandler.php
new file mode 100644
index 0000000..f90fc42
--- /dev/null
+++ b/handler/AdminHandler.php
@@ -0,0 +1,470 @@
+<?php
+
+class AdminHandler extends request_handler {
+
+ function __construct() {
+ parent::__construct();
+ add_static('css/admin.css', 'js/admin.js');
+ }
+
+ function before_dispatch(string $http_method, string $action) {
+ if ($action != 'login' && !is_admin())
+ forbidden();
+ }
+
+ function GET_index() {
+ set_title('$admin_title');
+ render('admin/index');
+ }
+
+ function GET_login() {
+ if (is_admin())
+ redirect('/admin/');
+ set_title('$admin_title');
+ render('admin/login');
+ }
+
+ function POST_login() {
+ csrf_check('adminlogin');
+ $password = $_POST['password'] ?? '';
+ $valid = admin_check_password($password);
+ if ($valid) {
+ admin_log_auth();
+ admin_set_cookie();
+ redirect('/admin/');
+ }
+ forbidden();
+ }
+
+ function GET_logout() {
+ csrf_check('logout');
+ admin_unset_cookie();
+ redirect('/admin/login/', HTTPCode::Found);
+ }
+
+ function GET_uploads() {
+ list($error) = input('error');
+ $uploads = uploads::getAllUploads();
+
+ set_title('$blog_upload');
+ render('admin/uploads',
+ error: $error,
+ uploads: $uploads);
+ }
+
+ function POST_uploads() {
+ csrf_check('addupl');
+
+ list($custom_name, $note) = input('name, note');
+
+ if (!isset($_FILES['files']))
+ redirect('/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'])
+ redirect('/uploads/?error='.urlencode('error code '.$f['error']));
+
+ if (!$f['size'])
+ redirect('/uploads/?error='.urlencode('received empty file'));
+
+ $ext = extension($f['name']);
+ if (!uploads::isExtensionAllowed($ext))
+ redirect('/uploads/?error='.urlencode('extension not allowed'));
+
+ $upload_id = uploads::add(
+ $f['tmp_name'],
+ $custom_name ?: $f['name'],
+ $note);
+
+ if (!$upload_id)
+ redirect('/uploads/?error='.urlencode('failed to create upload'));
+ }
+
+ redirect('/uploads/');
+ }
+
+ function GET_upload_delete() {
+ list($id) = input('i:id');
+ $upload = uploads::get($id);
+ if (!$upload)
+ redirect('/uploads/?error='.urlencode('upload not found'));
+ csrf_check('delupl'.$id);
+ uploads::delete($id);
+ redirect('/uploads/');
+ }
+
+ function POST_upload_edit_note() {
+ list($id, $note) = input('i:id, note');
+
+ $upload = uploads::get($id);
+ if (!$upload)
+ redirect('/uploads/?error='.urlencode('upload not found'));
+
+ csrf_check('editupl'.$id);
+
+ $upload->setNote($note);
+ redirect('/uploads/');
+ }
+
+ function POST_ajax_md_preview() {
+ list($md, $title, $use_image_previews) = 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
+ );
+ ajax_ok(['html' => $html]);
+ }
+
+ function GET_page_add() {
+ list($name) = input('short_name');
+ $page = pages::getByName($name);
+ if ($page)
+ not_found();
+ return $this->_get_pageAdd($name);
+ }
+
+ function POST_page_add() {
+ csrf_check('addpage');
+
+ list($name, $text, $title) = input('short_name, text, title');
+ $page = pages::getByName($name);
+ if ($page)
+ not_found();
+
+ $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::getByName($name);
+ redirect($page->getUrl());
+ }
+
+ function GET_post_add() {
+ return $this->_get_postAdd();
+ }
+
+ function POST_post_add() {
+ csrf_check('addpost');
+
+ list($text, $title, $tags, $visible, $short_name)
+ = input('text, title, tags, b:visible, short_name');
+ $tags = tags::splitString($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(
+ title: $title,
+ text: $text,
+ 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(
+ title: $title,
+ text: $text,
+ tags: $tags,
+ short_name: $short_name,
+ error_code: 'db_err'
+ );
+
+ // set tags
+ $post = posts::get($id);
+ $tag_ids = array_values(tags::getTags($tags));
+ $post->setTagIds($tag_ids);
+
+ redirect($post->getUrl());
+ }
+
+ function GET_auto_delete() {
+ list($name) = input('short_name');
+
+ $post = posts::getByName($name);
+ if ($post) {
+ csrf_check('delpost'.$post->id);
+ posts::delete($post);
+ redirect('/');
+ }
+
+ $page = pages::getByName($name);
+ if ($page) {
+ csrf_check('delpage'.$page->shortName);
+ pages::delete($page);
+ redirect('/');
+ }
+
+ not_found();
+ }
+
+ function GET_auto_edit() {
+ list($short_name, $saved) = input('short_name, b:saved');
+
+ $post = posts::getByName($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::getByName($short_name);
+ if ($page) {
+ return $this->_get_pageEdit($page,
+ title: $page->title,
+ text: $page->md,
+ saved: $saved,
+ visible: $page->visible,
+ );
+ }
+
+ not_found();
+ }
+
+ function POST_auto_edit() {
+ list($short_name) = input('short_name');
+
+ $post = posts::getByName($short_name);
+ if ($post) {
+ csrf_check('editpost'.$post->id);
+
+ list($text, $title, $tags, $visible, $toc, $short_name)
+ = input('text, title, tags, b:visible, b:toc, new_short_name');
+
+ $tags = tags::splitString($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 = array_values(tags::getTags($tags));
+ $post->setTagIds($tag_ids);
+
+ redirect($post->getUrl().'edit/?saved=1');
+ }
+
+ $page = pages::getByName($short_name);
+ if ($page) {
+ csrf_check('editpage'.$page->shortName);
+
+ list($text, $title, $visible, $short_name)
+ = 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,
+ ]);
+
+ redirect($page->getUrl().'edit/?saved=1');
+ }
+
+ not_found();
+ }
+
+ protected static function setWidePage() {
+ set_skin_opts([
+ 'full_width' => true,
+ 'no_footer' => true
+ ]);
+ }
+
+ protected function _get_pageAdd(
+ string $name,
+ string $title = '',
+ string $text = '',
+ ?string $error_code = null
+ ) {
+ add_skin_strings_re('/^(err_)?pages_/');
+ set_title(lang('pages_create_title', $name));
+ static::setWidePage();
+ render('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
+ ) {
+ add_skin_strings_re('/^(err_)?pages_/');
+ set_title(lang('pages_page_edit_title', $page->shortName.'.html'));
+ static::setWidePage();
+ render('admin/pageForm',
+ is_edit: true,
+ short_name: $page->shortName,
+ title: $title,
+ text: $text,
+ visible: $visible,
+ saved: $saved,
+ 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,
+ ) {
+ add_skin_strings_re('/^(err_)?blog_/');
+ set_title(lang('blog_post_edit_title', $post->title));
+ static::setWidePage();
+ render('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_postAdd(
+ string $title = '',
+ string $text = '',
+ ?array $tags = null,
+ string $short_name = '',
+ ?string $error_code = null
+ ) {
+ add_skin_strings_re('/^(err_)?blog_/');
+ set_title('$blog_write');
+ static::setWidePage();
+ render('admin/postForm',
+ title: $title,
+ text: $text,
+ tags: $tags ? implode(', ', $tags) : '',
+ short_name: $short_name,
+ error_code: $error_code);
+ }
+
+} \ No newline at end of file
diff --git a/handler/Auto.php b/handler/Auto.php
deleted file mode 100644
index 0656c44..0000000
--- a/handler/Auto.php
+++ /dev/null
@@ -1,110 +0,0 @@
-<?php
-
-namespace handler;
-
-use admin;
-use NotFoundException;
-use pages;
-use Post;
-use posts;
-use RedirectResponse;
-use RequestHandler;
-use Response;
-use Tag;
-
-class Auto extends RequestHandler {
-
- public function get(): Response {
- list($name) = $this->input('name');
- if ($name == 'coreboot-mba51-flashing')
- return new RedirectResponse('/coreboot-mba52-flashing/', 301);
-
- if (is_numeric($name)) {
- $post = posts::get((int)$name);
- } else {
- $post = posts::getPostByName($name);
- }
- if ($post)
- return $this->getPost($post);
-
- $tag = posts::getTag($name);
- if ($tag)
- return $this->getTag($tag);
-
- $page = pages::getPageByName($name);
- if ($page)
- return $this->getPage($page);
-
- if (admin::isAdmin()) {
- $this->skin->title = $name;
- return $this->skin->renderPage('admin/pageNew',
- short_name: $name);
- }
-
- throw new NotFoundException();
- }
-
- public function getPost(Post $post): Response {
- global $config;
-
- if (!$post->visible && !admin::isAdmin())
- throw new NotFoundException();
-
- $tags = $post->getTags();
-
- $s = $this->skin;
- $s->meta[] = ['property' => 'og:title', 'content' => $post->title];
- $s->meta[] = ['property' => 'og:url', 'content' => fullURL($post->getUrl())];
- if (($img = $post->getFirstImage()) !== null)
- $s->meta[] = ['property' => 'og:image', 'content' => $img->getDirectUrl()];
- $s->meta[] = [
- 'name' => 'description',
- 'property' => 'og:description',
- 'content' => $post->getDescriptionPreview(155)
- ];
-
- $s->title = $post->title;
-
- if ($post->toc)
- $s->setOptions(['wide' => true]);
-
- return $s->renderPage('main/post',
- title: $post->title,
- id: $post->id,
- unsafe_html: $post->getHtml($this->isRetina(), \themes::getUserTheme()),
- unsafe_toc_html: $post->getToc(),
- date: $post->getFullDate(),
- tags: $tags,
- visible: $post->visible,
- url: $post->getUrl(),
- email: $config['admin_email'],
- urlencoded_reply_subject: 'Re: '.$post->title);
- }
-
- public function getTag(Tag $tag): Response {
- $tag = posts::getTag($tag);
- if (!admin::isAdmin() && !$tag->visiblePostsCount)
- throw new NotFoundException();
-
- $count = posts::getPostsCountByTagId($tag->id, admin::isAdmin());
- $posts = $count ? posts::getPostsByTagId($tag->id, admin::isAdmin()) : [];
-
- $this->skin->title = '#'.$tag->tag;
- return $this->skin->renderPage('main/tag',
- count: $count,
- posts: $posts,
- tag: $tag->tag);
- }
-
- public function getPage(\Page $page): Response {
- if (!admin::isAdmin() && !$page->visible)
- throw new NotFoundException();
-
- $this->skin->title = $page ? $page->title : '???';
- return $this->skin->renderPage('main/page',
- unsafe_html: $page->getHtml($this->isRetina(), \themes::getUserTheme()),
- page_url: $page->getUrl(),
- short_name: $page->shortName);
- }
-
-} \ No newline at end of file
diff --git a/handler/Contacts.php b/handler/Contacts.php
deleted file mode 100644
index c60479d..0000000
--- a/handler/Contacts.php
+++ /dev/null
@@ -1,16 +0,0 @@
-<?php
-
-namespace handler;
-
-use Response;
-
-class Contacts extends \RequestHandler {
-
- public function get(): Response {
- global $config;
- $this->skin->title = $this->lang['contacts'];
- return $this->skin->renderPage('main/contacts',
- email: $config['admin_email']);
- }
-
-} \ No newline at end of file
diff --git a/handler/Index.php b/handler/Index.php
deleted file mode 100644
index c852511..0000000
--- a/handler/Index.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-namespace handler;
-
-use admin;
-use posts;
-
-class Index extends \RequestHandler {
-
- public function get(): \Response {
- $posts = posts::getPosts(include_hidden: admin::isAdmin());
- $tags = posts::getAllTags(include_hidden: admin::isAdmin());
-
- $this->skin->title = "ch1p's Blog";
- $this->skin->setOptions(['dynlogo_enabled' => false]);
- return $this->skin->renderPage('main/index',
- posts: $posts,
- tags: $tags);
- }
-} \ No newline at end of file
diff --git a/handler/MainHandler.php b/handler/MainHandler.php
new file mode 100644
index 0000000..889cb48
--- /dev/null
+++ b/handler/MainHandler.php
@@ -0,0 +1,147 @@
+<?php
+
+require_once 'lib/posts.php';
+require_once 'lib/themes.php';
+
+class MainHandler extends request_handler {
+
+ function GET_index() {
+ $posts = posts::getList(include_hidden: is_admin());
+ $tags = tags::getAll(include_hidden: is_admin());
+
+ set_title("ch1p's Blog");
+ set_skin_opts(['dynlogo_enabled' => false]);
+ render('main/index',
+ posts: $posts,
+ tags: $tags);
+ }
+
+ function GET_projects() {
+ redirect('/projects/');
+ }
+
+ function GET_contacts() {
+ global $config;
+ set_title(lang('contacts'));
+ render('main/contacts',
+ email: $config['admin_email']);
+ }
+
+ function GET_auto() {
+ list($name) = input('name');
+ if ($name == 'coreboot-mba51-flashing')
+ redirect('/coreboot-mba52-flashing/');
+
+ if (is_numeric($name)) {
+ $post = posts::get((int)$name);
+ } else {
+ $post = posts::getByName($name);
+ }
+ if ($post)
+ return $this->renderPost($post);
+
+ $tag = tags::get($name);
+ if ($tag)
+ return $this->renderTag($tag);
+
+ $page = pages::getByName($name);
+ if ($page)
+ return $this->renderPage($page);
+
+ if (is_admin()) {
+ set_title($name);
+ render('admin/pageNew',
+ short_name: $name);
+ }
+
+ not_found();
+ }
+
+ protected function renderPost(Post $post) {
+ global $config;
+
+ if (!$post->visible && !is_admin())
+ not_found();
+
+ $tags = $post->getTags();
+
+ add_meta(
+ ['property' => 'og:title', 'content' => $post->title],
+ ['property' => 'og:url', 'content' => $config['domain'].$post->getUrl()]
+ );
+ if (($img = $post->getFirstImage()) !== null)
+ add_meta(['property' => 'og:image', 'content' => $img->getDirectUrl()]);
+
+ add_meta([
+ 'name' => 'description',
+ 'property' => 'og:description',
+ 'content' => $post->getDescriptionPreview(155)
+ ]);
+
+ set_title($post->title);
+
+ if ($post->toc)
+ set_skin_opts(['wide' => true]);
+
+ render('main/post',
+ title: $post->title,
+ id: $post->id,
+ unsafe_html: $post->getHtml(is_retina(), getUserTheme()),
+ unsafe_toc_html: $post->getToc(),
+ date: $post->getFullDate(),
+ tags: $tags,
+ visible: $post->visible,
+ url: $post->getUrl(),
+ email: $config['admin_email'],
+ urlencoded_reply_subject: 'Re: '.$post->title);
+ }
+
+ protected function renderTag(Tag $tag) {
+ $tag = tags::get($tag);
+ if (!is_admin() && !$tag->visiblePostsCount)
+ not_found();
+
+ $count = posts::getCountByTagId($tag->id, is_admin());
+ $posts = $count ? posts::getPostsByTagId($tag->id, is_admin()) : [];
+
+ set_title('#'.$tag->tag);
+ render('main/tag',
+ count: $count,
+ posts: $posts,
+ tag: $tag->tag);
+ }
+
+ protected function renderPage(Page $page) {
+ if (!is_admin() && !$page->visible)
+ not_found();
+
+ set_title($page ? $page->title : '???');
+ render('main/page',
+ unsafe_html: $page->getHtml(is_retina(), getUserTheme()),
+ page_url: $page->getUrl(),
+ short_name: $page->shortName);
+ }
+
+ function GET_rss() {
+ global $config;
+
+ $items = array_map(fn(Post $post) => [
+ 'title' => $post->title,
+ 'link' => $post->getUrl(),
+ 'pub_date' => date(DATE_RSS, $post->ts),
+ 'description' => $post->getDescriptionPreview(500),
+ ], posts::getList(0, 20));
+
+ $ctx = new SkinContext('\\skin\\rss');
+ $body = $ctx->atom(
+ title: lang('site_title'),
+ link: 'https://'.$config['domain'],
+ rss_link: 'https://'.$config['domain'].'/feed.rss',
+ items: $items);
+
+ header('Content-Type: application/rss+xml; charset=utf-8');
+ echo $body;
+ exit;
+ }
+
+} \ No newline at end of file
diff --git a/handler/ProjectsHtml.php b/handler/ProjectsHtml.php
deleted file mode 100644
index 0c1dcbb..0000000
--- a/handler/ProjectsHtml.php
+++ /dev/null
@@ -1,11 +0,0 @@
-<?php
-
-namespace handler\main;
-
-class ProjectsHtml extends \RequestHandler {
-
- public function get(): \Response {
- return new \RedirectResponse('/projects/', 301);
- }
-
-} \ No newline at end of file
diff --git a/handler/RSS.php b/handler/RSS.php
deleted file mode 100644
index 08a2136..0000000
--- a/handler/RSS.php
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-
-namespace handler;
-use posts;
-use Response;
-use SkinContext;
-
-class RSS extends \RequestHandler {
-
- public function get(): Response {
- global $config;
-
- $items = array_map(fn(\Post $post) => [
- 'title' => $post->title,
- 'link' => $post->getUrl(),
- 'pub_date' => date(DATE_RSS, $post->ts),
- 'description' => $post->getDescriptionPreview(500),
- ], posts::getPosts(0, 20));
-
- $ctx = new SkinContext('\\skin\\rss');
- $body = $ctx->atom(
- title: ($this->lang)('site_title'),
- link: 'https://'.$config['domain'],
- rss_link: 'https://'.$config['domain'].'/feed.rss',
- items: $items);
-
- $response = new Response(200, $body);
- $response->addHeader('Content-Type: application/rss+xml; charset=utf-8');
- return $response;
- }
-
-} \ No newline at end of file
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