|
| 1 | +<?php |
| 2 | +// This file is part of Moodle-oembed-Filter |
| 3 | +// |
| 4 | +// Moodle-oembed-Filter is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle-oembed-Filter is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle-oembed-Filter. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace filter_oembed; |
| 18 | + |
| 19 | +/** |
| 20 | + * Filter for component 'filter_oembed' |
| 21 | + * |
| 22 | + * @package filter_oembed |
| 23 | + * @copyright Erich M. Wappis / Guy Thomas 2016 |
| 24 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 25 | + * @author Mat Cannings |
| 26 | + * @author James McQuillan |
| 27 | + * @author Vin Bhalerao |
| 28 | + * @author Erich M. Wappis <[email protected]> |
| 29 | + * @author Guy Thomas <[email protected]> |
| 30 | + * @author Mike Churchward <[email protected]> |
| 31 | + */ |
| 32 | + |
| 33 | +defined('MOODLE_INTERNAL') || die(); |
| 34 | + |
| 35 | +use filter_oembed\service\oembed; |
| 36 | + |
| 37 | +require_once($CFG->libdir.'/filelib.php'); |
| 38 | +/** |
| 39 | + * Main filter class for embedded remote content. |
| 40 | + * |
| 41 | + * @package filter_oembed |
| 42 | + * @copyright Erich M. Wappis / Guy Thomas 2016 |
| 43 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 44 | + */ |
| 45 | +class text_filter extends \moodle_text_filter { |
| 46 | + |
| 47 | + /** |
| 48 | + * content gets filtered, links either wrapped in an <a> tag or in a <div> tag with class="oembed" |
| 49 | + * will be replaced by embeded content |
| 50 | + * |
| 51 | + * @param $text HTML to be processed. |
| 52 | + * @param $options |
| 53 | + * @return string String containing processed HTML. |
| 54 | + */ |
| 55 | + public function filter($text, array $options = array()) { |
| 56 | + global $PAGE; |
| 57 | + |
| 58 | + static $initialised = false; |
| 59 | + |
| 60 | + if (!$initialised) { |
| 61 | + $PAGE->requires->js_call_amd('filter_oembed/oembed', 'init'); |
| 62 | + $initialised = true; |
| 63 | + } |
| 64 | + |
| 65 | + $targettag = get_config('filter_oembed', 'targettag'); |
| 66 | + |
| 67 | + if ($targettag == 'atag' && stripos($text, '</a>') === false) { |
| 68 | + // Performance shortcut - all regexes below end with the </a> tag. |
| 69 | + // If not present nothing can match. |
| 70 | + return $text; |
| 71 | + } |
| 72 | + |
| 73 | + $filtered = $text; // We need to return the original value if regex fails! |
| 74 | + if ($targettag == 'divtag') { |
| 75 | + $search = '/\<div\s[^\>]*data-oembed-href="(.*?)"(.*?)>(.*?)\<\/div\>/'; |
| 76 | + } else { // Using 'atag'. |
| 77 | + $search = '/\<a\s[^\>]*href="(.*?)"(?:.*?)>(?:.*?)\<\/a\>/is'; |
| 78 | + } |
| 79 | + |
| 80 | + $filtered = preg_replace_callback($search, [self::class, 'find_oembeds_callback'], $filtered); |
| 81 | + if (empty($filtered)) { |
| 82 | + // If $filtered is emtpy return original $text. |
| 83 | + return $text; |
| 84 | + } else { |
| 85 | + return $filtered; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Callback function to be used by the main filter |
| 91 | + * |
| 92 | + * @param $match array An array of matched groups, where [1] is the URL matched. |
| 93 | + * |
| 94 | + */ |
| 95 | + private static function find_oembeds_callback($match) { |
| 96 | + $instance = oembed::get_instance(); |
| 97 | + $result = $instance->html_output($match[1]); |
| 98 | + if (empty($result)) { |
| 99 | + $result = $match[0]; |
| 100 | + } |
| 101 | + return $result; |
| 102 | + } |
| 103 | +} |
0 commit comments