MDL-11328 Added the missing variable
[moodle-pu.git] / filter / mediaplugin / filter.php
blob7ed3b27eda90034e79e8ace8899dc8bc3e1046fa
1 <?php // $Id$
2 //////////////////////////////////////////////////////////////
3 // Media plugin filtering
4 //
5 // This filter will replace any links to a media file with
6 // a media plugin that plays that media inline
7 //
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) {
22 global $CFG;
24 include 'defaultsettings.php';
26 // You should never modify parameters passed to a method or function, it's BAD practice. Create a copy instead.
27 // The reason is that you must always be able to refer to the original parameter that was passed.
28 // For this reason, I changed $text = preg_replace(..,..,$text) into $newtext = preg.... (NICOLAS CONNAULT)
29 // Thanks to Pablo Etcheverry for pointing this out! MDL-10177
31 // We're using the UFO technique for flash to attain XHTML Strict 1.0
32 // See: http://www.bobbyvandersluis.com/ufo/
33 $newtext = fullclone($text);
35 if ($CFG->filter_mediaplugin_enable_mp3) {
36 $search = '/<a.*?href="([^<]+\.mp3)"[^>]*>.*?<\/a>/is';
37 $newtext = preg_replace_callback($search, 'mediaplugin_filter_mp3_callback', $newtext);
40 if ($CFG->filter_mediaplugin_enable_swf) {
41 $search = '/<a.*?href="([^<]+\.swf)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
42 $newtext = preg_replace_callback($search, 'mediaplugin_filter_swf_callback', $newtext);
45 if ($CFG->filter_mediaplugin_enable_flv) {
46 $search = '/<a.*?href="([^<]+\.flv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
47 $newtext = preg_replace_callback($search, 'mediaplugin_filter_flv_callback', $newtext);
50 if ($CFG->filter_mediaplugin_enable_mov) {
51 $search = '/<a.*?href="([^<]+\.mov)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
52 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
55 if ($CFG->filter_mediaplugin_enable_wmv) {
56 $search = '/<a.*?href="([^<]+\.wmv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
57 $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
60 if ($CFG->filter_mediaplugin_enable_mpg) {
61 $search = '/<a.*?href="([^<]+\.mpe?g)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
62 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
65 if ($CFG->filter_mediaplugin_enable_avi) {
66 $search = '/<a.*?href="([^<]+\.avi)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
67 $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
70 if ($CFG->filter_mediaplugin_enable_ram) {
71 $search = '/<a.*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
72 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
75 if ($CFG->filter_mediaplugin_enable_rpm) {
76 $search = '/<a.*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
77 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
80 if ($CFG->filter_mediaplugin_enable_rm) {
81 $search = '/<a.*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
82 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
85 if (is_null($newtext)) {
86 $newtext = $text;
88 return $newtext;
91 ///===========================
92 /// callback filter functions
94 function mediaplugin_filter_mp3_callback($link) {
95 global $CFG, $THEME;
97 if (!empty($THEME->filter_mediaplugin_colors)) {
98 $c = $THEME->filter_mediaplugin_colors; // You can set this up in your theme/xxx/config.php
99 } else {
100 $c = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&'.
101 'iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&'.
102 'waitForPlay=yes';
104 $c = htmlentities($c);
106 static $count = 0;
107 $count++;
108 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
110 $url = addslashes_js($link[1]);
112 return $link[0].
113 '<span class="mediaplugin mediaplugin_mp3" id="'.$id.'">('.get_string('mp3audio', 'mediaplugin').')</span>
114 <script type="text/javascript">
115 //<![CDATA[
116 var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/mp3player.swf?src='.$url.'",
117 width:"90", height:"15", majorversion:"6", build:"40", flashvars:"'.$c.'", quality: "high" };
118 UFO.create(FO, "'.$id.'");
119 //]]>
120 </script>';
123 function mediaplugin_filter_swf_callback($link) {
124 static $count = 0;
125 $count++;
126 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
128 $width = empty($link[3]) ? '400' : $link[3];
129 $height = empty($link[4]) ? '300' : $link[4];
130 $url = addslashes_js($link[1]);
132 return $link[0].
133 '<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'mediaplugin').')</span>
134 <script type="text/javascript">
135 //<![CDATA[
136 var FO = { movie:"'.$url.'", width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
137 allowscriptaccess:"never", quality: "high" };
138 UFO.create(FO, "'.$id.'");
139 //]]>
140 </script>';
143 function mediaplugin_filter_flv_callback($link) {
144 global $CFG;
146 static $count = 0;
147 $count++;
148 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
150 $width = empty($link[3]) ? '480' : $link[3];
151 $height = empty($link[4]) ? '360' : $link[4];
152 $url = addslashes_js($link[1]);
154 return $link[0].
155 '<span class="mediaplugin mediaplugin_flv" id="'.$id.'">('.get_string('flashvideo', 'mediaplugin').')</span>
156 <script type="text/javascript">
157 //<![CDATA[
158 var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf?file='.$url.'",
159 width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
160 allowscriptaccess:"never", quality: "high" };
161 UFO.create(FO, "'.$id.'");
162 //]]>
163 </script>';
166 function mediaplugin_filter_real_callback($link, $autostart=false) {
167 $url = addslashes_js($link[1]);
168 $mimetype = mimeinfo('type', $url);
169 $autostart = $autostart ? 'true' : 'false';
171 // embed kept for now see MDL-8674
172 return $link[0].
173 '<span class="mediaplugin mediaplugin_real">
174 <script type="text/javascript">
175 //<![CDATA[
176 document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
177 <param name="src" value="'.$url.'" />\\
178 <param name="autostart" value="'.$autostart.'" />\\
179 <param name="controls" value="imagewindow" />\\
180 <param name="console" value="video" />\\
181 <param name="loop" value="true" />\\
182 <embed src="'.$url.'" width=240" height="180" loop="true" type="'.$mimetype.'" controls="imagewindow" console="video" autostart="'.$autostart.'" />\\
183 </object><br />\\
184 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
185 <param name="src" value="'.$url.'" />\\
186 <param name="autostart" value="'.$autostart.'" />\\
187 <param name="controls" value="ControlPanel" />\\
188 <param name="console" value="video" />\\
189 <embed src="'.$url.'" width="240" height="30" controls="ControlPanel" type="'.$mimetype.'" console="video" autostart="'.$autostart.'" />\\
190 </object>\');
191 //]]>
192 </script></span>';
196 * Embed video using window media player if available
198 function mediaplugin_filter_wmp_callback($link, $autostart=false) {
199 $url = $link[1];
200 if (empty($link[3]) or empty($link[4])) {
201 $mpsize = '';
202 $size = 'width="300" height="260"';
203 $autosize = 'true';
204 } else {
205 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
206 $mpsize = $size;
207 $autosize = 'false';
209 $mimetype = mimeinfo('type', $url);
210 $autostart = $autostart ? 'true' : 'false';
212 return $link[0].
213 '<span class="mediaplugin mediaplugin_wmp">
214 <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
215 standby="Loading Microsoft(R) Windows(R) Media Player components..."
216 type="application/x-oleobject">
217 <param name="Filename" value="'.$url.'" />
218 <param name="src" value="'.$url.'" />
219 <param name="url" value="'.$url.'" />
220 <param name="ShowControls" value="true" />
221 <param name="AutoRewind" value="true" />
222 <param name="AutoStart" value="'.$autostart.'" />
223 <param name="Autosize" value="'.$autosize.'" />
224 <param name="EnableContextMenu" value="true" />
225 <param name="TransparentAtStart" value="false" />
226 <param name="AnimationAtStart" value="false" />
227 <param name="ShowGotoBar" value="false" />
228 <param name="EnableFullScreenControls" value="true" />
229 <!--[if !IE]>-->
230 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
231 <param name="src" value="'.$url.'" />
232 <param name="controller" value="true" />
233 <param name="autoplay" value="'.$autostart.'" />
234 <param name="autostart" value="'.$autostart.'" />
235 <param name="resize" value="scale" />
236 </object>
237 <!--<![endif]-->
238 </object></span>';
241 function mediaplugin_filter_qt_callback($link, $autostart=false) {
242 $url = $link[1];
243 if (empty($link[3]) or empty($link[4])) {
244 $size = 'width="280" height="210"';
245 } else {
246 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
248 $mimetype = mimeinfo('type', $url);
249 $autostart = $autostart ? 'true' : 'false';
251 return $link[0].
252 '<span class="mediaplugin mediaplugin_qt">
253 <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
254 codebase="http://www.apple.com/qtactivex/qtplugin.cab" '.$size.'>
255 <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
256 <param name="src" value="'.$url.'" />
257 <param name="coltroller" value="true" />
258 <param name="loop" value="true" />
259 <param name="autoplay" value="'.$autostart.'" />
260 <param name="autostart" value="'.$autostart.'" />
261 <param name="scale" value="aspect" />
262 <!--[if !IE]>-->
263 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
264 <param name="src" value="'.$url.'" />
265 <param name="pluginurl" value="http://www.apple.com/quicktime/download/" />
266 <param name="controller" value="true" />
267 <param name="loop" value="true" />
268 <param name="autoplay" value="'.$autostart.'" />
269 <param name="autostart" value="'.$autostart.'" />
270 <param name="scale" value="aspect" />
271 </object>
272 <!--<![endif]-->
273 </object></span>';