2 // This file is part of Moodle - http://moodle.org/
4 // Moodle 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.
9 // Moodle 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.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Media plugin filtering
20 * This filter will replace any links to a media file with
21 * a media plugin that plays that media inline
24 * @subpackage mediaplugin
25 * @copyright 2004 onwards Martin Dougiamas {@link http://moodle.com}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') ||
die();
32 * Automatic media embedding filter class.
34 * It is highly recommended to configure servers to be compatible with our slasharguments,
35 * otherwise the "?d=600x400" may not work.
38 * @subpackage mediaplugin
39 * @copyright 2004 onwards Martin Dougiamas {@link http://moodle.com}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class filter_mediaplugin
extends moodle_text_filter
{
43 /** @var bool True if currently filtering trusted text */
45 /** @var core_media_renderer Media renderer */
46 private $mediarenderer;
47 /** @var string Partial regex pattern indicating possible embeddable content */
48 private $embedmarkers;
50 public function filter($text, array $options = array()) {
53 if (!is_string($text) or empty($text)) {
54 // non string data can not be filtered anyway
58 if (stripos($text, '</a>') === false) {
59 // Performance shortcut - if not </a> tag, nothing can match.
63 if (!$this->mediarenderer
) {
64 $this->mediarenderer
= $PAGE->get_renderer('core', 'media');
65 $this->embedmarkers
= $this->mediarenderer
->get_embeddable_markers();
68 // Check SWF permissions.
69 $this->trusted
= !empty($options['noclean']) or !empty($CFG->allowobjectembed
);
71 // Handle all links that contain any 'embeddable' marker text (it could
72 // do all links, but the embeddable markers thing should make it faster
73 // by meaning for most links it doesn't drop into PHP code).
74 $newtext = preg_replace_callback($re = '~<a\s[^>]*href="([^"]*(?:' .
75 $this->embedmarkers
. ')[^"]*)"[^>]*>([^>]*)</a>~is',
76 array($this, 'callback'), $text);
78 if (empty($newtext) or $newtext === $text) {
79 // error or not filtered
87 * Replace link with embedded content, if supported.
89 * @param array $matches
92 private function callback(array $matches) {
94 // Check if we ignore it.
95 if (preg_match('/class="[^"]*nomediaplugin/i', $matches[0])) {
100 $name = trim($matches[2]);
101 if (empty($name) or strpos($name, 'http') === 0) {
102 $name = ''; // Use default name.
105 // Split provided URL into alternatives.
106 $urls = core_media
::split_alternatives($matches[1], $width, $height);
110 // Allow SWF (or not).
111 if ($this->trusted
) {
112 $options[core_media
::OPTION_TRUSTED
] = true;
115 // We could test whether embed is possible using can_embed, but to save
116 // time, let's just embed it with the 'fallback to blank' option which
117 // does most of the same stuff anyhow.
118 $options[core_media
::OPTION_FALLBACK_TO_BLANK
] = true;
120 // NOTE: Options are not passed through from filter because the 'embed'
121 // code does not recognise filter options (it's a different kind of
122 // option-space) as it can be used in non-filter situations.
123 $result = $this->mediarenderer
->embed_alternatives($urls, $name, $width, $height, $options);
125 // If something was embedded, return it, otherwise return original.
126 if ($result !== '') {