-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.php
97 lines (83 loc) · 3.44 KB
/
filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
// This file is part of Moodle-oembed-Filter
//
// Moodle-oembed-Filter 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-oembed-Filter 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-oembed-Filter. If not, see <http://www.gnu.org/licenses/>.
/**
* Filter for component 'filter_embedrc'
*
* @package filter_embedrc
* @copyright Erich M. Wappis / Guy Thomas 2016
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* code based on the following filter
* oEmbed filter ( Mike Churchward, James McQuillan, Vinayak (Vin) Bhalerao, Josh Gavant and Rob Dolin)
*/
defined('MOODLE_INTERNAL') || die();
use filter_embedrc\service\oembed;
require_once($CFG->libdir.'/filelib.php');
/**
* Main filter class for embedded remote content.
*
* @package filter_embedrc
* @copyright Erich M. Wappis / Guy Thomas 2016
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter_embedrc extends moodle_text_filter {
/**
* content gets filtered, links either wrapped in an <a> tag or in a <div> tag with class="oembed"
* will be replaced by embeded content
*
* @param $text HTML to be processed.
* @param $options
* @return string String containing processed HTML.
*/
public function filter($text, array $options = array()) {
global $PAGE;
static $initialised = false;
if (!$initialised) {
$PAGE->requires->js_call_amd('filter_embedrc/oembed', 'init');
$initialised = true;
}
$targettag = get_config('filter_embedrc', 'targettag');
if ($targettag == 'atag' && stripos($text, '</a>') === false) {
// Performance shortcut - all regexes below end with the </a> tag.
// If not present nothing can match.
return $text;
}
$filtered = $text; // We need to return the original value if regex fails!
if (get_config('filter_embedrc', 'targettag') == 'divtag') {
$search = '/\<div\s[^\>]*data-oembed-href="(.*?)"(.*?)>(.*?)\<\/div\>/';
$filtered = preg_replace_callback($search, function ($match) {
$instance = oembed::get_instance();
return $instance->html_output($match[0]);
}, $filtered);
}
if (get_config('filter_embedrc', 'targettag') == 'atag') {
$search = '/\<a\s[^\>]*href="(.*?)"(.*?)>(.*?)\<\/a\>/';
$filtered = preg_replace_callback($search, function ($match) {
$instance = oembed::get_instance();
$result = $instance->html_output($match[1]);
if (empty($result)) {
// This anchor does not contain an oembed url, return the original anchor html.
$result = $match[0];
}
return $result;
}, $filtered);
}
if (empty($filtered)) {
// If $filtered is emtpy return original $text.
return $text;
}
return $filtered;
}
}