Skip to content

Commit

Permalink
MDL-47371 weblib: Add option to disable escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
cameorn1730 committed Jul 12, 2016
1 parent 5a1728d commit 6fb1a71
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public function test_external_format_string() {
$correct = 'ENFR hi there%';
$this->assertSame($correct, external_format_string($test, $context->id, false, ['filter' => false]));

$this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));

$settings->set_raw($currentraw);
$settings->set_filter($currentfilter);
Expand Down
1 change: 1 addition & 0 deletions lib/tests/weblib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function test_format_string() {
$this->assertSame("ANother &amp; &amp;&amp;&amp;&amp;&amp; Category", format_string("ANother & &&&&& Category"));
$this->assertSame("ANother &amp; &amp;&amp;&amp;&amp;&amp; Category", format_string("ANother & &&&&& Category", true));
$this->assertSame("Nick's Test Site &amp; Other things", format_string("Nick's Test Site & Other things", true));
$this->assertSame("& < > \" '", format_string("& < > \" '", true, ['escape' => false]));

// String entities.
$this->assertSame("&quot;", format_string("&quot;"));
Expand Down
1 change: 1 addition & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ information provided here is intended especially for developers.
- get_user_max_upload_file_size()
* The following functions have been removed and should not be used any more:
- file_modify_html_header() - See MDL-29738 for more information.
* New option 'escape' added to format_string. When true (default), escapes HTML entities from the string

=== 3.1 ===

Expand Down
13 changes: 9 additions & 4 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1425,13 +1425,15 @@ function format_string($string, $striplinks = true, $options = null) {
$options['filter'] = true;
}

$options['escape'] = !isset($options['escape']) || $options['escape'];

if (!$options['context']) {
// We did not find any context? weird.
return $string = strip_tags($string);
}

// Calculate md5.
$md5 = md5($string.'<+>'.$striplinks.'<+>'.$options['context']->id.'<+>'.current_language());
$md5 = md5($string.'<+>'.$striplinks.'<+>'.$options['context']->id.'<+>'.$options['escape'].'<+>'.current_language());

// Fetch from cache if possible.
if (isset($strcache[$md5])) {
Expand All @@ -1440,7 +1442,7 @@ function format_string($string, $striplinks = true, $options = null) {

// First replace all ampersands not followed by html entity code
// Regular expression moved to its own method for easier unit testing.
$string = replace_ampersands_not_followed_by_entity($string);
$string = $options['escape'] ? replace_ampersands_not_followed_by_entity($string) : $string;

if (!empty($CFG->filterall) && $options['filter']) {
$filtermanager = filter_manager::instance();
Expand All @@ -1450,8 +1452,11 @@ function format_string($string, $striplinks = true, $options = null) {

// If the site requires it, strip ALL tags from this string.
if (!empty($CFG->formatstringstriptags)) {
$string = str_replace(array('<', '>'), array('&lt;', '&gt;'), strip_tags($string));

if ($options['escape']) {
$string = str_replace(array('<', '>'), array('&lt;', '&gt;'), strip_tags($string));
} else {
$string = strip_tags($string);
}
} else {
// Otherwise strip just links if that is required (default).
if ($striplinks) {
Expand Down

0 comments on commit 6fb1a71

Please sign in to comment.