From bc77a2697602921440b4efe90b56166ca1ec5e94 Mon Sep 17 00:00:00 2001 From: Lucas Marcouiller Date: Sat, 21 May 2022 23:24:03 +0200 Subject: [PATCH 1/4] New : dol_move_dir --- htdocs/core/lib/files.lib.php | 63 +++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index 4e972b0be4e..b310eafd308 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -964,6 +964,69 @@ function dol_move($srcfile, $destfile, $newmask = 0, $overwriteifexists = 1, $te return $result; } +/** + * Move a directory into another name. + * + * @param string $srcdir Source directory + * @param string $destdir Destination directory + * @param int $overwriteifexists Overwrite directory if exists (1 by default) + * @param int $indexdatabase Index new file into database. + * @param int $renamedircontent Rename contents inside srcdir. + * + * @return boolean True if OK, false if KO +*/ +function dol_move_dir($srcdir, $destdir, $overwriteifexists = 1, $indexdatabase = 1, $renamedircontent = 1) +{ + + global $user, $db, $conf; + $result = false; + + dol_syslog("files.lib.php::dol_dir_move srcdir=".$srcdir." destdir=".$destdir." overwritifexists=".$overwriteifexists); + $srcexists = dol_is_dir($srcdir); + $srcbasename = basename($srcdir); + $destexists = dol_is_dir($destdir); + + if (!$srcexists) { + dol_syslog("files.lib.php::dol_dir_move srcdir does not exists. we ignore the move request."); + return false; + } + + if ($overwriteifexists || !$destexists) { + $newpathofsrcdir = dol_osencode($srcdir); + $newpathofdestdir = dol_osencode($destdir); + + $result = @rename($newpathofsrcdir, $newpathofdestdir); + + if ($result && $renamedircontent) { + if (file_exists($newpathofdestdir)) { + $destbasename = basename($newpathofdestdir); + $files = dol_dir_list($newpathofdestdir); + if (!empty($files) && is_array($files)) { + foreach ($files as $key => $file) { + if (!file_exists($file["fullname"])) continue; + $oldpath = $file["fullname"]; + + $newpath = str_replace($srcbasename, $destbasename, $oldpath); + if (!empty($newpath)) { + if (dol_is_dir($oldpath)) { + $res = dol_move_dir($oldpath, $newpath, $overwriteifexists, $indexdatabase, $renamedircontent); + } else { + $res = dol_move($oldpath, $newpath); + } + if (!$res) { + return $result; + } + } + } + $result = true; + } + } + } + } + + return $result; +} + /** * Unescape a file submitted by upload. * PHP escape char " (%22) or char ' (%27) into $FILES. From dbf66988a6ab79198f5109191eb79d8da215caa9 Mon Sep 17 00:00:00 2001 From: Lucas Marcouiller Date: Sat, 21 May 2022 23:24:18 +0200 Subject: [PATCH 2/4] fix style errors --- htdocs/core/lib/files.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index b310eafd308..7b0aa332ccb 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -972,7 +972,7 @@ function dol_move($srcfile, $destfile, $newmask = 0, $overwriteifexists = 1, $te * @param int $overwriteifexists Overwrite directory if exists (1 by default) * @param int $indexdatabase Index new file into database. * @param int $renamedircontent Rename contents inside srcdir. - * + * * @return boolean True if OK, false if KO */ function dol_move_dir($srcdir, $destdir, $overwriteifexists = 1, $indexdatabase = 1, $renamedircontent = 1) From f43f2ab6703905cd988cf87df70d76c87a5b005c Mon Sep 17 00:00:00 2001 From: Lucas Marcouiller Date: Sun, 22 May 2022 15:44:57 +0200 Subject: [PATCH 3/4] push phpunit to of the function --- htdocs/core/lib/files.lib.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index 7b0aa332ccb..f768833cd39 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -981,13 +981,13 @@ function dol_move_dir($srcdir, $destdir, $overwriteifexists = 1, $indexdatabase global $user, $db, $conf; $result = false; - dol_syslog("files.lib.php::dol_dir_move srcdir=".$srcdir." destdir=".$destdir." overwritifexists=".$overwriteifexists); + dol_syslog("files.lib.php::dol_move_dir srcdir=".$srcdir." destdir=".$destdir." overwritifexists=".$overwriteifexists." indexdatabase=".$indexdatabase." renamedircontent=".$renamedircontent); $srcexists = dol_is_dir($srcdir); $srcbasename = basename($srcdir); $destexists = dol_is_dir($destdir); if (!$srcexists) { - dol_syslog("files.lib.php::dol_dir_move srcdir does not exists. we ignore the move request."); + dol_syslog("files.lib.php::dol_move_dir srcdir does not exists. we ignore the move request."); return false; } @@ -1004,14 +1004,15 @@ function dol_move_dir($srcdir, $destdir, $overwriteifexists = 1, $indexdatabase if (!empty($files) && is_array($files)) { foreach ($files as $key => $file) { if (!file_exists($file["fullname"])) continue; - $oldpath = $file["fullname"]; + $filepath = $file["path"]; + $oldname = $file["name"]; - $newpath = str_replace($srcbasename, $destbasename, $oldpath); - if (!empty($newpath)) { - if (dol_is_dir($oldpath)) { - $res = dol_move_dir($oldpath, $newpath, $overwriteifexists, $indexdatabase, $renamedircontent); + $newname = str_replace($srcbasename, $destbasename, $oldname); + if (!empty($newname) && $newname !== $oldname) { + if ($file["type"] == "dir") { + $res = dol_move_dir($filepath.'/'.$oldname, $filepath.'/'.$newname, $overwriteifexists, $indexdatabase, $renamedircontent); } else { - $res = dol_move($oldpath, $newpath); + $res = dol_move($filepath.'/'.$oldname, $filepath.'/'.$newname); } if (!$res) { return $result; @@ -1023,7 +1024,6 @@ function dol_move_dir($srcdir, $destdir, $overwriteifexists = 1, $indexdatabase } } } - return $result; } From 5c757c30be1918465035b66ae2d1e9d8ad83429f Mon Sep 17 00:00:00 2001 From: Lucas Marcouiller Date: Sun, 22 May 2022 15:46:04 +0200 Subject: [PATCH 4/4] part2 of test --- test/phpunit/FilesLibTest.php | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/phpunit/FilesLibTest.php b/test/phpunit/FilesLibTest.php index 48a00c8214d..8c819c6de64 100644 --- a/test/phpunit/FilesLibTest.php +++ b/test/phpunit/FilesLibTest.php @@ -559,4 +559,72 @@ class FilesLibTest extends PHPUnit\Framework\TestCase $user->rights->facture->lire = $savpermlire; $user->rights->facture->creer = $savpermcreer; } + + /** + * testDolDirMove + * + * @return void + */ + public function testDolDirMove() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + // To test a move of empty directory that should work + $dirsrcpath = $conf->admin->dir_temp.'/directory'; + $dirdestpath = $conf->admin->dir_temp.'/directory2'; + $file=dirname(__FILE__).'/Example_import_company_1.csv'; + dol_mkdir($dirsrcpath); + dol_delete_dir_recursive($dirdestpath, 0, 1); + $result=dol_move_dir($dirsrcpath, $dirdestpath, 1, 1, 1); + print __METHOD__." result=".$result."\n"; + $this->assertTrue($result, 'move of directory with empty directory'); + + // To test a move on existing directory with overwrite + dol_mkdir($dirsrcpath); + $result=dol_move_dir($dirsrcpath, $dirdestpath, 1, 1, 1); + print __METHOD__." result=".$result."\n"; + $this->assertTrue($result, 'move of directory on existing directory with empty directory'); + + // To test a move on existing directory without overwrite + dol_mkdir($dirsrcpath); + $result=dol_move_dir($dirsrcpath, $dirdestpath, 0, 1, 1); + print __METHOD__." result=".$result."\n"; + $this->assertFalse($result, 'move of directory on existing directory without overwrite'); + + // To test a move with a file to rename in src directory + dol_mkdir($dirsrcpath); + dol_delete_dir_recursive($dirdestpath, 0, 1); + dol_copy($file, $dirsrcpath.'/directory_file.csv'); + $result=dol_move_dir($dirsrcpath, $dirdestpath, 1, 1, 1); + print __METHOD__." result=".$result."\n"; + $this->assertTrue($result, 'move of directory with file in directory'); + + // To test a move without a file to rename in src directory + dol_mkdir($dirsrcpath); + dol_delete_dir_recursive($dirdestpath, 0, 1); + dol_copy($file, $dirsrcpath.'/file.csv'); + $result=dol_move_dir($dirsrcpath, $dirdestpath, 1, 1, 1); + print __METHOD__." result=".$result."\n"; + $this->assertTrue($result, 'move of directory with file whitout rename needed in directory'); + + // To test a move with a directory to rename in src directory + dol_mkdir($dirsrcpath); + dol_delete_dir_recursive($dirdestpath, 0, 1); + dol_mkdir($dirsrcpath.'/directory'); + $result=dol_move_dir($dirsrcpath, $dirdestpath, 1, 1, 1); + print __METHOD__." result=".$result."\n"; + $this->assertTrue($result, 'move of directory with file with rename needed in directory'); + + // To test a move without a directory to rename in src directory + dol_mkdir($dirsrcpath); + dol_delete_dir_recursive($dirdestpath, 0, 1); + dol_mkdir($dirsrcpath.'/notorename'); + $result=dol_move_dir($dirsrcpath, $dirdestpath, 1, 1, 1); + print __METHOD__." result=".$result."\n"; + $this->assertTrue($result, 'move of directory with directory whitout rename needed in directory'); + } }