From fb85f17ef4d9cb8a0bad6193ec4f5ad0d207dfa7 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 14 Mar 2024 12:09:35 +0100 Subject: [PATCH] FIX: Better error management in loading a website template --- htdocs/core/lib/files.lib.php | 23 +++++++++++++++-------- htdocs/core/lib/functions.lib.php | 2 +- htdocs/website/class/website.class.php | 19 ++++++++++++++----- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index 960d9ee7eec0f..5b18aee94af92 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -857,14 +857,14 @@ function dol_copy($srcfile, $destfile, $newmask = '0', $overwriteifexists = 1, $ /** * Copy a dir to another dir. This include recursive subdirectories. * - * @param string $srcfile Source file (a directory) - * @param string $destfile Destination file (a directory) - * @param string $newmask Mask for new file ('0' by default means getDolGlobalString('MAIN_UMASK')). Example: '0666' - * @param int $overwriteifexists Overwrite file if exists (1 by default) + * @param string $srcfile Source file (a directory) + * @param string $destfile Destination file (a directory) + * @param string $newmask Mask for new file ('0' by default means getDolGlobalString('MAIN_UMASK')). Example: '0666' + * @param int $overwriteifexists Overwrite file if exists (1 by default) * @param array $arrayreplacement Array to use to replace filenames with another one during the copy (works only on file names, not on directory names). - * @param int $excludesubdir 0=Do not exclude subdirectories, 1=Exclude subdirectories, 2=Exclude subdirectories if name is not a 2 chars (used for country codes subdirectories). - * @param string[] $excludefileext Exclude some file extensions - * @return int Return integer <0 if error, 0 if nothing done (all files already exists and overwriteifexists=0), >0 if OK + * @param int $excludesubdir 0=Do not exclude subdirectories, 1=Exclude subdirectories, 2=Exclude subdirectories if name is not a 2 chars (used for country codes subdirectories). + * @param string[] $excludefileext Exclude some file extensions + * @return int Return integer <0 if error, 0 if nothing done (all files already exists and overwriteifexists=0), >0 if OK * @see dol_copy() */ function dolCopyDir($srcfile, $destfile, $newmask, $overwriteifexists, $arrayreplacement = null, $excludesubdir = 0, $excludefileext = null) @@ -878,6 +878,7 @@ function dolCopyDir($srcfile, $destfile, $newmask, $overwriteifexists, $arrayrep } $destexists = dol_is_dir($destfile); + //if (! $overwriteifexists && $destexists) return 0; // The overwriteifexists is for files only, so propagated to dol_copy only. if (!$destexists) { @@ -888,7 +889,13 @@ function dolCopyDir($srcfile, $destfile, $newmask, $overwriteifexists, $arrayrep $dirmaskdec = octdec(getDolGlobalString('MAIN_UMASK')); } $dirmaskdec |= octdec('0200'); // Set w bit required to be able to create content for recursive subdirs files - dol_mkdir($destfile, '', decoct($dirmaskdec)); + + $result = dol_mkdir($destfile, '', decoct($dirmaskdec)); + + if (!dol_is_dir($destfile)) { + // The output directory does not exists and we failed to create it. So we stop here. + return -1; + } } $ossrcfile = dol_osencode($srcfile); diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index bcc2e9864e925..e74be328a4bdf 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -7424,7 +7424,7 @@ function dol_mkdir($dir, $dataroot = '', $newmask = '') } $dirmaskdec |= octdec('0111'); // Set x bit required for directories if (!@mkdir($ccdir_osencoded, $dirmaskdec)) { - // Si le is_dir a renvoye une fausse info, alors on passe ici. + // If the is_dir has returned a false information, we arrive here dol_syslog("functions.lib::dol_mkdir: Fails to create directory '".$ccdir."' or directory already exists.", LOG_WARNING); $nberr++; } else { diff --git a/htdocs/website/class/website.class.php b/htdocs/website/class/website.class.php index 47d572654be21..7bfe8b0373e97 100644 --- a/htdocs/website/class/website.class.php +++ b/htdocs/website/class/website.class.php @@ -1262,8 +1262,17 @@ public function importWebSite($pathtofile) $error++; } - dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/medias/image/websitekey', $conf->website->dir_output.'/'.$object->ref.'/medias/image/'.$object->ref, 0, 1); // Medias can be shared, do not overwrite if exists - dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/medias/js/websitekey', $conf->website->dir_output.'/'.$object->ref.'/medias/js/'.$object->ref, 0, 1); // Medias can be shared, do not overwrite if exists + $result = dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/medias/image/websitekey', $conf->website->dir_output.'/'.$object->ref.'/medias/image/'.$object->ref, 0, 1); + if ($result < 0) { + $this->errors[] = 'Failed to copy files into '.$conf->website->dir_output.'/'.$object->ref.'/medias/image/'.$object->ref.'.'; + return -5; + } + + $result = dolCopyDir($conf->website->dir_temp.'/'.$object->ref.'/medias/js/websitekey', $conf->website->dir_output.'/'.$object->ref.'/medias/js/'.$object->ref, 0, 1); + if ($result < 0) { + $this->errors[] = 'Failed to copy files into '.$conf->website->dir_output.'/'.$object->ref.'/medias/js/'.$object->ref.'.'; + return -5; + } $sqlfile = $conf->website->dir_temp."/".$object->ref.'/website_pages.sql'; @@ -1301,13 +1310,13 @@ public function importWebSite($pathtofile) $newid = ($reg[2] + $maxrowid); $aliasesarray = explode(',', $reg[3]); - dol_syslog("Found ID ".$oldid." to replace with ID ".$newid." and shortcut aliases to create: ".$reg[3]); + dol_syslog("In sql source file, we have the page ID ".$oldid." to replace with the new ID ".$newid.", and we must create the shortcut aliases: ".$reg[3]); - dol_move($conf->website->dir_output.'/'.$object->ref.'/page'.$oldid.'.tpl.php', $conf->website->dir_output.'/'.$object->ref.'/page'.$newid.'.tpl.php', 0, 1, 0, 0); + //dol_move($conf->website->dir_output.'/'.$object->ref.'/page'.$oldid.'.tpl.php', $conf->website->dir_output.'/'.$object->ref.'/page'.$newid.'.tpl.php', 0, 1, 0, 0); $objectpagestatic->fetch($newid); - // The move is not enough, so we regenerate pageX.tpl.php + // We regenerate the pageX.tpl.php $filetpl = $conf->website->dir_output.'/'.$object->ref.'/page'.$newid.'.tpl.php'; $result = dolSavePageContent($filetpl, $object, $objectpagestatic); if (!$result) {