mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
MDL-66397 filter_h5p: converts H5P URLs to embed code
This commit is contained in:
parent
9528b1ff5b
commit
e83fd27717
46
filter/h5p/classes/privacy/provider.php
Normal file
46
filter/h5p/classes/privacy/provider.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Privacy Subsystem implementation for filter_h5p.
|
||||
*
|
||||
* @package filter_h5p
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace filter_h5p\privacy;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
/**
|
||||
* Privacy Subsystem for filter_h5p implementing null_provider.
|
||||
*
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class provider implements \core_privacy\local\metadata\null_provider {
|
||||
|
||||
/**
|
||||
* Get the language string identifier with the component's language
|
||||
* file to explain why this plugin stores no data.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_reason(): string {
|
||||
return 'privacy:metadata';
|
||||
}
|
||||
}
|
120
filter/h5p/filter.php
Normal file
120
filter/h5p/filter.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
/**
|
||||
* H5P filter
|
||||
*
|
||||
* @package filter_h5p
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
/**
|
||||
* H5P filter
|
||||
*
|
||||
* This filter will replace any occurrence of H5P URLs with the corresponding H5P content embed code
|
||||
*
|
||||
* @package filter_h5p
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class filter_h5p extends moodle_text_filter {
|
||||
|
||||
/**
|
||||
* @var boolean $loadresizerjs This is whether to request the resize.js script.
|
||||
*/
|
||||
private static $loadresizerjs = true;
|
||||
|
||||
/**
|
||||
* Function filter replaces any h5p-sources.
|
||||
*
|
||||
* @param string $text HTML content to process
|
||||
* @param array $options options passed to the filters
|
||||
* @return string
|
||||
*/
|
||||
public function filter($text, array $options = array()) {
|
||||
|
||||
if (!is_string($text) or empty($text)) {
|
||||
// Non string data can not be filtered anyway.
|
||||
return $text;
|
||||
}
|
||||
|
||||
if (stripos($text, 'http') === false) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
$allowedsources = get_config('filter_h5p', 'allowedsources');
|
||||
$allowedsources = array_filter(array_map('trim', explode("\n", $allowedsources)));
|
||||
if (empty($allowedsources)) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
$params = array(
|
||||
'tagbegin' => "<iframe src=",
|
||||
'tagend' => "</iframe>"
|
||||
);
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
// Convert special chars.
|
||||
$specialchars = ['*', '?', '&'];
|
||||
$escapedspecialchars = ['[^.]+', '\?', '&'];
|
||||
$sourceid = str_replace('[id]', '[0-9]+', $source);
|
||||
$escapechars = str_replace($specialchars, $escapedspecialchars, $sourceid);
|
||||
$ultimatepattern = '#(' . $escapechars . ')#';
|
||||
|
||||
$h5pcontenturl = new filterobject($source, null, null, false,
|
||||
false, null, [$this, 'filterobject_prepare_replacement_callback'], $params);
|
||||
|
||||
$h5pcontenturl->workregexp = $ultimatepattern;
|
||||
$h5pcontents[] = $h5pcontenturl;
|
||||
}
|
||||
|
||||
return filter_phrases($text, $h5pcontents, null, null, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback used by filterobject / filter_phrases.
|
||||
*
|
||||
* @param string $tagbegin HTML to insert before any match
|
||||
* @param string $tagend HTML to insert after any match
|
||||
* @param string $urlmodifier string to add to the match URL
|
||||
* @return array [$hreftagbegin, $hreftagend, $replacementphrase] for filterobject.
|
||||
*/
|
||||
public function filterobject_prepare_replacement_callback($tagbegin, $tagend, $urlmodifier) {
|
||||
|
||||
$sourceurl = "$1";
|
||||
if ($urlmodifier !== "") {
|
||||
$sourceurl .= $urlmodifier;
|
||||
}
|
||||
|
||||
$h5piframesrc = "\"".$sourceurl."\" width=\"100%\" height=\"637\" allowfullscreen=\"allowfullscreen\" style=\"border: 0;\">";
|
||||
|
||||
// We want to request the resizing script only once.
|
||||
if (self::$loadresizerjs) {
|
||||
$tagend .= '<script src="https://h5p.org/sites/all/modules/h5p/library/js/h5p-resizer.js"></script>';
|
||||
self::$loadresizerjs = false;
|
||||
}
|
||||
|
||||
return [$tagbegin, $tagend, $h5piframesrc];
|
||||
}
|
||||
}
|
34
filter/h5p/lang/en/filter_h5p.php
Normal file
34
filter/h5p/lang/en/filter_h5p.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Strings for filter_h5p
|
||||
*
|
||||
* @package filter_h5p
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
$string['allowedsourceslist'] = 'Allowed sources';
|
||||
$string['allowedsourceslistdesc'] = 'List of sources from which users can embed H5P content. If empty, the filter won\'t convert any external URL.
|
||||
|
||||
<b>[id]</b> is a placeholder for the H5P content id in the external source.
|
||||
|
||||
<b>*</b> wildcard is supported. For example, *.example.com will embed H5P content from any subdomain of example.com, but not from the example.com domain.';
|
||||
$string['filtername'] = 'H5P';
|
||||
$string['privacy:metadata'] = 'This H5P filter does not store any personal data.';
|
31
filter/h5p/settings.php
Normal file
31
filter/h5p/settings.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* H5P filter settings
|
||||
*
|
||||
* @package filter_h5p
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
if ($ADMIN->fulltree) {
|
||||
$settings->add(new admin_setting_configtextarea('filter_h5p/allowedsources', get_string('allowedsourceslist', 'filter_h5p'),
|
||||
get_string('allowedsourceslistdesc', 'filter_h5p'),
|
||||
"https://h5p.org/h5p/embed/[id]\nhttps://*.h5p.com/content/[id]/embed\nhttps://*.h5p.com/content/[id]"));
|
||||
}
|
87
filter/h5p/tests/filter_test.php
Normal file
87
filter/h5p/tests/filter_test.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Unit tests for the filter_h5p
|
||||
*
|
||||
* @package filter_h5p
|
||||
* @category test
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot.'/filter/h5p/filter.php');
|
||||
|
||||
/**
|
||||
* Unit tests for the H5P filter.
|
||||
*
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class filter_h5p_testcase extends advanced_testcase {
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
set_config('allowedsources', "https://h5p.org/h5p/embed/[id]\nhttps://*.h5p.com/content/[id]/embed\nhttps://*.h5p.com/content/[id]
|
||||
\nhttps://generic.wordpress.soton.ac.uk/altc/wp-admin/admin-ajax.php?action=h5p_embed&id=[id]", 'filter_h5p');
|
||||
// Enable h5p filter at top level.
|
||||
filter_set_global_state('h5p', TEXTFILTER_ON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that h5p tags with urls from allowed domains are filtered.
|
||||
*
|
||||
* @param string $text Original text
|
||||
* @param string $filteredtextpattern Text pattern after H5P filter
|
||||
*
|
||||
* @dataProvider texts_provider
|
||||
*/
|
||||
public function test_filter_urls($text, $filteredtextpattern) {
|
||||
|
||||
$filterplugin = new filter_h5p(null, array());
|
||||
|
||||
$filteredtext = $filterplugin->filter($text);
|
||||
$this->assertRegExp($filteredtextpattern, $filteredtext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides texts to filter for the {@link self::test_filter_urls} method.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function texts_provider() {
|
||||
return [
|
||||
["http:://example.com", "#http:://example.com#"],
|
||||
["http://google.es/h5p/embed/3425234", "#http://google.es/h5p/embed/3425234#"],
|
||||
["https://h5p.org/h5p/embed/547225", "#<iframe src=\"https://h5p.org/h5p/embed/547225\"[^>]+?>#"],
|
||||
["https://moodle.h5p.com/content/1290729733828858779/embed", "#<iframe src=\"https://moodle.h5p.com/content/1290729733828858779/embed\"[^>]+?>#"],
|
||||
["https://moodle.h5p.com/content/1290729733828858779", "#<iframe src=\"https://moodle.h5p.com/content/1290729733828858779/embed\"[^>]+?>#"],
|
||||
["<a href=\"https://h5p.org/h5p/embed/547225\">link</a>", "#^((?!iframe).)*$#"],
|
||||
["this is a text with an h5p url https://h5p.org/h5p/embed/547225 inside",
|
||||
"#this is a text with an h5p url <iframe src=\"https://h5p.org/h5p/embed/547225\"(.|\n)*> inside#"],
|
||||
["https://generic.wordpress.soton.ac.uk/altc/wp-admin/admin-ajax.php?action=h5p_embed&id=13",
|
||||
"#<iframe src=\"https://generic.wordpress.soton.ac.uk/altc/wp-admin/admin-ajax.php\?action=h5p_embed\&\;id=13\"[^>]+?>#"],
|
||||
["https://h5p.org/h5p/embed/547225 another content in the same page https://moodle.h5p.com/content/1290729733828858779/embed",
|
||||
"#<iframe src=\"https://h5p.org/h5p/embed/547225\"[^>]+?>((?!<iframe).)*<iframe src=\"https://moodle.h5p.com/content/1290729733828858779/embed\"[^>]+?>#"]
|
||||
];
|
||||
}
|
||||
}
|
29
filter/h5p/version.php
Normal file
29
filter/h5p/version.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Version of filter_h5p.
|
||||
*
|
||||
* @package filter_h5p
|
||||
* @copyright 2019 Victor Deniz <victor@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
$plugin->version = 2019092000;
|
||||
$plugin->requires = 2019092000;
|
||||
$plugin->component = 'filter_h5p';
|
@ -1778,7 +1778,7 @@ class core_plugin_manager {
|
||||
|
||||
'filter' => array(
|
||||
'activitynames', 'algebra', 'censor', 'emailprotect',
|
||||
'emoticon', 'mathjaxloader', 'mediaplugin', 'multilang', 'tex', 'tidy',
|
||||
'emoticon', 'h5p', 'mathjaxloader', 'mediaplugin', 'multilang', 'tex', 'tidy',
|
||||
'urltolink', 'data', 'glossary'
|
||||
),
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user