Skip to content

Commit

Permalink
Add StringUtil::getRawDecodedValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed May 19, 2021
1 parent 6b28659 commit ee9eedf
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,16 @@ protected function compile()
}
elseif ($objEvent->title)
{
$responseContext->setTitle(StringUtil::decodeEntities(Controller::replaceInsertTags($objEvent->title)));
$responseContext->setTitle(StringUtil::getRawDecodedValue($objEvent->title));
}

if ($objEvent->description)
{
$responseContext->setMetaDescription(StringUtil::decodeEntities(Controller::replaceInsertTags($objEvent->description)));
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($objEvent->description));
}
elseif ($objEvent->teaser)
{
$responseContext->setMetaDescription(StringUtil::decodeEntities(Controller::replaceInsertTags($objEvent->teaser)));
$responseContext->setMetaDescription(trim(preg_replace('/\s+/', ' ', StringUtil::getRawDecodedValue($objEvent->teaser))));
}

if ($objEvent->robots)
Expand Down
35 changes: 35 additions & 0 deletions core-bundle/src/Resources/contao/library/Contao/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,41 @@ public static function ampersand($strString, $blnEncode=true): string
{
return preg_replace('/&(amp;)?/i', ($blnEncode ? '&' : '&'), $strString);
}

/**
* Converts an input-encoded string back to its raw UTF-8 value it originated from.
*/
public static function getRawDecodedValue(string $strValue, bool $blnRemoveInsertTags = false): string
{
if ($blnRemoveInsertTags)
{
$strValue = static::stripInsertTags($strValue);
}
else
{
$strValue = Controller::replaceInsertTags($strValue, false);
}

$strValue = strip_tags($strValue);
$strValue = static::restoreBasicEntities($strValue);
$strValue = static::decodeEntities($strValue);

// Ensure valid UTF-8
if (preg_match('//u', $strValue) !== 1)
{
$substituteCharacter = mb_substitute_character();
mb_substitute_character(0xFFFD);

$strValue = mb_convert_encoding($strValue, 'UTF-8', 'UTF-8');

mb_substitute_character($substituteCharacter);
}

// TODO: We have to prevent insert tags from existing in this stage for security reasons, is using a ZWNJ the best we can do?
$strValue = str_replace('{', "\u{200C}{", $strValue);

return $strValue;
}
}

class_alias(StringUtil::class, 'StringUtil');
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/models/PageModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ public function __set($strKey, $varValue)
switch ($strKey)
{
case 'pageTitle':
$responseContext->setTitle(StringUtil::decodeEntities($varValue));
$responseContext->setTitle(StringUtil::getRawDecodedValue($varValue ?? ''));
break;

case 'description':
$responseContext->setMetaDescription(StringUtil::decodeEntities($varValue));
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($varValue ?? ''));
break;

case 'robots':
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/modules/ModuleArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ protected function compile()

if ($responseContext instanceof WebpageResponseContext)
{
$responseContext->setTitle(StringUtil::decodeEntities($this->title));
$responseContext->setTitle(StringUtil::getRawDecodedValue($this->title ?? ''));

if ($this->teaser)
{
$responseContext->setMetaDescription(Controller::replaceInsertTags($this->teaser));
$responseContext->setMetaDescription(trim(preg_replace('/\s+/', ' ', StringUtil::getRawDecodedValue($this->teaser))));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class ContaoWebpageResponseContext extends WebpageResponseContext
{
public function __construct(PageModel $pageModel)
{
$title = $pageModel->pageTitle ?: StringUtil::decodeEntities($pageModel->title ?: '');
$title = $pageModel->pageTitle ?: StringUtil::getRawDecodedValue($pageModel->title ?: '');

$this
->setTitle($title ?: '')
->setMetaDescription($pageModel->description ?: '')
->setMetaDescription(StringUtil::getRawDecodedValue($pageModel->description ?: ''))
;

if ($pageModel->robots) {
Expand Down
12 changes: 0 additions & 12 deletions core-bundle/src/Routing/ResponseContext/WebpageResponseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public function getTitle(): string

public function setTitle(string $title): self
{
$title = self::cleanString($title);

$this->title = $title;

return $this;
Expand All @@ -51,8 +49,6 @@ public function getMetaDescription(): string

public function setMetaDescription(string $metaDescription): self
{
$metaDescription = self::cleanString($metaDescription);

$this->metaDescription = $metaDescription;

return $this;
Expand All @@ -69,12 +65,4 @@ public function setMetaRobots(string $metaRobots): self

return $this;
}

protected static function cleanString(string $string): string
{
$string = strip_tags($string);
$string = str_replace("\n", ' ', $string);

return trim($string);
}
}
6 changes: 3 additions & 3 deletions faq-bundle/src/Resources/contao/modules/ModuleFaqReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ protected function compile()
}
elseif ($objFaq->question)
{
$responseContext->setTitle(StringUtil::decodeEntities(Controller::replaceInsertTags($objFaq->title)));
$responseContext->setTitle(StringUtil::getRawDecodedValue($objFaq->question));
}

if ($objFaq->description)
{
$responseContext->setMetaDescription(StringUtil::decodeEntities(Controller::replaceInsertTags($objFaq->description)));
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($objFaq->description));
}
elseif ($objFaq->question)
{
$responseContext->setMetaDescription(StringUtil::decodeEntities(Controller::replaceInsertTags($objFaq->question)));
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($objFaq->question));
}

if ($objFaq->robots)
Expand Down
6 changes: 3 additions & 3 deletions news-bundle/src/Resources/contao/modules/ModuleNewsReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ protected function compile()
}
elseif ($objArticle->headline)
{
$responseContext->setTitle(StringUtil::decodeEntities(Controller::replaceInsertTags($objArticle->headline)));
$responseContext->setTitle(StringUtil::getRawDecodedValue($objArticle->headline));
}

if ($objArticle->description)
{
$responseContext->setMetaDescription(StringUtil::decodeEntities(Controller::replaceInsertTags($objArticle->description)));
$responseContext->setMetaDescription(StringUtil::getRawDecodedValue($objArticle->description));
}
elseif ($objArticle->teaser)
{
$responseContext->setMetaDescription(StringUtil::decodeEntities(Controller::replaceInsertTags($objArticle->teaser)));
$responseContext->setMetaDescription(trim(preg_replace('/\s+/', ' ', StringUtil::getRawDecodedValue($objArticle->teaser))));
}

if ($objArticle->robots)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function compile()

if ($responseContext instanceof WebpageResponseContext)
{
$responseContext->setTitle(Controller::replaceInsertTags($objNewsletter->subject)); // Already stored decoded
$responseContext->setTitle(StringUtil::getRawDecodedValue($objNewsletter->subject));
}
}

Expand Down

0 comments on commit ee9eedf

Please sign in to comment.