weekly release
[moodle.git] / filter / mediaplugin / filter.php
CommitLineData
4317f92f 1<?php
35716b86
PS
2
3// This file is part of Moodle - http://moodle.org/
6de17fde 4//
35716b86
PS
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.
6de17fde 9//
35716b86
PS
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.
6de17fde 14//
35716b86
PS
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/>.
17
18/**
19 * Media plugin filtering
20 *
21 * This filter will replace any links to a media file with
22 * a media plugin that plays that media inline
23 *
24 * @package filter
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
28 */
6de17fde 29
35716b86 30defined('MOODLE_INTERNAL') || die();
6de17fde 31
7e64d361 32require_once($CFG->libdir.'/filelib.php');
33
35716b86 34class filter_mediaplugin extends moodle_text_filter {
ccc161f8 35 private $eolas_fix_applied = false;
dcfffe30 36 function filter($text, array $options = array()) {
82afb587 37 global $CFG, $PAGE;
9e3f34d1 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
42
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
47 return $text;
48 }
49 $newtext = $text; // fullclone is slow and not needed here
50
8e7fecd9 51 if (!empty($CFG->filter_mediaplugin_enable_mp3)) {
3a42ad12 52 $search = '/<a[^>]*?href="([^<]+\.mp3)"[^>]*>.*?<\/a>/is';
35716b86 53 $newtext = preg_replace_callback($search, 'filter_mediaplugin_mp3_callback', $newtext);
9e3f34d1 54 }
55
8e7fecd9 56 if (!empty($CFG->filter_mediaplugin_enable_ogg)) {
ce5dc36e
RW
57 $search = '/<a[^>]*?href="([^<]+\.ogg)"[^>]*>.*?<\/a>/is';
58 $newtext = preg_replace_callback($search, 'filter_mediaplugin_ogg_callback', $newtext);
59 }
60
8e7fecd9 61 if (!empty($CFG->filter_mediaplugin_enable_ogv)) {
ce5dc36e
RW
62 $search = '/<a[^>]*?href="([^<]+\.ogv)"[^>]*>.*?<\/a>/is';
63 $newtext = preg_replace_callback($search, 'filter_mediaplugin_ogv_callback', $newtext);
64 }
8e7fecd9
PS
65
66 if (!empty($CFG->filter_mediaplugin_enable_swf)) {
3a42ad12 67 $search = '/<a[^>]*?href="([^<]+\.swf)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
35716b86 68 $newtext = preg_replace_callback($search, 'filter_mediaplugin_swf_callback', $newtext);
9e3f34d1 69 }
70
8e7fecd9 71 if (!empty($CFG->filter_mediaplugin_enable_flv)) {
3a42ad12 72 $search = '/<a[^>]*?href="([^<]+\.flv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
35716b86 73 $newtext = preg_replace_callback($search, 'filter_mediaplugin_flv_callback', $newtext);
9e3f34d1 74 }
75
8e7fecd9 76 if (!empty($CFG->filter_mediaplugin_enable_mov)) {
3a42ad12 77 $search = '/<a[^>]*?href="([^<]+\.mov)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
35716b86 78 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
46936592 79
3a42ad12 80 $search = '/<a[^>]*?href="([^<]+\.mp4)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
35716b86 81 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
46936592 82
3a42ad12 83 $search = '/<a[^>]*?href="([^<]+\.m4v)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
35716b86 84 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
a184479a 85
3a42ad12 86 $search = '/<a[^>]*?href="([^<]+\.m4a)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
35716b86 87 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
9e3f34d1 88 }
89
8e7fecd9 90 if (!empty($CFG->filter_mediaplugin_enable_wmv)) {
3a42ad12 91 $search = '/<a[^>]*?href="([^<]+\.wmv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
35716b86 92 $newtext = preg_replace_callback($search, 'filter_mediaplugin_wmp_callback', $newtext);
9e3f34d1 93 }
94
8e7fecd9 95 if (!empty($CFG->filter_mediaplugin_enable_mpg)) {
3a42ad12 96 $search = '/<a[^>]*?href="([^<]+\.mpe?g)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
35716b86 97 $newtext = preg_replace_callback($search, 'filter_mediaplugin_qt_callback', $newtext);
9e3f34d1 98 }
99
8e7fecd9 100 if (!empty($CFG->filter_mediaplugin_enable_avi)) {
3a42ad12 101 $search = '/<a[^>]*?href="([^<]+\.avi)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
35716b86 102 $newtext = preg_replace_callback($search, 'filter_mediaplugin_wmp_callback', $newtext);
9e3f34d1 103 }
104
8e7fecd9 105 if (!empty($CFG->filter_mediaplugin_enable_ram)) {
3a42ad12 106 $search = '/<a[^>]*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
35716b86 107 $newtext = preg_replace_callback($search, 'filter_mediaplugin_real_callback', $newtext);
9e3f34d1 108 }
109
8e7fecd9 110 if (!empty($CFG->filter_mediaplugin_enable_rpm)) {
3a42ad12 111 $search = '/<a[^>]*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
35716b86 112 $newtext = preg_replace_callback($search, 'filter_mediaplugin_real_callback', $newtext);
9e3f34d1 113 }
114
8e7fecd9 115 if (!empty($CFG->filter_mediaplugin_enable_rm)) {
3a42ad12 116 $search = '/<a[^>]*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
35716b86 117 $newtext = preg_replace_callback($search, 'filter_mediaplugin_real_callback', $newtext);
9e3f34d1 118 }
119
120 if (!empty($CFG->filter_mediaplugin_enable_youtube)) {
4842422f
AD
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';
35716b86 124 $newtext = preg_replace_callback($search, 'filter_mediaplugin_youtube_callback', $newtext);
9e3f34d1 125
ee149244 126 $search = '/<a[^>]*href="([^<]*)youtube.com\/v\/([^"]*)"[^>]*>(.*?)<\/a>/is';
35716b86 127 $newtext = preg_replace_callback($search, 'filter_mediaplugin_youtube_callback', $newtext);
9e3f34d1 128 }
129
224eccbf 130 if (!empty($CFG->filter_mediaplugin_enable_img)) {
3a42ad12 131 $search = '/<a[^>]*?href="([^<]+\.jpg)"[^>]*>(.*?)<\/a>/is';
35716b86 132 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
3a42ad12 133 $search = '/<a[^>]*?href="([^<]+\.png)"[^>]*>(.*?)<\/a>/is';
35716b86 134 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
3a42ad12 135 $search = '/<a[^>]*?href="([^<]+\.gif)"[^>]*>(.*?)<\/a>/is';
35716b86 136 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
c159f28b 137 }
138
9e3f34d1 139 if (empty($newtext) or $newtext === $text) {
140 // error or not filtered
141 unset($newtext);
142 return $text;
143 }
144
145 if (!$this->eolas_fix_applied) {
9dec75db 146 $PAGE->requires->js('/filter/mediaplugin/eolas_fix.js');
9e3f34d1 147 $this->eolas_fix_applied = true;
148 }
149
150 return $newtext;
d07851e1 151 }
6de17fde 152}
153
7e64d361 154///===========================
155/// callback filter functions
156
35716b86 157function filter_mediaplugin_mp3_callback($link) {
088ccb43
PS
158 global $CFG, $OUTPUT, $PAGE;
159
160 $c = $OUTPUT->filter_mediaplugin_colors(); // You can set this up in your theme/xxx/config.php
7e64d361 161
162 static $count = 0;
163 $count++;
164 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
165
166 $url = addslashes_js($link[1]);
167
3a42ad12
RW
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];
175 }
82afb587 176
3a42ad12
RW
177 $output = <<<OET
178 <span class="mediaplugin mediaplugin_mp3" id="$id"></span>
388d1c02 179 <noscript><div>
3a42ad12
RW
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": {
185 "fullscreen": false,
186 "height": 15,
187 "autoHide": false,
188 "all": false,
189 "play": true,
190 "pause": true,
191 "scrubber": true
192 },
193 "audio": {"url": "$audioplayerpath"}
194 },
195 "clip":{"url":"$url",
196 "autoPlay": false},
197 "content":{"url":"$playerpath"}}}' />
4d4d20e7
PS
198 </object>
199 </div></noscript>
3a42ad12
RW
200OET;
201
202 $jsoutput = create_flowplayer($id, $url, 'mp3', $playercolors);
203 $output .= $jsoutput;
82afb587 204
205 return $output;
7e64d361 206}
207
ce5dc36e
RW
208function filter_mediaplugin_ogg_callback($link) {
209 global $CFG, $OUTPUT, $PAGE;
210
211 static $count = 0;
212 $count++;
213 $id = 'filter_ogg_'.time().$count; //we need something unique because it might be stored in text cache
214
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);
218 $output = <<<OET
219 <audio id="$id" src="$url" controls="true" width="100">
8e7fecd9 220 $unsupportedplugins
ce5dc36e
RW
221 </audio>
222OET;
8e7fecd9 223
ce5dc36e
RW
224 return $output;
225}
226
227function filter_mediaplugin_ogv_callback($link) {
228 global $CFG, $OUTPUT, $PAGE;
229
230 static $count = 0;
231 $count++;
232 $id = 'filter_ogv_'.time().$count; //we need something unique because it might be stored in text cache
233
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);
237 $output = <<<OET
238 <video id="$id" src="$url" controls="true" width="600" >
8e7fecd9 239 $unsupportedplugins
ce5dc36e
RW
240 </video>
241OET;
242
243 return $output;
244}
245
35716b86 246function filter_mediaplugin_swf_callback($link) {
82afb587 247 global $PAGE;
7e64d361 248 static $count = 0;
249 $count++;
250 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
251
252 $width = empty($link[3]) ? '400' : $link[3];
253 $height = empty($link[4]) ? '300' : $link[4];
254 $url = addslashes_js($link[1]);
255
82afb587 256 $args = Array();
257 $args['movie'] = $url;
258 $args['width'] = $width;
259 $args['height'] = $height;
260 $args['majorversion'] = 6;
261 $args['build'] = 40;
262 $args['allowscriptaccess'] = 'never';
263 $args['quality'] = 'high';
82afb587 264
10eaeca8 265 $jsoutput = create_ufo_inline($id, $args);
35716b86 266
a845d68b 267 $output = $link[0].'<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'filter_mediaplugin').')</span>'.$jsoutput;
82afb587 268
269 return $output;
7e64d361 270}
271
35716b86 272function filter_mediaplugin_flv_callback($link) {
82afb587 273 global $CFG, $PAGE;
7e64d361 274
275 static $count = 0;
276 $count++;
277 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
278
279 $width = empty($link[3]) ? '480' : $link[3];
280 $height = empty($link[4]) ? '360' : $link[4];
281 $url = addslashes_js($link[1]);
282
3a42ad12
RW
283 $playerpath = $CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf';
284
285 $output = <<<EOT
286 <span class="mediaplugin mediaplugin_flv" id="$id"></span>
388d1c02 287 <noscript><div>
3a42ad12
RW
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",
293 "autoPlay": false},
294 "content":{"url":"$playerpath"}}}' />
4d4d20e7
PS
295 </object>
296 </div></noscript>
3a42ad12
RW
297EOT;
298
299 $jsoutput = create_flowplayer($id, $url, 'flv');
300 $output .= $jsoutput;
82afb587 301 return $output;
7e64d361 302}
303
35716b86 304function filter_mediaplugin_real_callback($link, $autostart=false) {
7e64d361 305 $url = addslashes_js($link[1]);
306 $mimetype = mimeinfo('type', $url);
307 $autostart = $autostart ? 'true' : 'false';
308
309// embed kept for now see MDL-8674
310 return $link[0].
311'<span class="mediaplugin mediaplugin_real">
312<script type="text/javascript">
313//<![CDATA[
314document.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" />\\
7c59f3be 320 <embed src="'.$url.'" width="240" height="180" loop="true" type="'.$mimetype.'" controls="imagewindow" console="video" autostart="'.$autostart.'" />\\
7e64d361 321 </object><br />\\
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.'" />\\
328 </object>\');
329//]]>
330</script></span>';
331}
332
4d8bccf4 333/**
334 * Change links to Youtube into embedded Youtube videos
335 */
35716b86 336function filter_mediaplugin_youtube_callback($link, $autostart=false) {
4d8bccf4 337
338 $site = addslashes_js($link[1]);
339 $url = addslashes_js($link[2]);
4842422f 340 $info = addslashes_js(strip_tags($link[3]));//strip out html tags as they won't work in the title attribute
4d8bccf4 341
4317f92f
PS
342 return '<object title="'.$info.'"
343 class="mediaplugin mediaplugin_youtube" type="application/x-shockwave-flash"
4d8bccf4 344 data="'.$site.'youtube.com/v/'.$url.'&amp;fs=1&amp;rel=0" width="425" height="344">'.
6188deb8 345 '<param name="movie" value="'.$site.'youtube.com/v/'.$url.'&amp;fs=1&amp;rel=0" />'.
4d8bccf4 346 '<param name="FlashVars" value="playerMode=embedded" />'.
347 '<param name="wmode" value="transparent" />'.
348 '<param name="allowFullScreen" value="true" />'.
e2cf2d7f 349 '</object>';
4d8bccf4 350}
351
c159f28b 352/**
353 * Change links to images into embedded images
354 */
35716b86 355function filter_mediaplugin_img_callback($link, $autostart=false) {
c159f28b 356 $url = addslashes_js($link[1]);
357 $info = addslashes_js($link[2]);
358
359 return '<img class="mediaplugin mediaplugin_img" alt="" title="'.$info.'" src="'.$url.'" />';
360}
361
7e64d361 362/**
363 * Embed video using window media player if available
364 */
35716b86 365function filter_mediaplugin_wmp_callback($link, $autostart=false) {
7e64d361 366 $url = $link[1];
367 if (empty($link[3]) or empty($link[4])) {
368 $mpsize = '';
369 $size = 'width="300" height="260"';
370 $autosize = 'true';
371 } else {
372 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
373 $mpsize = $size;
374 $autosize = 'false';
375 }
376 $mimetype = mimeinfo('type', $url);
377 $autostart = $autostart ? 'true' : 'false';
378
379 return $link[0].
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" />
396<!--[if !IE]>-->
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" />
403 </object>
404<!--<![endif]-->
405</object></span>';
406}
407
35716b86 408function filter_mediaplugin_qt_callback($link, $autostart=false) {
7e64d361 409 $url = $link[1];
410 if (empty($link[3]) or empty($link[4])) {
1387d5eb 411 $size = 'width="440" height="315"';
7e64d361 412 } else {
413 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
414 }
415 $mimetype = mimeinfo('type', $url);
416 $autostart = $autostart ? 'true' : 'false';
417
418 return $link[0].
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.'" />
d42df9da 424 <param name="controller" value="true" />
7e64d361 425 <param name="loop" value="true" />
426 <param name="autoplay" value="'.$autostart.'" />
427 <param name="autostart" value="'.$autostart.'" />
428 <param name="scale" value="aspect" />
429<!--[if !IE]>-->
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" />
438 </object>
439<!--<![endif]-->
440</object></span>';
441}
6de17fde 442
4317f92f 443