create youtube playlist filter. Fixed regex, removed addslash_js, use variable param...
[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);
469aa3ba
RW
128
129 $search = '/<a(\s+[^>]+?)?\s+href="((([^"]+)youtube\.com)\/view_play_list\?p=([^"]*))"[^>]*>(.*?)<\/a>/is';
130 $newtext = preg_replace_callback($search, 'filter_mediaplugin_youtube_playlist_callback', $newtext);
9e3f34d1 131 }
132
224eccbf 133 if (!empty($CFG->filter_mediaplugin_enable_img)) {
3a42ad12 134 $search = '/<a[^>]*?href="([^<]+\.jpg)"[^>]*>(.*?)<\/a>/is';
35716b86 135 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
3a42ad12 136 $search = '/<a[^>]*?href="([^<]+\.png)"[^>]*>(.*?)<\/a>/is';
35716b86 137 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
3a42ad12 138 $search = '/<a[^>]*?href="([^<]+\.gif)"[^>]*>(.*?)<\/a>/is';
35716b86 139 $newtext = preg_replace_callback($search, 'filter_mediaplugin_img_callback', $newtext);
c159f28b 140 }
141
9e3f34d1 142 if (empty($newtext) or $newtext === $text) {
143 // error or not filtered
144 unset($newtext);
145 return $text;
146 }
147
148 if (!$this->eolas_fix_applied) {
9dec75db 149 $PAGE->requires->js('/filter/mediaplugin/eolas_fix.js');
9e3f34d1 150 $this->eolas_fix_applied = true;
151 }
152
153 return $newtext;
d07851e1 154 }
6de17fde 155}
156
7e64d361 157///===========================
158/// callback filter functions
159
35716b86 160function filter_mediaplugin_mp3_callback($link) {
088ccb43
PS
161 global $CFG, $OUTPUT, $PAGE;
162
163 $c = $OUTPUT->filter_mediaplugin_colors(); // You can set this up in your theme/xxx/config.php
7e64d361 164
165 static $count = 0;
166 $count++;
167 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
168
169 $url = addslashes_js($link[1]);
170
3a42ad12
RW
171 $playerpath = $CFG->wwwroot.'/filter/mediaplugin/mp3player.swf';
172 $audioplayerpath = $CFG->wwwroot .'/filter/mediaplugin/flowplayer.audio.swf';
173 $colors = explode('&', $c);
174 $playercolors = array();
175 foreach ($colors as $color) {
176 $color = explode('=', $color);
177 $playercolors[$color[0]] = $color[1];
178 }
82afb587 179
3a42ad12
RW
180 $output = <<<OET
181 <span class="mediaplugin mediaplugin_mp3" id="$id"></span>
388d1c02 182 <noscript><div>
3a42ad12
RW
183 <object width="100" height="15" id="nonjsmp3plugin" name="undefined" data="$playerpath" type="application/x-shockwave-flash">
184 <param name="movie" value="$playerpath" />
185 <param name="allowfullscreen" value="false" />
186 <param name="allowscriptaccess" value="always" />
187 <param name="flashvars" value='config={"plugins": {"controls": {
188 "fullscreen": false,
189 "height": 15,
190 "autoHide": false,
191 "all": false,
192 "play": true,
193 "pause": true,
194 "scrubber": true
195 },
196 "audio": {"url": "$audioplayerpath"}
197 },
198 "clip":{"url":"$url",
199 "autoPlay": false},
200 "content":{"url":"$playerpath"}}}' />
4d4d20e7
PS
201 </object>
202 </div></noscript>
3a42ad12
RW
203OET;
204
205 $jsoutput = create_flowplayer($id, $url, 'mp3', $playercolors);
206 $output .= $jsoutput;
82afb587 207
208 return $output;
7e64d361 209}
210
ce5dc36e
RW
211function filter_mediaplugin_ogg_callback($link) {
212 global $CFG, $OUTPUT, $PAGE;
213
214 static $count = 0;
215 $count++;
216 $id = 'filter_ogg_'.time().$count; //we need something unique because it might be stored in text cache
217
218 $url = addslashes_js($link[1]);
219 $printlink = html_writer::link($url, get_string('oggaudio', 'filter_mediaplugin'));
220 $unsupportedplugins = get_string('unsupportedplugins', 'filter_mediaplugin', $printlink);
221 $output = <<<OET
222 <audio id="$id" src="$url" controls="true" width="100">
8e7fecd9 223 $unsupportedplugins
ce5dc36e
RW
224 </audio>
225OET;
8e7fecd9 226
ce5dc36e
RW
227 return $output;
228}
229
230function filter_mediaplugin_ogv_callback($link) {
231 global $CFG, $OUTPUT, $PAGE;
232
233 static $count = 0;
234 $count++;
235 $id = 'filter_ogv_'.time().$count; //we need something unique because it might be stored in text cache
236
237 $url = addslashes_js($link[1]);
238 $printlink = html_writer::link($url, get_string('ogvvideo', 'filter_mediaplugin'));
239 $unsupportedplugins = get_string('unsupportedplugins', 'filter_mediaplugin', $printlink);
240 $output = <<<OET
241 <video id="$id" src="$url" controls="true" width="600" >
8e7fecd9 242 $unsupportedplugins
ce5dc36e
RW
243 </video>
244OET;
245
246 return $output;
247}
248
35716b86 249function filter_mediaplugin_swf_callback($link) {
82afb587 250 global $PAGE;
7e64d361 251 static $count = 0;
252 $count++;
253 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
254
255 $width = empty($link[3]) ? '400' : $link[3];
256 $height = empty($link[4]) ? '300' : $link[4];
257 $url = addslashes_js($link[1]);
258
82afb587 259 $args = Array();
260 $args['movie'] = $url;
261 $args['width'] = $width;
262 $args['height'] = $height;
263 $args['majorversion'] = 6;
264 $args['build'] = 40;
265 $args['allowscriptaccess'] = 'never';
266 $args['quality'] = 'high';
82afb587 267
10eaeca8 268 $jsoutput = create_ufo_inline($id, $args);
35716b86 269
a845d68b 270 $output = $link[0].'<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'filter_mediaplugin').')</span>'.$jsoutput;
82afb587 271
272 return $output;
7e64d361 273}
274
35716b86 275function filter_mediaplugin_flv_callback($link) {
82afb587 276 global $CFG, $PAGE;
7e64d361 277
278 static $count = 0;
279 $count++;
280 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
281
282 $width = empty($link[3]) ? '480' : $link[3];
283 $height = empty($link[4]) ? '360' : $link[4];
284 $url = addslashes_js($link[1]);
285
3a42ad12
RW
286 $playerpath = $CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf';
287
288 $output = <<<EOT
289 <span class="mediaplugin mediaplugin_flv" id="$id"></span>
388d1c02 290 <noscript><div>
3a42ad12
RW
291 <object width="800" height="600" id="undefined" name="undefined" data="$playerpath" type="application/x-shockwave-flash">
292 <param name="movie" value="$playerpath" />
293 <param name="allowfullscreen" value="true" />
294 <param name="allowscriptaccess" value="always" />
295 <param name="flashvars" value='config={"clip":{"url":"$url",
296 "autoPlay": false},
297 "content":{"url":"$playerpath"}}}' />
4d4d20e7
PS
298 </object>
299 </div></noscript>
3a42ad12
RW
300EOT;
301
302 $jsoutput = create_flowplayer($id, $url, 'flv');
303 $output .= $jsoutput;
82afb587 304 return $output;
7e64d361 305}
306
35716b86 307function filter_mediaplugin_real_callback($link, $autostart=false) {
7e64d361 308 $url = addslashes_js($link[1]);
309 $mimetype = mimeinfo('type', $url);
310 $autostart = $autostart ? 'true' : 'false';
311
312// embed kept for now see MDL-8674
313 return $link[0].
314'<span class="mediaplugin mediaplugin_real">
315<script type="text/javascript">
316//<![CDATA[
317document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
318 <param name="src" value="'.$url.'" />\\
319 <param name="autostart" value="'.$autostart.'" />\\
320 <param name="controls" value="imagewindow" />\\
321 <param name="console" value="video" />\\
322 <param name="loop" value="true" />\\
7c59f3be 323 <embed src="'.$url.'" width="240" height="180" loop="true" type="'.$mimetype.'" controls="imagewindow" console="video" autostart="'.$autostart.'" />\\
7e64d361 324 </object><br />\\
325 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
326 <param name="src" value="'.$url.'" />\\
327 <param name="autostart" value="'.$autostart.'" />\\
328 <param name="controls" value="ControlPanel" />\\
329 <param name="console" value="video" />\\
330 <embed src="'.$url.'" width="240" height="30" controls="ControlPanel" type="'.$mimetype.'" console="video" autostart="'.$autostart.'" />\\
331 </object>\');
332//]]>
333</script></span>';
334}
335
4d8bccf4 336/**
337 * Change links to Youtube into embedded Youtube videos
338 */
35716b86 339function filter_mediaplugin_youtube_callback($link, $autostart=false) {
4d8bccf4 340
341 $site = addslashes_js($link[1]);
342 $url = addslashes_js($link[2]);
4842422f 343 $info = addslashes_js(strip_tags($link[3]));//strip out html tags as they won't work in the title attribute
4d8bccf4 344
4317f92f
PS
345 return '<object title="'.$info.'"
346 class="mediaplugin mediaplugin_youtube" type="application/x-shockwave-flash"
4d8bccf4 347 data="'.$site.'youtube.com/v/'.$url.'&amp;fs=1&amp;rel=0" width="425" height="344">'.
6188deb8 348 '<param name="movie" value="'.$site.'youtube.com/v/'.$url.'&amp;fs=1&amp;rel=0" />'.
4d8bccf4 349 '<param name="FlashVars" value="playerMode=embedded" />'.
350 '<param name="wmode" value="transparent" />'.
351 '<param name="allowFullScreen" value="true" />'.
e2cf2d7f 352 '</object>';
4d8bccf4 353}
354
469aa3ba
RW
355/**
356 * Change Youtube playlist into embedded Youtube playlist videos
357 */
358function filter_mediaplugin_youtube_playlist_callback($link, $autostart=false) {
359
360 $site = s($link[4]);
361 $param = s($link[5]);
362 $info = s($link[6]);
363
364 return '<object title="'.$info.'"
365 class="mediaplugin mediaplugin_youtube" type="application/x-shockwave-flash"
366 data="'.$site.'youtube.com/p/'.$param.'&amp;fs=1&amp;rel=0" width="400" height="320">'.
367 '<param name="movie" value="'.$site.'youtube.com/p/'.$param.'&amp;fs=1&amp;rel=0" />'.
368 '<param name="FlashVars" value="playerMode=embedded" />'.
369 '<param name="wmode" value="transparent" />'.
370 '<param name="allowFullScreen" value="true" />'.
371 '</object>';
372}
373
c159f28b 374/**
375 * Change links to images into embedded images
376 */
35716b86 377function filter_mediaplugin_img_callback($link, $autostart=false) {
c159f28b 378 $url = addslashes_js($link[1]);
379 $info = addslashes_js($link[2]);
380
381 return '<img class="mediaplugin mediaplugin_img" alt="" title="'.$info.'" src="'.$url.'" />';
382}
383
7e64d361 384/**
385 * Embed video using window media player if available
386 */
35716b86 387function filter_mediaplugin_wmp_callback($link, $autostart=false) {
7e64d361 388 $url = $link[1];
389 if (empty($link[3]) or empty($link[4])) {
390 $mpsize = '';
391 $size = 'width="300" height="260"';
392 $autosize = 'true';
393 } else {
394 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
395 $mpsize = $size;
396 $autosize = 'false';
397 }
398 $mimetype = mimeinfo('type', $url);
399 $autostart = $autostart ? 'true' : 'false';
400
401 return $link[0].
402'<span class="mediaplugin mediaplugin_wmp">
403<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
404 standby="Loading Microsoft(R) Windows(R) Media Player components..."
405 type="application/x-oleobject">
406 <param name="Filename" value="'.$url.'" />
407 <param name="src" value="'.$url.'" />
408 <param name="url" value="'.$url.'" />
409 <param name="ShowControls" value="true" />
410 <param name="AutoRewind" value="true" />
411 <param name="AutoStart" value="'.$autostart.'" />
412 <param name="Autosize" value="'.$autosize.'" />
413 <param name="EnableContextMenu" value="true" />
414 <param name="TransparentAtStart" value="false" />
415 <param name="AnimationAtStart" value="false" />
416 <param name="ShowGotoBar" value="false" />
417 <param name="EnableFullScreenControls" value="true" />
418<!--[if !IE]>-->
419 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
420 <param name="src" value="'.$url.'" />
421 <param name="controller" value="true" />
422 <param name="autoplay" value="'.$autostart.'" />
423 <param name="autostart" value="'.$autostart.'" />
424 <param name="resize" value="scale" />
425 </object>
426<!--<![endif]-->
427</object></span>';
428}
429
35716b86 430function filter_mediaplugin_qt_callback($link, $autostart=false) {
7e64d361 431 $url = $link[1];
432 if (empty($link[3]) or empty($link[4])) {
1387d5eb 433 $size = 'width="440" height="315"';
7e64d361 434 } else {
435 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
436 }
437 $mimetype = mimeinfo('type', $url);
438 $autostart = $autostart ? 'true' : 'false';
439
440 return $link[0].
441'<span class="mediaplugin mediaplugin_qt">
442<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
443 codebase="http://www.apple.com/qtactivex/qtplugin.cab" '.$size.'>
444 <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
445 <param name="src" value="'.$url.'" />
d42df9da 446 <param name="controller" value="true" />
7e64d361 447 <param name="loop" value="true" />
448 <param name="autoplay" value="'.$autostart.'" />
449 <param name="autostart" value="'.$autostart.'" />
450 <param name="scale" value="aspect" />
451<!--[if !IE]>-->
452 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
453 <param name="src" value="'.$url.'" />
454 <param name="pluginurl" value="http://www.apple.com/quicktime/download/" />
455 <param name="controller" value="true" />
456 <param name="loop" value="true" />
457 <param name="autoplay" value="'.$autostart.'" />
458 <param name="autostart" value="'.$autostart.'" />
459 <param name="scale" value="aspect" />
460 </object>
461<!--<![endif]-->
462</object></span>';
463}
6de17fde 464
4317f92f 465