MDL-10680:
[moodle-linuxchix.git] / filter / mediaplugin / filter.php
blobe5de22f6ea06d9a7323730e354f1923212c41a33
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');
20 function mediaplugin_filter($courseid, $text) {
21 global $CFG;
23 include 'defaultsettings.php';
25 // We're using the UFO technique for flash to attain XHTML Strict 1.0
26 // See: http://www.bobbyvandersluis.com/ufo/
28 if ($CFG->filter_mediaplugin_enable_mp3) {
29 $search = '/<a.*?href="([^<]+\.mp3)"[^>]*>.*?<\/a>/is';
30 $text = preg_replace_callback($search, 'mediaplugin_filter_mp3_callback', $text);
33 if ($CFG->filter_mediaplugin_enable_swf) {
34 $search = '/<a.*?href="([^<]+\.swf)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
35 $text = preg_replace_callback($search, 'mediaplugin_filter_swf_callback', $text);
38 if ($CFG->filter_mediaplugin_enable_flv) {
39 $search = '/<a.*?href="([^<]+\.flv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
40 $text = preg_replace_callback($search, 'mediaplugin_filter_flv_callback', $text);
43 if ($CFG->filter_mediaplugin_enable_mov) {
44 $search = '/<a.*?href="([^<]+\.mov)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
45 $text = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $text);
48 if ($CFG->filter_mediaplugin_enable_wmv) {
49 $search = '/<a.*?href="([^<]+\.wmv)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
50 $text = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $text);
53 if ($CFG->filter_mediaplugin_enable_mpg) {
54 $search = '/<a.*?href="([^<]+\.mpe?g)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
55 $text = preg_replace_callback($search, 'mediaplugin_filter_qt_callback', $text);
58 if ($CFG->filter_mediaplugin_enable_avi) {
59 $search = '/<a.*?href="([^<]+\.avi)(\?d=([\d]{1,3}%?)x([\d]{1,3}%?))?"[^>]*>.*?<\/a>/is';
60 $text = preg_replace_callback($search, 'mediaplugin_filter_wmp_callback', $text);
63 if ($CFG->filter_mediaplugin_enable_ram) {
64 $search = '/<a.*?href="([^<]+\.ram)"[^>]*>.*?<\/a>/is';
65 $text = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $text);
68 if ($CFG->filter_mediaplugin_enable_rpm) {
69 $search = '/<a.*?href="([^<]+\.rpm)"[^>]*>.*?<\/a>/is';
70 $text = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $text);
73 if ($CFG->filter_mediaplugin_enable_rm) {
74 $search = '/<a.*?href="([^<]+\.rm)"[^>]*>.*?<\/a>/is';
75 $text = preg_replace_callback($search, 'mediaplugin_filter_real_callback', $text);
78 return $text;
81 ///===========================
82 /// callback filter functions
84 function mediaplugin_filter_mp3_callback($link) {
85 global $CFG, $THEME;
87 if (!empty($THEME->filter_mediaplugin_colors)) {
88 $c = $THEME->filter_mediaplugin_colors; // You can set this up in your theme/xxx/config.php
89 } else {
90 $c = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&'.
91 'iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&'.
92 'waitForPlay=yes';
94 $c = htmlentities($c);
96 static $count = 0;
97 $count++;
98 $id = 'filter_mp3_'.time().$count; //we need something unique because it might be stored in text cache
100 $url = addslashes_js($link[1]);
102 return $link[0].
103 '<span class="mediaplugin mediaplugin_mp3" id="'.$id.'">('.get_string('mp3audio', 'mediaplugin').')</span>
104 <script type="text/javascript">
105 //<![CDATA[
106 var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/mp3player.swf?src='.$url.'",
107 width:"90", height:"15", majorversion:"6", build:"40", flashvars:"'.$c.'", quality: "high" };
108 UFO.create(FO, "'.$id.'");
109 //]]>
110 </script>';
113 function mediaplugin_filter_swf_callback($link) {
114 static $count = 0;
115 $count++;
116 $id = 'filter_swf_'.time().$count; //we need something unique because it might be stored in text cache
118 $width = empty($link[3]) ? '400' : $link[3];
119 $height = empty($link[4]) ? '300' : $link[4];
120 $url = addslashes_js($link[1]);
122 return $link[0].
123 '<span class="mediaplugin mediaplugin_swf" id="'.$id.'">('.get_string('flashanimation', 'mediaplugin').')</span>
124 <script type="text/javascript">
125 //<![CDATA[
126 var FO = { movie:"'.$url.'", width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
127 allowscriptaccess:"never", quality: "high" };
128 UFO.create(FO, "'.$id.'");
129 //]]>
130 </script>';
133 function mediaplugin_filter_flv_callback($link) {
134 global $CFG;
136 static $count = 0;
137 $count++;
138 $id = 'filter_flv_'.time().$count; //we need something unique because it might be stored in text cache
140 $width = empty($link[3]) ? '480' : $link[3];
141 $height = empty($link[4]) ? '360' : $link[4];
142 $url = addslashes_js($link[1]);
144 return $link[0].
145 '<span class="mediaplugin mediaplugin_flv" id="'.$id.'">('.get_string('flashvideo', 'mediaplugin').')</span>
146 <script type="text/javascript">
147 //<![CDATA[
148 var FO = { movie:"'.$CFG->wwwroot.'/filter/mediaplugin/flvplayer.swf?file='.$url.'",
149 width:"'.$width.'", height:"'.$height.'", majorversion:"6", build:"40",
150 allowscriptaccess:"never", quality: "high" };
151 UFO.create(FO, "'.$id.'");
152 //]]>
153 </script>';
156 function mediaplugin_filter_real_callback($link, $autostart=false) {
157 $url = addslashes_js($link[1]);
158 $mimetype = mimeinfo('type', $url);
159 $autostart = $autostart ? 'true' : 'false';
161 // embed kept for now see MDL-8674
162 return $link[0].
163 '<span class="mediaplugin mediaplugin_real">
164 <script type="text/javascript">
165 //<![CDATA[
166 document.write(\'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="180">\\
167 <param name="src" value="'.$url.'" />\\
168 <param name="autostart" value="'.$autostart.'" />\\
169 <param name="controls" value="imagewindow" />\\
170 <param name="console" value="video" />\\
171 <param name="loop" value="true" />\\
172 <embed src="'.$url.'" width=240" height="180" loop="true" type="'.$mimetype.'" controls="imagewindow" console="video" autostart="'.$autostart.'" />\\
173 </object><br />\\
174 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="240" height="30">\\
175 <param name="src" value="'.$url.'" />\\
176 <param name="autostart" value="'.$autostart.'" />\\
177 <param name="controls" value="ControlPanel" />\\
178 <param name="console" value="video" />\\
179 <embed src="'.$url.'" width="240" height="30" controls="ControlPanel" type="'.$mimetype.'" console="video" autostart="'.$autostart.'" />\\
180 </object>\');
181 //]]>
182 </script></span>';
186 * Embed video using window media player if available
188 function mediaplugin_filter_wmp_callback($link, $autostart=false) {
189 $url = $link[1];
190 if (empty($link[3]) or empty($link[4])) {
191 $mpsize = '';
192 $size = 'width="300" height="260"';
193 $autosize = 'true';
194 } else {
195 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
196 $mpsize = $size;
197 $autosize = 'false';
199 $mimetype = mimeinfo('type', $url);
200 $autostart = $autostart ? 'true' : 'false';
202 return $link[0].
203 '<span class="mediaplugin mediaplugin_wmp">
204 <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '.$mpsize.'
205 standby="Loading Microsoft(R) Windows(R) Media Player components..."
206 type="application/x-oleobject">
207 <param name="Filename" value="'.$url.'" />
208 <param name="src" value="'.$url.'" />
209 <param name="url" value="'.$url.'" />
210 <param name="ShowControls" value="true" />
211 <param name="AutoRewind" value="true" />
212 <param name="AutoStart" value="'.$autostart.'" />
213 <param name="Autosize" value="'.$autosize.'" />
214 <param name="EnableContextMenu" value="true" />
215 <param name="TransparentAtStart" value="false" />
216 <param name="AnimationAtStart" value="false" />
217 <param name="ShowGotoBar" value="false" />
218 <param name="EnableFullScreenControls" value="true" />
219 <!--[if !IE]>-->
220 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
221 <param name="src" value="'.$url.'" />
222 <param name="controller" value="true" />
223 <param name="autoplay" value="'.$autostart.'" />
224 <param name="autostart" value="'.$autostart.'" />
225 <param name="resize" value="scale" />
226 </object>
227 <!--<![endif]-->
228 </object></span>';
231 function mediaplugin_filter_qt_callback($link, $autostart=false) {
232 $url = $link[1];
233 if (empty($link[3]) or empty($link[4])) {
234 $size = 'width="280" height="210"';
235 } else {
236 $size = 'width="'.$link[3].'" height="'.$link[4].'"';
238 $mimetype = mimeinfo('type', $url);
239 $autostart = $autostart ? 'true' : 'false';
241 return $link[0].
242 '<span class="mediaplugin mediaplugin_qt">
243 <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
244 codebase="http://www.apple.com/qtactivex/qtplugin.cab" '.$size.'>
245 <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
246 <param name="src" value="'.$url.'" />
247 <param name="coltroller" value="true" />
248 <param name="loop" value="true" />
249 <param name="autoplay" value="'.$autostart.'" />
250 <param name="autostart" value="'.$autostart.'" />
251 <param name="scale" value="aspect" />
252 <!--[if !IE]>-->
253 <object data="'.$url.'" type="'.$mimetype.'" '.$size.'>
254 <param name="src" value="'.$url.'" />
255 <param name="pluginurl" value="http://www.apple.com/quicktime/download/" />
256 <param name="controller" value="true" />
257 <param name="loop" value="true" />
258 <param name="autoplay" value="'.$autostart.'" />
259 <param name="autostart" value="'.$autostart.'" />
260 <param name="scale" value="aspect" />
261 </object>
262 <!--<![endif]-->
263 </object></span>';