Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Correctly handle the upload path during them import/export #7473

Merged
merged 4 commits into from
Nov 25, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 49 additions & 67 deletions system/modules/core/classes/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,7 @@ protected function extractThemeFiles($arrFiles, $arrDbFields)
// Extract the files
try
{
$strFileName = $objArchive->file_name;

// Support the old "tl_files" directory
if (strncmp($strFileName, 'tl_files/', 9) === 0)
{
$strFileName = substr($strFileName, 3);
}

// Override the files directory
if (\Config::get('uploadPath') != 'files' && strncmp($strFileName, 'files/', 6) === 0)
{
$strFileName = preg_replace('@^files/@', \Config::get('uploadPath') . '/', $strFileName);
}

\File::putContent($strFileName, $objArchive->unzip());
\File::putContent($this->customizeUploadPath($objArchive->file_name), $objArchive->unzip());
}
catch (\Exception $e)
{
Expand Down Expand Up @@ -380,19 +366,7 @@ protected function extractThemeFiles($arrFiles, $arrDbFields)
{
foreach ($arrNewFolders as $strFolder)
{
// Support the old "tl_files" folder
if (strncmp($strFolder, 'tl_files/', 9) === 0)
{
$strFolder = substr($strFolder, 3);
}

// Override the files directory
if (\Config::get('uploadPath') != 'files')
{
$strFolder = preg_replace('@^files/@', \Config::get('uploadPath') . '/', $strFolder);
}

\Dbafs::addResource($strFolder);
\Dbafs::addResource($this->customizeUploadPath($strFolder));
}
}

Expand Down Expand Up @@ -457,12 +431,6 @@ protected function extractThemeFiles($arrFiles, $arrDbFields)
$value = $fields->item($k)->nodeValue;
$name = $fields->item($k)->getAttribute('name');

// Support the old "tl_files" folder
if (strncmp($value, 'tl_files/', 9) === 0)
{
$value = substr($value, 3);
}

// Skip NULL values
if ($value == 'NULL')
{
Expand Down Expand Up @@ -549,22 +517,22 @@ protected function extractThemeFiles($arrFiles, $arrDbFields)
}

// Adjust the file paths in style sheets
elseif (\Config::get('uploadPath') != 'files' && ($table == 'tl_style_sheet' || $table == 'tl_style') && strpos($value, 'files') !== false)
elseif (($table == 'tl_style_sheet' || $table == 'tl_style') && strpos($value, 'files') !== false)
{
$tmp = deserialize($value);

if (is_array($tmp))
{
foreach ($tmp as $kk=>$vv)
{
$tmp[$kk] = preg_replace('@^files/@', \Config::get('uploadPath') . '/', $vv);
$tmp[$kk] = $this->customizeUploadPath($vv);
}

$value = serialize($tmp);
}
else
{
$value = preg_replace('@^files/@', \Config::get('uploadPath') . '/', $value);
$value = $this->customizeUploadPath($value);
}
}

Expand All @@ -580,7 +548,7 @@ protected function extractThemeFiles($arrFiles, $arrDbFields)
// Do not use the FilesModel here – tables are locked!
$objFile = $this->Database->prepare("SELECT uuid FROM tl_files WHERE path=?")
->limit(1)
->execute($value);
->execute($this->customizeUploadPath($value));

$value = $objFile->uuid;
}
Expand All @@ -595,16 +563,10 @@ protected function extractThemeFiles($arrFiles, $arrDbFields)
{
foreach ($tmp as $kk=>$vv)
{
// Support the old "tl_files" folder
if (strncmp($vv, 'tl_files/', 9) === 0)
{
$vv = substr($vv, 3);
}

// Do not use the FilesModel here – tables are locked!
$objFile = $this->Database->prepare("SELECT uuid FROM tl_files WHERE path=?")
->limit(1)
->execute($vv);
->execute($this->customizeUploadPath($vv));

$tmp[$kk] = $objFile->uuid;
}
Expand Down Expand Up @@ -983,15 +945,7 @@ protected function addDataRow(\DOMDocument $xml, \DOMElement $table, \Database\R

if ($objFile !== null)
{
// Standardize the upload path if it is not "files"
if (\Config::get('uploadPath') != 'files')
{
$v = 'files/' . preg_replace('@^'.preg_quote(\Config::get('uploadPath'), '@').'/@', '', $objFile->path);
}
else
{
$v = $objFile->path;
}
$v = $this->standardizeUploadPath($objFile->path);
}
else
{
Expand All @@ -1010,29 +964,25 @@ protected function addDataRow(\DOMDocument $xml, \DOMElement $table, \Database\R

if ($objFiles !== null)
{
// Standardize the upload path if it is not "files"
if (\Config::get('uploadPath') != 'files')
{
$arrTmp = array();

while ($objFiles->next())
{
$arrTmp[] = 'files/' . preg_replace('@^'.preg_quote(\Config::get('uploadPath'), '@').'/@', '', $objFiles->path);
}
$arrTmp = array();

$v = serialize($arrTmp);
}
else
while ($objFiles->next())
{
$v = serialize($objFiles->fetchEach('path'));
$arrTmp[] = $this->standardizeUploadPath($objFiles->path);
}

$v = serialize($arrTmp);
}
else
{
$v = 'NULL';
}
}
}
elseif ($t == 'tl_style' && ($k == 'bgimage' || $k == 'liststyleimage'))
{
$v = $this->standardizeUploadPath($v);
}

$value = $xml->createTextNode($v);
$field->appendChild($value);
Expand Down Expand Up @@ -1138,4 +1088,36 @@ protected function addTemplatesToArchive(\ZipWriter $objArchive, $strFolder)
}
}
}


/**
* Replace files/ with the custom upload folder name
* @param string
* @return string
*/
protected function customizeUploadPath($strPath)
{
if ($strPath == '')
{
return '';
}

return preg_replace('@^(tl_)?files/@', \Config::get('uploadPath') . '/', $strPath);
}


/**
* Replace a custom upload folder name with files/
* @param string
* @return string
*/
protected function standardizeUploadPath($strPath)
{
if ($strPath == '')
{
return '';
}

return preg_replace('@^' . preg_quote(\Config::get('uploadPath'), '@') . '/@', 'files/', $strPath);
}
}