Fix a user without perm for dyn content can import dyn templates -

Reported by Paweł Bednarz.
This commit is contained in:
Laurent Destailleur 2026-08-06 19:07:48 +02:00
parent 95eafaf131
commit 7992efdc6f

View file

@ -1005,6 +1005,7 @@ class Website extends CommonObject
* Generate a zip with all data of web site.
*
* @return string Path to file with zip or '' if error
* @see importWebSite()
*/
public function exportWebSite()
{
@ -1272,10 +1273,11 @@ class Website extends CommonObject
*
* @param string $pathtofile Full path of zip file
* @return int Return integer <0 if KO, Id of new website if OK
* @see exportWebSite()
*/
public function importWebSite($pathtofile)
{
global $conf, $mysoc;
global $conf, $mysoc, $user;
$error = 0;
@ -1325,6 +1327,21 @@ class Website extends CommonObject
$cssindestdir = $conf->website->dir_output.'/'.$object->ref.'/styles.css.php';
$result = dolReplaceInFile($cssindestdir, $arrayreplacement);
// Test if imported CSS page contains dynamic PHP content
if (!$user->hasRight('website', 'writephp')) {
$newpathofsrcfile = dol_osencode($cssindestdir);
$csscontent = file_get_contents($newpathofsrcfile);
// Check there is no PHP content into the imported file (must be only HTML + JS)
$phpcontent = dolKeepOnlyPhpCode($csscontent);
if ($phpcontent) {
$this->error = 'Error: you try to import a website with a page with PHP dynamic content in style sheet without having permissions for that.';
$this->errors[] = $this->error;
return -1;
}
}
$htmldeaderindestdir = $conf->website->dir_output.'/'.$object->ref.'/htmlheader.html';
$result = dolReplaceInFile($htmldeaderindestdir, $arrayreplacement);
@ -1390,7 +1407,7 @@ class Website extends CommonObject
$reg = array();
// Warning fgets with second parameter that is null or 0 hang.
$buf = fgets($fp, 65000); // No needto have a high value here for second parameter. We will process only short lines starting with '-- Page ID ...'
$buf = fgets($fp, 65000); // No need to have a high value here for second parameter. We will process only short lines starting with '-- Page ID ...'
$newid = 0;
// Scan the line
@ -1414,7 +1431,7 @@ class Website extends CommonObject
if ($newid) {
$objectpagestatic->fetch($newid);
// We regenerate the pageX.tpl.php
// We write the pageX.tpl.php
$filetpl = $conf->website->dir_output.'/'.$object->ref.'/page'.$newid.'.tpl.php';
$result = dolSavePageContent($filetpl, $object, $objectpagestatic);
if (!$result) {
@ -1422,6 +1439,21 @@ class Website extends CommonObject
$error++;
}
// Test if imported page contains dynamic PHP content
if (!$user->hasRight('website', 'writephp')) {
$newpathofsrcfile = dol_osencode($filetpl);
$tplcontent = file_get_contents($newpathofsrcfile);
// Check there is no PHP content into the imported file (must be only HTML + JS)
$phpcontent = dolKeepOnlyPhpCode($tplcontent);
if ($phpcontent) {
$this->error = 'Error: you try to import a website with a page with PHP dynamic content without having permissions for that.';
$this->errors[] = $this->error;
$error++;
}
}
// Regenerate also the main alias + alternative aliases pages
if (is_array($aliasesarray)) {
foreach ($aliasesarray as $aliasshortcuttocreate) {