Browse Source

Add backup feature

Add backup feature to the core.
pull/31/merge
Danang Probo Sayekti 11 years ago
parent
commit
e5d718dc6d
7 changed files with 266 additions and 7 deletions
  1. +3
    -4
      .htaccess
  2. +3
    -1
      config/config.ini.example
  3. +63
    -1
      system/admin/admin.php
  4. +17
    -0
      system/admin/views/backup-start.html.php
  5. +15
    -0
      system/admin/views/backup.html.php
  6. +35
    -0
      system/htmly.php
  7. +130
    -1
      system/includes/functions.php

+ 3
- 4
.htaccess View File

@ -76,10 +76,9 @@ DirectoryIndex index.php index.html index.htm
# RewriteBase /
# Pass all requests not referring directly to files in the filesystem to index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>


+ 3
- 1
config/config.ini.example View File

@ -13,7 +13,9 @@ social.facebook = "https://www.facebook.com"
social.google = "https://plus.google.com"
social.tumblr = "http://www.tumblr.com"
; Custom menu link
; Custom menu link.
; See example below:
; "Google->http://www.google.com|Wikipedia->http://www.wikipedia.org".
blog.menu = ""
; Breadcrumb home text. Useful when installed on subfolder.


+ 63
- 1
system/admin/admin.php View File

