From 13a4ab3cf978c7a8503fd67f1815e8ea0a7579ca Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Sat, 9 Feb 2019 14:44:30 +0100 Subject: [PATCH] Miglioramenti vari backup --- lib/functions.php | 22 ++----- modules/backups/actions.php | 7 +++ modules/backups/edit.php | 114 +++++++++++++++++++++++------------- modules/backups/modutil.php | 46 --------------- src/Backup.php | 8 +-- src/Util/FileSystem.php | 114 ++++++++++++++++++++++++++++++++++++ 6 files changed, 200 insertions(+), 111 deletions(-) delete mode 100644 modules/backups/modutil.php create mode 100644 src/Util/FileSystem.php diff --git a/lib/functions.php b/lib/functions.php index 69c4b3384..5d1bda6e3 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -72,22 +72,7 @@ function delete($files) */ function directory($path) { - if (is_dir($path) && is_writable($path)) { - return true; - } elseif (!is_dir($path)) { - // Filesystem Symfony - $fs = new Symfony\Component\Filesystem\Filesystem(); - - // Tentativo di creazione - try { - $fs->mkdir($path); - - return true; - } catch (Symfony\Component\Filesystem\Exception\IOException $e) { - } - } - - return false; + return Util\FileSystem::directory($path); } /** @@ -410,6 +395,7 @@ function operationLog($operation, array $ids = [], array $options = []) * * @return string */ -function clean($string, $permitted = '') { - return preg_replace('/[^A-Za-z0-9'.$permitted.']/', '', $string); // Removes special chars. +function clean($string, $permitted = '') +{ + return preg_replace('/[^A-Za-z0-9'.$permitted.']/', '', $string); // Removes special chars. } diff --git a/modules/backups/actions.php b/modules/backups/actions.php index 3f23064d9..3242b7575 100644 --- a/modules/backups/actions.php +++ b/modules/backups/actions.php @@ -36,6 +36,13 @@ switch (filter('op')) { flash()->error(tr('Errore durante la creazione del backup!').' '.str_replace('_DIR_', '"'.$backup_dir.'"', tr('Verifica che la cartella _DIR_ abbia i permessi di scrittura!'))); } + break; + + case 'size': + $file = filter('file'); + + echo Util\FileSystem::size($backup_dir.'/'.$file); + break; } diff --git a/modules/backups/edit.php b/modules/backups/edit.php index e2b6723d6..8ad551948 100644 --- a/modules/backups/edit.php +++ b/modules/backups/edit.php @@ -3,6 +3,7 @@ include_once __DIR__.'/../../core.php'; $backup_dir = Backup::getDirectory(); +$backups = Backup::getList(); echo '

'.tr('Il backup è molto importante perché permette di creare una copia della propria installazione e relativi dati per poterla poi ripristinare in seguito a errori, cancellazioni accidentali o guasti hardware').'.

'; @@ -35,26 +36,10 @@ if (!empty($backup_dir)) { $message = tr('Sembra che tu non abbia ancora specificato un percorso per il backup').'.'; } -echo ' -
-
-
-

'.$message.'

-

'.tr('Dimensione totale: _SPAZIO_', [ - '_SPAZIO_' => format_size(foldersize($backup_dir)), - ]).'
'.tr('Numero di backup: _NUM_', [ - '_NUM_' => count(Backup::getList()), - ]).'
- '.tr('Puoi modificare il percorso di backup dal tuo file _FILE_', [ - '_FILE_' => 'config.inc.php', - ]).'

-
-
'; - -// Ripristino backup +// Operazioni JavaScript echo ' '; +echo ' +
+
+
+

'.$message.'

+

'.tr('Dimensione totale: _SPAZIO_', [ + '_SPAZIO_' => '', + ]).'

+

'.tr('Numero di backup: _NUM_', [ + '_NUM_' => count($backups), + ]).'

+

'.tr('Puoi modificare il percorso di backup dal tuo file _FILE_', [ + '_FILE_' => 'config.inc.php', + ]).'

+
+
+ + '; + $upload_max_filesize = ini_get('upload_max_filesize'); $max_execution_time = ini_get('max_execution_time'); echo ' @@ -104,7 +143,6 @@ if (file_exists($backup_dir)) { $backups_zip = []; $backups_file = []; - $backups = Backup::getList(); foreach ($backups as $backup) { if (ends_with($backup, '.zip')) { $backups_zip[] = $backup; @@ -126,7 +164,7 @@ if (file_exists($backup_dir)) {

'.tr('Backup compressi').'

'; if (!empty($backups_zip)) { - foreach ($backups_zip as $backup) { + foreach ($backups_zip as $id => $backup) { $name = basename($backup); $info = Backup::readName($backup); @@ -141,9 +179,13 @@ if (file_exists($backup_dir)) { ]).'

'.tr('Nome del file').': '.$name.'
- '.tr('Dimensione').': '.format_size(filesize($backup)).' + '.tr('Dimensione').':

- + + + '.tr('Scarica').'
@@ -187,8 +229,12 @@ if (file_exists($backup_dir)) { ]).'

'.tr('Nome del file').': '.$name.'
- '.tr('Dimensione').': '.format_size(filesize($backup)).' + '.tr('Dimensione').':

+ + '.tr('Non scaricabile').' @@ -219,26 +265,10 @@ if (file_exists($backup_dir)) {
'.tr('La cartella di backup non esiste!').' '.tr('Non è possibile eseguire i backup!').'
'; } +// Creazione backup if (!empty($backup_dir)) { - // Creazione backup echo ' - + -
- -'; +
'; } diff --git a/modules/backups/modutil.php b/modules/backups/modutil.php deleted file mode 100644 index 215d9419a..000000000 --- a/modules/backups/modutil.php +++ /dev/null @@ -1,46 +0,0 @@ -"." && $t<>"..") { - $currentFile = $cleanPath . $t; - if (is_dir($currentFile)) { - $size = foldersize($currentFile); - $total_size += $size; - } - else { - $size = filesize($currentFile); - $total_size += $size; - } - } - } - - return $total_size; -} - - -function format_size($size) { - $units = explode(' ', 'B KB MB GB TB PB'); - - $mod = 1024; - - for ($i = 0; $size > $mod; $i++) { - $size /= $mod; - } - - $endIndex = strpos($size, ".")+3; - - return substr( $size, 0, $endIndex).' '.$units[$i]; -} - -?> \ No newline at end of file diff --git a/src/Backup.php b/src/Backup.php index 525612b4d..2e2dc8346 100644 --- a/src/Backup.php +++ b/src/Backup.php @@ -28,10 +28,7 @@ class Backup $result = App::getConfig()['backup_dir']; $result = rtrim($result, '/'); - if (!directory($result)){ - $fileSystem->mkdir($result, 0700); - } - else if (!is_writable($result)) { + if (!directory($result) || !is_writable($result)) { throw new UnexpectedValueException(); } @@ -59,7 +56,8 @@ class Backup $backups = Symfony\Component\Finder\Finder::create() ->name('/^'.$pattern.'/') ->sortByName() - ->in(self::getDirectory()); + ->in(self::getDirectory()) + ->depth('== 0'); $results = []; foreach ($backups as $backup) { diff --git a/src/Util/FileSystem.php b/src/Util/FileSystem.php new file mode 100644 index 000000000..cf22200e0 --- /dev/null +++ b/src/Util/FileSystem.php @@ -0,0 +1,114 @@ +mkdir($path); + + return true; + } catch (\Symfony\Component\Filesystem\Exception\IOException $e) { + } + } + + return false; + } + + /** + * Individua la dimensione di una cartella indicata. + * + * @param string $path + * + * @return int + */ + public static function folderSize($path) + { + $total = 0; + $path = realpath($path); + + if ($path !== false && $path != '' && file_exists($path)) { + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object) { + $total += $object->getSize(); + } + } + + return $total; + } + + /** + * Individua la dimensione del file indicato. + * + * @param string $path + * + * @return int + */ + public static function fileSize($path) + { + $path = realpath($path); + + return filesize($path); + } + + /** + * Individua la dimensione del file o cartella indicati, formattati in modo standard. + * + * @param string $path + * + * @return string + */ + public static function size($path) + { + $path = realpath($path); + + $result = is_file($path) ? self::fileSize($path) : self::folderSize($path); + + return self::formatBytes($result); + } + + /** + * Formatta i byte nell'unità di misura relativa. + * + * @param int $bytes + * @param int $precision + * + * @return string + */ + public static function formatBytes($bytes, $precision = 2) + { + $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; + + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); + + // Uncomment one of the following alternatives + $bytes /= pow(1024, $pow); + // $bytes /= (1 << (10 * $pow)); + + return round($bytes, $precision).' '.$units[$pow]; + } +}