Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'bug/17247-zip-file-name-should-not-matter-for-question-…
…theme-zip-import'
  • Loading branch information
olleharstedt committed Apr 22, 2021
2 parents 0a41357 + a1f12a9 commit bd52d91
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions application/controllers/admin/themes.php
Expand Up @@ -24,7 +24,6 @@ class themes extends Survey_Common_Action

public function runWithParams($params)
{

$sTemplateName = Yii::app()->request->getPost('templatename', '');
if (Permission::model()->hasGlobalPermission('templates', 'read') || Permission::model()->hasTemplatePermission($sTemplateName)) {
parent::runWithParams($params);
Expand Down Expand Up @@ -285,7 +284,7 @@ protected function uploadTemplate()
// Redirect back at file size error.
$this->checkFileSizeError();

$sNewDirectoryName = sanitize_dirname(pathinfo($_FILES['the_file']['name'], PATHINFO_FILENAME));
$sNewDirectoryName = $this->getNewDirectoryName($themeType, $_FILES['the_file']['tmp_name']);

if ($themeType == 'question') {
$destdir = App()->getConfig('userquestionthemerootdir') . DIRECTORY_SEPARATOR . $sNewDirectoryName;
Expand Down Expand Up @@ -1359,4 +1358,64 @@ protected function checkDestDir($destdir, $sNewDirectoryName, $themeType)
$this->getController()->redirect(array($redirectUrl));
}
}

/**
* Get directory name for $themeType in zip file $src based on <metadata><name> tag
*
* @param string $themeType 'question' or 'survey'
* @param string $src
* @return string
* @throws Exception
* @todo Move to service class
* @todo Same logic for survey theme
*/
protected function getNewDirectoryName($themeType, $src)
{
if ($themeType === 'question') {
$zip = new ZipArchive();
$err = $zip->open($src);
if ($err !== true) {
throw new Exception('Could not open zip file');
}
/** @var string */
$configFilename = $this->findConfigXml($zip);
$configString = $zip->getFromName($configFilename);
$zip->close();
if ($configString === null) {
throw new Exception('Config file is empty');
}
$dom = new DOMDocument();
$dom->loadXML($configString);
$metadata = $dom->getElementsByTagName('metadata');
if (count($metadata) !== 1) {
throw new Exception('Did not find exactly one <metadata> tag');
}
$nameTags = $metadata[0]->getElementsByTagName('name');
if (count($nameTags) !== 1) {
throw new Exception('Did not find exactly one <name> tag in config.xml');
}
$nameFromConfig = $nameTags[0]->nodeValue;
if (empty($nameFromConfig)) {
throw new Exception('<name> tag is empty in config.xml');
}
return $nameFromConfig;
} else {
return sanitize_dirname(pathinfo($_FILES['the_file']['name'], PATHINFO_FILENAME));
}
}

/**
* @param ZipArchive $zip
* @return string|null
*/
public function findConfigXml(ZipArchive $zip)
{
for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
if (strpos($filename, 'config.xml') !== false) {
return $filename;
}
}
return null;
}
}

0 comments on commit bd52d91

Please sign in to comment.