Skip to content

Commit

Permalink
Merge branch 'feature/fix-448'
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Jan 2, 2017
2 parents 1663837 + 849d33a commit 825aa5b
Show file tree
Hide file tree
Showing 2 changed files with 1,314 additions and 1,354 deletions.
69 changes: 69 additions & 0 deletions system/classes/gltext.class.php
Expand Up @@ -38,6 +38,9 @@

class GLText
{
// Temporary markers to process JavaScript
const SCRIPT_MARKER = '__SCRIPT_%s_MARKER__';

/**
* Returns text ready for the edit fields.
*
Expand Down Expand Up @@ -560,4 +563,70 @@ public static function remove4byteUtf8Chars($text, $replace = '')

return $text;
}

/**
* Parse a string and replace JavaScript code with temporary markers
*
* @param string $text
* @return array array(0 => 'modified text', 1=> array of temporary markers)
*/
public static function protectJavascript($text)
{
$new_text = '';
$markers = array();

while ($text !== '') {
$posStart = stripos($text, '<script');

if ($posStart === false) {
// There is no JavaScript left
$new_text .= $text;
$text = '';
} else {
if ($posStart > 0) {
$new_text .= substr($text, 0, $posStart);
$text = substr($text, $posStart);
}

$posEnd = stripos($text, '</script>');

if ($posEnd === false) {
// '</script>' tag is missing
$posEnd = strlen($text);
} else {
$posEnd += strlen('</script>');
}

$part = substr($text, 0, $posEnd);
$marker = sprintf(self::SCRIPT_MARKER, microtime(true));
$marker = str_replace('.', '', $marker);
$markers[] = array(
'text' => $part,
'marker' => $marker,
);
$new_text .= $marker;
$text = substr($text, $posEnd);
}
}

return array($new_text, $markers);
}

/**
* Parse a string and replace temporary markers with the original JavaScript code
*
* @param string $text the first element of the value returned by self::protectJavascript
* @param array $markers the second element of the value returned by self::protectJavascript
* @return string
*/
public static function unprotectJavaScript($text, array $markers = array())
{
if (count($markers) > 0) {
foreach ($markers as $marker) {
$text = str_replace($marker['marker'], $marker['text'], $text);
}
}

return $text;
}
}

0 comments on commit 825aa5b

Please sign in to comment.