diff --git a/system/admin/admin.php b/system/admin/admin.php index 3fa6a75d..f150cc3d 100644 --- a/system/admin/admin.php +++ b/system/admin/admin.php @@ -45,9 +45,7 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null) $oldurl = explode('_', $oldfile); $post_title = $title; - $post_tag = preg_replace('/[^A-Za-z0-9,.-]/u', '', $tag); - $post_tag = str_replace(' ', '-',$post_tag); - $post_tag = rtrim(ltrim($post_tag, ',\.\-'), ',\.\-'); + $post_tag = preg_replace(array('/[^a-zA-Z0-9,.-]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($tag)); $post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url))); $post_content = '' . "\n\n" . $content; @@ -76,6 +74,8 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null) // The post URL $posturl = site_url().date('Y/m', $postdate).'/'.$post_url; + rebuilt_cache('all'); + if ($destination == 'post') { header("Location: $posturl"); } @@ -112,6 +112,8 @@ function edit_page($title, $url, $content, $oldfile, $destination = null) { $posturl = site_url() . $post_url; + rebuilt_cache('all'); + if ($destination == 'post') { header("Location: $posturl"); } @@ -129,8 +131,7 @@ function add_post($title, $tag, $url, $content, $user) { $post_date = date('Y-m-d-H-i-s'); $post_title = $title; - $post_tag = preg_replace('/[^A-Za-z0-9,.-]/u', '', $tag); - $post_tag = rtrim(ltrim($post_tag, ',\.\-'), ',\.\-'); + $post_tag = preg_replace(array('/[^a-zA-Z0-9,.-]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($tag)); $post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url))); $post_content = '' . "\n\n" . $content; @@ -147,6 +148,9 @@ function add_post($title, $tag, $url, $content, $user) { mkdir($dir, 0777, true); file_put_contents($dir . $filename, print_r($post_content, true)); } + + rebuilt_cache('all'); + $redirect = site_url() . 'admin/mine'; header("Location: $redirect"); } @@ -173,6 +177,9 @@ function add_page($title, $url, $content) { mkdir($dir, 0777, true); file_put_contents($dir . $filename, print_r($post_content, true)); } + + rebuilt_cache('all'); + $redirect = site_url() . 'admin'; header("Location: $redirect"); } @@ -184,6 +191,7 @@ function delete_post($file, $destination) { $deleted_content = $file; if(!empty($deleted_content)) { unlink($deleted_content); + rebuilt_cache('all'); if($destination == 'post') { $redirect = site_url(); header("Location: $redirect"); @@ -200,6 +208,7 @@ function delete_page($file, $destination) { $deleted_content = $file; if(!empty($deleted_content)) { unlink($deleted_content); + rebuilt_cache('all'); if($destination == 'post') { $redirect = site_url(); header("Location: $redirect"); @@ -230,6 +239,7 @@ function edit_profile($title, $content, $user) { mkdir($dir, 0777, true); file_put_contents($filename, print_r($user_content, true)); } + rebuilt_cache('all'); $redirect = site_url() . 'author/' . $user; header("Location: $redirect"); } @@ -263,7 +273,7 @@ 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'; header("Location: $redirect"); } @@ -290,7 +300,7 @@ function get_feed($feed_url, $credit, $message=null) { $time = new DateTime($entry->pubDate); $timestamp= $time->format("Y-m-d H:i:s"); $time = strtotime($timestamp); - $tags = strip_tags(preg_replace('/[^A-Za-z0-9,.-]/u', '', $entry->category)); + $tags = strip_tags(preg_replace(array('/[^a-zA-Z0-9,.-]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($entry->category))); $title = rtrim($entry->title, ' \,\.\-'); $title = ltrim($title, ' \,\.\-'); $user = $_SESSION['user']; diff --git a/system/admin/views/rebuilt-cache.html.php b/system/admin/views/rebuilt-cache.html.php new file mode 100644 index 00000000..d94a2ae5 --- /dev/null +++ b/system/admin/views/rebuilt-cache.html.php @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/system/htmly.php b/system/htmly.php index 67373e52..97e434ab 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -145,10 +145,10 @@ // Edit blog post get('/:year/:month/:name/edit', function($year, $month, $name){ - $user = $_SESSION['user']; - $role = user('role', $user); - if(login()) { + + $user = $_SESSION['user']; + $role = user('role', $user); config('views.root', 'system/admin/views'); $post = find_post($year, $month, $name); @@ -231,12 +231,11 @@ // Delete blog post get('/:year/:month/:name/delete', function($year, $month, $name){ - $user = $_SESSION['user']; - - $role = user('role', $user); - if(login()) { + $user = $_SESSION['user']; + $role = user('role', $user); + config('views.root', 'system/admin/views'); $post = find_post($year, $month, $name); @@ -896,6 +895,23 @@ die; }); +// Create Zip file +get('/admin/rebuilt-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' + )); + } + else { + $login = site_url() . 'login'; + header("location: $login"); + } + die; +}); + // The tag page get('/tag/:tag',function($tag){ diff --git a/system/includes/functions.php b/system/includes/functions.php index 18c1c691..b81a10ef 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -11,90 +11,127 @@ // Get blog post path. Unsorted. Mostly used on widget. function get_post_unsorted(){ - static $_cache = array(); - - if(empty($_cache)){ - - // Get the names of all the posts - - $_cache = glob('content/*/blog/*.md', GLOB_NOSORT); + static $_unsorted = array(); + + if(empty($_unsorted)){ + + $url = 'cache/index/index-unsorted.txt'; + if (file_exists($url)) { + $_unsorted = unserialize(file_get_contents($url)); + } + else { + rebuilt_cache('all'); + $_unsorted = unserialize(file_get_contents($url)); + } + + if(empty($_unsorted)){ + $_unsorted = glob('content/*/blog/*.md', GLOB_NOSORT); + } + } - return $_cache; + return $_unsorted; } // Get blog post with more info about the path. Sorted by filename. function get_post_sorted(){ - static $tmp= array(); + static $_sorted = array(); - static $_cache = array(); - - if(empty($_cache)){ - - // Get the names of all the posts + $url = 'cache/index/index-sorted.txt'; + if (file_exists($url)) { + $_sorted = unserialize(file_get_contents($url)); + } + else { + rebuilt_cache('all'); + $_sorted = unserialize(file_get_contents($url)); + } - $tmp = glob('content/*/blog/*.md', GLOB_NOSORT); + if(empty($_sorted)){ + + $url = 'cache/index/index-sorted.txt'; + if (file_exists($url)) { + $_sorted = unserialize(file_get_contents($url)); + } + else { + rebuilt_cache('all'); + $_sorted = unserialize(file_get_contents($url)); + } - if (is_array($tmp)) { - foreach($tmp as $file) { - $_cache[] = pathinfo($file); + if(empty($_sorted)){ + $tmp = array(); + $tmp = glob('content/*/blog/*.md', GLOB_NOSORT); + if (is_array($tmp)) { + foreach($tmp as $file) { + $_sorted[] = pathinfo($file); + } } + usort($_sorted, "sortfile"); } - } - - usort($_cache, "sortfile"); - - return $_cache; + + return $_sorted; } // Get static page path. Unsorted. function get_static_pages(){ - static $_cache = array(); - - if(empty($_cache)){ - - // Get the names of all the - // static page. - - $_cache = glob('content/static/*.md', GLOB_NOSORT); + static $_page = array(); + + if(empty($_page)){ + $url = 'cache/index/index-page.txt'; + if (file_exists($url)) { + $_page = unserialize(file_get_contents($url)); + } + else { + rebuilt_cache('all'); + $_page = unserialize(file_get_contents($url)); + } + + if(empty($_page)){ + $_page = glob('content/static/*.md', GLOB_NOSORT); + } } - return $_cache; + return $_page; } // Get author bio path. Unsorted. function get_author_names(){ - static $_cache = array(); - - if(empty($_cache)){ - - // Get the names of all the - // author. + static $_author = array(); - $_cache = glob('content/*/author.md', GLOB_NOSORT); + if(empty($_author)){ + $url = 'cache/index/index-author.txt'; + if (file_exists($url)) { + $_author = unserialize(file_get_contents($url)); + } + else { + rebuilt_cache('all'); + $_author = unserialize(file_get_contents($url)); + } + if(empty($_author)){ + $_author = glob('content/*/author.md', GLOB_NOSORT); + } } - return $_cache; + return $_author; } // Get backup file. function get_zip_files(){ - static $_cache = array(); + static $_zip = array(); - if(empty($_cache)){ + if(empty($_zip)){ // Get the names of all the // zip files. - $_cache = glob('backup/*.zip'); + $_zip = glob('backup/*.zip'); } - return $_cache; + return $_zip; } // usort function. Sort by filename. @@ -107,6 +144,82 @@ function sortdate($a, $b) { return $a->date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1; } +// Rebuilt cache index +function rebuilt_cache($type) { + + $dir = 'cache/index'; + $posts_cache_sorted = array(); + $posts_cache_unsorted = array(); + $page_cache = array(); + $author_cache = array(); + + if(is_dir($dir) === false) { + mkdir($dir, 0777, true); + } + + if($type === 'posts') { + $posts_cache_unsorted = glob('content/*/blog/*.md', GLOB_NOSORT); + $string = serialize($posts_cache_unsorted); + file_put_contents('cache/index/index-unsorted.txt', print_r($string, true)); + + $tmp= array(); + $tmp = glob('content/*/blog/*.md', GLOB_NOSORT); + + if (is_array($tmp)) { + foreach($tmp as $file) { + $posts_cache_sorted[] = pathinfo($file); + } + } + usort($posts_cache_sorted, "sortfile"); + $string = serialize($posts_cache_sorted); + file_put_contents('cache/index/index-sorted.txt', print_r($string, true)); + + } + + elseif ($type === 'page') { + + $page_cache = glob('content/static/*.md', GLOB_NOSORT); + $string = serialize($page_cache); + file_put_contents('cache/index/index-page.txt', print_r($string, true)); + + } + + elseif ($type === 'author') { + + $author_cache = glob('content/*/author.md', GLOB_NOSORT); + $string = serialize($author_cache); + file_put_contents('cache/index/index-author.txt', print_r($string, true)); + + } + + elseif ($type === 'all') { + + $posts_cache_unsorted = glob('content/*/blog/*.md', GLOB_NOSORT); + $string = serialize($posts_cache_unsorted); + file_put_contents('cache/index/index-unsorted.txt', print_r($string, true)); + + $tmp= array(); + $tmp = glob('content/*/blog/*.md', GLOB_NOSORT); + if (is_array($tmp)) { + foreach($tmp as $file) { + $posts_cache_sorted[] = pathinfo($file); + } + } + usort($posts_cache_sorted, "sortfile"); + $string = serialize($posts_cache_sorted); + file_put_contents('cache/index/index-sorted.txt', print_r($string, true)); + + $page_cache = glob('content/static/*.md', GLOB_NOSORT); + $string = serialize($page_cache); + file_put_contents('cache/index/index-page.txt', print_r($string, true)); + + $author_cache = glob('content/*/author.md', GLOB_NOSORT); + $string = serialize($author_cache); + file_put_contents('cache/index/index-author.txt', print_r($string, true)); + + } +} + // Return blog posts. function get_posts($posts, $page = 1, $perpage = 0){ @@ -1161,7 +1274,7 @@ function generate_rss($posts){ echo $feed; } -// Return post, archive url. +// Return post, archive url for sitemap function get_path(){ $posts = get_post_sorted(); @@ -1212,7 +1325,7 @@ function get_path(){ return $tmp; } -// Return static page path. +// Return static page path for sitemap function get_static_path(){ $posts = get_static_pages(); @@ -1572,6 +1685,7 @@ function toolbar() { echo '