From d4bd0aa236c1431010dd0d871b8e600c89723184 Mon Sep 17 00:00:00 2001 From: Eric Villard Date: Thu, 13 Jun 2019 15:53:35 +0200 Subject: [PATCH 1/3] fix image stored files were never retrieved in file storage Signed-off-by: Eric Villard --- classes/pdf.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/classes/pdf.php b/classes/pdf.php index 9a6c7f0..7f4c028 100644 --- a/classes/pdf.php +++ b/classes/pdf.php @@ -719,18 +719,29 @@ protected function process_content_images($content) { // Process pluginfile images. $imagetypes = get_string('imagetypes', 'dataformview_pdf'); - if (preg_match_all("%$CFG->wwwroot/pluginfile.php(/[^.]+.($imagetypes))%", $content, $matches)) { + $contextid = $this->df->get_context()->id; + $component = 'mod_dataform'; + $filearea = 'content'; + $baseurl = "$CFG->wwwroot/pluginfile.php/$contextid/$component/$filearea/"; + + if (preg_match_all("%$baseurl([^.]+.($imagetypes))%", $content, $matches)) { $replacements = array(); $fs = get_file_storage(); foreach ($matches[1] as $imagepath) { - if (!$file = $fs->get_file_by_hash(sha1($imagepath)) or $file->is_directory()) { + $pathparts = explode('/', $imagepath); + $hash = urldecode(array_shift($pathparts)); + $filename = array_pop($pathparts); + $filepath = '/' . implode('/', $pathparts); + $contentid = $this->df->get_content_id_from_hash($hash); + + if (!$file = $fs->get_file($contextid, $component, $filearea, $contentid, $filepath, $filename) or $file->is_directory()) { continue; } $filename = $file->get_filename(); $filepath = "$tmpdir/$filename"; if ($file->copy_content_to($filepath)) { - $replacements["$CFG->wwwroot/pluginfile.php$imagepath"] = $filepath; + $replacements["$baseurl$imagepath"] = $filepath; $this->_tmpfiles[] = $filepath; } } From 1459be6b16db2d561016be9983ed7b1a3c794861 Mon Sep 17 00:00:00 2001 From: Eric Villard Date: Fri, 14 Jun 2019 10:31:01 +0200 Subject: [PATCH 2/3] fix remove harcoded component and filearea values This allows to embed images from other plugins. Signed-off-by: Eric Villard --- classes/pdf.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/classes/pdf.php b/classes/pdf.php index 7f4c028..4ebe512 100644 --- a/classes/pdf.php +++ b/classes/pdf.php @@ -719,23 +719,23 @@ protected function process_content_images($content) { // Process pluginfile images. $imagetypes = get_string('imagetypes', 'dataformview_pdf'); - $contextid = $this->df->get_context()->id; - $component = 'mod_dataform'; - $filearea = 'content'; - $baseurl = "$CFG->wwwroot/pluginfile.php/$contextid/$component/$filearea/"; + $baseurl = "$CFG->wwwroot/pluginfile.php/"; if (preg_match_all("%$baseurl([^.]+.($imagetypes))%", $content, $matches)) { $replacements = array(); - $fs = get_file_storage(); foreach ($matches[1] as $imagepath) { $pathparts = explode('/', $imagepath); + $contextid = array_shift($pathparts); + $component = array_shift($pathparts); + $filearea = array_shift($pathparts); + $hash = urldecode(array_shift($pathparts)); $filename = array_pop($pathparts); $filepath = '/' . implode('/', $pathparts); - $contentid = $this->df->get_content_id_from_hash($hash); + $itemid = $component == 'mod_dataform' ? $this->df->get_content_id_from_hash($hash) : $hash; - if (!$file = $fs->get_file($contextid, $component, $filearea, $contentid, $filepath, $filename) or $file->is_directory()) { + if (!$file = $fs->get_file($contextid, $component, $filearea, $itemid, $filepath, $filename) or $file->is_directory()) { continue; } $filename = $file->get_filename(); From 6682c0b8b0da2df2f45c948c6bfa50bb4f92f836 Mon Sep 17 00:00:00 2001 From: Eric Villard Date: Mon, 1 Jul 2019 13:29:23 +0200 Subject: [PATCH 3/3] fix image filenames were url encoded Signed-off-by: Eric Villard --- classes/pdf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/pdf.php b/classes/pdf.php index 4ebe512..f889cb4 100644 --- a/classes/pdf.php +++ b/classes/pdf.php @@ -731,7 +731,7 @@ protected function process_content_images($content) { $filearea = array_shift($pathparts); $hash = urldecode(array_shift($pathparts)); - $filename = array_pop($pathparts); + $filename = urldecode(array_pop($pathparts)); $filepath = '/' . implode('/', $pathparts); $itemid = $component == 'mod_dataform' ? $this->df->get_content_id_from_hash($hash) : $hash;