MDL-23479 backup - choice revised & old code deleted (but restore logs)
[moodle.git] / filter / mediaplugin / filter.php
CommitLineData
4317f92f 1<?php
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) {
82afb587 23 global $CFG, $PAGE;
9e3f34d1 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);
46936592 55
56 $search = '/<a.*?href="([^<]+\.mp4)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
57 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
58
59 $search = '/<a.*?href="([^<]+\.m4v)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
60 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
a184479a 61
62 $search = '/<a.*?href="([^<]+\.m4a)(\?d=([\d]{1,4}%?)x([\d]{1,4}%?))?"[^>]*>.*?<\/a>/is';
63 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
9e3f34d1 64 }
65
66 if ($CFG->filter_mediaplugin_enable_wmv) {
67 $search = '/<a.*?href="([^<]+\.wmv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
68 $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
69 }
70
71 if ($CFG->filter_mediaplugin_enable_mpg) {
72 $search = '/<a.*?href="([^<]+\.mpe?g)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
73 $newtext = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $newtext);
74 }
75
76 if ($CFG->filter_mediaplugin_enable_avi) {
77 $search = '/<a.*?href="([^<]+\.avi)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
78 $newtext = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $newtext);
79 }
80
81 if ($CFG->filter_mediaplugin_enable_ram) {
82 $search = '/<a.*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
83 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
84 }
85
86 if ($CFG->filter_mediaplugin_enable_rpm) {
87 $search = '/<a.*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
88 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
89 }
90
91 if ($CFG->filter_mediaplugin_enable_rm) {
92 $search = '/<a.*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
93 $newtext = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $newtext);
94 }
95
96 if (!empty($CFG->filter_mediaplugin_enable_youtube)) {
97 $search = '/<a.*?href="([^<]*)youtube.com\/watch\?v=([^"]*)"[^>]*>(.*?)<\/a>/is';
98 $newtext = preg_replace_callback($search, 'mediaplugin_filter_youtube_callback', $newtext);
99
100 $search = '/<a.*?href="([^<]*)youtube.com\/v\/([^"]*)"[^>]*>(.*?)<\/a>/is';
101 $newtext = preg_replace_callback($search, 'mediaplugin_filter_youtube_callback', $newtext);
102 }
103
224eccbf 104 if (!empty($CFG->filter_mediaplugin_enable_img)) {
c159f28b 105 $search = '/<a.*?href="([^<]+\.jpg)"[^>]*>(.*?)<\/a>/is';
106 $newtext = preg_replace_callback($search, 'mediaplugin_filter_img_callback', $newtext);
107 $search = '/<a.*?href="([^<]+\.png)"[^>]*>(.*?)<\/a>/is';
108 $newtext = preg_replace_callback($search, 'mediaplugin_filter_img_callback', $newtext);
109 $search = '/<a.*?href="([^<]+\.gif)"[^>]*>(.*?)<\/a>/is';
110 $newtext = preg_replace_callback($search, 'mediaplugin_filter_img_callback', $newtext);
111 }
112
9e3f34d1 113 if (empty($newtext) or $newtext === $text) {
114 // error or not filtered
115 unset($newtext);
116 return $text;
117 }
118
119 if (!$this->eolas_fix_applied) {
9dec75db 120 $PAGE->requires->js('/filter/mediaplugin/eolas_fix.js');
9e3f34d1 121 $this->eolas_fix_applied = true;
122 }
123
124 return $newtext;
d07851e1 125 }
6de17fde 126}
127
7e64d361 128///===========================
129/// callback filter functions
130
131function mediaplugin_filter_mp3_callback($link) {
088ccb43
PS
132 global $CFG, $OUTPUT, $PAGE;
133
134 $c = $OUTPUT->filter_mediaplugin_colors(); // You can set this up in your theme/xxx/config.php
7e64d361 135
136 static $count = 0;
137 $count++;
138 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
139
140 $url = addslashes_js($link[1]);
141
82afb587 142 $args = Array();
143 $args['movie'] = $CFG->wwwroot.'/filter/mediaplugin/mp3player.swf?src='.$url;
144 $args['width'] = 90;
145 $args['height'] = 15;
146 $args['majorversion'] = 6;
147 $args['build'] = 40;
148 $args['flashvars'] = $c;
149 $args['quality'] = 'high';
4d49202f 150
10eaeca8 151 $jsoutput = create_ufo_inline($id, $args);
82afb587 152
a845d68b 153 $output = $link[0].'<span class="mediaplugin mediaplugin_mp3" id="'.$id.'">('.get_string('mp3audio', 'filter_mediaplugin').')</span>'.$jsoutput;
82afb587 154
155 return $output;
7e64d361 156}
157
158function mediaplugin_filter_swf_callback($link) {
82afb587 159 global $PAGE;
7e64d361 160 static $count = 0;
161 $count++;
162 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
163
164 $width = empty($link[3]) ? '400' : $link[3];
165 $height = empty($link[4]) ? '300' : $link[4];
166 $url = addslashes_js($link[1]);
167
82afb587 168 $args = Array();
169 $args['movie'] = $url;
170 $args['width'] = $width;
171 $args['height'] = $height;
172 $args['majorversion'] = 6;
173 $args['build'] = 40;
174 $args['allowscriptaccess'] = 'never';
175 $args['quality'] = 'high';
82afb587 176
10eaeca8
PS
177 $jsoutput = create_ufo_inline($id, $args);
178
a845d68b 179 $output = $link[0].'<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'filter_mediaplugin').')</span>'.$jsoutput;
82afb587 180
181 return $output;
7e64d361 182}
183
184function mediaplugin_filter_flv_callback($link) {
82afb587 185 global $CFG, $PAGE;
7e64d361 186
187 static $count = 0;
188 $count++;
189 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
190
191 $width = empty($link[3]) ? '480' : $link[3];
192 $height = empty($link[4]) ? '360' : $link[4];
193 $url = addslashes_js($link[1]);
194
82afb587 195 $args = Array();
196 $args['movie'] = $CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf?file='.$url;
197 $args['width'] = $width;
198 $args['height'] = $height;
199 $args['majorversion'] = 6;
200 $args['build'] = 40;
201 $args['allowscriptaccess'] = 'never';
202 $args['quality'] = 'high';
203 $args['allowfullscreen'] = 'true';
10eaeca8 204 $jsoutput = create_ufo_inline($id, $args);
82afb587 205
a845d68b 206 $output = $link[0].'<span class="mediaplugin mediaplugin_flv" id="'.$id.'">('.get_string('flashvideo', 'filter_mediaplugin').')</span>'.$jsoutput;
82afb587 207
208 return $output;
7e64d361 209}
210
211function mediaplugin_filter_real_callback($link, $autostart=false) {
212 $url = addslashes_js($link[1]);
213 $mimetype = mimeinfo('type', $url);
214 $autostart = $autostart ? 'true' : 'false';
215
216// embed kept for now see MDL-8674
217 return $link[0].
218'<span class="mediaplugin mediaplugin_real">
219<script type="text/javascript">
220//<![CDATA[
221document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
222 <param name="src" value="'.$url.'" />\\
223 <param name="autostart" value="'.$autostart.'" />\\
224 <param name="controls" value="imagewindow" />\\
225 <param name="console" value="video" />\\
226 <param name="loop" value="true" />\\
7c59f3be 227 <embed src="'.$url.'" width="240" height="180" loop="true" type="'.$mimetype.'" controls="imagewindow" console="video" autostart="'.$autostart.'" />\\
7e64d361 228 </object><br />\\
229 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
230 <param name="src" value="'.$url.'" />\\
231 <param name="autostart" value="'.$autostart.'" />\\
232 <param name="controls" value="ControlPanel" />\\
233 <param name="console" value="video" />\\
234 <embed src="'.$url.'" width="240" height="30" controls="ControlPanel" type="'.$mimetype.'" console="video" autostart="'.$autostart.'" />\\
235 </object>\');
236//]]>
237</script></span>';
238}
239
4d8bccf4 240/**
241 * Change links to Youtube into embedded Youtube videos
242 */
243function mediaplugin_filter_youtube_callback($link, $autostart=false) {
244
245 $site = addslashes_js($link[1]);
246 $url = addslashes_js($link[2]);
247 $info = addslashes_js($link[3]);
248
4317f92f
PS
249 return '<object title="'.$info.'"
250 class="mediaplugin mediaplugin_youtube" type="application/x-shockwave-flash"
4d8bccf4 251 data="'.$site.'youtube.com/v/'.$url.'&amp;fs=1&amp;rel=0" width="425" height="344">'.
6188deb8 252 '<param name="movie" value="'.$site.'youtube.com/v/'.$url.'&amp;fs=1&amp;rel=0" />'.
4d8bccf4 253 '<param name="FlashVars" value="playerMode=embedded" />'.
254 '<param name="wmode" value="transparent" />'.
255 '<param name="allowFullScreen" value="true" />'.
e2cf2d7f 256 '</object>';
4d8bccf4 257}
258
c159f28b 259/**
260 * Change links to images into embedded images
261 */
262function mediaplugin_filter_img_callback($link, $autostart=false) {
263 $url = addslashes_js($link[1]);
264 $info = addslashes_js($link[2]);
265
266 return '<img class="mediaplugin mediaplugin_img" alt="" title="'.$info.'" src="'.$url.'" />';
267}
268
7e64d361 269/**
270 * Embed video using window media player if available
271 */
272function mediaplugin_filter_wmp_callback($link, $autostart=false) {
273 $url = $link[1];
274 if (empty($link[3]) or empty($link[4])) {
275 $mpsize = '';
276 $size = 'width="300" height="260"';
277 $autosize = 'true';
278 } else {
279 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
280 $mpsize = $size;
281 $autosize = 'false';
282 }
283 $mimetype = mimeinfo('type', $url);
284 $autostart = $autostart ? 'true' : 'false';
285
286 return $link[0].
287'<span class="mediaplugin mediaplugin_wmp">
288<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
289 standby="Loading Microsoft(R) Windows(R) Media Player components..."
290 type="application/x-oleobject">
291 <param name="Filename" value="'.$url.'" />
292 <param name="src" value="'.$url.'" />
293 <param name="url" value="'.$url.'" />
294 <param name="ShowControls" value="true" />
295 <param name="AutoRewind" value="true" />
296 <param name="AutoStart" value="'.$autostart.'" />
297 <param name="Autosize" value="'.$autosize.'" />
298 <param name="EnableContextMenu" value="true" />
299 <param name="TransparentAtStart" value="false" />
300 <param name="AnimationAtStart" value="false" />
301 <param name="ShowGotoBar" value="false" />
302 <param name="EnableFullScreenControls" value="true" />
303<!--[if !IE]>-->
304 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
305 <param name="src" value="'.$url.'" />
306 <param name="controller" value="true" />
307 <param name="autoplay" value="'.$autostart.'" />
308 <param name="autostart" value="'.$autostart.'" />
309 <param name="resize" value="scale" />
310 </object>
311<!--<![endif]-->
312</object></span>';
313}
314
315function mediaplugin_filter_qt_callback($link, $autostart=false) {
316 $url = $link[1];
317 if (empty($link[3]) or empty($link[4])) {
1387d5eb 318 $size = 'width="440" height="315"';
7e64d361 319 } else {
320 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
321 }
322 $mimetype = mimeinfo('type', $url);
323 $autostart = $autostart ? 'true' : 'false';
324
325 return $link[0].
326'<span class="mediaplugin mediaplugin_qt">
327<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
328 codebase="http://www.apple.com/qtactivex/qtplugin.cab" '.$size.'>
329 <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
330 <param name="src" value="'.$url.'" />
d42df9da 331 <param name="controller" value="true" />
7e64d361 332 <param name="loop" value="true" />
333 <param name="autoplay" value="'.$autostart.'" />
334 <param name="autostart" value="'.$autostart.'" />
335 <param name="scale" value="aspect" />
336<!--[if !IE]>-->
337 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
338 <param name="src" value="'.$url.'" />
339 <param name="pluginurl" value="http://www.apple.com/quicktime/download/" />
340 <param name="controller" value="true" />
341 <param name="loop" value="true" />
342 <param name="autoplay" value="'.$autostart.'" />
343 <param name="autostart" value="'.$autostart.'" />
344 <param name="scale" value="aspect" />
345 </object>
346<!--<![endif]-->
347</object></span>';
348}
6de17fde 349
4317f92f 350