3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Media plugin filtering
21 * This filter will replace any links to a media file with
22 * a media plugin that plays that media inline
25 * @subpackage mediaplugin
26 * @copyright 2004 onwards Martin Dougiamas {@link http://moodle.com}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
32 require_once($CFG->libdir.'/filelib.php');
34 class filter_mediaplugin extends moodle_text_filter {
35 private $eolas_fix_applied = false;
36 function filter($text, array $options = array()) {
38 // You should never modify parameters passed to a method or function, it's BAD practice. Create a copy instead.
39 // The reason is that you must always be able to refer to the original parameter that was passed.
40 // For this reason, I changed $text = preg_replace(..,..,$text) into $newtext = preg.... (NICOLAS CONNAULT)
41 // Thanks to Pablo Etcheverry for pointing this out! MDL-10177
43 // We're using the UFO technique for flash to attain XHTML Strict 1.0
44 // See: http://www.bobbyvandersluis.com/ufo/
45 if (!is_string($text)) {
46 // non string data can not be filtered anyway
49 $newtext = $text; // fullclone is slow and not needed here
51 if (!empty($CFG->filter_mediaplugin_enable_mp3)) {
52 $search = '/<a[^>]*?href="([^<]+\.mp3)"[^>]*>.*?<\/a>/is';
53 $newtext = preg_replace_callback($search, 'filter_mediaplugin_mp3_callback', $newtext);
56 if (!empty($CFG->filter_mediaplugin_enable_ogg)) {
57 $search = '/<a[^>]*?href="([^<]+\.ogg)"[^>]*>.*?<\/a>/is';
58 $newtext = preg_replace_callback($search, 'filter_mediaplugin_ogg_callback', $newtext);
61 if (!empty($CFG->filter_mediaplugin_enable_ogv)) {
62 $search = '/<a[^>]*?href="([^<]+\.ogv)"[^>]*>.*?<\/a>/is';
63 $newtext = preg_replace_callback($search, 'filter_mediaplugin_ogv_callback', $newtext);
66 if (!empty($CFG->filter_mediaplugin_enable_swf)) {
67 $search = '/<a[^>]*?href="([^<]+\.swf)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
68 $newtext = preg_replace_callback($search, 'filter_mediaplugin_swf_callback', $newtext);
71 if (!empty($CFG->filter_mediaplugin_enable_flv)) {
72 $search = '/<a[^>]*?href="([^<]+\.flv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
73 $newtext = preg_replace_callback($search, 'filter_mediaplugin_flv_callback', $newtext);
76 if (!empty($CFG->filter_mediaplugin_enable_mov)) {
77 $search = '/<a[^>]*?href="([^<]+\.mov)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
78 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
80 $search = '/<a[^>]*?href="([^<]+\.mp4)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
81 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
83 $search = '/<a[^>]*?href="([^<]+\.m4v)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
84 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
86 $search = '/<a[^>]*?href="([^<]+\.m4a)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
87 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
90 if (!empty($CFG->filter_mediaplugin_enable_wmv)) {
91 $search = '/<a[^>]*?href="([^<]+\.wmv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
92 $newtext = preg_replace_callback($search, 'filter_mediaplugin_wmp_callback', $newtext);
95 if (!empty($CFG->filter_mediaplugin_enable_mpg)) {
96 $search = '/<a[^>]*?href="([^<]+\.mpe?g)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
97 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
100 if (!empty($CFG->filter_mediaplugin_enable_avi)) {
101 $search = '/<a[^>]*?href="([^<]+\.avi)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
102 $newtext = preg_replace_callback($search, 'filter_mediaplugin_wmp_callback', $newtext);
105 if (!empty($CFG->filter_mediaplugin_enable_ram)) {
106 $search = '/<a[^>]*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
107 $newtext = preg_replace_callback($search, 'filter_mediaplugin_real_callback', $newtext);
110 if (!empty($CFG->filter_mediaplugin_enable_rpm)) {
111 $search = '/<a[^>]*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
112 $newtext = preg_replace_callback($search, 'filter_mediaplugin_real_callback', $newtext);
115 if (!empty($CFG->filter_mediaplugin_enable_rm)) {
116 $search = '/<a[^>]*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
117 $newtext = preg_replace_callback($search, 'filter_mediaplugin_real_callback', $newtext);
120 if (!empty($CFG->filter_mediaplugin_enable_youtube)) {
121 //see MDL-23903 for description of recent changes to this regex
122 //$search = '/<a.*?href="([^<]*)youtube.com\/watch\?v=([^"]*)"[^>]*>(.*?)<\/a>/is';
123 $search = '/<a[^>]*href="([^<]*?)youtube.com\/watch\?v=([^"]*)"[^>]*>(.*?)<\/a>/is';
124 $newtext = preg_replace_callback($search, 'filter_mediaplugin_youtube_callback', $newtext);
126 $search = '/<a[^>]*href="([^<]*)youtube.com\/v\/([^"]*)"[^>]*>(.*?)<\/a>/is';
127 $newtext = preg_replace_callback($search, 'filter_mediaplugin_youtube_callback', $newtext);
130 if (!empty($CFG->filter_mediaplugin_enable_img)) {
131 $search = '/<a[^>]*?href="([^<]+\.jpg)"[^>]*>(.*?)<\/a>/is';
132 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
133 $search = '/<a[^>]*?href="([^<]+\.png)"[^>]*>(.*?)<\/a>/is';
134 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
135 $search = '/<a[^>]*?href="([^<]+\.gif)"[^>]*>(.*?)<\/a>/is';
136 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
139 if (empty($newtext) or $newtext === $text) {
140 // error or not filtered
145 if (!$this->eolas_fix_applied) {
146 $PAGE->requires->js('/filter/mediaplugin/eolas_fix.js');
147 $this->eolas_fix_applied = true;
154 ///===========================
155 /// callback filter functions
157 function filter_mediaplugin_mp3_callback($link) {
158 global $CFG, $OUTPUT, $PAGE;
160 $c = $OUTPUT->filter_mediaplugin_colors(); // You can set this up in your theme/xxx/config.php
164 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
166 $url = addslashes_js($link[1]);
168 $playerpath = $CFG->wwwroot.'/filter/mediaplugin/mp3player.swf';
169 $audioplayerpath = $CFG->wwwroot .'/filter/mediaplugin/flowplayer.audio.swf';
170 $colors = explode('&', $c);
171 $playercolors = array();
172 foreach ($colors as $color) {
173 $color = explode('=', $color);
174 $playercolors[$color[0]] = $color[1];
178 <span class="mediaplugin mediaplugin_mp3" id="$id"></span>
180 <object width="100" height="15" id="nonjsmp3plugin" name="undefined" data="$playerpath" type="application/x-shockwave-flash">
181 <param name="movie" value="$playerpath" />
182 <param name="allowfullscreen" value="false" />
183 <param name="allowscriptaccess" value="always" />
184 <param name="flashvars" value='config={"plugins": {"controls": {
193 "audio": {"url": "$audioplayerpath"}
195 "clip":{"url":"$url",
197 "content":{"url":"$playerpath"}}}' />
202 $jsoutput = create_flowplayer($id, $url, 'mp3', $playercolors);
203 $output .= $jsoutput;
208 function filter_mediaplugin_ogg_callback($link) {
209 global $CFG, $OUTPUT, $PAGE;
213 $id = 'filter_ogg_'.time().$count; //we need something unique because it might be stored in text cache
215 $url = addslashes_js($link[1]);
216 $printlink = html_writer::link($url, get_string('oggaudio', 'filter_mediaplugin'));
217 $unsupportedplugins = get_string('unsupportedplugins', 'filter_mediaplugin', $printlink);
219 <audio id="$id" src="$url" controls="true" width="100">
227 function filter_mediaplugin_ogv_callback($link) {
228 global $CFG, $OUTPUT, $PAGE;
232 $id = 'filter_ogv_'.time().$count; //we need something unique because it might be stored in text cache
234 $url = addslashes_js($link[1]);
235 $printlink = html_writer::link($url, get_string('ogvvideo', 'filter_mediaplugin'));
236 $unsupportedplugins = get_string('unsupportedplugins', 'filter_mediaplugin', $printlink);
238 <video id="$id" src="$url" controls="true" width="600" >
246 function filter_mediaplugin_swf_callback($link) {
250 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
252 $width = empty($link[3]) ? '400' : $link[3];
253 $height = empty($link[4]) ? '300' : $link[4];
254 $url = addslashes_js($link[1]);
257 $args['movie'] = $url;
258 $args['width'] = $width;
259 $args['height'] = $height;
260 $args['majorversion'] = 6;
262 $args['allowscriptaccess'] = 'never';
263 $args['quality'] = 'high';
265 $jsoutput = create_ufo_inline($id, $args);
267 $output = $link[0].'<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'filter_mediaplugin').')</span>'.$jsoutput;
272 function filter_mediaplugin_flv_callback($link) {
277 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
279 $width = empty($link[3]) ? '480' : $link[3];
280 $height = empty($link[4]) ? '360' : $link[4];
281 $url = addslashes_js($link[1]);
283 $playerpath = $CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf';
286 <span class="mediaplugin mediaplugin_flv" id="$id"></span>
288 <object width="800" height="600" id="undefined" name="undefined" data="$playerpath" type="application/x-shockwave-flash">
289 <param name="movie" value="$playerpath" />
290 <param name="allowfullscreen" value="true" />
291 <param name="allowscriptaccess" value="always" />
292 <param name="flashvars" value='config={"clip":{"url":"$url",
294 "content":{"url":"$playerpath"}}}' />
299 $jsoutput = create_flowplayer($id, $url, 'flv');
300 $output .= $jsoutput;
304 function filter_mediaplugin_real_callback($link, $autostart=false) {
305 $url = addslashes_js($link[1]);
306 $mimetype = mimeinfo('type', $url);
307 $autostart = $autostart ? 'true' : 'false';
309 // embed kept for now see MDL-8674
311 '<span class="mediaplugin mediaplugin_real">
312 <script type="text/javascript">
314 document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
315 <param name="src" value="'.$url.'" />\\
316 <param name="autostart" value="'.$autostart.'" />\\
317 <param name="controls" value="imagewindow" />\\
318 <param name="console" value="video" />\\
319 <param name="loop" value="true" />\\
320 <embed src="'.$url.'" width="240" height="180" loop="true" type="'.$mimetype.'" controls="imagewindow" console="video" autostart="'.$autostart.'" />\\
322 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
323 <param name="src" value="'.$url.'" />\\
324 <param name="autostart" value="'.$autostart.'" />\\
325 <param name="controls" value="ControlPanel" />\\
326 <param name="console" value="video" />\\
327 <embed src="'.$url.'" width="240" height="30" controls="ControlPanel" type="'.$mimetype.'" console="video" autostart="'.$autostart.'" />\\
334 * Change links to Youtube into embedded Youtube videos
336 function filter_mediaplugin_youtube_callback($link, $autostart=false) {
338 $site = addslashes_js($link[1]);
339 $url = addslashes_js($link[2]);
340 $info = addslashes_js(strip_tags($link[3]));//strip out html tags as they won't work in the title attribute
342 return '<object title="'.$info.'"
343 class="mediaplugin mediaplugin_youtube" type="application/x-shockwave-flash"
344 data="'.$site.'youtube.com/v/'.$url.'&fs=1&rel=0" width="425" height="344">'.
345 '<param name="movie" value="'.$site.'youtube.com/v/'.$url.'&fs=1&rel=0" />'.
346 '<param name="FlashVars" value="playerMode=embedded" />'.
347 '<param name="wmode" value="transparent" />'.
348 '<param name="allowFullScreen" value="true" />'.
353 * Change links to images into embedded images
355 function filter_mediaplugin_img_callback($link, $autostart=false) {
356 $url = addslashes_js($link[1]);
357 $info = addslashes_js($link[2]);
359 return '<img class="mediaplugin mediaplugin_img" alt="" title="'.$info.'" src="'.$url.'" />';
363 * Embed video using window media player if available
365 function filter_mediaplugin_wmp_callback($link, $autostart=false) {
367 if (empty($link[3]) or empty($link[4])) {
369 $size = 'width="300" height="260"';
372 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
376 $mimetype = mimeinfo('type', $url);
377 $autostart = $autostart ? 'true' : 'false';
380 '<span class="mediaplugin mediaplugin_wmp">
381 <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
382 standby="Loading Microsoft(R) Windows(R) Media Player components..."
383 type="application/x-oleobject">
384 <param name="Filename" value="'.$url.'" />
385 <param name="src" value="'.$url.'" />
386 <param name="url" value="'.$url.'" />
387 <param name="ShowControls" value="true" />
388 <param name="AutoRewind" value="true" />
389 <param name="AutoStart" value="'.$autostart.'" />
390 <param name="Autosize" value="'.$autosize.'" />
391 <param name="EnableContextMenu" value="true" />
392 <param name="TransparentAtStart" value="false" />
393 <param name="AnimationAtStart" value="false" />
394 <param name="ShowGotoBar" value="false" />
395 <param name="EnableFullScreenControls" value="true" />
397 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
398 <param name="src" value="'.$url.'" />
399 <param name="controller" value="true" />
400 <param name="autoplay" value="'.$autostart.'" />
401 <param name="autostart" value="'.$autostart.'" />
402 <param name="resize" value="scale" />
408 function filter_mediaplugin_qt_callback($link, $autostart=false) {
410 if (empty($link[3]) or empty($link[4])) {
411 $size = 'width="440" height="315"';
413 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
415 $mimetype = mimeinfo('type', $url);
416 $autostart = $autostart ? 'true' : 'false';
419 '<span class="mediaplugin mediaplugin_qt">
420 <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
421 codebase="http://www.apple.com/qtactivex/qtplugin.cab" '.$size.'>
422 <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
423 <param name="src" value="'.$url.'" />
424 <param name="controller" value="true" />
425 <param name="loop" value="true" />
426 <param name="autoplay" value="'.$autostart.'" />
427 <param name="autostart" value="'.$autostart.'" />
428 <param name="scale" value="aspect" />
430 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
431 <param name="src" value="'.$url.'" />
432 <param name="pluginurl" value="http://www.apple.com/quicktime/download/" />
433 <param name="controller" value="true" />
434 <param name="loop" value="true" />
435 <param name="autoplay" value="'.$autostart.'" />
436 <param name="autostart" value="'.$autostart.'" />
437 <param name="scale" value="aspect" />