From 26a1a97082d53f45fb17941e11ff8d711e8edfd3 Mon Sep 17 00:00:00 2001 From: Danang Probo Sayekti Date: Fri, 14 Aug 2015 01:58:08 +0700 Subject: [PATCH] Permalink without year-month --- config/config.ini.example | 3 + system/admin/admin.php | 38 +++- system/admin/views/edit-audio.html.php | 8 +- system/admin/views/edit-image.html.php | 8 +- system/admin/views/edit-link.html.php | 7 +- system/admin/views/edit-post.html.php | 7 +- system/admin/views/edit-quote.html.php | 7 +- system/admin/views/edit-video.html.php | 7 +- system/htmly.php | 352 ++++++++++++++++++++++++++++++++- system/includes/functions.php | 75 ++++++- 10 files changed, 491 insertions(+), 21 deletions(-) diff --git a/config/config.ini.example b/config/config.ini.example index 3289e07..acfa01c 100644 --- a/config/config.ini.example +++ b/config/config.ini.example @@ -10,6 +10,9 @@ blog.tagline = "Just another HTMLy blog" blog.description = "Proudly powered by HTMLy, a databaseless blogging platform." blog.copyright = "(c) Your name." +; Set permalink type. "default" using /year/month/title. "post" using /post/title +permalink.type = "default" + ; Show the /blog url as the blog homepage blog.enable = "false" diff --git a/system/admin/admin.php b/system/admin/admin.php index 1a5ea3f..58c1a3a 100644 --- a/system/admin/admin.php +++ b/system/admin/admin.php @@ -174,9 +174,13 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null, // The post date $postdate = strtotime($timestamp); - + // The post URL - $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + if (config('permalink.type') == 'post') { + $posturl = site_url() . 'post/' . $post_url; + } else { + $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + } save_tag_i18n($post_tag, $post_tagmd); @@ -300,7 +304,11 @@ function edit_image($title, $tag, $url, $content, $oldfile, $destination = null, $postdate = strtotime($timestamp); // The post URL - $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + if (config('permalink.type') == 'post') { + $posturl = site_url() . 'post/' . $post_url; + } else { + $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + } save_tag_i18n($post_tag, $post_tagmd); @@ -424,7 +432,11 @@ function edit_video($title, $tag, $url, $content, $oldfile, $destination = null, $postdate = strtotime($timestamp); // The post URL - $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + if (config('permalink.type') == 'post') { + $posturl = site_url() . 'post/' . $post_url; + } else { + $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + } save_tag_i18n($post_tag, $post_tagmd); @@ -548,7 +560,11 @@ function edit_link($title, $tag, $url, $content, $oldfile, $destination = null, $postdate = strtotime($timestamp); // The post URL - $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + if (config('permalink.type') == 'post') { + $posturl = site_url() . 'post/' . $post_url; + } else { + $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + } save_tag_i18n($post_tag, $post_tagmd); @@ -672,7 +688,11 @@ function edit_quote($title, $tag, $url, $content, $oldfile, $destination = null, $postdate = strtotime($timestamp); // The post URL - $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + if (config('permalink.type') == 'post') { + $posturl = site_url() . 'post/' . $post_url; + } else { + $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + } save_tag_i18n($post_tag, $post_tagmd); @@ -796,7 +816,11 @@ function edit_audio($title, $tag, $url, $content, $oldfile, $destination = null, $postdate = strtotime($timestamp); // The post URL - $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + if (config('permalink.type') == 'post') { + $posturl = site_url() . 'post/' . $post_url; + } else { + $posturl = site_url() . date('Y/m', $postdate) . '/' . $post_url; + } save_tag_i18n($post_tag, $post_tagmd); diff --git a/system/admin/views/edit-audio.html.php b/system/admin/views/edit-audio.html.php index 220848b..27061c8 100644 --- a/system/admin/views/edit-audio.html.php +++ b/system/admin/views/edit-audio.html.php @@ -34,9 +34,13 @@ $time = new DateTime($t); $timestamp = $time->format("Y-m-d H:i:s"); // The post date $postdate = strtotime($timestamp); -// The post URL -$delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +if (config('permalink.type') == 'post') { + $delete = site_url() . 'post/' . $oldmd . '/delete?destination=' . $destination; +} else { + // The post URL + $delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +} ?> diff --git a/system/admin/views/edit-image.html.php b/system/admin/views/edit-image.html.php index 6c7836a..96f98af 100644 --- a/system/admin/views/edit-image.html.php +++ b/system/admin/views/edit-image.html.php @@ -35,8 +35,12 @@ $timestamp = $time->format("Y-m-d H:i:s"); // The post date $postdate = strtotime($timestamp); // The post URL -$delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; - +if (config('permalink.type') == 'post') { + $delete = site_url() . 'post/' . $oldmd . '/delete?destination=' . $destination; +} else { + // The post URL + $delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +} ?> diff --git a/system/admin/views/edit-link.html.php b/system/admin/views/edit-link.html.php index af06c09..1ddd4b7 100644 --- a/system/admin/views/edit-link.html.php +++ b/system/admin/views/edit-link.html.php @@ -35,7 +35,12 @@ $timestamp = $time->format("Y-m-d H:i:s"); // The post date $postdate = strtotime($timestamp); // The post URL -$delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +if (config('permalink.type') == 'post') { + $delete = site_url() . 'post/' . $oldmd . '/delete?destination=' . $destination; +} else { + // The post URL + $delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +} ?> diff --git a/system/admin/views/edit-post.html.php b/system/admin/views/edit-post.html.php index 48f2e61..9bfed1a 100644 --- a/system/admin/views/edit-post.html.php +++ b/system/admin/views/edit-post.html.php @@ -34,7 +34,12 @@ $timestamp = $time->format("Y-m-d H:i:s"); // The post date $postdate = strtotime($timestamp); // The post URL -$delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +if (config('permalink.type') == 'post') { + $delete = site_url() . 'post/' . $oldmd . '/delete?destination=' . $destination; +} else { + // The post URL + $delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +} ?> diff --git a/system/admin/views/edit-quote.html.php b/system/admin/views/edit-quote.html.php index 7609f29..e47b8b9 100644 --- a/system/admin/views/edit-quote.html.php +++ b/system/admin/views/edit-quote.html.php @@ -35,7 +35,12 @@ $timestamp = $time->format("Y-m-d H:i:s"); // The post date $postdate = strtotime($timestamp); // The post URL -$delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +if (config('permalink.type') == 'post') { + $delete = site_url() . 'post/' . $oldmd . '/delete?destination=' . $destination; +} else { + // The post URL + $delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +} ?> diff --git a/system/admin/views/edit-video.html.php b/system/admin/views/edit-video.html.php index 15dcb0f..72f9709 100644 --- a/system/admin/views/edit-video.html.php +++ b/system/admin/views/edit-video.html.php @@ -36,7 +36,12 @@ $timestamp = $time->format("Y-m-d H:i:s"); // The post date $postdate = strtotime($timestamp); // The post URL -$delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +if (config('permalink.type') == 'post') { + $delete = site_url() . 'post/' . $oldmd . '/delete?destination=' . $destination; +} else { + // The post URL + $delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; +} ?> diff --git a/system/htmly.php b/system/htmly.php index a49f224..4160945 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -1353,6 +1353,351 @@ get('/feed/opml', function () { echo generate_opml(); }); +// Show blog post without year-month +get('/post/:name', function ($name) { + + if (config('permalink.type') != 'post') { + $post = find_post(null, null, $name); + $current = $post['current']; + $redir = site_url() . date('Y/m', $current->date) . '/' . $name; + header("location: $redir", TRUE, 301); + } + + if (config("views.counter") != "true") { + if (!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + } + + $post = find_post(null, null, $name); + + $current = $post['current']; + + if (!$current) { + not_found(); + } + + if (config("views.counter") == "true") { + add_view($current->file); + + if (!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + } + + $author = get_author($current->author); + + if (isset($author[0])) { + $author = $author[0]; + } else { + $author = default_profile($current->author); + } + + if (array_key_exists('prev', $post)) { + $prev = $post['prev']; + } else { + $prev = array(); + } + + if (array_key_exists('next', $post)) { + $next = $post['next']; + } else { + $next = array(); + } + + if (isset($current->image)) { + $var = 'imagePost'; + } elseif (isset($current->link)) { + $var = 'linkPost'; + } elseif (isset($current->quote)) { + $var = 'quotePost'; + } elseif (isset($current->audio)) { + $var = 'audioPost'; + } elseif (isset($current->video)) { + $var = 'videoPost'; } + else { + $var = 'blogPost'; + } + + if (config('blog.enable') === 'true') { + $blog = ' Blog » '; + } else { + $blog = ''; + } + + render('post', array( + 'title' => $current->title . ' - ' . blog_title(), + 'description' => $current->description, + 'canonical' => $current->url, + 'p' => $current, + 'author' => $author, + 'bodyclass' => 'inpost', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » '. $blog . $current->tagb . ' » ' . $current->title, + 'prev' => has_prev($prev), + 'next' => has_next($next), + 'type' => $var, + 'is_post' => is_post(true), + )); + +}); + +// Edit blog post +get('/post/:name/edit', function ($name) { + + if (login()) { + + $user = $_SESSION[config("site.url")]['user']; + $role = user('role', $user); + + config('views.root', 'system/admin/views'); + $post = find_post(null, null, $name); + + if (!$post) { + $post = find_draft(null, null, $name); + if (!$post) { + not_found(); + } + } + + $current = $post['current']; + + if (isset($current->image)) { + $var = 'edit-image'; + } elseif (isset($current->link)) { + $var = 'edit-link'; + } elseif (isset($current->quote)) { + $var = 'edit-quote'; + } elseif (isset($current->audio)) { + $var = 'edit-audio'; + } elseif (isset($current->video)) { + $var = 'edit-video'; + } else { + $var = 'edit-post'; + } + + if ($user === $current->author || $role === 'admin') { + render($var, array( + 'title' => $var .' '. blog_title(), + 'description' => blog_description(), + 'canonical' => site_url(), + 'p' => $current, + 'bodyclass' => 'editcontent', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->tagb . ' » ' . $current->title + )); + } else { + render('denied', array( + 'title' => $var .' '. blog_title(), + 'description' => blog_description(), + 'canonical' => site_url(), + 'p' => $current, + 'bodyclass' => 'denied', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->tagb . ' » ' . $current->title + )); + } + } else { + $login = site_url() . 'login'; + header("location: $login"); + } +}); + +// Get edited data from blog post +post('/post/:name/edit', function () { + + $proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); + + $title = from($_REQUEST, 'title'); + $is_post = from($_REQUEST, 'is_post'); + $image = from($_REQUEST, 'image'); + $is_image = from($_REQUEST, 'is_image'); + $video = from($_REQUEST, 'video'); + $is_video = from($_REQUEST, 'is_video'); + $link = from($_REQUEST, 'link'); + $is_link = from($_REQUEST, 'is_link'); + $audio = from($_REQUEST, 'audio'); + $is_audio = from($_REQUEST, 'is_audio'); + $quote = from($_REQUEST, 'quote'); + $is_quote = from($_REQUEST, 'is_quote'); + $tag = from($_REQUEST, 'tag'); + $url = from($_REQUEST, 'url'); + $content = from($_REQUEST, 'content'); + $oldfile = from($_REQUEST, 'oldfile'); + $destination = from($_GET, 'destination'); + $description = from($_REQUEST, 'description'); + $date = from($_REQUEST, 'date'); + $time = from($_REQUEST, 'time'); + $dateTime = null; + $revertPost = from($_REQUEST, 'revertpost'); + $publishDraft = from($_REQUEST, 'publishdraft'); + if ($date !== null && $time !== null) { + $dateTime = $date . ' ' . $time; + } + + if (!empty($is_image)) { + $var = 'edit-image'; + } elseif (!empty($is_video)) { + $var = 'edit-video'; + } elseif (!empty($is_link)) { + $var = 'edit-link'; + } elseif (!empty($is_quote)) { + $var = 'edit-quote'; + } elseif (!empty($is_audio)) { + $var = 'edit-audio'; + } elseif (!empty($is_post)) { + $var = 'edit-post'; + } + + if ($proper && !empty($title) && !empty($tag) && !empty($content) && !empty($image)) { + if (empty($url)) { + $url = $title; + } + edit_image($title, $tag, $url, $content, $oldfile, $destination, $description, $dateTime, $image, $revertPost, $publishDraft); + + } else if ($proper && !empty($title) && !empty($tag) && !empty($content) && !empty($video)) { + if (empty($url)) { + $url = $title; + } + edit_video($title, $tag, $url, $content, $oldfile, $destination, $description, $dateTime, $video, $revertPost, $publishDraft); + + } else if ($proper && !empty($title) && !empty($tag) && !empty($content) && !empty($link)) { + if (empty($url)) { + $url = $title; + } + edit_link($title, $tag, $url, $content, $oldfile, $destination, $description, $dateTime, $link, $revertPost, $publishDraft); + + } else if ($proper && !empty($title) && !empty($tag) && !empty($content) && !empty($quote)) { + if (empty($url)) { + $url = $title; + } + edit_quote($title, $tag, $url, $content, $oldfile, $destination, $description, $dateTime, $quote, $revertPost, $publishDraft); + + } else if ($proper && !empty($title) && !empty($tag) && !empty($content) && !empty($audio)) { + if (empty($url)) { + $url = $title; + } + edit_audio($title, $tag, $url, $content, $oldfile, $destination, $description, $dateTime, $audio, $revertPost, $publishDraft); + + } else if ($proper && !empty($title) && !empty($tag) && !empty($content) && !empty($is_post)) { + if (empty($url)) { + $url = $title; + } + edit_post($title, $tag, $url, $content, $oldfile, $destination, $description, $dateTime, $revertPost, $publishDraft); + + } else { + $message['error'] = ''; + if (empty($title)) { + $message['error'] .= '
  • Title field is required.
  • '; + } + if (empty($tag)) { + $message['error'] .= '
  • Tag field is required.
  • '; + } + if (empty($content)) { + $message['error'] .= '
  • Content field is required.
  • '; + } + if (!$proper) { + $message['error'] .= '
  • CSRF Token not correct.
  • '; + } + + if (!empty($is_image)) { + if (empty($image)) { + $message['error'] .= '
  • Image field is required.
  • '; + } + } elseif (!empty($is_video)) { + if (empty($video)) { + $message['error'] .= '
  • Video field is required.
  • '; + } + } elseif (!empty($is_link)) { + if (empty($link)) { + $message['error'] .= '
  • Link field is required.
  • '; + } + } elseif (!empty($is_quote)) { + if (empty($quote)) { + $message['error'] .= '
  • Quote field is required.
  • '; + } + } elseif (!empty($is_audio)) { + if (empty($audio)) { + $message['error'] .= '
  • Audio field is required.
  • '; + } + } + + config('views.root', 'system/admin/views'); + + render($var, array( + 'title' => 'Edit content - ' . blog_title(), + 'description' => blog_description(), + 'canonical' => site_url(), + 'error' => '', + 'oldfile' => $oldfile, + 'postTitle' => $title, + 'postImage' => $image, + 'postVideo' => $video, + 'postLink' => $link, + 'postQuote' => $quote, + 'postAudio' => $audio, + 'postTag' => $tag, + 'postUrl' => $url, + 'postContent' => $content, + 'bodyclass' => 'editcontent', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Edit content' + )); + } +}); + +// Delete blog post +get('/post/:name/delete', function ($name) { + + if (login()) { + + $user = $_SESSION[config("site.url")]['user']; + $role = user('role', $user); + + config('views.root', 'system/admin/views'); + $post = find_post(null, null, $name); + + if (!$post) { + $post = find_draft(null, null, $name); + if (!$post) { + not_found(); + } + } + + $current = $post['current']; + + if ($user === $current->author || $role === 'admin') { + render('delete-post', array( + 'title' => 'Delete post - ' . blog_title(), + 'description' => blog_description(), + 'canonical' => site_url(), + 'p' => $current, + 'bodyclass' => 'deletepost', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->tagb . ' » ' . $current->title + )); + } else { + render('denied', array( + 'title' => 'Delete post - ' . blog_title(), + 'description' => blog_description(), + 'canonical' => site_url(), + 'p' => $current, + 'bodyclass' => 'deletepost', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->tagb . ' » ' . $current->title + )); + } + } else { + $login = site_url() . 'login'; + header("location: $login"); + } +}); + +// Get deleted data from blog post +post('/post/:name/delete', function () { + + $proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); + if ($proper && login()) { + $file = from($_REQUEST, 'file'); + $destination = from($_GET, 'destination'); + delete_post($file, $destination); + } +}); + // Show various page (top-level), admin, login, sitemap, static page. get('/:static', function ($static) { @@ -1864,8 +2209,13 @@ post('/:static/:sub/delete', function () { } }); -// Show blog post page +// Show blog post with year-month get('/:year/:month/:name', function ($year, $month, $name) { + + if (config('permalink.type') == 'post') { + $redir = site_url() . 'post/' . $name; + header("location: $redir", TRUE, 301); + } if (config("views.counter") != "true") { if (!login()) { diff --git a/system/includes/functions.php b/system/includes/functions.php index 6655729..bc40763 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -232,10 +232,13 @@ function get_posts($posts, $page = 1, $perpage = 0) // The archive per day $post->archive = site_url() . 'archive/' . date('Y-m', $post->date); - - // The post URL - $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]); - + + if (config('permalink.type') == 'post') { + $post->url = site_url() . 'post/' . str_replace('.md', '', $arr[2]); + } else { + $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]); + } + $post->file = $filepath; $content = file_get_contents($filepath); @@ -335,6 +338,35 @@ function find_post($year, $month, $name) 'prev' => $pr[0] ); } + } else if (strpos($url, $name . '.md') !== false) { + $ar = get_posts($posts, $index + 1, 1); + $nx = get_posts($posts, $index, 1); + $pr = get_posts($posts, $index + 2, 1); + + if ($index == 0) { + if (isset($pr[0])) { + return array( + 'current' => $ar[0], + 'prev' => $pr[0] + ); + } else { + return array( + 'current' => $ar[0], + 'prev' => null + ); + } + } elseif (count($posts) == $index + 1) { + return array( + 'current' => $ar[0], + 'next' => $nx[0] + ); + } else { + return array( + 'current' => $ar[0], + 'next' => $nx[0], + 'prev' => $pr[0] + ); + } } } } @@ -379,6 +411,35 @@ function find_draft($year, $month, $name) 'prev' => $pr[0] ); } + } else if (strpos($url, $name . '.md') !== false) { + $ar = get_posts($posts, $index + 1, 1); + $nx = get_posts($posts, $index, 1); + $pr = get_posts($posts, $index + 2, 1); + + if ($index == 0) { + if (isset($pr[0])) { + return array( + 'current' => $ar[0], + 'prev' => $pr[0] + ); + } else { + return array( + 'current' => $ar[0], + 'prev' => null + ); + } + } elseif (count($posts) == $index + 1) { + return array( + 'current' => $ar[0], + 'next' => $nx[0] + ); + } else { + return array( + 'current' => $ar[0], + 'next' => $nx[0], + 'prev' => $pr[0] + ); + } } } } @@ -1558,7 +1619,11 @@ function sitemap_post_path() $post->archiveyear = site_url() . 'archive/' . date('Y', $post->date); // The post URL - $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]); + if (config('permalink.type') == 'post') { + $post->url = site_url() . 'post/' . str_replace('.md', '', $arr[2]); + } else { + $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]); + } $tmp[] = $post; }