From 9e5955ab2edf838c26852bd01a591397a3b727ec Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 20:38:00 +0200 Subject: [PATCH 01/16] Refining description. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4ede947..c1b5c67 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Simple File Manager -Simple file management for PHP. When you're looking for simple syntax to do complex things. It does everything recursively by default and with a minimum amount of complaining. +Simple file management for PHP. When you're looking for simple syntax to do complex things. The methods do everything recursively by default and with a minimum amount of complaining. ## Methods From 880398f539bf1316f0ab999e74618549ca6b801b Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:00:14 +0200 Subject: [PATCH 02/16] Updating comments. --- simple-file-manager.php | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index 18eb1b4..2dda432 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -3,11 +3,11 @@ class sfm { /** - * Creates a zip file from a file or a folder recursively without a full nested folder structure inside the zip file + * Creates a zip file from a file or a folder recursively (without a full nested folder structure inside the zip file). * Based on: http://stackoverflow.com/a/1334949/3073849 - * @param string $source The path of the folder you want to zip - * @param string $destination The path of the zip file you want to create - * @return bool Returns TRUE on success or FALSE on failure. + * @param string $source The path of the folder you want to zip + * @param string $destination The path of the zip file you want to create + * @return bool Returns TRUE on success or FALSE on failure. */ public static function zip( $source, $destination ) { @@ -48,13 +48,13 @@ public static function zip( $source, $destination ) { return $zip->close(); } - + /** - * Extracts a zip file to given folder. Overwrite deletes an existing destination folder and replaces it with the content of the zip file. - * @param string $source The path of the zip file you want to extract - * @param string $destination The path of the folder you want to extract to - * @param bool $overwrite (Optional) Whether to overwrite an existing destination folder - * @return bool Returns TRUE on success or FALSE on failure. + * Extracts a zip file to a given folder. If optional overwrite is true, then the method deletes an existing destination folder and replaces it with the contents of the zip file. + * @param string $source The path of the zip file you want to extract + * @param string $destination The path of the folder you want to extract to + * @param bool $overwrite (Optional) Whether to overwrite an existing destination folder + * @return bool Returns TRUE on success or FALSE on failure. **/ public static function unzip( $source, $destination, $overwrite = false ) { @@ -99,8 +99,8 @@ public static function unzip( $source, $destination, $overwrite = false ) { /** * Delete a file, or recursively delete a folder and it's contents * Based on: http://stackoverflow.com/a/15111679/3073849 - * @param string $source The path of the file or folder - * @return bool Returns TRUE on success or if file already deleted or FALSE on failure. + * @param string $source The path of the file or folder + * @return bool Returns TRUE on success or if file already deleted or FALSE on failure. **/ public static function delete( $source ) { @@ -134,10 +134,10 @@ public static function delete( $source ) { /** * Copy a file, or recursively copy a folder and its contents * Based on: http://stackoverflow.com/a/12763962/3073849 - * @param string $source Source path - * @param string $destination Destination path - * @param array $excludes (Optional) Name of files and folders to exclude from copying - * @return bool Returns true on success, false on failure + * @param string $source Source path + * @param string $destination Destination path + * @param array $excludes (Optional) An array containing the names of files and folders to exclude from copying as strings + * @return bool Returns TRUE on success, FALSE on failure **/ public static function copy( $source, $destination, $excludes = array() ) { @@ -166,6 +166,7 @@ public static function copy( $source, $destination, $excludes = array() ) { while ( false !== $entry = $dir->read() ) { // Skip pointers + // TODO: use recursive iterators if ( '.' === $entry || '..' === $entry ) { continue; } @@ -182,9 +183,9 @@ public static function copy( $source, $destination, $excludes = array() ) { /** * Creates a folder recursively. - * @param string $path The path of the folder to create - * @param int $permissions (Optional) The mode given for the folder. The default mode (0764) is less permissive than the php default of (0777). - * @return bool Returns true if the folder already existed or if it was created on successfully, false on failure. + * @param string $path The path of the folder to create + * @param int $permissions (Optional) The mode given for the folder. The default mode (0764) is less permissive than the php default of (0777). + * @return bool Returns TRUE if the folder already existed or if it was created on successfully, FALSE on failure. */ public static function mkdir( $path, $permissions = SFM_DEFAULT_PERMISSIONS ) { From a7fbe167d8ec65c6adac6af6178d45b95b8e1136 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:00:33 +0200 Subject: [PATCH 03/16] Changing class name. --- simple-file-manager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index 2dda432..7c40758 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -1,6 +1,6 @@ Date: Tue, 3 Jan 2017 21:01:27 +0200 Subject: [PATCH 04/16] Renaming delete method. --- simple-file-manager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index 7c40758..1782d48 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -102,7 +102,7 @@ public static function unzip( $source, $destination, $overwrite = false ) { * @param string $source The path of the file or folder * @return bool Returns TRUE on success or if file already deleted or FALSE on failure. **/ - public static function delete( $source ) { + public static function rm( $source ) { if ( ! file_exists( $source ) ) { return true; From 047e06453d93c724bc6ae7edd09665e8b641c295 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:04:01 +0200 Subject: [PATCH 05/16] Adding delet as alias for rm. --- simple-file-manager.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/simple-file-manager.php b/simple-file-manager.php index 1782d48..96f46d5 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -131,6 +131,13 @@ public static function rm( $source ) { } + /** + * Alias for self:rm + */ + public static function delete( $source ) { + return self:rm( $source ); + } + /** * Copy a file, or recursively copy a folder and its contents * Based on: http://stackoverflow.com/a/12763962/3073849 From 6a105b1655803e61fb5040629bcc7fa4db340440 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:07:55 +0200 Subject: [PATCH 06/16] Adding cp as alias for copy. --- simple-file-manager.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/simple-file-manager.php b/simple-file-manager.php index 96f46d5..f2e2e72 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -188,6 +188,13 @@ public static function copy( $source, $destination, $excludes = array() ) { return true; } + /** + * Alias for self:copy + */ + public static function cp( $source, $destination, $excludes = array() ) { + return self:copy( $source, $destination, $excludes = array() ); + } + /** * Creates a folder recursively. * @param string $path The path of the folder to create From 61f6b48ed9b10d28b25a0bc987c66f2c232347f0 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:10:16 +0200 Subject: [PATCH 07/16] Check that folder exists instead of file exists. --- simple-file-manager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index f2e2e72..d401729 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -204,7 +204,7 @@ public static function cp( $source, $destination, $excludes = array() ) { public static function mkdir( $path, $permissions = SFM_DEFAULT_PERMISSIONS ) { // Folder exists already, return true - if ( file_exists( $path ) ) { + if ( is_dir( $path ) ) { return true; } From 55980adf946ae43d17c4cfe1ac46ceaa40875ee9 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:12:45 +0200 Subject: [PATCH 08/16] Changing default permission mode. --- simple-file-manager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index d401729..9745eb9 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -198,7 +198,7 @@ public static function cp( $source, $destination, $excludes = array() ) { /** * Creates a folder recursively. * @param string $path The path of the folder to create - * @param int $permissions (Optional) The mode given for the folder. The default mode (0764) is less permissive than the php default of (0777). + * @param int $permissions (Optional) The mode given for the folder. The default mode (0774) is less permissive than the php default of (0777). * @return bool Returns TRUE if the folder already existed or if it was created on successfully, FALSE on failure. */ public static function mkdir( $path, $permissions = SFM_DEFAULT_PERMISSIONS ) { @@ -216,5 +216,5 @@ public static function mkdir( $path, $permissions = SFM_DEFAULT_PERMISSIONS ) { // Set default file/folder permission mode if not already defined if ( ! defined( 'SFM_DEFAULT_PERMISSIONS' ) ) { - define( 'SFM_DEFAULT_PERMISSIONS', 0764 ); + define( 'SFM_DEFAULT_PERMISSIONS', 0774 ); } \ No newline at end of file From f550095028a5a1802acc8f62e3894263d8d97b76 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:17:24 +0200 Subject: [PATCH 09/16] Clearing up comments. --- simple-file-manager.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index 9745eb9..b867ae7 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -4,7 +4,7 @@ class Sfm { /** * Creates a zip file from a file or a folder recursively (without a full nested folder structure inside the zip file). - * Based on: http://stackoverflow.com/a/1334949/3073849 + * @see http://stackoverflow.com/a/1334949/3073849 * @param string $source The path of the folder you want to zip * @param string $destination The path of the zip file you want to create * @return bool Returns TRUE on success or FALSE on failure. @@ -98,7 +98,7 @@ public static function unzip( $source, $destination, $overwrite = false ) { /** * Delete a file, or recursively delete a folder and it's contents - * Based on: http://stackoverflow.com/a/15111679/3073849 + * @see http://stackoverflow.com/a/15111679/3073849 * @param string $source The path of the file or folder * @return bool Returns TRUE on success or if file already deleted or FALSE on failure. **/ @@ -140,7 +140,7 @@ public static function delete( $source ) { /** * Copy a file, or recursively copy a folder and its contents - * Based on: http://stackoverflow.com/a/12763962/3073849 + * @see http://stackoverflow.com/a/12763962/3073849 * @param string $source Source path * @param string $destination Destination path * @param array $excludes (Optional) An array containing the names of files and folders to exclude from copying as strings From 14332e9f6fc44626482b22aa3f73b7aa9538f228 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:21:30 +0200 Subject: [PATCH 10/16] Rename path variable. --- simple-file-manager.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index b867ae7..1106a5c 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -50,7 +50,8 @@ public static function zip( $source, $destination ) { } /** - * Extracts a zip file to a given folder. If optional overwrite is true, then the method deletes an existing destination folder and replaces it with the contents of the zip file. + * Extracts a zip file to a given folder. If optional overwrite is true, then the method deletes + * an existing destination folder and replaces it with the contents of the zip file. * @param string $source The path of the zip file you want to extract * @param string $destination The path of the folder you want to extract to * @param bool $overwrite (Optional) Whether to overwrite an existing destination folder @@ -99,34 +100,34 @@ public static function unzip( $source, $destination, $overwrite = false ) { /** * Delete a file, or recursively delete a folder and it's contents * @see http://stackoverflow.com/a/15111679/3073849 - * @param string $source The path of the file or folder + * @param string $path The path of the file or folder * @return bool Returns TRUE on success or if file already deleted or FALSE on failure. **/ - public static function rm( $source ) { + public static function rm( $path ) { - if ( ! file_exists( $source ) ) { + if ( ! file_exists( $path ) ) { return true; } - if ( is_dir( $source ) ) { + if ( is_dir( $path ) ) { foreach ( new RecursiveIteratorIterator( - new RecursiveDirectoryIterator( $source, FilesystemIterator::SKIP_DOTS ), - RecursiveIteratorIterator::CHILD_FIRST ) as $path ) { + new RecursiveDirectoryIterator( $path, FilesystemIterator::SKIP_DOTS ), + RecursiveIteratorIterator::CHILD_FIRST ) as $child_path ) { - if ( $path->isDir() && ! $path->isLink() ) { - rmdir( $path->getPathname() ); + if ( $child_path->isDir() && ! $child_path->isLink() ) { + rmdir( $child_path->getPathname() ); } else { - unlink( $path->getPathname() ); + unlink( $child_path->getPathname() ); } } - return rmdir( $source ); + return rmdir( $path ); } else { - return unlink( $source ); + return unlink( $path ); } } From eb9f24793525863d43d91c689b112165ddb90047 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:30:07 +0200 Subject: [PATCH 11/16] Cleaning up code. --- simple-file-manager.php | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index 1106a5c..a6aaaed 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -33,16 +33,13 @@ public static function zip( $source, $destination ) { $path = realpath( $path ); if ( is_dir( $path ) ) { - $zip->addEmptyDir( str_replace( $source . '/', '', $path . '/' ) ); - } - - elseif ( is_file( $path ) ) { - $zip->addFile( $path, str_replace( $source . '/', '', $path ) ); + $zip->addEmptyDir( str_replace( "$source/", '', "$path/" ) ); + } elseif ( is_file( $path ) ) { + $zip->addFile( $path, str_replace( "$source/", '', $path ) ); } } - } - elseif ( is_file( $source ) ) { + } elseif ( is_file( $source ) ) { $zip->addFile( $source, basename( $source ) ); } @@ -74,11 +71,10 @@ public static function unzip( $source, $destination, $overwrite = false ) { if ( ! self::mkdir( $destination ) ) { return false; } - } - elseif ( $overwrite ) { + } elseif ( $overwrite ) { - self::delete( $destination ); + self::rm( $destination ); if ( ! self::mkdir( $destination ) ) { return false; @@ -91,7 +87,7 @@ public static function unzip( $source, $destination, $overwrite = false ) { $resource_fork = $destination . '/__MACOSX/'; if ( file_exists( $resource_fork ) ) { - self::delete( $resource_fork ); + self::rm( $resource_fork ); } return $zip->close(); @@ -117,16 +113,14 @@ public static function rm( $path ) { if ( $child_path->isDir() && ! $child_path->isLink() ) { rmdir( $child_path->getPathname() ); - } - else { + } else { unlink( $child_path->getPathname() ); } } return rmdir( $path ); - } - else { + } else { return unlink( $path ); } From 45b91b8c9aa82619299150a272300f408a1e4d8a Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:30:45 +0200 Subject: [PATCH 12/16] Continueing code clean up. --- simple-file-manager.php | 1 + 1 file changed, 1 insertion(+) diff --git a/simple-file-manager.php b/simple-file-manager.php index a6aaaed..e2cd43a 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -165,6 +165,7 @@ public static function copy( $source, $destination, $excludes = array() ) { // Loop through the folder $dir = dir( $source ); + while ( false !== $entry = $dir->read() ) { // Skip pointers From 73a57f119c07c763746d6a5bbc3a4b31b7ff9295 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:50:49 +0200 Subject: [PATCH 13/16] Adding comments. --- simple-file-manager.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index e2cd43a..a69a015 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -11,20 +11,25 @@ class Sfm { */ public static function zip( $source, $destination ) { + // Check that zip extensions is loaded and the source file/folder exists if ( ! extension_loaded( 'zip' ) || ! file_exists( $source ) ) { return false; } + // Create the zip $zip = new ZipArchive(); if ( ! $zip->open( $destination, ZIPARCHIVE::CREATE ) ) { return false; } + // Clean up the path $source = str_replace( '\\', '/', realpath( $source ) ); + // If the source is a folder, add the files recursively if ( is_dir( $source ) ) { + // Loop through the source folder foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $source, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::SELF_FIRST ) as $path ) { @@ -33,16 +38,19 @@ public static function zip( $source, $destination ) { $path = realpath( $path ); if ( is_dir( $path ) ) { + // If a folder, add a coresponding placeholder folder to the zip $zip->addEmptyDir( str_replace( "$source/", '', "$path/" ) ); } elseif ( is_file( $path ) ) { + // If a file, add it to the zip $zip->addFile( $path, str_replace( "$source/", '', $path ) ); } } - } elseif ( is_file( $source ) ) { + // Otherwise if the source is a file add it to the zip alone $zip->addFile( $source, basename( $source ) ); } + // Finish up return $zip->close(); } @@ -56,31 +64,39 @@ public static function zip( $source, $destination ) { **/ public static function unzip( $source, $destination, $overwrite = false ) { + // Check that zip extensions is loaded and the source file/folder exists if ( ! extension_loaded( 'zip' ) || ! file_exists( $source ) ) { return false; } + // Create the zip $zip = new ZipArchive(); + // Check if able to open zip if ( ! $zip->open( $source ) ) { return false; } + // If the destination folder doesn't exist try to create it if ( ! is_dir( $destination ) ) { + // Try to create the destination folder if ( ! self::mkdir( $destination ) ) { return false; } } elseif ( $overwrite ) { + // If the destination folder needs to be overwritten, delete it self::rm( $destination ); + // Try to re-create the folder if ( ! self::mkdir( $destination ) ) { return false; } } + // Exctract the zip to the destination folder $zip->extractTo( $destination ); // If we have a resource fork, get rid of it @@ -90,6 +106,7 @@ public static function unzip( $source, $destination, $overwrite = false ) { self::rm( $resource_fork ); } + // Finish up return $zip->close(); } @@ -101,26 +118,33 @@ public static function unzip( $source, $destination, $overwrite = false ) { **/ public static function rm( $path ) { + // If file doesn't exist we've achieved what is needed if ( ! file_exists( $path ) ) { return true; } + // If the given path is a folder, we'll delete the contents recursively if ( is_dir( $path ) ) { + // Loop through the folder starting from the childs (rmdir only works on empty folders) foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST ) as $child_path ) { if ( $child_path->isDir() && ! $child_path->isLink() ) { + // Remove a folder, which is now empty rmdir( $child_path->getPathname() ); } else { + // Remove a file unlink( $child_path->getPathname() ); } } + // Finally remove the given folder return rmdir( $path ); } else { + // Otherwise if the given path is a file, delete the file return unlink( $path ); } @@ -178,7 +202,7 @@ public static function copy( $source, $destination, $excludes = array() ) { self::copy( "$source/$entry", "$destination/$entry", $excludes ); } - // Clean up + // Finish up $dir->close(); return true; @@ -204,6 +228,7 @@ public static function mkdir( $path, $permissions = SFM_DEFAULT_PERMISSIONS ) { return true; } + // Create the folder return mkdir( $path, $permissions, true ); } From 457cc7a37def80d16bba50a387d18004f46e21af Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 21:54:03 +0200 Subject: [PATCH 14/16] Removing unneeded linebreaks. --- simple-file-manager.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index a69a015..e128dd3 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -147,7 +147,6 @@ public static function rm( $path ) { // Otherwise if the given path is a file, delete the file return unlink( $path ); } - } /** @@ -232,7 +231,6 @@ public static function mkdir( $path, $permissions = SFM_DEFAULT_PERMISSIONS ) { return mkdir( $path, $permissions, true ); } - } // Set default file/folder permission mode if not already defined From 3a1703525bdc669861cebecd3cd2fd740506c9e9 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 22:00:53 +0200 Subject: [PATCH 15/16] Added a comment. --- simple-file-manager.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/simple-file-manager.php b/simple-file-manager.php index e128dd3..4fd9c4b 100644 --- a/simple-file-manager.php +++ b/simple-file-manager.php @@ -1,5 +1,9 @@ read() ) { // Skip pointers - // TODO: use recursive iterators if ( '.' === $entry || '..' === $entry ) { continue; } From 166bf7422081de058d33d59a19df1746dfba8483 Mon Sep 17 00:00:00 2001 From: Harri Heljala Date: Tue, 3 Jan 2017 22:05:01 +0200 Subject: [PATCH 16/16] Updating composer info. --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6d99976..31f2c37 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,14 @@ { "name": "html2wp/simple-file-manager", "description": "Simple file management for PHP", + "version": "1.1.0", "license": "GPL-2.0+", - "homepage": "http://htmltowordpress.io", + "homepage": "https://htmltowordpress.io", "authors": [ { "name": "Harri Heljala", "email": "harri@htmltowordpress.io", - "homepage": "http://helja.la", + "homepage": "https://helja.la", "role": "Developer" } ],