diff --git a/admin/src/Backupper.php b/admin/src/Backupper.php new file mode 100644 index 00000000..f60f38dd --- /dev/null +++ b/admin/src/Backupper.php @@ -0,0 +1,80 @@ +options = array_merge($this->defaults(), $options); + } + + public function defaults() + { + return array( + 'maxExecutionTime' => 180, + 'name' => 'formwork-backup', + 'path' => Formwork::instance()->option('backup.path'), + 'ignore' => array( + '.git/*', + '*.DS_Store', + '*.gitignore', + '*.gitkeep', + 'admin/node_modules/*', + 'backup/*' + ) + ); + } + + public function backup() + { + $previousMaxExecutionTime = ini_set('max_execution_time', $this->options['maxExecutionTime']); + + $source = ROOT_PATH; + + $path = $this->options['path']; + if (!FileSystem::exists($this->options['path'])) { + FileSystem::createDirectory($this->options['path'], true); + } + + $destination = $path . $this->options['name'] . '-' . date(self::DATE_FORMAT) . '.zip'; + + $files = FileSystem::scanRecursive($source, true); + $files = array_filter($files, function ($item) use ($source) { + return $this->isCopiable(substr($item, strlen($source))); + }); + + $zip = new ZipArchive(); + + if ($zip->open($destination, ZipArchive::CREATE)) { + foreach ($files as $file) { + $zip->addFile($file, substr($file, strlen($source))); + } + $zip->close(); + } + + if ($previousMaxExecutionTime !== false) { + ini_set('max_execution_time', $previousMaxExecutionTime); + } + + return $destination; + } + + protected function isCopiable($file) + { + foreach ($this->options['ignore'] as $pattern) { + if (fnmatch($pattern, $file)) { + return false; + } + } + return true; + } +} diff --git a/formwork/Core/Formwork.php b/formwork/Core/Formwork.php index 16f86e74..1bb24996 100755 --- a/formwork/Core/Formwork.php +++ b/formwork/Core/Formwork.php @@ -86,6 +86,7 @@ class Formwork 'cache.enabled' => false, 'cache.path' => ROOT_PATH . 'cache' . DS, 'cache.time' => 604800, + 'backup.path' => ROOT_PATH . 'backup' . DS, 'admin.enabled' => true, 'admin.lang' => 'en', 'admin.logout_redirect' => 'login' diff --git a/formwork/Utils/FileSystem.php b/formwork/Utils/FileSystem.php index ca7371cf..c2c4c7bc 100755 --- a/formwork/Utils/FileSystem.php +++ b/formwork/Utils/FileSystem.php @@ -300,6 +300,20 @@ class FileSystem return $items; } + public static function scanRecursive($path, $all = false) + { + $list = array(); + $path = static::normalize($path); + foreach (FileSystem::scan($path, $all) as $item) { + if (FileSystem::isDirectory($path . $item)) { + $list = array_merge($list, static::scanRecursive($path . $item, $all)); + } else { + $list[] = $path . $item; + } + } + return $list; + } + public static function listFiles($path = null, $all = false) { $path = static::normalize($path);