From 16143d33dbe904620bdccd13e6719cb9be2a50a9 Mon Sep 17 00:00:00 2001 From: Danang Probo Sayekti Date: Fri, 27 Jun 2014 07:14:17 +0700 Subject: [PATCH] File cache support File cache support. Any visited pages by anonymous user will generating .cache file inside cache/page folder. --- COPYRIGHT.txt | 2 + README.md | 3 +- changelog.txt | 2 + robots.txt | 1 + system/admin/admin.php | 121 ++++++++++++++++++++-- system/admin/views/clear-cache.html.php | 12 +++ system/admin/views/rebuilt-cache.html.php | 7 -- system/htmly.php | 42 ++++++-- system/includes/dispatch.php | 21 +++- system/includes/functions.php | 15 ++- 10 files changed, 203 insertions(+), 23 deletions(-) create mode 100644 system/admin/views/clear-cache.html.php delete mode 100644 system/admin/views/rebuilt-cache.html.php diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index 5c15db13..47fa78c5 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -35,3 +35,5 @@ license, including: Pagedown modifications and bugfixes (c) 2009-2013 Stack Exchange Inc. Lightbox2 (c) Lokesh Dhakar + + jQuery (c) The jQuery Foundation \ No newline at end of file diff --git a/README.md b/README.md index 773beb86..ce90d381 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Features - Lightbox - User role - Online backup +- File cache Requirements ------------ @@ -107,7 +108,7 @@ server { } location / { - try_files $uri $uri/ /index.php; + try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { diff --git a/changelog.txt b/changelog.txt index 28b55811..8ce36c8a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,5 @@ +2014-06-27: File cache support +2014-06-14: Foreign Char Support 2014-02-25: HTTPS Support 2014-02-15: HTMLy v1.2 2014-02-08: HTMLy v1.1. diff --git a/robots.txt b/robots.txt index dca63e78..6a4d5dda 100644 --- a/robots.txt +++ b/robots.txt @@ -24,6 +24,7 @@ Disallow: /content/ Disallow: /system/ Disallow: /themes/ Disallow: /vendor/ +Disallow: /cache/ # Files Disallow: /changelog.txt Disallow: /composer.json diff --git a/system/admin/admin.php b/system/admin/admin.php index f150cc3d..61a11ea2 100644 --- a/system/admin/admin.php +++ b/system/admin/admin.php @@ -67,7 +67,7 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null) $t = str_replace('-','',$dt); $time = new DateTime($t); $timestamp= $time->format("Y-m-d"); - + // The post date $postdate = strtotime($timestamp); @@ -75,6 +75,7 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null) $posturl = site_url().date('Y/m', $postdate).'/'.$post_url; rebuilt_cache('all'); + clear_post_cache($dt, $post_tag, $post_url, $newfile); if ($destination == 'post') { header("Location: $posturl"); @@ -113,7 +114,7 @@ function edit_page($title, $url, $content, $oldfile, $destination = null) { $posturl = site_url() . $post_url; rebuilt_cache('all'); - + clear_page_cache($post_url); if ($destination == 'post') { header("Location: $posturl"); } @@ -150,7 +151,7 @@ function add_post($title, $tag, $url, $content, $user) { } rebuilt_cache('all'); - + clear_post_cache($post_date, $post_tag, $post_url, $dir . $filename); $redirect = site_url() . 'admin/mine'; header("Location: $redirect"); } @@ -179,7 +180,7 @@ function add_page($title, $url, $content) { } rebuilt_cache('all'); - + clear_page_cache($post_url); $redirect = site_url() . 'admin'; header("Location: $redirect"); } @@ -189,6 +190,13 @@ function add_page($title, $url, $content) { // Delete blog post function delete_post($file, $destination) { $deleted_content = $file; + + // Get cache file + $arr = explode('_', $file); + $replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/'; + $dt = str_replace($replaced,'',$arr[0]); + clear_post_cache($dt, $arr[1], str_replace('.md','',$arr[2]), $file); + if(!empty($deleted_content)) { unlink($deleted_content); rebuilt_cache('all'); @@ -206,6 +214,18 @@ function delete_post($file, $destination) { // Delete static page function delete_page($file, $destination) { $deleted_content = $file; + + if (!empty($menu)) { + foreach(glob('cache/page/*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + } + else { + $replaced = substr($file, 0, strrpos($file, '/')) . '/'; + $url = str_replace($replaced,'',$file); + clear_page_cache($url); + } + if(!empty($deleted_content)) { unlink($deleted_content); rebuilt_cache('all'); @@ -273,8 +293,8 @@ function migrate($title, $time, $tags, $content, $url, $user, $source) { mkdir($dir, 0777, true); file_put_contents($dir . $filename, print_r($post_content, true)); } - rebuilt_cache('all'); - $redirect = site_url() . 'admin/mine'; + + $redirect = site_url() . 'admin/clear-cache'; header("Location: $redirect"); } @@ -429,4 +449,93 @@ function get_backup_files () { echo 'No available backup!'; } } +} + +function clear_post_cache($post_date, $post_tag, $post_url, $filename) { + + $b = str_replace('/', '#', site_path() . '/'); + $t = explode('-', $post_date); + $c = explode(',', $post_tag); + $p = 'cache/page/'.$b.$t[0].'#'.$t[1].'#'.$post_url.'.cache'; + + // Delete post + if (file_exists($p)) { + unlink($p); + } + + // Delete homepage + $yd = 'cache/page/'.$b.'.cache'; + if (file_exists($yd)) { + unlink($yd); + } + foreach(glob('cache/page/'.$b.'~*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + + // Delete year + $yd = 'cache/page/'.$b.'archive#'.$t[0].'.cache'; + if (file_exists($yd)) { + unlink($yd); + } + foreach(glob('cache/page/'.$b.'archive#'.$t[0].'~*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + + // Delete year-month + $yd = 'cache/page/'.$b.'archive#'.$t[0].'-'.$t[1].'.cache'; + if (file_exists($yd)) { + unlink($yd); + } + foreach(glob('cache/page/'.$b.'archive#'.$t[0].'-'.$t[1].'~*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + + // Delete year-month-day + $yd = 'cache/page/'.$b.'archive#'.$t[0].'-'.$t[1].'-'.$t[2].'.cache'; + if (file_exists($yd)) { + unlink($yd); + } + foreach(glob('cache/page/'.$b.'archive#'.$t[0].'-'.$t[1].'-'.$t[2].'~*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + + // Delete tag + foreach($c as $tag) { + $yd = 'cache/page/'.$b.'tag#'.$tag.'.cache'; + if (file_exists($yd)) { + unlink($yd); + } + foreach(glob('cache/page/'.$b.'tag#'.$tag.'~*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + } + + // Delete search + foreach(glob('cache/page/'.$b.'search#*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + + + // Get cache post author + $arr = explode('_', $filename); + $replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/'; + $str = explode('/', $replaced); + $author = $str[count($str)-3]; + // Delete author post list cache + $a = 'cache/page/'.$b.'author#'.$author.'.cache'; + if (file_exists($a)) { + unlink($a); + } + foreach(glob('cache/page/'.$b.'author#'.$author.'~*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + +} + +function clear_page_cache($url) { + $b = str_replace('/', '#', site_path() . '/'); + $p = 'cache/page/'.$b.$url.'.cache'; + if (file_exists($p)) { + unlink($p); + } } \ No newline at end of file diff --git a/system/admin/views/clear-cache.html.php b/system/admin/views/clear-cache.html.php new file mode 100644 index 00000000..408ebbdb --- /dev/null +++ b/system/admin/views/clear-cache.html.php @@ -0,0 +1,12 @@ + \ No newline at end of file diff --git a/system/admin/views/rebuilt-cache.html.php b/system/admin/views/rebuilt-cache.html.php deleted file mode 100644 index d94a2ae5..00000000 --- a/system/admin/views/rebuilt-cache.html.php +++ /dev/null @@ -1,7 +0,0 @@ - \ No newline at end of file diff --git a/system/htmly.php b/system/htmly.php index 97e434ab..c04412f9 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -18,6 +18,10 @@ // This will match the root url get('/index', function () { + if(!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + $page = from($_GET, 'page'); $page = $page ? (int)$page : 1; $perpage = config('posts.perpage'); @@ -99,6 +103,10 @@ // The blog post page get('/:year/:month/:name', function($year, $month, $name){ + if(!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + $post = find_post($year, $month, $name); $current = $post['current']; @@ -140,6 +148,7 @@ 'next' => has_next($next), 'type' => 'blogpost', )); + }); // Edit blog post @@ -280,6 +289,10 @@ // The author page get('/author/:profile', function($profile){ + if(!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + $page = from($_GET, 'page'); $page = $page ? (int)$page : 1; $perpage = config('profile.perpage'); @@ -560,6 +573,11 @@ die; } else { + + if(!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + $post = get_static_post($static); if(!$post){ @@ -895,14 +913,14 @@ die; }); -// Create Zip file -get('/admin/rebuilt-cache',function(){ +// Delete all cache +get('/admin/clear-cache',function(){ if(login()) { config('views.root', 'system/admin/views'); - render('rebuilt-cache', array( - 'head_contents' => head_contents('Rebuilt cache started - ' . blog_title(), blog_description(), site_url()), - 'bodyclass' => 'rebuiltcache', - 'breadcrumb' => '' .config('breadcrumb.home'). ' » Rebuilt cache started' + render('clear-cache', array( + 'head_contents' => head_contents('Clearing cache started - ' . blog_title(), blog_description(), site_url()), + 'bodyclass' => 'clearcache', + 'breadcrumb' => '' .config('breadcrumb.home'). ' » Clearing cache started' )); } else { @@ -916,6 +934,10 @@ // The tag page get('/tag/:tag',function($tag){ + if(!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + $page = from($_GET, 'page'); $page = $page ? (int)$page : 1; $perpage = config('tag.perpage'); @@ -942,6 +964,10 @@ // The archive page get('/archive/:req',function($req){ + if(!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + $page = from($_GET, 'page'); $page = $page ? (int)$page : 1; $perpage = config('archive.perpage'); @@ -986,6 +1012,10 @@ // The search page get('/search/:keyword', function($keyword){ + if(!login()) { + file_cache($_SERVER['REQUEST_URI']); + } + $page = from($_GET, 'page'); $page = $page ? (int)$page : 1; $perpage = config('search.perpage'); diff --git a/system/includes/dispatch.php b/system/includes/dispatch.php index dd74b179..81df1d40 100644 --- a/system/includes/dispatch.php +++ b/system/includes/dispatch.php @@ -273,6 +273,15 @@ function content($value = null) { function render($view, $locals = null, $layout = null) { + if(!login()) { + $c = str_replace('/', '#', str_replace('?', '~', $_SERVER['REQUEST_URI'])); + $dir = 'cache/page'; + $cachefile = $dir. '/' . $c . '.cache'; + if(is_dir($dir) === false) { + mkdir($dir, 0777, true); + } + } + if (is_array($locals) && count($locals)) { extract($locals, EXTR_SKIP); } @@ -297,11 +306,19 @@ function render($view, $locals = null, $layout = null) { ob_start(); require $layout; - echo trim(ob_get_clean()); + + if(!login()) { + if (!file_exists($cachefile)) { + file_put_contents($cachefile, ob_get_contents()); + } + } + echo trim(ob_get_clean()); + } else { echo content(); } + } function json($obj, $code = 200) { @@ -496,4 +513,4 @@ function dispatch() { route(method(), "/{$uri}"); } -?> +?> \ No newline at end of file diff --git a/system/includes/functions.php b/system/includes/functions.php index b81a10ef..55acedec 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -1685,8 +1685,21 @@ function toolbar() { echo '
  • Edit profile
  • '; echo '
  • Import
  • '; echo '
  • Backup
  • '; - echo '
  • Rebuilt cache
  • '; + echo '
  • Clear cache
  • '; echo '
  • Logout
  • '; echo ''; +} + +// File cache +function file_cache($request) { + + $c = str_replace('/', '#', str_replace('?', '~', $request)); + $cachefile = 'cache/page/' . $c . '.cache'; + + if (file_exists($cachefile)) { + header('Content-type: text/html; charset=utf-8'); + readfile($cachefile); + die; + } } \ No newline at end of file