MDL-66593 core_backup: Replace h5p/embed.php url for H5PEMBED@$
authorcescobedo <carlos.escobedo@gmail.com>
Fri, 4 Oct 2019 13:55:44 +0000 (15:55 +0200)
committercescobedo <carlos.escobedo@gmail.com>
Wed, 30 Oct 2019 05:46:35 +0000 (06:46 +0100)
backup/moodle2/backup_xml_transformer.class.php
backup/moodle2/tests/backup_xml_transformer_test.php
backup/util/helper/restore_structure_parser_processor.class.php
backup/util/helper/tests/restore_structure_parser_processor_test.php

index eb5b176..e646165 100644 (file)
@@ -85,6 +85,10 @@ class backup_xml_transformer extends xml_contenttransformer {
         }
 
         $content = $this->process_filephp_links($content); // Replace all calls to file.php by $@FILEPHP@$ in a normalised way
+
+        // Replace all calls to h5p/embed.php by $@H5PEMBED@$.
+        $content = $this->process_h5pembedphp_links($content);
+
         $content = $this->encode_absolute_links($content); // Pass the content against all the found encoders
 
         return $content;
@@ -120,6 +124,25 @@ class backup_xml_transformer extends xml_contenttransformer {
         return $content;
     }
 
+    /**
+     * Replace all calls to /h5p/embed.php by $@H5PEMBED@$
+     * to allow restore the /h5p/embed.php url in
+     * other domains.
+     *
+     * @param  string $content
+     * @return string
+     */
+    private function process_h5pembedphp_links($content) {
+        global $CFG;
+
+        // No /h5p/embed.php, nothing to convert.
+        if (strpos($content, '/h5p/embed.php') === false) {
+            return $content;
+        }
+
+        return str_replace($CFG->wwwroot.'/h5p/embed.php', '$@H5PEMBED@$', $content);
+    }
+
     private function encode_absolute_links($content) {
         foreach ($this->absolute_links_encoders as $classname => $methodname) {
             $content = call_user_func(array($classname, $methodname), $content);
index 55af176..554aa0b 100644 (file)
@@ -76,6 +76,7 @@ class backup_xml_transformer_testcase extends advanced_testcase {
             array('http://test.test/file.php?file=%2F2', 'http://test.test/file.php?file=%2F2'),
             array('http://test.test/file.php?file=%2F1%2F1.jpg', '$@FILEPHP@$$@SLASH@$1.jpg'),
             array('http://test.test/file.php?file=%2F1%2F%2F1.jpg', '$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'),
+            array('http://test.test/h5p/embed.php?url=testurl', '$@H5PEMBED@$?url=testurl'),
         );
     }
 
index add8d85..9fc1f6e 100644 (file)
@@ -57,30 +57,39 @@ class restore_structure_parser_processor extends grouped_parser_processor {
             return $cdata;
         } else if (strlen($cdata) < 32) { // Impossible to have one link in 32cc
             return $cdata;                // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=)
-        } else if (strpos($cdata, '$@FILEPHP@$') === false) { // No $@FILEPHP@$, nothing to convert
-            return $cdata;
         }
 
-        if ($CFG->slasharguments) {
-            $slash = '/';
-            $forcedownload = '?forcedownload=1';
-        } else {
-            $slash = '%2F';
-            $forcedownload = '&amp;forcedownload=1';
-        }
+        if (strpos($cdata, '$@FILEPHP@$') !== false) {
+            // We need to convert $@FILEPHP@$.
+            if ($CFG->slasharguments) {
+                $slash = '/';
+                $forcedownload = '?forcedownload=1';
+            } else {
+                $slash = '%2F';
+                $forcedownload = '&amp;forcedownload=1';
+            }
+
+            // We have to remove trailing slashes, otherwise file URLs will be restored with an extra slash.
+            $basefileurl = rtrim(moodle_url::make_legacyfile_url($this->courseid, null)->out(true), $slash);
+            // Decode file.php calls.
+            $search = array ("$@FILEPHP@$");
+            $replace = array($basefileurl);
+            $result = str_replace($search, $replace, $cdata);
 
-        // We have to remove trailing slashes, otherwise file URLs will be restored with an extra slash.
-        $basefileurl = rtrim(moodle_url::make_legacyfile_url($this->courseid, null)->out(true), $slash);
-        // Decode file.php calls
-        $search = array ("$@FILEPHP@$");
-        $replace = array($basefileurl);
-        $result = str_replace($search, $replace, $cdata);
+            // Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799.
+            $search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
+            $replace = array($slash, $forcedownload);
 
-        // Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799
-        $search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
-        $replace = array($slash, $forcedownload);
+            $cdata = str_replace($search, $replace, $result);
+        }
+
+        if (strpos($cdata, '$@H5PEMBED@$') !== false) {
+            // We need to convert $@H5PEMBED@$.
+            // Decode embed.php calls.
+            $cdata = str_replace('$@H5PEMBED@$', $CFG->wwwroot.'/h5p/embed.php', $cdata);
+        }
 
-        return str_replace($search, $replace, $result);
+        return $cdata;
     }
 
     /**
index 069163a..ea5ab2c 100644 (file)
@@ -107,6 +107,11 @@ class restore_structure_parser_processor_test extends advanced_testcase {
                 "<a href='http://test.test/file.php?file=%2F1%2F1.jpg&amp;forcedownload=1'>Image</a>",
                 false
             ),
+            array(
+                "<iframe src='$@H5PEMBED@$?url=testurl'></iframe>",
+                "<iframe src='http://test.test/h5p/embed.php?url=testurl'></iframe>",
+                true
+            ),
         );
     }