Skip to content

Commit

Permalink
MDL-70976 core_files: Allow for draft files url inserted in content
Browse files Browse the repository at this point in the history
* The file file_remove_editor_orphaned_files should take into account URL that
have been embedded in a tag content instead of an attribute (like src attribute)
* This will fix issue with inserting H5P content in calendar events.
  • Loading branch information
laurentdavid committed Mar 14, 2023
1 parent 0fae900 commit 68456ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/filelib.php
Expand Up @@ -928,7 +928,7 @@ function file_remove_editor_orphaned_files($editor) {
// Find those draft files included in the text, and generate their hashes.
$context = context_user::instance($USER->id);
$baseurl = $CFG->wwwroot . '/draftfile.php/' . $context->id . '/user/draft/' . $editor['itemid'] . '/';
$pattern = "/" . preg_quote($baseurl, '/') . "(.+?)[\?\"']/";
$pattern = "/" . preg_quote($baseurl, '/') . "(.+?)[\?\"'<>\s:\\\\]/";
preg_match_all($pattern, $editor['text'], $matches);
$usedfilehashes = [];
foreach ($matches[1] as $matchedfilename) {
Expand Down
17 changes: 11 additions & 6 deletions lib/tests/filelib_test.php
Expand Up @@ -1725,25 +1725,30 @@ public function test_file_remove_editor_orphaned_files() {
$filerecord['filename'] = 'file 3.png';
self::create_draft_file($filerecord);

$filerecord['filename'] = 'file4.png';
self::create_draft_file($filerecord);

// Confirm the user drafts area lists 3 files.
$fs = get_file_storage();
$usercontext = \context_user::instance($USER->id);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'itemid', 0);
$this->assertCount(3, $draftfiles);
$this->assertCount(4, $draftfiles);

// Now, spoof some editor text content, referencing 2 of the files; one requiring name encoding, one not.
$editor = [
'itemid' => $draftitemid,
'text' => '
<img src="'.$CFG->wwwroot.'/draftfile.php/'.$usercontext->id.'/user/draft/'.$draftitemid.'/file%203.png" alt="">
<img src="'.$CFG->wwwroot.'/draftfile.php/'.$usercontext->id.'/user/draft/'.$draftitemid.'/file1.png" alt="">'
'text' => "
<img src=\"{$CFG->wwwroot}/draftfile.php/{$usercontext->id}/user/draft/{$draftitemid}/file%203.png\" alt=\"\">
<img src=\"{$CFG->wwwroot}/draftfile.php/{$usercontext->id}/user/draft/{$draftitemid}/file1.png\" alt=\"\">
<span>{$CFG->wwwroot}/draftfile.php/{$usercontext->id}/user/draft/{$draftitemid}/file4.png</span>"
];

// Run the remove orphaned drafts function and confirm that only the referenced files remain in the user drafts.
$expected = ['file1.png', 'file 3.png']; // The drafts we expect will not be removed (are referenced in the online text).
// The drafts we expect will not be removed (are referenced in the online text).
$expected = ['file1.png', 'file 3.png', 'file4.png'];
file_remove_editor_orphaned_files($editor);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'itemid', 0);
$this->assertCount(2, $draftfiles);
$this->assertCount(3, $draftfiles);
foreach ($draftfiles as $file) {
$this->assertContains($file->get_filename(), $expected);
}
Expand Down

0 comments on commit 68456ca

Please sign in to comment.