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