lib MDL-19077 Created output redirect method and implemented against weblib redirect...
[moodle.git] / filter / mediaplugin / filter.php
CommitLineData
7e64d361 1<?php // $Id$
6de17fde 2//////////////////////////////////////////////////////////////
3// Media plugin filtering
7e64d361 4//
5// This filter will replace any links to a media file with
6de17fde 6// a media plugin that plays that media inline
7//
4da85078 8// To activate this filter, add a line like this to your
9// list of filters in your Filter configuration:
6de17fde 10//
4da85078 11// filter/mediaplugin/filter.php
6de17fde 12//
13//////////////////////////////////////////////////////////////
14
7e64d361 15/// This is the filtering function itself. It accepts the
6de17fde 16/// courseid and the text to be filtered (in HTML form).
17
7e64d361 18require_once($CFG->libdir.'/filelib.php');
19
ccc161f8 20class mediaplugin_filter extends moodle_text_filter {
21 private $eolas_fix_applied = false;
9e3f34d1 22 function filter($text) {
23 global $CFG;
24 // You should never modify parameters passed to a method or function, it's BAD practice. Create a copy instead.
25 // The reason is that you must always be able to refer to the original parameter that was passed.
26 // For this reason, I changed $text = preg_replace(..,..,$text) into $newtext = preg.... (NICOLAS CONNAULT)
27 // Thanks to Pablo Etcheverry for pointing this out! MDL-10177
28
29 // We're using the UFO technique for flash to attain XHTML Strict 1.0
30 // See: http://www.bobbyvandersluis.com/ufo/
31 if (!is_string($text)) {
32 // non string data can not be filtered anyway
33 return $text;
34 }
35 $newtext = $text; // fullclone is slow and not needed here
36
37 if ($CFG->filter_mediaplugin_enable_mp3) {
38 $search = '/<a.*?href="([^<]+\.mp3)"[^>]*>.*?<\/a>/is';
39 $newtext = preg_replace_callback($search, 'mediaplugin_filter_mp3_callback', $newtext);
40 }
41
42 if ($CFG->filter_mediaplugin_enable_swf) {
43 $search = '/<a.*?href="([^<]+\.swf)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
44 $newtext = preg_replace_callback($search, 'mediaplugin_filter_swf_callback', $newtext);
45 }
46
47 if ($CFG->filter_mediaplugin_enable_flv) {
48 $search = '/<a.*?href="([^<]+\.flv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
49 $newtext = preg_replace_callback($search, 'mediaplugin_filter_flv_callback', $newtext);
50 }
51
52 if ($CFG->filter_mediaplugin_enable_mov) {
53 $search = '/<a.*?href="([^<]+\.mov)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
54 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
55 }
56
57 if ($CFG->filter_mediaplugin_enable_wmv) {
58 $search = '/<a.*?href="([^<]+\.wmv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
59 $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
60 }
61
62 if ($CFG->filter_mediaplugin_enable_mpg) {
63 $search = '/<a.*?href="([^<]+\.mpe?g)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
64 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
65 }
66
67 if ($CFG->filter_mediaplugin_enable_avi) {
68 $search = '/<a.*?href="([^<]+\.avi)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
69 $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
70 }
71
72 if ($CFG->filter_mediaplugin_enable_ram) {
73 $search = '/<a.*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
74 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
75 }
76
77 if ($CFG->filter_mediaplugin_enable_rpm) {
78 $search = '/<a.*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
79 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
80 }
81
82 if ($CFG->filter_mediaplugin_enable_rm) {
83 $search = '/<a.*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
84 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
85 }
86
87 if (!empty($CFG->filter_mediaplugin_enable_youtube)) {
88 $search = '/<a.*?href="([^<]*)youtube.com\/watch\?v=([^"]*)"[^>]*>(.*?)<\/a>/is';
89 $newtext = preg_replace_callback($search, 'mediaplugin_filter_youtube_callback', $newtext);
90
91 $search = '/<a.*?href="([^<]*)youtube.com\/v\/([^"]*)"[^>]*>(.*?)<\/a>/is';
92 $newtext = preg_replace_callback($search, 'mediaplugin_filter_youtube_callback', $newtext);
93 }
94
224eccbf 95 if (!empty($CFG->filter_mediaplugin_enable_img)) {
c159f28b 96 $search = '/<a.*?href="([^<]+\.jpg)"[^>]*>(.*?)<\/a>/is';
97 $newtext = preg_replace_callback($search, 'mediaplugin_filter_img_callback', $newtext);
98 $search = '/<a.*?href="([^<]+\.png)"[^>]*>(.*?)<\/a>/is';
99 $newtext = preg_replace_callback($search, 'mediaplugin_filter_img_callback', $newtext);
100 $search = '/<a.*?href="([^<]+\.gif)"[^>]*>(.*?)<\/a>/is';
101 $newtext = preg_replace_callback($search, 'mediaplugin_filter_img_callback', $newtext);
102 }
103
9e3f34d1 104 if (empty($newtext) or $newtext === $text) {
105 // error or not filtered
106 unset($newtext);
107 return $text;
108 }
109
110 if (!$this->eolas_fix_applied) {
111 $newtext .= '<script defer="defer" src="' . $CFG->wwwroot . '/filter/mediaplugin/eolas_fix.js" type="text/javascript">// <![CDATA[ ]]></script>';
112 $this->eolas_fix_applied = true;
113 }
114
115 return $newtext;
d07851e1 116 }
6de17fde 117}
118
7e64d361 119///===========================
120/// callback filter functions
121
122function mediaplugin_filter_mp3_callback($link) {
123 global $CFG, $THEME;
124
125 if (!empty($THEME->filter_mediaplugin_colors)) {
126 $c = $THEME->filter_mediaplugin_colors; // You can set this up in your theme/xxx/config.php
127 } else {
128 $c = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&'.
129 'iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&'.
130 'waitForPlay=yes';
131 }
132 $c = htmlentities($c);
133
134 static $count = 0;
135 $count++;
136 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
137
138 $url = addslashes_js($link[1]);
139
140 return $link[0].
141'<span class="mediaplugin mediaplugin_mp3" id="'.$id.'">('.get_string('mp3audio', 'mediaplugin').')</span>
142<script type="text/javascript">
143//<![CDATA[
144 var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/mp3player.swf?src='.$url.'",
145 width:"90", height:"15", majorversion:"6", build:"40", flashvars:"'.$c.'", quality: "high" };
5bed8102 146 UFO.create(FO, "'.$id.'");
7e64d361 147//]]>
148</script>';
149}
150
151function mediaplugin_filter_swf_callback($link) {
152 static $count = 0;
153 $count++;
154 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
155
156 $width = empty($link[3]) ? '400' : $link[3];
157 $height = empty($link[4]) ? '300' : $link[4];
158 $url = addslashes_js($link[1]);
159
160 return $link[0].
161'<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'mediaplugin').')</span>
162<script type="text/javascript">
163//<![CDATA[
164 var FO = { movie:"'.$url.'", width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
165 allowscriptaccess:"never", quality: "high" };
5bed8102 166 UFO.create(FO, "'.$id.'");
7e64d361 167//]]>
168</script>';
169}
170
171function mediaplugin_filter_flv_callback($link) {
172 global $CFG;
173
174 static $count = 0;
175 $count++;
176 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
177
178 $width = empty($link[3]) ? '480' : $link[3];
179 $height = empty($link[4]) ? '360' : $link[4];
180 $url = addslashes_js($link[1]);
181
182 return $link[0].
183'<span class="mediaplugin mediaplugin_flv" id="'.$id.'">('.get_string('flashvideo', 'mediaplugin').')</span>
184<script type="text/javascript">
185//<![CDATA[
186 var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf?file='.$url.'",
187 width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
c7ad563f 188 allowscriptaccess:"never", quality: "high", allowfullscreen: "true" };
5bed8102 189 UFO.create(FO, "'.$id.'");
7e64d361 190//]]>
191</script>';
192}
193
194function mediaplugin_filter_real_callback($link, $autostart=false) {
195 $url = addslashes_js($link[1]);
196 $mimetype = mimeinfo('type', $url);
197 $autostart = $autostart ? 'true' : 'false';
198
199// embed kept for now see MDL-8674
200 return $link[0].
201'<span class="mediaplugin mediaplugin_real">
202<script type="text/javascript">
203//<![CDATA[
204document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
205 <param name="src" value="'.$url.'" />\\
206 <param name="autostart" value="'.$autostart.'" />\\
207 <param name="controls" value="imagewindow" />\\
208 <param name="console" value="video" />\\
209 <param name="loop" value="true" />\\
7c59f3be 210 <embed src="'.$url.'" width="240" height="180" loop="true" type="'.$mimetype.'" controls="imagewindow" console="video" autostart="'.$autostart.'" />\\
7e64d361 211 </object><br />\\
212 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
213 <param name="src" value="'.$url.'" />\\
214 <param name="autostart" value="'.$autostart.'" />\\
215 <param name="controls" value="ControlPanel" />\\
216 <param name="console" value="video" />\\
217 <embed src="'.$url.'" width="240" height="30" controls="ControlPanel" type="'.$mimetype.'" console="video" autostart="'.$autostart.'" />\\
218 </object>\');
219//]]>
220</script></span>';
221}
222
4d8bccf4 223/**
224 * Change links to Youtube into embedded Youtube videos
225 */
226function mediaplugin_filter_youtube_callback($link, $autostart=false) {
227
228 $site = addslashes_js($link[1]);
229 $url = addslashes_js($link[2]);
230 $info = addslashes_js($link[3]);
231
e2cf2d7f 232 return '<object title="'.$info.'"
233 class="mediaplugin mediaplugin_youtube" type="application/x-shockwave-flash"
4d8bccf4 234 data="'.$site.'youtube.com/v/'.$url.'&amp;fs=1&amp;rel=0" width="425" height="344">'.
6188deb8 235 '<param name="movie" value="'.$site.'youtube.com/v/'.$url.'&amp;fs=1&amp;rel=0" />'.
4d8bccf4 236 '<param name="FlashVars" value="playerMode=embedded" />'.
237 '<param name="wmode" value="transparent" />'.
238 '<param name="allowFullScreen" value="true" />'.
e2cf2d7f 239 '</object>';
4d8bccf4 240}
241
c159f28b 242/**
243 * Change links to images into embedded images
244 */
245function mediaplugin_filter_img_callback($link, $autostart=false) {
246 $url = addslashes_js($link[1]);
247 $info = addslashes_js($link[2]);
248
249 return '<img class="mediaplugin mediaplugin_img" alt="" title="'.$info.'" src="'.$url.'" />';
250}
251
7e64d361 252/**
253 * Embed video using window media player if available
254 */
255function mediaplugin_filter_wmp_callback($link, $autostart=false) {
256 $url = $link[1];
257 if (empty($link[3]) or empty($link[4])) {
258 $mpsize = '';
259 $size = 'width="300" height="260"';
260 $autosize = 'true';
261 } else {
262 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
263 $mpsize = $size;
264 $autosize = 'false';
265 }
266 $mimetype = mimeinfo('type', $url);
267 $autostart = $autostart ? 'true' : 'false';
268
269 return $link[0].
270'<span class="mediaplugin mediaplugin_wmp">
271<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
272 standby="Loading Microsoft(R) Windows(R) Media Player components..."
273 type="application/x-oleobject">
274 <param name="Filename" value="'.$url.'" />
275 <param name="src" value="'.$url.'" />
276 <param name="url" value="'.$url.'" />
277 <param name="ShowControls" value="true" />
278 <param name="AutoRewind" value="true" />
279 <param name="AutoStart" value="'.$autostart.'" />
280 <param name="Autosize" value="'.$autosize.'" />
281 <param name="EnableContextMenu" value="true" />
282 <param name="TransparentAtStart" value="false" />
283 <param name="AnimationAtStart" value="false" />
284 <param name="ShowGotoBar" value="false" />
285 <param name="EnableFullScreenControls" value="true" />
286<!--[if !IE]>-->
287 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
288 <param name="src" value="'.$url.'" />
289 <param name="controller" value="true" />
290 <param name="autoplay" value="'.$autostart.'" />
291 <param name="autostart" value="'.$autostart.'" />
292 <param name="resize" value="scale" />
293 </object>
294<!--<![endif]-->
295</object></span>';
296}
297
298function mediaplugin_filter_qt_callback($link, $autostart=false) {
299 $url = $link[1];
300 if (empty($link[3]) or empty($link[4])) {
301 $size = 'width="280" height="210"';
302 } else {
303 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
304 }
305 $mimetype = mimeinfo('type', $url);
306 $autostart = $autostart ? 'true' : 'false';
307
308 return $link[0].
309'<span class="mediaplugin mediaplugin_qt">
310<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
311 codebase="http://www.apple.com/qtactivex/qtplugin.cab" '.$size.'>
312 <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
313 <param name="src" value="'.$url.'" />
d42df9da 314 <param name="controller" value="true" />
7e64d361 315 <param name="loop" value="true" />
316 <param name="autoplay" value="'.$autostart.'" />
317 <param name="autostart" value="'.$autostart.'" />
318 <param name="scale" value="aspect" />
319<!--[if !IE]>-->
320 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
321 <param name="src" value="'.$url.'" />
322 <param name="pluginurl" value="http://www.apple.com/quicktime/download/" />
323 <param name="controller" value="true" />
324 <param name="loop" value="true" />
325 <param name="autoplay" value="'.$autostart.'" />
326 <param name="autostart" value="'.$autostart.'" />
327 <param name="scale" value="aspect" />
328 </object>
329<!--<![endif]-->
330</object></span>';
331}
6de17fde 332
333?>