2 //////////////////////////////////////////////////////////////
3 // Media plugin filtering
5 // This filter will replace any links to a media file with
6 // a media plugin that plays that media inline
8 // To activate this filter, add a line like this to your
9 // list of filters in your Filter configuration:
11 // filter/mediaplugin/filter.php
13 //////////////////////////////////////////////////////////////
15 /// This is the filtering function itself. It accepts the
16 /// courseid and the text to be filtered (in HTML form).
18 require_once($CFG->libdir
.'/filelib.php');
21 function mediaplugin_filter($courseid, $text) {
23 static $eolas_fix_applied = false;
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
30 // We're using the UFO technique for flash to attain XHTML Strict 1.0
31 // See: http://www.bobbyvandersluis.com/ufo/
32 if (!is_string($text)) {
33 // non string data can not be filtered anyway
36 $newtext = $text; // fullclone is slow and not needed here
38 if ($CFG->filter_mediaplugin_enable_mp3
) {
39 $search = '/<a.*?href="([^<]+\.mp3)"[^>]*>.*?<\/a>/is';
40 $newtext = preg_replace_callback($search, 'mediaplugin_filter_mp3_callback', $newtext);
43 if ($CFG->filter_mediaplugin_enable_swf
) {
44 $search = '/<a.*?href="([^<]+\.swf)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
45 $newtext = preg_replace_callback($search, 'mediaplugin_filter_swf_callback', $newtext);
48 if ($CFG->filter_mediaplugin_enable_flv
) {
49 $search = '/<a.*?href="([^<]+\.flv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
50 $newtext = preg_replace_callback($search, 'mediaplugin_filter_flv_callback', $newtext);
53 if ($CFG->filter_mediaplugin_enable_mov
) {
54 $search = '/<a.*?href="([^<]+\.mov)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
55 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
58 if ($CFG->filter_mediaplugin_enable_wmv
) {
59 $search = '/<a.*?href="([^<]+\.wmv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
60 $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
63 if ($CFG->filter_mediaplugin_enable_mpg
) {
64 $search = '/<a.*?href="([^<]+\.mpe?g)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
65 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
68 if ($CFG->filter_mediaplugin_enable_avi
) {
69 $search = '/<a.*?href="([^<]+\.avi)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
70 $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
73 if ($CFG->filter_mediaplugin_enable_ram
) {
74 $search = '/<a.*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
75 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
78 if ($CFG->filter_mediaplugin_enable_rpm
) {
79 $search = '/<a.*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
80 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
83 if ($CFG->filter_mediaplugin_enable_rm
) {
84 $search = '/<a.*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
85 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
88 if (is_null($newtext) or $newtext === $text) {
89 // error or not filtered
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;
101 ///===========================
102 /// callback filter functions
104 function mediaplugin_filter_mp3_callback($link) {
107 if (!empty($THEME->filter_mediaplugin_colors
)) {
108 $c = $THEME->filter_mediaplugin_colors
; // You can set this up in your theme/xxx/config.php
110 $c = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&'.
111 'iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&'.
114 $c = htmlentities($c);
118 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
120 $url = addslashes_js($link[1]);
123 '<span class="mediaplugin mediaplugin_mp3" id="'.$id.'">('.get_string('mp3audio', 'mediaplugin').')</span>
124 <script type="text/javascript">
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.'");
133 function mediaplugin_filter_swf_callback($link) {
136 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
138 $width = empty($link[3]) ?
'400' : $link[3];
139 $height = empty($link[4]) ?
'300' : $link[4];
140 $url = addslashes_js($link[1]);
143 '<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'mediaplugin').')</span>
144 <script type="text/javascript">
146 var FO = { movie:"'.$url.'", width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
147 allowscriptaccess:"never", quality: "high" };
148 UFO.create(FO, "'.$id.'");
153 function mediaplugin_filter_flv_callback($link) {
158 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
160 $width = empty($link[3]) ?
'480' : $link[3];
161 $height = empty($link[4]) ?
'360' : $link[4];
162 $url = addslashes_js($link[1]);
165 '<span class="mediaplugin mediaplugin_flv" id="'.$id.'">('.get_string('flashvideo', 'mediaplugin').')</span>
166 <script type="text/javascript">
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.'");
176 function mediaplugin_filter_real_callback($link, $autostart=false) {
177 $url = addslashes_js($link[1]);
178 $mimetype = mimeinfo('type', $url);
179 $autostart = $autostart ?
'true' : 'false';
181 // embed kept for now see MDL-8674
183 '<span class="mediaplugin mediaplugin_real">
184 <script type="text/javascript">
186 document.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.'" />\\
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.'" />\\
206 * Embed video using window media player if available
208 function mediaplugin_filter_wmp_callback($link, $autostart=false) {
210 if (empty($link[3]) or empty($link[4])) {
212 $size = 'width="300" height="260"';
215 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
219 $mimetype = mimeinfo('type', $url);
220 $autostart = $autostart ?
'true' : 'false';
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" />
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" />
251 function mediaplugin_filter_qt_callback($link, $autostart=false) {
253 if (empty($link[3]) or empty($link[4])) {
254 $size = 'width="280" height="210"';
256 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
258 $mimetype = mimeinfo('type', $url);
259 $autostart = $autostart ?
'true' : 'false';
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.'" />
267 <param name="coltroller" value="true" />
268 <param name="loop" value="true" />
269 <param name="autoplay" value="'.$autostart.'" />
270 <param name="autostart" value="'.$autostart.'" />
271 <param name="scale" value="aspect" />
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" />