diff --git a/bl-kernel/boot/init.php b/bl-kernel/boot/init.php index 00f6890f..73528138 100644 --- a/bl-kernel/boot/init.php +++ b/bl-kernel/boot/init.php @@ -116,6 +116,9 @@ define('FILENAME', 'index.txt'); // Database date format define('DB_DATE_FORMAT', 'Y-m-d H:i:s'); +// Database date format +define('BACKUP_DATE_FORMAT', 'Y-m-d-H-i-s'); + // Sitemap date format define('SITEMAP_DATE_FORMAT', 'Y-m-d'); diff --git a/bl-kernel/helpers/filesystem.class.php b/bl-kernel/helpers/filesystem.class.php index d34edb17..0913db35 100644 --- a/bl-kernel/helpers/filesystem.class.php +++ b/bl-kernel/helpers/filesystem.class.php @@ -102,4 +102,36 @@ class Filesystem { return rmdir($source); } + + public static function zip($source, $destination) + { + if (!extension_loaded('zip') || !file_exists($source)) { + return false; + } + + $zip = new ZipArchive(); + if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { + return false; + } + + if (is_dir($source) === true) { + $iterator = new RecursiveDirectoryIterator($source); + $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS); + $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST); + + foreach ($files as $file) { + $file = realpath($file); + if (is_dir($file)) { + $zip->addEmptyDir(str_replace($source, '', $file)); + } elseif (is_file($file)) { + $zip->addFromString(str_replace($source, '', $file), file_get_contents($file)); + } + } + } elseif (is_file($source)) { + $zip->addFromString(basename($source), file_get_contents($source)); + } + + return $zip->close(); + } + } \ No newline at end of file diff --git a/bl-plugins/backup/languages/en.json b/bl-plugins/backup/languages/en.json new file mode 100644 index 00000000..1667362a --- /dev/null +++ b/bl-plugins/backup/languages/en.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "Backup", + "description": "Backup system" + } +} \ No newline at end of file diff --git a/bl-plugins/backup/languages/es.json b/bl-plugins/backup/languages/es.json new file mode 100644 index 00000000..1667362a --- /dev/null +++ b/bl-plugins/backup/languages/es.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "Backup", + "description": "Backup system" + } +} \ No newline at end of file diff --git a/bl-plugins/backup/metadata.json b/bl-plugins/backup/metadata.json new file mode 100644 index 00000000..72882d05 --- /dev/null +++ b/bl-plugins/backup/metadata.json @@ -0,0 +1,10 @@ +{ + "author": "Bludit", + "email": "", + "website": "https://pro.bludit.com", + "version": "2.0", + "releaseDate": "2017-05-19", + "license": "Bludit PRO", + "compatible": "2.0", + "notes": "This plugin is delivery with Bludit PRO. Read more about it on https://pro.bludit.com" +} \ No newline at end of file diff --git a/bl-plugins/backup/plugin.php b/bl-plugins/backup/plugin.php new file mode 100644 index 00000000..99004b06 --- /dev/null +++ b/bl-plugins/backup/plugin.php @@ -0,0 +1,97 @@ +formButtons = false; + } + + // Install the plugin and create the workspace directory + public function install($position=0) + { + parent::install($position); + $workspace = $this->workspace(); + return mkdir($workspace, 0755, true); + } + + // Uninstall the plugin and delete the workspace directory + public function uninstall() + { + parent::uninstall(); + $workspace = $this->workspace(); + return Filesystem::deleteRecursive($workspace); + } + + // Redefine workspace + public function workspace() + { + return PATH_CONTENT.'backup'.DS; + } + + public function form() + { + $this->createBackup(); + } + + public function createBackup() + { + $currentDate = Date::current(BACKUP_DATE_FORMAT); + + // Create backup directory with the current date + $tmp = $this->workspace().'backup-'.$currentDate; + + // Copy pages directory + $destination = $tmp.DS.'pages'; + mkdir($destination, 0755, true); + $source = rtrim(PATH_PAGES, '/'); + Filesystem::copyRecursive($source, $destination); + + // Copy databases directory + $destination = $tmp.DS.'databases'; + mkdir($destination, 0755, true); + $source = rtrim(PATH_DATABASES, '/'); + Filesystem::copyRecursive($source, $destination); + + // Copy uploads directory + $destination = $tmp.DS.'uploads'; + mkdir($destination, 0755, true); + $source = rtrim(PATH_UPLOADS, '/'); + Filesystem::copyRecursive($source, $destination); + + // Compress backup directory + if (Filesystem::zip($tmp, $tmp.'.zip')) { + Filesystem::deleteRecursive($tmp); + } + } + + // Copy the content from the backup to /bl-content/ + private function replaceContent($idExecution) + { + $source = $this->workspace().$idExecution; + $dest = rtrim(PATH_CONTENT, '/'); + return Filesystem::copyRecursive($source, $dest); + } + + // Delete old backups until the $idExecution + private function cleanUp($idExecution) + { + $backups = $this->getBackupsDirectories(); + foreach ($backups as $dir) { + $backupIDExecution = basename($dir); + Filesystem::deleteRecursive($dir); + if($backupIDExecution==$idExecution) { + return true; + } + } + return true; + } + + // Returns array with all backups directories sorted by date newer first + private function getBackupsDirectories() + { + $workspace = $this->workspace(); + return Filesystem::listDirectories($workspace, $regex='*', $sortByDate=true); + } +} \ No newline at end of file diff --git a/bl-plugins/simplemde/plugin.php b/bl-plugins/simplemde/plugin.php index a2c11b8e..a8215c05 100644 --- a/bl-plugins/simplemde/plugin.php +++ b/bl-plugins/simplemde/plugin.php @@ -136,7 +136,7 @@ class pluginsimpleMDE extends Plugin { output = "\n'.PAGE_BREAK.'\n"; cm.replaceSelection(output); }, - className: "fa fa-minus-square-o", + className: "fa fa-scissors", title: "'.$Language->get('Pagebreak').'", }] });';