Navigation Menu

Skip to content

Commit

Permalink
MDL-53175 file: Allow file_rewrite_pluginfile_urls to reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Mar 30, 2016
1 parent 1912e61 commit 7cd6eef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/filelib.php
Expand Up @@ -445,6 +445,8 @@ function file_prepare_draft_area(&$draftitemid, $contextid, $component, $fileare

/**
* Convert encoded URLs in $text from the @@PLUGINFILE@@/... form to an actual URL.
* Passing a new option reverse = true in the $options var will make the function to convert actual URLs in $text to encoded URLs
* in the @@PLUGINFILE@@ form.
*
* @category files
* @global stdClass $CFG
Expand All @@ -454,7 +456,7 @@ function file_prepare_draft_area(&$draftitemid, $contextid, $component, $fileare
* @param string $component
* @param string $filearea helps identify the file area.
* @param int $itemid helps identify the file area.
* @param array $options text and file options ('forcehttps'=>false)
* @param array $options text and file options ('forcehttps'=>false), use reverse = true to reverse the behaviour of the function.
* @return string the processed text.
*/
function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid, array $options=null) {
Expand All @@ -479,7 +481,11 @@ function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $fil
$baseurl = str_replace('http://', 'https://', $baseurl);
}

return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
if (!empty($options['reverse'])) {
return str_replace($baseurl, '@@PLUGINFILE@@/', $text);
} else {
return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions lib/tests/filelib_test.php
Expand Up @@ -951,6 +951,26 @@ public function test_curl_useragent() {
$this->assertSame(0, $extcurl->get_errno());
$this->assertSame('', $contents);
}

/**
* Test file_rewrite_pluginfile_urls.
*/
public function test_file_rewrite_pluginfile_urls() {

$syscontext = context_system::instance();
$originaltext = 'Fake test with an image <img src="@@PLUGINFILE@@/image.png">';

// Do the rewrite.
$finaltext = file_rewrite_pluginfile_urls($originaltext, 'pluginfile.php', $syscontext->id, 'user', 'private', 0);
$this->assertContains("pluginfile.php", $finaltext);

// Now undo.
$options = array('reverse' => true);
$finaltext = file_rewrite_pluginfile_urls($finaltext, 'pluginfile.php', $syscontext->id, 'user', 'private', 0, $options);

// Compare the final text is the same that the original.
$this->assertEquals($originaltext, $finaltext);
}
}

/**
Expand Down

0 comments on commit 7cd6eef

Please sign in to comment.