Skip to content

Commit

Permalink
FIX: Better error management in loading a website template
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Mar 14, 2024
1 parent 5008a32 commit fb85f17
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
23 changes: 15 additions & 8 deletions htdocs/core/lib/files.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,string> $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)
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion htdocs/core/lib/functions.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 14 additions & 5 deletions htdocs/website/class/website.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit fb85f17

Please sign in to comment.