Merge branch 'MDL-34171_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / lib / resourcelib.php
blob8988c614ff6b5072f4c5d4fb5ecca53dad00e1c6
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Recourse module like helper functions
21 * @package core
22 * @subpackage lib
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /** Try the best way */
30 define('RESOURCELIB_DISPLAY_AUTO', 0);
31 /** Display using object tag */
32 define('RESOURCELIB_DISPLAY_EMBED', 1);
33 /** Display inside frame */
34 define('RESOURCELIB_DISPLAY_FRAME', 2);
35 /** Display normal link in new window */
36 define('RESOURCELIB_DISPLAY_NEW', 3);
37 /** Force download of file instead of display */
38 define('RESOURCELIB_DISPLAY_DOWNLOAD', 4);
39 /** Open directly */
40 define('RESOURCELIB_DISPLAY_OPEN', 5);
41 /** Open in "emulated" pop-up without navigation */
42 define('RESOURCELIB_DISPLAY_POPUP', 6);
44 /** Legacy files not needed or new resource */
45 define('RESOURCELIB_LEGACYFILES_NO', 0);
46 /** Legacy files conversion marked as completed */
47 define('RESOURCELIB_LEGACYFILES_DONE', 1);
48 /** Legacy files conversion in progress*/
49 define('RESOURCELIB_LEGACYFILES_ACTIVE', 2);
52 /**
53 * Try on demand migration of file from old course files
54 * @param string $filepath old file path
55 * @param int $cmid migrated course module if
56 * @param int $courseid
57 * @param string $component
58 * @param string $filearea new file area
59 * @param int $itemid migrated file item id
60 * @return mixed, false if not found, stored_file instance if migrated to new area
62 function resourcelib_try_file_migration($filepath, $cmid, $courseid, $component, $filearea, $itemid) {
63 $fs = get_file_storage();
65 if (stripos($filepath, '/backupdata/') === 0 or stripos($filepath, '/moddata/') === 0) {
66 // do not steal protected files!
67 return false;
70 if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
71 return false;
73 if (!$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid)) {
74 return false;
77 $fullpath = rtrim("/$coursecontext->id/course/legacy/0".$filepath, '/');
78 do {
79 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
80 if ($file = $fs->get_file_by_hash(sha1("$fullpath/.")) and $file->is_directory()) {
81 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
82 break;
84 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
85 break;
87 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
88 break;
91 return false;
93 } while (false);
95 // copy and keep the same path, name, etc.
96 $file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid);
97 try {
98 return $fs->create_file_from_storedfile($file_record, $file);
99 } catch (Exception $e) {
100 // file may exist - highly unlikely, we do not want upgrades to stop here
101 return false;
106 * Returns list of available display options
107 * @param array $enabled list of options enabled in module configuration
108 * @param int $current current display options for existing instances
109 * @return array of key=>name pairs
111 function resourcelib_get_displayoptions(array $enabled, $current=null) {
112 if (is_number($current)) {
113 $enabled[] = $current;
116 $options = array(RESOURCELIB_DISPLAY_AUTO => get_string('resourcedisplayauto'),
117 RESOURCELIB_DISPLAY_EMBED => get_string('resourcedisplayembed'),
118 RESOURCELIB_DISPLAY_FRAME => get_string('resourcedisplayframe'),
119 RESOURCELIB_DISPLAY_NEW => get_string('resourcedisplaynew'),
120 RESOURCELIB_DISPLAY_DOWNLOAD => get_string('resourcedisplaydownload'),
121 RESOURCELIB_DISPLAY_OPEN => get_string('resourcedisplayopen'),
122 RESOURCELIB_DISPLAY_POPUP => get_string('resourcedisplaypopup'));
124 $result = array();
126 foreach ($options as $key=>$value) {
127 if (in_array($key, $enabled)) {
128 $result[$key] = $value;
132 if (empty($result)) {
133 // there should be always something in case admin misconfigures module
134 $result[RESOURCELIB_DISPLAY_OPEN] = $options[RESOURCELIB_DISPLAY_OPEN];
137 return $result;
141 * Tries to guess correct mimetype for arbitrary URL
142 * @param string $fullurl
143 * @return string mimetype
145 function resourcelib_guess_url_mimetype($fullurl) {
146 global $CFG;
147 require_once("$CFG->libdir/filelib.php");
149 if ($fullurl instanceof moodle_url) {
150 $fullurl = $fullurl->out(false);
153 $matches = null;
154 if (preg_match("|^(.*)/[a-z]*file.php(\?file=)?(/[^&\?#]*)|", $fullurl, $matches)) {
155 // remove the special moodle file serving hacks so that the *file.php is ignored
156 $fullurl = $matches[1].$matches[3];
159 if (preg_match("|^(.*)#.*|", $fullurl, $matches)) {
160 // ignore all anchors
161 $fullurl = $matches[1];
164 if (strpos($fullurl, '.php')){
165 // we do not really know what is in general php script
166 return 'text/html';
168 } else if (substr($fullurl, -1) === '/') {
169 // directory index (http://example.com/smaples/)
170 return 'text/html';
172 } else if (strpos($fullurl, '//') !== false and substr_count($fullurl, '/') == 2) {
173 // just a host name (http://example.com), solves Australian servers "audio" problem too
174 return 'text/html';
176 } else {
177 // ok, this finally looks like a real file
178 $parts = explode('?', $fullurl);
179 $url = reset($parts);
180 return mimeinfo('type', $url);
185 * Looks for the extension.
187 * @param string $fullurl
188 * @return string file extension
190 function resourcelib_get_extension($fullurl) {
192 if ($fullurl instanceof moodle_url) {
193 $fullurl = $fullurl->out(false);
196 $matches = null;
197 if (preg_match("|^(.*)/[a-z]*file.php(\?file=)?(/.*)|", $fullurl, $matches)) {
198 // remove the special moodle file serving hacks so that the *file.php is ignored
199 $fullurl = $matches[1].$matches[3];
202 $matches = null;
203 if (preg_match('/^[^#\?]+\.([a-z0-9]+)([#\?].*)?$/i', $fullurl, $matches)) {
204 return strtolower($matches[1]);
207 return '';
211 * Returns image embedding html.
212 * @param string $fullurl
213 * @param string $title
214 * @return string html
216 function resourcelib_embed_image($fullurl, $title) {
217 $code = '';
218 $code .= '<div class="resourcecontent resourceimg">';
219 $code .= "<img title=\"".strip_tags(format_string($title))."\" class=\"resourceimage\" src=\"$fullurl\" alt=\"\" />";
220 $code .= '</div>';
222 return $code;
226 * Returns mp3 embedding html.
227 * @param string $fullurl
228 * @param string $title
229 * @param string $clicktoopen
230 * @return string html
232 function resourcelib_embed_mp3($fullurl, $title, $clicktoopen) {
234 if ($fullurl instanceof moodle_url) {
235 $fullurl = $fullurl->out(false);
236 } else {
237 $fullurl = str_replace('&amp;', '&', $fullurl);
240 $id = 'resource_mp3_'.time(); //we need something unique because it might be stored in text cache
242 // note: size is specified in theme, it can be made as wide as necessary, but the height can not be changed
244 $output = '<div class="resourcecontent resourcemp3">';
245 $output .= html_writer::tag('span', $clicktoopen, array('id'=>$id, 'class'=>'resourcemediaplugin resourcemediaplugin_mp3', 'title'=>$title));
246 $output .= html_writer::script(js_writer::function_call('M.util.add_audio_player', array($id, $fullurl, false)));
247 $output .= '</div>';
249 return $output;
253 * Returns flash video embedding html.
254 * @param string $fullurl
255 * @param string $title
256 * @param string $clicktoopen
257 * @return string html
259 function resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen) {
260 global $CFG, $PAGE;
262 if ($fullurl instanceof moodle_url) {
263 $fullurl = $fullurl->out(false);
264 } else {
265 $fullurl = str_replace('&amp;', '&', $fullurl);
268 $id = 'resource_flv_'.time(); //we need something unique because it might be stored in text cache
270 //note: nobody should be adding any dimensions to themes!!!
272 if (preg_match('/\?d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $fullurl, $matches)) {
273 $width = $matches[1];
274 $height = $matches[2];
275 $autosize = false;
276 } else {
277 $width = 400;
278 $height = 300;
279 $autosize = true;
281 $output = '<div class="resourcecontent resourceflv">';
282 $output .= html_writer::tag('span', $clicktoopen, array('id'=>$id, 'class'=>'resourcemediaplugin resourcemediaplugin_flv', 'title'=>$title));
283 $output .= html_writer::script(js_writer::function_call('M.util.add_video_player', array($id, rawurlencode($fullurl), $width, $height, $autosize)));
284 $output .= '</div>';
286 return $output;
290 * Returns flash embedding html.
291 * @param string $fullurl
292 * @param string $title
293 * @param string $clicktoopen
294 * @return string html
296 function resourcelib_embed_flash($fullurl, $title, $clicktoopen) {
297 if (preg_match('/[#\?]d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $fullurl, $matches)) {
298 $width = $matches[1];
299 $height = $matches[2];
300 } else {
301 $width = 400;
302 $height = 300;
305 $code = <<<EOT
306 <div class="resourcecontent resourceswf">
307 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="$width" height="$height">
308 <param name="movie" value="$fullurl" />
309 <param name="autoplay" value="true" />
310 <param name="loop" value="true" />
311 <param name="controller" value="true" />
312 <param name="scale" value="aspect" />
313 <param name="base" value="." />
314 <!--[if !IE]>-->
315 <object type="application/x-shockwave-flash" data="$fullurl" width="$width" height="$height">
316 <param name="controller" value="true" />
317 <param name="autoplay" value="true" />
318 <param name="loop" value="true" />
319 <param name="scale" value="aspect" />
320 <param name="base" value="." />
321 <!--<![endif]-->
322 $clicktoopen
323 <!--[if !IE]>-->
324 </object>
325 <!--<![endif]-->
326 </object>
327 </div>
328 EOT;
330 return $code;
334 * Returns ms media embedding html.
335 * @param string $fullurl
336 * @param string $title
337 * @param string $clicktoopen
338 * @return string html
340 function resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen) {
341 $code = <<<EOT
342 <div class="resourcecontent resourcewmv">
343 <object type="video/x-ms-wmv" data="$fullurl">
344 <param name="controller" value="true" />
345 <param name="autostart" value="true" />
346 <param name="src" value="$fullurl" />
347 <param name="scale" value="noScale" />
348 $clicktoopen
349 </object>
350 </div>
351 EOT;
353 return $code;
357 * Returns quicktime embedding html.
358 * @param string $fullurl
359 * @param string $title
360 * @param string $clicktoopen
361 * @return string html
363 function resourcelib_embed_quicktime($fullurl, $title, $clicktoopen) {
364 $code = <<<EOT
365 <div class="resourcecontent resourceqt">
366 <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">
367 <param name="src" value="$fullurl" />
368 <param name="autoplay" value="true" />
369 <param name="loop" value="true" />
370 <param name="controller" value="true" />
371 <param name="scale" value="aspect" />
372 <!--[if !IE]>-->
373 <object type="video/quicktime" data="$fullurl">
374 <param name="controller" value="true" />
375 <param name="autoplay" value="true" />
376 <param name="loop" value="true" />
377 <param name="scale" value="aspect" />
378 <!--<![endif]-->
379 $clicktoopen
380 <!--[if !IE]>-->
381 </object>
382 <!--<![endif]-->
383 </object>
384 </div>
385 EOT;
387 return $code;
391 * Returns mpeg embedding html.
392 * @param string $fullurl
393 * @param string $title
394 * @param string $clicktoopen
395 * @return string html
397 function resourcelib_embed_mpeg($fullurl, $title, $clicktoopen) {
398 $code = <<<EOT
399 <div class="resourcecontent resourcempeg">
400 <object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsm p2inf.cab#Version=5,1,52,701" type="application/x-oleobject">
401 <param name="fileName" value="$fullurl" />
402 <param name="autoStart" value="true" />
403 <param name="animationatStart" value="true" />
404 <param name="transparentatStart" value="true" />
405 <param name="showControls" value="true" />
406 <param name="Volume" value="-450" />
407 <!--[if !IE]>-->
408 <object type="video/mpeg" data="$fullurl">
409 <param name="controller" value="true" />
410 <param name="autostart" value="true" />
411 <param name="src" value="$fullurl" />
412 <!--<![endif]-->
413 $clicktoopen
414 <!--[if !IE]>-->
415 </object>
416 <!--<![endif]-->
417 </object>
418 </div>
419 EOT;
421 return $code;
425 * Returns real media embedding html.
426 * @param string $fullurl
427 * @param string $title
428 * @param string $clicktoopen
429 * @return string html
431 function resourcelib_embed_real($fullurl, $title, $clicktoopen) {
432 $code = <<<EOT
433 <div class="resourcecontent resourcerm">
434 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" data="$fullurl" width="320" height="240">
435 <param name="src" value="$fullurl" />
436 <param name="controls" value="All" />
437 <!--[if !IE]>-->
438 <object type="audio/x-pn-realaudio-plugin" data="$fullurl" width="320" height="240">
439 <param name="src" value="$fullurl" />
440 <param name="controls" value="All" />
441 <!--<![endif]-->
442 $clicktoopen
443 <!--[if !IE]>-->
444 </object>
445 <!--<![endif]-->
446 </object>
447 </div>
448 EOT;
450 return $code;
454 * Returns general link or pdf embedding html.
455 * @param string $fullurl
456 * @param string $title
457 * @param string $clicktoopen
458 * @return string html
460 function resourcelib_embed_pdf($fullurl, $title, $clicktoopen) {
461 global $CFG, $PAGE;
463 $code = <<<EOT
464 <div class="resourcecontent resourcepdf">
465 <object id="resourceobject" data="$fullurl" type="application/pdf" width="800" height="600">
466 <param name="src" value="$fullurl" />
467 $clicktoopen
468 </object>
469 </div>
470 EOT;
472 // the size is hardcoded in the boject obove intentionally because it is adjusted by the following function on-the-fly
473 $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
475 return $code;
480 * Returns general link or file embedding html.
481 * @param string $fullurl
482 * @param string $title
483 * @param string $clicktoopen
484 * @param string $mimetype
485 * @return string html
487 function resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype) {
488 global $CFG, $PAGE;
490 if ($fullurl instanceof moodle_url) {
491 $fullurl = $fullurl->out();
494 $iframe = false;
496 $param = '<param name="src" value="'.$fullurl.'" />';
498 // IE can not embed stuff properly, that is why we use iframe instead.
499 // Unfortunately this tag does not validate in xhtml strict mode,
500 // but in any case it is undeprecated in HTML 5 - we will use it everywhere soon!
501 if ($mimetype === 'text/html' and check_browser_version('MSIE', 5)) {
502 $iframe = true;
505 if ($iframe) {
506 $code = <<<EOT
507 <div class="resourcecontent resourcegeneral">
508 <iframe id="resourceobject" src="$fullurl">
509 $clicktoopen
510 </iframe>
511 </div>
512 EOT;
513 } else {
514 $code = <<<EOT
515 <div class="resourcecontent resourcegeneral">
516 <object id="resourceobject" data="$fullurl" type="$mimetype" width="800" height="600">
517 $param
518 $clicktoopen
519 </object>
520 </div>
521 EOT;
524 // the size is hardcoded in the boject obove intentionally because it is adjusted by the following function on-the-fly
525 $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
527 return $code;