Skip to content

Commit

Permalink
Fixed issue #18557: Email Template Attachments appear to carry over w…
Browse files Browse the repository at this point in the history
…ith copied survey, but do not work (#3403)

Co-authored-by: lapiudevgit <devgit@lapiu.biz>
  • Loading branch information
gabrieljenik and lapiudevgit committed Aug 30, 2023
1 parent 656053c commit e9449a7
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 7 deletions.
3 changes: 3 additions & 0 deletions application/helpers/admin/import_helper.php
Expand Up @@ -1426,6 +1426,9 @@ function XMLImportSurvey($sFullFilePath, $sXMLdata = null, $sNewSurveyName = nul
if (!isAbsolutePath($attachment['url'])) {
$attachment['url'] = $uploadDir . DIRECTORY_SEPARATOR . $attachment['url'];
}
if ($bTranslateInsertansTags) {
$attachment['url'] = translateLinks('survey', $iOldSID, $iNewSID, $attachment['url'], true);
}
}
}
} elseif (is_null($attachments)) {
Expand Down
19 changes: 12 additions & 7 deletions application/helpers/common_helper.php
Expand Up @@ -2621,24 +2621,29 @@ function hasTemplateManageRights($userid, $sThemeFolder)
* Translate links which are in any answer/question/survey/email template/label set to their new counterpart
*
* @param string $sType 'survey' or 'label'
* @param mixed $iOldSurveyID
* @param mixed $iNewSurveyID
* @param mixed $sString
* @param mixed $iOldSurveyID Source SurveyId to be replaced
* @param mixed $iNewSurveyID New SurveyId to be used
* @param string $sString Link (url or local path) to be translated
* @param bool $isLocalPath Indicates if the link ($sString) is a local path or a url.
* @return string
*/
function translateLinks($sType, $iOldSurveyID, $iNewSurveyID, $sString)
function translateLinks($sType, $iOldSurveyID, $iNewSurveyID, $sString, $isLocalPath = false)
{
if ($sString == '') {
return $sString;
}
$iOldSurveyID = (int) $iOldSurveyID;
$iNewSurveyID = (int) $iNewSurveyID; // To avoid injection of a /e regex modifier without having to check all execution paths
if ($sType == 'survey') {
$sPattern = '(http(s)?:\/\/)?(([a-z0-9\/\.])*(?=(\/upload))\/upload\/surveys\/' . $iOldSurveyID . '\/)';
$sReplace = Yii::app()->getConfig("publicurl") . "upload/surveys/{$iNewSurveyID}/";
$sPattern = '(http(s)?:\/\/)?(([a-z0-9\/\.\-\_])*(?=(\/upload))\/upload\/surveys\/' . $iOldSurveyID . '\/)';
if ($isLocalPath) {
$sReplace = Yii::app()->getConfig("uploaddir") . "/surveys/{$iNewSurveyID}/";
} else {
$sReplace = Yii::app()->getConfig("publicurl") . "upload/surveys/{$iNewSurveyID}/";
}
return preg_replace('/' . $sPattern . '/u', $sReplace, $sString);
} elseif ($sType == 'label') {
$sPattern = '(http(s)?:\/\/)?(([a-z0-9\/\.])*(?=(\/upload))\/upload\/labels\/' . $iOldSurveyID . '\/)';
$sPattern = '(http(s)?:\/\/)?(([a-z0-9\/\.\-\_])*(?=(\/upload))\/upload\/labels\/' . $iOldSurveyID . '\/)';
$sReplace = Yii::app()->getConfig("publicurl") . "upload/labels/{$iNewSurveyID}/";
return preg_replace("/" . $sPattern . "/u", $sReplace, $sString);
} else // unknown type
Expand Down
124 changes: 124 additions & 0 deletions tests/unit/helpers/TranslateLinksTest.php
@@ -0,0 +1,124 @@
<?php

namespace ls\tests;

/**
* Tests for the translateLinks function.
*/

class TranslateLinksTest extends TestBaseClass
{
public static function setupBeforeClass(): void
{
parent::setupBeforeClass();
\Yii::import('application.helpers.common_helper', true);
}

/**
* Tests for the link string parameter.
*/
public function testLinkString(): void
{
//Empty string
$link = translateLinks('survey', '111111', '222222', '');

$this->assertEmpty($link);

//Old survey ID should be in the link string
$linkString = 'https://limesurvey.org/upload/surveys/111111/files/file.ext';

$link = translateLinks('survey', '333333', '444444', $linkString);

$this->assertEquals($link, $linkString);

//Link string should point to labels, not to surveys
$linkString = 'https://limesurvey.org/upload/surveys/111111/files/file.ext';

$link = translateLinks('label', '111111', '222222', $linkString);

$this->assertEquals($link, $linkString);

//Type should be survey or label
$linkString = 'https://limesurvey.org/upload/surveys/111111/files/file.ext';

$link = translateLinks('other', '111111', '222222', $linkString);

$this->assertEquals($link, $linkString);
}

/**
* Translating label links.
*/
public function testTranslateLabelLinks(): void
{
//HTTP only
$linkString = 'http://limesurvey.org/upload/labels/111111/files/file.ext';

$link = translateLinks('label', '111111', '222222', $linkString);

$publicUrl = \Yii::app()->getConfig("publicurl") . 'upload/labels/222222/files/file.ext';
$this->assertEquals($link, $publicUrl);

//HTTPS
$linkString = 'https://limesurvey.org/upload/labels/333333/files/file.ext';

$link = translateLinks('label', '333333', '444444', $linkString);

$publicUrl = \Yii::app()->getConfig("publicurl") . 'upload/labels/444444/files/file.ext';
$this->assertEquals($link, $publicUrl);

//Url with dashes and or underscores
$linkString = 'https://lime-survey.org/lime_survey/upload/labels/333333/files/file.ext';

$link = translateLinks('label', '333333', '444444', $linkString);

$publicUrl = \Yii::app()->getConfig("publicurl") . 'upload/labels/444444/files/file.ext';
$this->assertEquals($link, $publicUrl);

//Trying to translate a local path
$linkString = '/var/www/html/limesurvey/upload/labels/555555/files/file.ext';

$link = translateLinks('label', '555555', '666666', $linkString, true);

$uploadDir = \Yii::app()->getConfig("uploaddir") . '/labels/666666/files/file.ext';
$this->assertNotEquals($link, $uploadDir);
}

/**
* Translating survey links.
*/
public function testTranslateSurveyLinks(): void
{
//HTTP only
$linkString = 'http://limesurvey.org/lime_survey/upload/surveys/111111/files/file.ext';

$link = translateLinks('survey', '111111', '222222', $linkString);

$publicUrl = \Yii::app()->getConfig("publicurl") . 'upload/surveys/222222/files/file.ext';
$this->assertEquals($link, $publicUrl);

//HTTPS
$linkString = 'https://limesurvey.org/upload/surveys/333333/files/file.ext';

$link = translateLinks('survey', '333333', '444444', $linkString);

$publicUrl = \Yii::app()->getConfig("publicurl") . 'upload/surveys/444444/files/file.ext';
$this->assertEquals($link, $publicUrl);

//Url with dashes and or underscores
$linkString = 'https://lime-survey.org/upload/surveys/333333/files/file.ext';

$link = translateLinks('survey', '333333', '444444', $linkString);

$publicUrl = \Yii::app()->getConfig("publicurl") . 'upload/surveys/444444/files/file.ext';
$this->assertEquals($link, $publicUrl);

//Translating a local path
$linkString = '/var/www/html/limesurvey/upload/surveys/555555/files/file.ext';

$link = translateLinks('survey', '555555', '666666', $linkString, true);

$uploadDir = \Yii::app()->getConfig("uploaddir") . '/surveys/666666/files/file.ext';
$this->assertEquals($link, $uploadDir);
}
}

0 comments on commit e9449a7

Please sign in to comment.