Automatically generated installer lang files
[moodle.git] / filter / mediaplugin / filter.php
CommitLineData
4317f92f 1<?php
35716b86 2// This file is part of Moodle - http://moodle.org/
6de17fde 3//
35716b86
PS
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.
6de17fde 8//
35716b86
PS
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.
6de17fde 13//
35716b86
PS
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/>.
16
17/**
18 * Media plugin filtering
19 *
20 * This filter will replace any links to a media file with
21 * a media plugin that plays that media inline
22 *
23 * @package filter
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
27 */
6de17fde 28
35716b86 29defined('MOODLE_INTERNAL') || die();
6de17fde 30
fcd2cbaf
PS
31/**
32 * Automatic media embedding filter class.
33 *
34 * It is highly recommended to configure servers to be compatible with our slasharguments,
35 * otherwise the "?d=600x400" may not work.
36 *
37 * @package filter
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
41 */
35716b86 42class filter_mediaplugin extends moodle_text_filter {
daefd6eb 43 /** @var bool True if currently filtering trusted text */
44 private $trusted;
45 /** @var core_media_renderer Media renderer */
46 private $mediarenderer;
47 /** @var string Partial regex pattern indicating possible embeddable content */
48 private $embedmarkers;
fcd2cbaf 49
daefd6eb 50 public function filter($text, array $options = array()) {
51 global $CFG, $PAGE;
fcd2cbaf
PS
52
53 if (!is_string($text) or empty($text)) {
9e3f34d1 54 // non string data can not be filtered anyway
55 return $text;
56 }
daefd6eb 57
e68789c5 58 if (stripos($text, '</a>') === false) {
daefd6eb 59 // Performance shortcut - if not </a> tag, nothing can match.
e68789c5
PS
60 return $text;
61 }
9e3f34d1 62
daefd6eb 63 if (!$this->mediarenderer) {
64 $this->mediarenderer = $PAGE->get_renderer('core', 'media');
65 $this->embedmarkers = $this->mediarenderer->get_embeddable_markers();
9e3f34d1 66 }
67
daefd6eb 68 // Check SWF permissions.
69 $this->trusted = !empty($options['noclean']) or !empty($CFG->allowobjectembed);
9e3f34d1 70
daefd6eb 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);
9e3f34d1 77
fcd2cbaf
PS
78 if (empty($newtext) or $newtext === $text) {
79 // error or not filtered
fcd2cbaf 80 return $text;
9e3f34d1 81 }
82
fcd2cbaf
PS
83 return $newtext;
84 }
fcd2cbaf 85
daefd6eb 86 /**
87 * Replace link with embedded content, if supported.
88 *
89 * @param array $matches
90 * @return string
91 */
92 private function callback(array $matches) {
93 global $CFG, $PAGE;
94 // Check if we ignore it.
95 if (preg_match('/class="[^"]*nomediaplugin/i', $matches[0])) {
96 return $matches[0];
fcd2cbaf 97 }
fcd2cbaf 98
daefd6eb 99 // Get name.
100 $name = trim($matches[2]);
101 if (empty($name) or strpos($name, 'http') === 0) {
102 $name = ''; // Use default name.
fcd2cbaf 103 }
fcd2cbaf 104
daefd6eb 105 // Split provided URL into alternatives.
106 $urls = core_media::split_alternatives($matches[1], $width, $height);
7e64d361 107
daefd6eb 108 $options = array();
ce5dc36e 109
daefd6eb 110 // Allow SWF (or not).
111 if ($this->trusted) {
112 $options[core_media::OPTION_TRUSTED] = true;
fcd2cbaf
PS
113 }
114
daefd6eb 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;
fcd2cbaf 119
daefd6eb 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);
ce5dc36e 124
daefd6eb 125 // If something was embedded, return it, otherwise return original.
126 if ($result !== '') {
127 return $result;
fcd2cbaf 128 } else {
daefd6eb 129 return $matches[0];
fcd2cbaf
PS
130 }
131 }
7e64d361 132}