user's role / capabiltiy report: sort the context tree.
[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
38 if ($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
c96bdc02 43 if ($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
d07851e1 48 if ($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
c96bdc02 53 if ($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
c96bdc02 58 if ($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
c96bdc02 63 if ($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
c96bdc02 68 if ($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
d539ccf3 73 if ($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
03d843b2 78 if ($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
f73eb30b 83 if ($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
15fb52fb 88 if (is_null($newtext) or $newtext === $text) {
89 // error or not filtered
90 return $text;
2619224c 91 }
2f39c5ef 92
0760ba2c 93 if (!$eolas_fix_applied) {
94 $newtext .= '<script defer="defer" src="' . $CFG->wwwroot . '/filter/mediaplugin/eolas_fix.js" type="text/javascript">// <![CDATA[ ]]></script>';
95 $eolas_fix_applied = true;
96 }
2f39c5ef 97
2619224c 98 return $newtext;
6de17fde 99}
100
7e64d361 101///===========================
102/// callback filter functions
103
104function mediaplugin_filter_mp3_callback($link) {
105 global $CFG, $THEME;
106
107 if (!empty($THEME->filter_mediaplugin_colors)) {
108 $c = $THEME->filter_mediaplugin_colors; // You can set this up in your theme/xxx/config.php
109 } else {
110 $c = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&'.
111 'iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&'.
112 'waitForPlay=yes';
113 }
114 $c = htmlentities($c);
115
116 static $count = 0;
117 $count++;
118 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
119
120 $url = addslashes_js($link[1]);
121
122 return $link[0].
123'<span class="mediaplugin mediaplugin_mp3" id="'.$id.'">('.get_string('mp3audio', 'mediaplugin').')</span>
124<script type="text/javascript">
125//<![CDATA[
126 var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/mp3player.swf?src='.$url.'",
127 width:"90", height:"15", majorversion:"6", build:"40", flashvars:"'.$c.'", quality: "high" };
128 UFO.create(FO, "'.$id.'");
129//]]>
130</script>';
131}
132
133function mediaplugin_filter_swf_callback($link) {
134 static $count = 0;
135 $count++;
136 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
137
138 $width = empty($link[3]) ? '400' : $link[3];
139 $height = empty($link[4]) ? '300' : $link[4];
140 $url = addslashes_js($link[1]);
141
142 return $link[0].
143'<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'mediaplugin').')</span>
144<script type="text/javascript">
145//<![CDATA[
146 var FO = { movie:"'.$url.'", width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
147 allowscriptaccess:"never", quality: "high" };
148 UFO.create(FO, "'.$id.'");
149//]]>
150</script>';
151}
152
153function mediaplugin_filter_flv_callback($link) {
154 global $CFG;
155
156 static $count = 0;
157 $count++;
158 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
159
160 $width = empty($link[3]) ? '480' : $link[3];
161 $height = empty($link[4]) ? '360' : $link[4];
162 $url = addslashes_js($link[1]);
163
164 return $link[0].
165'<span class="mediaplugin mediaplugin_flv" id="'.$id.'">('.get_string('flashvideo', 'mediaplugin').')</span>
166<script type="text/javascript">
167//<![CDATA[
168 var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf?file='.$url.'",
169 width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
170 allowscriptaccess:"never", quality: "high" };
171 UFO.create(FO, "'.$id.'");
172//]]>
173</script>';
174}
175
176function mediaplugin_filter_real_callback($link, $autostart=false) {
177 $url = addslashes_js($link[1]);
178 $mimetype = mimeinfo('type', $url);
179 $autostart = $autostart ? 'true' : 'false';
180
181// embed kept for now see MDL-8674
182 return $link[0].
183'<span class="mediaplugin mediaplugin_real">
184<script type="text/javascript">
185//<![CDATA[
186document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
187 <param name="src" value="'.$url.'" />\\
188 <param name="autostart" value="'.$autostart.'" />\\
189 <param name="controls" value="imagewindow" />\\
190 <param name="console" value="video" />\\
191 <param name="loop" value="true" />\\
192 <embed src="'.$url.'" width=240" height="180" loop="true" type="'.$mimetype.'" controls="imagewindow" console="video" autostart="'.$autostart.'" />\\
193 </object><br />\\
194 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
195 <param name="src" value="'.$url.'" />\\
196 <param name="autostart" value="'.$autostart.'" />\\
197 <param name="controls" value="ControlPanel" />\\
198 <param name="console" value="video" />\\
199 <embed src="'.$url.'" width="240" height="30" controls="ControlPanel" type="'.$mimetype.'" console="video" autostart="'.$autostart.'" />\\
200 </object>\');
201//]]>
202</script></span>';
203}
204
205/**
206 * Embed video using window media player if available
207 */
208function mediaplugin_filter_wmp_callback($link, $autostart=false) {
209 $url = $link[1];
210 if (empty($link[3]) or empty($link[4])) {
211 $mpsize = '';
212 $size = 'width="300" height="260"';
213 $autosize = 'true';
214 } else {
215 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
216 $mpsize = $size;
217 $autosize = 'false';
218 }
219 $mimetype = mimeinfo('type', $url);
220 $autostart = $autostart ? 'true' : 'false';
221
222 return $link[0].
223'<span class="mediaplugin mediaplugin_wmp">
224<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
225 standby="Loading Microsoft(R) Windows(R) Media Player components..."
226 type="application/x-oleobject">
227 <param name="Filename" value="'.$url.'" />
228 <param name="src" value="'.$url.'" />
229 <param name="url" value="'.$url.'" />
230 <param name="ShowControls" value="true" />
231 <param name="AutoRewind" value="true" />
232 <param name="AutoStart" value="'.$autostart.'" />
233 <param name="Autosize" value="'.$autosize.'" />
234 <param name="EnableContextMenu" value="true" />
235 <param name="TransparentAtStart" value="false" />
236 <param name="AnimationAtStart" value="false" />
237 <param name="ShowGotoBar" value="false" />
238 <param name="EnableFullScreenControls" value="true" />
239<!--[if !IE]>-->
240 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
241 <param name="src" value="'.$url.'" />
242 <param name="controller" value="true" />
243 <param name="autoplay" value="'.$autostart.'" />
244 <param name="autostart" value="'.$autostart.'" />
245 <param name="resize" value="scale" />
246 </object>
247<!--<![endif]-->
248</object></span>';
249}
250
251function mediaplugin_filter_qt_callback($link, $autostart=false) {
252 $url = $link[1];
253 if (empty($link[3]) or empty($link[4])) {
254 $size = 'width="280" height="210"';
255 } else {
256 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
257 }
258 $mimetype = mimeinfo('type', $url);
259 $autostart = $autostart ? 'true' : 'false';
260
261 return $link[0].
262'<span class="mediaplugin mediaplugin_qt">
263<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
264 codebase="http://www.apple.com/qtactivex/qtplugin.cab" '.$size.'>
265 <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
266 <param name="src" value="'.$url.'" />
d42df9da 267 <param name="controller" value="true" />
7e64d361 268 <param name="loop" value="true" />
269 <param name="autoplay" value="'.$autostart.'" />
270 <param name="autostart" value="'.$autostart.'" />
271 <param name="scale" value="aspect" />
272<!--[if !IE]>-->
273 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
274 <param name="src" value="'.$url.'" />
275 <param name="pluginurl" value="http://www.apple.com/quicktime/download/" />
276 <param name="controller" value="true" />
277 <param name="loop" value="true" />
278 <param name="autoplay" value="'.$autostart.'" />
279 <param name="autostart" value="'.$autostart.'" />
280 <param name="scale" value="aspect" />
281 </object>
282<!--<![endif]-->
283</object></span>';
284}
6de17fde 285
286?>