From 6879f09ff89d91a32c4ebc3cfbb6529b1bdd0e53 Mon Sep 17 00:00:00 2001 From: Yaya Laressa Date: Wed, 7 Jul 2021 10:33:34 +0700 Subject: [PATCH] Add features CRUD Authors via Admin panel --- system/admin/admin.php | 220 +++++++++++++++++++- system/admin/views/add-author.html.php | 95 +++++++++ system/admin/views/add-content.html.php | 16 +- system/admin/views/add-page.html.php | 4 +- system/admin/views/authors-list.html.php | 61 ++++++ system/admin/views/delete-author.html.php | 31 +++ system/admin/views/edit-author.html.php | 115 +++++++++++ system/admin/views/edit-content.html.php | 16 +- system/admin/views/edit-page.html.php | 4 +- system/admin/views/layout.html.php | 16 ++ system/htmly.php | 323 +++++++++++++++++++++++++++--- system/includes/functions.php | 59 ++++++ 12 files changed, 897 insertions(+), 63 deletions(-) create mode 100644 system/admin/views/add-author.html.php create mode 100644 system/admin/views/authors-list.html.php create mode 100644 system/admin/views/delete-author.html.php create mode 100644 system/admin/views/edit-author.html.php diff --git a/system/admin/admin.php b/system/admin/admin.php index b1831af..8daf023 100644 --- a/system/admin/admin.php +++ b/system/admin/admin.php @@ -39,37 +39,149 @@ function create_user($userName, $password, $role = "user") } } -// Create a session -function session($user, $pass) +// Add author +function add_author($title, $user, $password, $content) { - $user_file = 'config/users/' . $user . '.ini'; - if (!file_exists($user_file)) { - return $str = '
'; + create_user($user, $password); + + $user_title = safe_html($title); + $user_content = '' . "\n\n" . $content; + + if (!empty($user_title) && !empty($user_content)) { + + $user_content = stripslashes($user_content); + + $dir = 'content/' . $user . '/'; + $filename = 'content/' . $user . '/author.md'; + if (is_dir($dir)) { + file_put_contents($filename, print_r($user_content, true)); + } else { + mkdir($dir, 0775, true); + file_put_contents($filename, print_r($user_content, true)); + } + rebuilt_cache('all'); + $redirect = site_url() . 'admin/authors'; + header("Location: $redirect"); + } +} + +// Edit author +function edit_author($name, $title, $user, $password, $content) +{ + $name = get_author_info($name); + $name = $name[0]; + + create_user($user, $password, $name->role); + + $user_title = safe_html($title); + $user_content = '' . "\n\n" . $content; + + if (!empty($user_title) && !empty($user_content)) { + + $user_content = stripslashes($user_content); + + $dir = 'content/' . $user . '/'; + $filename = 'content/' . $user . '/author.md'; + if (is_dir($dir)) { + file_put_contents($filename, print_r($user_content, true)); + } else { + mkdir($dir, 0775, true); + file_put_contents($filename, print_r($user_content, true)); + } + + // Jika username lama tidak sama dengan yang baru maka file username lama akan dihapus + if($name->username !== $user) { + copy_folders('content/' . $name->username, 'content/' . $user); + remove_folders('content/' . $name->username); + // Memastikan kalau username sesi sama dengan username lama + if($_SESSION[config("site.url")]['user'] === $name->username) { + if (session_status() == PHP_SESSION_NONE) session_start(); + $_SESSION[config("site.url")]['user'] = $user; + } + unlink($name->file); + } + + rebuilt_cache('all'); + $redirect = site_url() . 'admin/authors'; + header("Location: $redirect"); } +} +// Check old password +function valid_password($user, $pass) +{ $user_enc = user('encryption', $user); $user_pass = user('password', $user); $user_role = user('role', $user); if ($user_enc == "password_hash") { if (password_verify($pass, $user_pass)) { - if (session_status() == PHP_SESSION_NONE) session_start(); if (password_needs_rehash($user_pass, PASSWORD_DEFAULT)) { update_user($user, $pass, $user_role); } - $_SESSION[config("site.url")]['user'] = $user; - header('location: admin'); + return true; } else { - return $str = '
'; + return false; } } else if (old_password_verify($pass, $user_enc, $user_pass)) { - if (session_status() == PHP_SESSION_NONE) session_start(); update_user($user, $pass, $user_role); + return true; + } else { + return false; + } +} + +// Check username exists +function username_exists($username, $user = null) +{ + // Jika username baru tidak sama dengan username lama + if($username !== $user || $user === null) { + $file = 'config/users/' . $username . '.ini'; + if(file_exists($file)) + { + return true; + } else { + return false; + } + } else { // Jika username baru sama dengan username lama + $file = 'config/users/' . $username . '.ini'; + if(!file_exists($file)) + { + return true; + } else { + return false; + } + } +} + +// Matching password and password confirm +function password_match($password, $confirm) +{ + if($password === $confirm) + { + return true; + } else { + return false; + } +} + +// Create a session +function session($user, $pass) +{ + $user_file = 'config/users/' . $user . '.ini'; + if (!file_exists($user_file)) { + return $str = '
'; + } + + if(valid_password($user, $pass)) + { + if (session_status() == PHP_SESSION_NONE) session_start(); $_SESSION[config("site.url")]['user'] = $user; header('location: admin'); } else { return $str = '
'; } + } function old_password_verify($pass, $user_enc, $user_pass) @@ -702,6 +814,94 @@ function edit_frontpage($title, $content) } } +// Move folder and files +function copy_folders($oldfolder, $newfolder) +{ + if (is_dir($oldfolder)) + { + $dir = opendir($oldfolder); + if (!is_dir($newfolder)) + { + mkdir($newfolder, 0775, true); + } + while (($file = readdir($dir))) + { + if (($file != '.') && ($file != '..')) + { + if (is_dir($oldfolder . '/' . $file)) + { + copy_folders($oldfolder . '/' . $file, $newfolder . '/' . $file); + } + else + { + copy($oldfolder . '/' . $file, $newfolder . '/' . $file); + } + } + } + closedir($dir); + } +} + + +// Delete folder and files +function remove_folders($dir) +{ + if (false === file_exists($dir)) { + return false; + } + + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ($files as $fileinfo) { + if ($fileinfo->isDir()) { + if (false === rmdir($fileinfo->getRealPath())) { + return false; + } + } else { + if (false === unlink($fileinfo->getRealPath())) { + return false; + } + } + } + + return rmdir($dir); +} + +// Delete author +function delete_author($file, $destination) +{ + if (!login()) + return null; + $deleted_content = $file; + + if (!empty($deleted_content)) { + + $str = explode('/', $file); + $str = str_replace('.ini', '', $str); + $username = $str[2]; + + $dir = 'content/' . $username . '/'; + + $user = $_SESSION[config("site.url")]['user']; + // Melarang untuk menghapus diri sendiri, karena bunuh diri itu dosa :D + if($user !== $username) { + remove_folders($dir); + unlink($deleted_content); + rebuilt_cache('all'); + } + if ($destination == 'author') { + $redirect = site_url(); + header("Location: $redirect"); + } else { + $redirect = site_url() . $destination; + header("Location: $redirect"); + } + } +} + // Delete blog post function delete_post($file, $destination) { diff --git a/system/admin/views/add-author.html.php b/system/admin/views/add-author.html.php new file mode 100644 index 0000000..5b65667 --- /dev/null +++ b/system/admin/views/add-author.html.php @@ -0,0 +1,95 @@ + + + + + + + + + + + +
+ + +
+
+
+
+
+ + +
+
+
+ + +
+
+
+
+
+ + +
+
+
+ + +
+
+
+ +
+
+ +
+ +
+ + +
+
+ +
+
+
+
+
+
+ + + +
+ + + \ No newline at end of file diff --git a/system/admin/views/add-content.html.php b/system/admin/views/add-content.html.php index bf22196..ed114d4 100644 --- a/system/admin/views/add-content.html.php +++ b/system/admin/views/add-content.html.php @@ -87,7 +87,7 @@ $( function() {
- +

- +
@@ -113,35 +113,35 @@ $( function() { - +
- +
- +
- +
- +
@@ -157,7 +157,7 @@ $( function() {
-
+


diff --git a/system/admin/views/add-page.html.php b/system/admin/views/add-page.html.php index fcea17f..4829cb7 100644 --- a/system/admin/views/add-page.html.php +++ b/system/admin/views/add-page.html.php @@ -18,7 +18,7 @@
- +
@@ -40,7 +40,7 @@
- +
diff --git a/system/admin/views/authors-list.html.php b/system/admin/views/authors-list.html.php new file mode 100644 index 0000000..8c6a35b --- /dev/null +++ b/system/admin/views/authors-list.html.php @@ -0,0 +1,61 @@ + +

+
+ +

+ + + + + + + + + + + + + + + + + + + + + +
title ?>username ?> username): ?>
+ +
+
+
    + +
  • + +
  • + +
  • + +
  • + +
  • + +
+
+ + \ No newline at end of file diff --git a/system/admin/views/delete-author.html.php b/system/admin/views/delete-author.html.php new file mode 100644 index 0000000..d9e0b76 --- /dev/null +++ b/system/admin/views/delete-author.html.php @@ -0,0 +1,31 @@ + +file; + +$dir = substr($url, 0, strrpos($url, '/')); +$oldurl = str_replace($dir . '/', '', $url); +$oldmd = str_replace('.md', '', $oldurl); + +$author = $a->url; + +if (isset($destination)) { + + if ($destination == 'author') { + $back = $author; + } else { + $back = site_url() . $destination; + } +} else { + $back = site_url(); +} +?> +

title);?>

+
+
+ + + ' . i18n('Cancel');?> +
\ No newline at end of file diff --git a/system/admin/views/edit-author.html.php b/system/admin/views/edit-author.html.php new file mode 100644 index 0000000..912ca4c --- /dev/null +++ b/system/admin/views/edit-author.html.php @@ -0,0 +1,115 @@ + +title; + $aUsername = $a->username; + $aContent = $a->content; +} + +?> + + + + + + + + + + + +
+ + +
+
+
+
+
+ + +
+
+
+ + +
+
+
+
+
+ + +
+
+
+
+
+ + +
+
+
+ + +
+
+
+ +
+
+ +
+ +
+ + +
+
+ +
+
+
+
+
+
+ + + +
+ + + \ No newline at end of file diff --git a/system/admin/views/edit-content.html.php b/system/admin/views/edit-content.html.php index e69cdce..459a8a1 100644 --- a/system/admin/views/edit-content.html.php +++ b/system/admin/views/edit-content.html.php @@ -132,7 +132,7 @@ $( function() {
- +

- +
@@ -169,35 +169,35 @@ $( function() { - +
- +
- +
- +
- +
@@ -214,7 +214,7 @@ $( function() {
-
+
diff --git a/system/admin/views/edit-page.html.php b/system/admin/views/edit-page.html.php index b3599d5..3fe415e 100644 --- a/system/admin/views/edit-page.html.php +++ b/system/admin/views/edit-page.html.php @@ -83,7 +83,7 @@ if ($type == 'is_frontpage') {
- +
@@ -106,7 +106,7 @@ if ($type == 'is_frontpage') {
- +
diff --git a/system/admin/views/layout.html.php b/system/admin/views/layout.html.php index 42c6ec1..2a8aee6 100644 --- a/system/admin/views/layout.html.php +++ b/system/admin/views/layout.html.php @@ -95,6 +95,22 @@ +
  • Title field is required.
  • '; + } + if (empty($username)) { + $message['error'] .= '
  • Username field is required.
  • '; + } + if (!preg_match('/(?=.{6})^[a-z0-9]+$/', $username)) { + $message['error'] .= '
  • Username only letters, numbers, and must be 6 or more.
  • '; + } + if (username_exists($username)) { + $message['error'] .= '
  • Username is already exist.
  • '; + } + if (empty($password)) { + $message['error'] .= '
  • Password field is required.
  • '; + } + if (empty($passconfirm)) { + $message['error'] .= '
  • Password Confirm field is required.
  • '; + } + if (!password_match($password, $passconfirm)) { + $message['error'] .= '
  • Password and Password Confirm is not match.
  • '; + } + if (!$proper) { + $message['error'] .= '
  • CSRF Token not correct.
  • '; + } + config('views.root', 'system/admin/views'); + render('add-author', array( + 'title' => 'Add author - ' . blog_title(), + 'description' => strip_tags(blog_description()), + 'canonical' => site_url(), + 'error' => '
      ' . $message['error'] . '
    ', + 'aTitle' => $title, + 'aUsername' => $username, + 'aPassword' => $password, + 'aPassConfirm' => $passconfirm, + 'aContent' => $content, + 'heading' => 'Add author', + 'is_admin' => true, + 'bodyclass' => 'add-author', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Add author' + )); + } +}); + +// Edit author +get('/author/:name/edit', function ($name) { + + if (login()) { + config('views.root', 'system/admin/views'); + if (is_admin()) { + render('edit-author', array( + 'title' => 'Edit author - ' . blog_title(), + 'description' => strip_tags(blog_description()), + 'canonical' => site_url(), + 'username' => $name, + 'is_admin' => true, + 'bodyclass' => 'edit-author', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Edit author' + )); + } else { + render('denied', array( + 'title' => 'Edit author - ' . blog_title(), + 'description' => strip_tags(blog_description()), + 'canonical' => site_url(), + 'is_admin' => true, + 'bodyclass' => 'edit-author', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Edit author' + )); + } + } else { + $login = site_url() . 'login'; + header("location: $login"); + } +}); + +// Get data Edit author +post('/author/:name/edit', function ($name) { + + $proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); + + if (!login()) { + $login = site_url() . 'login'; + header("location: $login"); + } + + $title = from($_REQUEST, 'title'); + $username = strtolower(from($_REQUEST, 'username')); + $oldpassword = from($_REQUEST, 'oldpassword'); + $password = from($_REQUEST, 'password'); + $passconfirm = from($_REQUEST, 'passconfirm'); + $content = from($_REQUEST, 'content'); + + if ($proper && !empty($title) && !empty($username) && preg_match('/(?=.{6})^[a-z0-9]+$/', $username) && !username_exists($username, $name) && !empty($password) && !empty($passconfirm) && password_match($password, $passconfirm) && valid_password($name, $oldpassword) && login()) { + edit_author($name, $title, $username, $password, $content); + } else { + $message['error'] = ''; + if (empty($title)) { + $message['error'] .= '
  • Title field is required.
  • '; + } + if (empty($username)) { + $message['error'] .= '
  • Username field is required.
  • '; + } + if (!preg_match('/(?=.{6})^[a-z0-9]+$/', $username)) { + $message['error'] .= '
  • Username only letters, numbers, and must be 6 or more.
  • '; + } + if (username_exists($username, $name)) { + $message['error'] .= '
  • Username is already exist.
  • '; + } + if (empty($password)) { + $message['error'] .= '
  • Password field is required.
  • '; + } + if (empty($passconfirm)) { + $message['error'] .= '
  • Password Confirm field is required.
  • '; + } + if (!password_match($password, $passconfirm)) { + $message['error'] .= '
  • Password and Password Confirm is not match.
  • '; + } + if (!valid_password($name, $oldpassword)) { + $message['error'] .= '
  • Old Password is not valid.
  • '; + } + if (!$proper) { + $message['error'] .= '
  • CSRF Token not correct.
  • '; + } + config('views.root', 'system/admin/views'); + render('edit-author', array( + 'title' => 'Edit author - ' . blog_title(), + 'description' => strip_tags(blog_description()), + 'canonical' => site_url(), + 'error' => '
      ' . $message['error'] . '
    ', + 'aTitle' => $title, + 'aUsername' => $username, + 'aOldPassword' => $oldpassword, + 'aPassword' => $password, + 'aPassConfirm' => $passconfirm, + 'aContent' => $content, + 'heading' => 'Edit author', + 'is_admin' => true, + 'bodyclass' => 'edit-author', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Edit author' + )); + } + + +}); + +// Delete author +get('/author/:name/delete', function ($name) { + + if (login()) { + if (is_admin()) { + config('views.root', 'system/admin/views'); + $author = get_author_info($name); + + if (!$author) { + not_found(); + } + + $author = $author[0]; + + render('delete-author', array( + 'title' => 'Delete author - ' . blog_title(), + 'description' => strip_tags(blog_description()), + 'canonical' => site_url(), + 'a' => $author, + 'is_admin' => true, + 'bodyclass' => 'delete-author', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Delete author' + )); + } else { + render('denied', array( + 'title' => 'Delete author - ' . blog_title(), + 'description' => strip_tags(blog_description()), + 'canonical' => site_url(), + 'is_admin' => true, + 'bodyclass' => 'delete-author', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Delete author' + )); + } + } else { + $login = site_url() . 'login'; + header("location: $login"); + } +}); + +// Get data Delete author +post('/author/:name/delete', function () { + $proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); + if ($proper && login()) { + $file = from($_REQUEST, 'file'); + $destination = from($_GET, 'destination'); + delete_author($file, $destination); + } +}); + +// Show authors page +get('/admin/authors', function () { + + if (login()) { + config('views.root', 'system/admin/views'); + if (is_admin()) { + $authors = get_authors(); + + render('authors-list', array( + 'title' => 'Authors list - ' . blog_title(), + 'description' => strip_tags(blog_description()), + 'canonical' => site_url(), + 'heading' => 'Authors', + 'authors' => $authors, + 'is_admin' => true, + 'bodyclass' => 'authors-list', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Authors list' + )); + } else { + render('denied', array( + 'title' => 'Authors list - ' . blog_title(), + 'description' => strip_tags(blog_description()), + 'canonical' => site_url(), + 'heading' => 'Authors', + 'is_admin' => true, + 'bodyclass' => 'authors-list', + 'breadcrumb' => '' . config('breadcrumb.home') . ' » Authors list' + )); + } + } else { + $login = site_url() . 'login'; + header("location: $login"); + } +}); + // Edit the profile get('/edit/profile', function () { @@ -710,12 +991,10 @@ post('/add/category', function () { // Show admin/posts get('/admin/posts', function () { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { config('views.root', 'system/admin/views'); $page = from($_GET, 'page'); @@ -781,12 +1060,10 @@ get('/admin/posts', function () { // Show admin/popular get('/admin/popular', function () { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { config('views.root', 'system/admin/views'); $page = from($_GET, 'page'); $page = $page ? (int)$page : 1; @@ -1092,12 +1369,9 @@ post('/admin/import', function () { // Show Config page get('/admin/config', function () { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); - if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { render('config', array( 'title' => 'Config - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -1158,12 +1432,9 @@ post('/admin/config', function () { // Show Config page get('/admin/config/custom', function () { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); - if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { render('config-custom', array( 'title' => 'Config - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -1226,12 +1497,9 @@ post('/admin/config/custom', function () { // Show Config page get('/admin/config/reading', function () { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); - if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { render('config-reading', array( 'title' => 'Config - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -1293,12 +1561,9 @@ post('/admin/config/reading', function () { // Show Config page get('/admin/config/widget', function () { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); - if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { render('config-widget', array( 'title' => 'Config - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -1360,12 +1625,9 @@ post('/admin/config/widget', function () { // Show Config page get('/admin/config/metatags', function () { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); - if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { render('config-metatags', array( 'title' => 'Config - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -1427,12 +1689,9 @@ post('/admin/config/metatags', function () { // Show Config page get('/admin/config/performance', function () { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); - if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { render('config-performance', array( 'title' => 'Config - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -1658,12 +1917,10 @@ get('/admin/categories', function () { // Show the category page get('/admin/categories/:category', function ($category) { - $user = $_SESSION[config("site.url")]['user']; - $role = user('role', $user); if (login()) { config('views.root', 'system/admin/views'); - if ($role === 'admin') { + if (is_admin()) { $page = from($_GET, 'page'); $page = $page ? (int)$page : 1; diff --git a/system/includes/functions.php b/system/includes/functions.php index e712378..5f5cef3 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -6,6 +6,65 @@ use \Suin\RSSWriter\Feed; use \Suin\RSSWriter\Channel; use \Suin\RSSWriter\Item; +// Get all authors +function get_authors() +{ + $tmp = array(); + foreach (glob('config/users/*.ini', GLOB_NOSORT) as $key => $value) { + if(preg_match('/config\/users\/(.*)\.ini/i', $value, $matches)) { + + $user = new stdClass; + $user->username = $matches[1]; + $user->password = user('password', $matches[1]); + $user->role = user('role', $matches[1]); + $user->url = site_url() . 'author/' . $matches[1]; + $user->file = $value; + + $filename = 'content/' . $matches[1] . '/author.md'; + if (file_exists($filename)) { + $content = file_get_contents($filename); + $user->title = get_content_tag('t', $content, 'user'); + $user->content = remove_html_comments($content); + } else { + $user->title = $matches[1]; + $user->content = 'Just another HTMLy user.'; + } + + $tmp[] = $user; + } + } + return $tmp; +} + +// Get author info +function get_author_info($author) +{ + $tmp = array(); + $value = 'config/users/' . $author . '.ini'; + if(preg_match('/config\/users\/(.*)\.ini/i', $value, $matches)) { + + $user = new stdClass; + $user->username = $matches[1]; + $user->password = user('password', $matches[1]); + $user->role = user('role', $matches[1]); + $user->url = site_url() . 'author/' . $matches[1]; + $user->file = $value; + + $filename = 'content/' . $matches[1] . '/author.md'; + if (file_exists($filename)) { + $content = file_get_contents($filename); + $user->title = get_content_tag('t', $content, 'user'); + $user->content = remove_html_comments($content); + } else { + $user->title = $matches[1]; + $user->content = 'Just another HTMLy user.'; + } + + $tmp[] = $user; + } + return $tmp; +} + // Get blog post path. Unsorted. Mostly used on widget. function get_post_unsorted() {