@ -1,5 +1,6 @@
<?php
// Return username.ini value
function user($key, $user=null) {
$value = 'config/users/' . $user . '.ini';
static $_config = array();
@ -11,6 +12,7 @@ function user($key, $user=null) {
}
}
// Create a session
function session($user, $pass, $str = null) {
$user_file = 'config/users/' . $user . '.ini';
$user_pass = user('password', $user);
@ -29,6 +31,7 @@ function session($user, $pass, $str = null) {
}
}
// Edit blog posts
function edit_post($title, $tag, $url, $content, $oldfile, $destination = null) {
$oldurl = explode('_', $oldfile);
@ -80,6 +83,7 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null)
}
// Edit static page
function edit_page($title, $url, $content, $oldfile, $destination = null) {
$dir = substr($oldfile, 0, strrpos($oldfile, '/'));
@ -118,6 +122,7 @@ function edit_page($title, $url, $content, $oldfile, $destination = null) {
}
// Add blog post
function add_post($title, $tag, $url, $content, $user) {
$post_date = date('Y-m-d-H-i-s');
@ -149,6 +154,7 @@ function add_post($title, $tag, $url, $content, $user) {
}
// Add static page
function add_page($title, $url, $content) {
$post_title = $title;
@ -177,6 +183,7 @@ function add_page($title, $url, $content) {
}
// Delete blog post
function delete_post($file, $destination) {
$deleted_content = $file;
if(!empty($deleted_content)) {
@ -192,6 +199,7 @@ function delete_post($file, $destination) {
}
}
// Delete static page
function delete_page($file, $destination) {
$deleted_content = $file;
if(!empty($deleted_content)) {
@ -207,6 +215,7 @@ function delete_page($file, $destination) {
}
}
// Edit user profile
function edit_profile($title, $content, $user) {
$user_title = $title;
@ -231,6 +240,7 @@ function edit_profile($title, $content, $user) {
}
// Import RSS feed
function migrate($title, $time, $tags, $content, $url, $user, $source) {
$post_date = date('Y-m-d-H-i-s', $time);
@ -267,6 +277,7 @@ function migrate($title, $time, $tags, $content, $url, $user, $source) {
}
// Fetch RSS feed
function get_feed($feed_url, $credit, $message=null) {
$source = file_get_contents($feed_url);
$feed = new SimpleXmlElement($source);
@ -310,6 +321,7 @@ function get_feed($feed_url, $credit, $message=null) {
}
// Get recent posts by user
function get_recent_posts() {
if (isset($_SESSION['user'])) {
$posts = get_profile($_SESSION['user'], 1, 5);
@ -340,7 +352,7 @@ function get_recent_posts() {
}
}
// Auto generate menu from static page
// Get all static pages
function get_recent_pages() {
if (isset($_SESSION['user'])) {
$posts = get_static_post(null);
@ -368,4 +380,54 @@ function get_recent_pages() {
echo '</table>';
}
}
}
// Get all available zip files
function get_backup_files () {
if (isset($_SESSION['user'])) {
$files = get_zip_files();
if(!empty($files)) {
krsort($files);
echo '<table class="backup-list">';
echo '<tr class="head"><th>Filename</th><th>Date</th><th>Operations</th></tr>';
$i = 0; $len = count($files);
foreach($files as $file) {
if ($i == 0) {
$class = 'item first';
}
elseif ($i == $len - 1) {
$class = 'item last';
}
else {
$class = 'item';
}
$i++;
// Extract the date
$arr = explode('_', $file);
// Replaced string
$replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/';
$name = str_replace($replaced,'',$file);
$date = str_replace('.zip','',$arr[1]);
$t = str_replace('-', '', $date);
$time = new DateTime($t);
$timestamp= $time->format("D, d F Y, H:i:s");
$url = site_url() . $file;
echo '<tr class="' . $class . '">';
echo '<td>' . $name . '</td>';
echo '<td>' . $timestamp . '</td>';
echo '<td><a target="_blank" href="' . $url . '">Download</a> <form method="GET"><input type="hidden" name="file" value="' . $file . '"/><input type="submit" name="submit" value="Delete"/></form></td>';
echo '</tr>';
}
echo '</table>';
}
else {
echo 'No available backup!';
}
}
}

+ 17
- 0
system/admin/views/backup-start.html.php View File

@ -0,0 +1,17 @@
<?php
$timestamp = date('Y-m-d-H-i-s');
$dir = 'backup';
if(is_dir($dir)) {
Zip('content/', 'backup/content_' . $timestamp . '.zip', true);
}
else {
mkdir($dir, 0777, true);
Zip('content/', 'backup/content_' . $timestamp . '.zip', true);
}
$redirect = site_url() . 'admin/backup';
header("Location: $redirect");
?>

+ 15
- 0
system/admin/views/backup.html.php View File

@ -0,0 +1,15 @@
<?php
if(login()) {
if(isset($_GET['file'])) {
$file = $_GET['file'];
if(!empty($file)) {
unlink($file);
}
}
}
?>
<a href="<?php echo site_url() ?>admin/backup-start">Create backup</a>
<h2>Your backups</h2>
<?php echo get_backup_files() ?>

+ 35
- 0
system/htmly.php View File

@ -862,6 +862,41 @@ post('/admin/import', function() {
});
// Backup page
get('/admin/backup',function(){
if(login()) {
config('views.root', 'system/admin/views');
render('backup', array(
'head_contents' => head_contents('Backup content - ' . blog_title(), blog_description(), site_url()),
'bodyclass' => 'backup',
'breadcrumb' => '<a href="' . site_url() . '">' .config('breadcrumb.home'). '</a> &#187; Backup'
));
}
else {
$login = site_url() . 'login';
header("location: $login");
}
die;
});
// Create Zip file
get('/admin/backup-start',function(){
if(login()) {
config('views.root', 'system/admin/views');
render('backup-start', array(
'head_contents' => head_contents('Backup content started - ' . blog_title(), blog_description(), site_url()),
'bodyclass' => 'startbackup',
'breadcrumb' => '<a href="' . site_url() . '">' .config('breadcrumb.home'). '</a> &#187; Backup started'
));
}
else {
$login = site_url() . 'login';
header("location: $login");
}
die;
});
// The tag page
get('/tag/:tag',function($tag){


+ 130
- 1
system/includes/functions.php View File

@ -81,6 +81,22 @@ function get_author_names(){
return $_cache;
}
// Get backup file.
function get_zip_files(){
static $_cache = array();
if(empty($_cache)){
// Get the names of all the
// zip files.
$_cache = glob('backup/*.zip');
}
return $_cache;
}
// usort function. Sort by filename.
function sortfile($a, $b) {
return $a['filename'] == $b['filename'] ? 0 : ( $a['filename'] < $b['filename'] ) ? 1 : -1;
@ -967,8 +983,60 @@ EOF;
// Menu
function menu(){
$menu = config('blog.menu');
$req = $_SERVER['REQUEST_URI'];
if (!empty($menu)) {
return $menu;
$links = explode('|', $menu);
echo '<ul class="nav">';
if($req == site_path() . '/') {
echo '<li class="item first active"><a href="' . site_url() . '">' .config('breadcrumb.home'). '</a></li>';
}
else {
echo '<li class="item first"><a href="' . site_url() . '">' .config('breadcrumb.home'). '</a></li>';
}
$i = 0;
$len = count($links);
foreach($links as $link) {
if ($i == $len - 1) {
$class = 'item last';
}
else {
$class = 'item';
}
$i++;
$anc = explode('->', $link);
if(isset($anc[0]) && isset($anc[1])) {
if(strpos($link, site_url()) !== false) {
$id = substr($link, strrpos($link, '/')+1 );
$file = 'content/static/' . $id . '.md';
if(file_exists($file)) {
if(strpos($req, $id) !== false){
echo '<li class="' . $class . ' active"><a href="' . $anc[1] . '">' . $anc[0] . '</a></li>';
}
else {
echo '<li class="' . $class . '"><a href="' . $anc[1] . '">' . $anc[0] . '</a></li>';
}
}
else {
echo '<li class="' . $class . '"><a href="' . $anc[1] . '">' . $anc[0] . '</a></li>';
}
}
else {
echo '<li class="' . $class . '"><a target="_blank" href="' . $anc[1] . '">' . $anc[0] . '</a></li>';
}
}
}
echo '</ul>';
}
else {
get_menu();
@ -1035,6 +1103,17 @@ function get_menu() {
echo '</ul>';
}
else {
echo '<ul class="nav">';
if($req == site_path() . '/') {
echo '<li class="item first active"><a href="' . site_url() . '">' .config('breadcrumb.home'). '</a></li>';
}
else {
echo '<li class="item first"><a href="' . site_url() . '">' .config('breadcrumb.home'). '</a></li>';
}
}
}
@ -1369,6 +1448,55 @@ function generate_json($posts){
return json_encode($posts);
}
// Create Zip files
function Zip($source, $destination, $include_dir = false)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
if (file_exists($destination)) {
unlink ($destination);
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = str_replace('\\', '/', $file);
// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
// TRUE if the current page is the front page.
function is_front() {
$req = $_SERVER['REQUEST_URI'];
@ -1472,6 +1600,7 @@ EOF;
echo '<li><a href="'.$base.'add/page">Add page</a></li>';
echo '<li><a href="'.$base.'edit/profile">Edit profile</a></li>';
echo '<li><a href="'.$base.'admin/import">Import</a></li>';
echo '<li><a href="'.$base.'admin/backup">Backup</a></li>';
echo '<li><a href="'.$base.'logout">Logout</a></li>';
echo '</ul></div>';

Loading…
Cancel
Save