diff --git a/filter/displayh5p/db/install.php b/filter/displayh5p/db/install.php new file mode 100644 index 00000000000..a5a03e043c4 --- /dev/null +++ b/filter/displayh5p/db/install.php @@ -0,0 +1,38 @@ +. + +/** + * Display H5P active by default + * + * @package filter_displayh5p + * @copyright 2019 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Enable displayh5p filter by default to render H5P contents. + * @throws coding_exception + */ +function xmldb_filter_displayh5p_install() { + global $CFG; + + require_once("$CFG->libdir/filterlib.php"); + + // Display H5P filter should be enabled by default because we need this filter for H5P atto button to work. + filter_set_global_state('displayh5p', TEXTFILTER_ON, -1); +} diff --git a/filter/displayh5p/filter.php b/filter/displayh5p/filter.php index bd31b685ac5..d13ff33da1a 100644 --- a/filter/displayh5p/filter.php +++ b/filter/displayh5p/filter.php @@ -47,49 +47,89 @@ class filter_displayh5p extends moodle_text_filter { * @return string */ public function filter($text, array $options = array()) { + global $CFG; if (!is_string($text) or empty($text)) { // Non string data can not be filtered anyway. return $text; } - if (stripos($text, 'http') === false) { + // We are trying to minimize performance impact checking there's some H5P related URL. + $h5purl = '(http[^ &<]*h5p)'; + if (!preg_match($h5purl, $text)) { return $text; } $allowedsources = get_config('filter_displayh5p', 'allowedsources'); $allowedsources = array_filter(array_map('trim', explode("\n", $allowedsources))); - if (empty($allowedsources)) { - return $text; - } + + $localsource = '('.preg_quote($CFG->wwwroot).'/[^ &<]*\.h5p([?][^ <]*)?[^ &<]*)'; + $allowedsources[] = $localsource; $params = array( - 'tagbegin' => "' ); + $specialchars = ['*', '?', '&', '[^<]']; + $escapedspecialchars = ['[^.]+', '\?', '&', '[^<]*']; + $h5pcontents = array(); + + // Check all allowed sources. foreach ($allowedsources as $source) { // It is needed to add "/embed" at the end of URLs like https:://*.h5p.com/content/12345 (H5P.com). $params['urlmodifier'] = ''; - if (!(stripos($source, 'embed'))) { - $params['urlmodifier'] = '/embed'; + + if (($source == $localsource)) { + $params['tagbegin'] = '#"], + [$CFG->wwwroot."/pluginfile.php/5/user/private/accordion-6-7138%20%281%29.h5p.h5p", + "##"] ]; } } \ No newline at end of file