3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Recourse module like helper functions
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);
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);
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!
70 if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
73 if (!$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid)) {
77 $fullpath = rtrim("/$coursecontext->id/course/legacy/0".$filepath, '/');
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"))) {
84 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
87 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
95 // copy and keep the same path, name, etc.
96 $file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid);
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
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'));
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];
141 * Tries to guess correct mimetype for arbitrary URL
142 * @param string $fullurl
143 * @return string mimetype
145 function resourcelib_guess_url_mimetype($fullurl) {
147 require_once("$CFG->libdir/filelib.php");
149 if ($fullurl instanceof moodle_url) {
150 $fullurl = $fullurl->out(false);
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 (strpos($fullurl, '.php')){
160 // we do not really know what is in general php script
163 } else if (substr($fullurl, -1) === '/') {
164 // directory index (http://example.com/smaples/)
167 } else if (strpos($fullurl, '//') !== false and substr_count($fullurl, '/') == 2) {
168 // just a host name (http://example.com), solves Australian servers "audio" problem too
172 // ok, this finally looks like a real file
173 $parts = explode('?', $fullurl);
174 $url = reset($parts);
175 return mimeinfo('type', $url);
180 * Looks for the extension.
182 * @param string $fullurl
183 * @return string file extension
185 function resourcelib_get_extension($fullurl) {
187 if ($fullurl instanceof moodle_url) {
188 $fullurl = $fullurl->out(false);
192 if (preg_match("|^(.*)/[a-z]*file.php(\?file=)?(/.*)|", $fullurl, $matches)) {
193 // remove the special moodle file serving hacks so that the *file.php is ignored
194 $fullurl = $matches[1].$matches[3];
198 if (preg_match('/^[^#\?]+\.([a-z0-9]+)([#\?].*)?$/i', $fullurl, $matches)) {
199 return strtolower($matches[1]);
206 * Returns image embedding html.
207 * @param string $fullurl
208 * @param string $title
209 * @return string html
211 function resourcelib_embed_image($fullurl, $title) {
213 $code .= '<div class="resourcecontent resourceimg">';
214 $code .= "<img title=\"".strip_tags(format_string($title))."\" class=\"resourceimage\" src=\"$fullurl\" alt=\"\" />";
221 * Returns mp3 embedding html.
222 * @param string $fullurl
223 * @param string $title
224 * @param string $clicktoopen
225 * @return string html
227 function resourcelib_embed_mp3($fullurl, $title, $clicktoopen) {
229 if ($fullurl instanceof moodle_url) {
230 $fullurl = $fullurl->out(false);
233 $id = 'resource_mp3_'.time(); //we need something unique because it might be stored in text cache
235 // note: size is specified in theme, it can be made as wide as necessary, but the height can not be changed
237 $output = '<div class="resourcecontent resourcemp3">';
238 $output .= html_writer::tag('span', $clicktoopen, array('id'=>$id, 'class'=>'resourcemediaplugin resourcemediaplugin_mp3', 'title'=>$title));
239 $output .= html_writer::script(js_writer::function_call('M.util.add_audio_player', array($id, $fullurl, false)));
246 * Returns flash video embedding html.
247 * @param string $fullurl
248 * @param string $title
249 * @param string $clicktoopen
250 * @return string html
252 function resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen) {
255 if ($fullurl instanceof moodle_url) {
256 $fullurl = $fullurl->out(false);
259 $id = 'resource_flv_'.time(); //we need something unique because it might be stored in text cache
261 //note: nobody should be adding any dimensions to themes!!!
263 if (preg_match('/\?d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $fullurl, $matches)) {
264 $width = $matches[1];
265 $height = $matches[2];
272 $output = '<div class="resourcecontent resourceflv">';
273 $output .= html_writer::tag('span', $clicktoopen, array('id'=>$id, 'class'=>'resourcemediaplugin resourcemediaplugin_flv', 'title'=>$title));
274 $output .= html_writer::script(js_writer::function_call('M.util.add_video_player', array($id, $fullurl, $width, $height, $autosize)));
281 * Returns flash embedding html.
282 * @param string $fullurl
283 * @param string $title
284 * @param string $clicktoopen
285 * @return string html
287 function resourcelib_embed_flash($fullurl, $title, $clicktoopen) {
289 <div class="resourcecontent resourceswf">
290 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
291 <param name="movie" value="$fullurl" />
292 <param name="autoplay" value="true" />
293 <param name="loop" value="true" />
294 <param name="controller" value="true" />
295 <param name="scale" value="aspect" />
296 <param name="base" value="." />
298 <object type="application/x-shockwave-flash" data="$fullurl">
299 <param name="controller" value="true" />
300 <param name="autoplay" value="true" />
301 <param name="loop" value="true" />
302 <param name="scale" value="aspect" />
303 <param name="base" value="." />
317 * Returns ms media embedding html.
318 * @param string $fullurl
319 * @param string $title
320 * @param string $clicktoopen
321 * @return string html
323 function resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen) {
325 <div class="resourcecontent resourcewmv">
326 <object type="video/x-ms-wmv" data="$fullurl">
327 <param name="controller" value="true" />
328 <param name="autostart" value="true" />
329 <param name="src" value="$fullurl" />
330 <param name="scale" value="noScale" />
340 * Returns quicktime embedding html.
341 * @param string $fullurl
342 * @param string $title
343 * @param string $clicktoopen
344 * @return string html
346 function resourcelib_embed_quicktime($fullurl, $title, $clicktoopen) {
348 <div class="resourcecontent resourceqt">
349 <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">
350 <param name="src" value="$fullurl" />
351 <param name="autoplay" value="true" />
352 <param name="loop" value="true" />
353 <param name="controller" value="true" />
354 <param name="scale" value="aspect" />
356 <object type="video/quicktime" data="$fullurl">
357 <param name="controller" value="true" />
358 <param name="autoplay" value="true" />
359 <param name="loop" value="true" />
360 <param name="scale" value="aspect" />
374 * Returns mpeg embedding html.
375 * @param string $fullurl
376 * @param string $title
377 * @param string $clicktoopen
378 * @return string html
380 function resourcelib_embed_mpeg($fullurl, $title, $clicktoopen) {
382 <div class="resourcecontent resourcempeg">
383 <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">
384 <param name="fileName" value="$fullurl" />
385 <param name="autoStart" value="true" />
386 <param name="animationatStart" value="true" />
387 <param name="transparentatStart" value="true" />
388 <param name="showControls" value="true" />
389 <param name="Volume" value="-450" />
391 <object type="video/mpeg" data="$fullurl">
392 <param name="controller" value="true" />
393 <param name="autostart" value="true" />
394 <param name="src" value="$fullurl" />
408 * Returns real media embedding html.
409 * @param string $fullurl
410 * @param string $title
411 * @param string $clicktoopen
412 * @return string html
414 function resourcelib_embed_real($fullurl, $title, $clicktoopen) {
416 <div class="resourcecontent resourcerm">
417 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" data="$fullurl" width="320" height="240">
418 <param name="src" value="$fullurl" />
419 <param name="controls" value="All" />
421 <object type="audio/x-pn-realaudio-plugin" data="$fullurl" width="320" height="240">
422 <param name="src" value="$fullurl" />
423 <param name="controls" value="All" />
437 * Returns general link or pdf embedding html.
438 * @param string $fullurl
439 * @param string $title
440 * @param string $clicktoopen
441 * @return string html
443 function resourcelib_embed_pdf($fullurl, $title, $clicktoopen) {
447 <div class="resourcecontent resourcepdf">
448 <object id="resourceobject" data="$fullurl" type="application/pdf" width="800" height="600">
449 <param name="src" value="$fullurl" />
455 // the size is hardcoded in the boject obove intentionally because it is adjusted by the following function on-the-fly
456 $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
463 * Returns general link or file embedding html.
464 * @param string $fullurl
465 * @param string $title
466 * @param string $clicktoopen
467 * @return string html
469 function resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype) {
472 if ($fullurl instanceof moodle_url) {
473 $fullurl = $fullurl->out();
477 // IE can not embed stuff properly if stored on different server
478 // that is why we use iframe instead, unfortunately this tag does not validate
479 // in xhtml strict mode
480 if ($mimetype === 'text/html' and check_browser_version('MSIE', 5)) {
481 if (preg_match('(^https?://[^/]*)', $fullurl, $matches)) {
482 if (strpos($CFG->wwwroot, $matches[0]) !== 0) {
490 <div class="resourcecontent resourcegeneral">
491 <iframe id="resourceobject" src="$fullurl">
498 <div class="resourcecontent resourcegeneral">
499 <object id="resourceobject" data="$fullurl" type="$mimetype" width="800" height="600">
500 <param name="src" value="$fullurl" />
507 // the size is hardcoded in the boject obove intentionally because it is adjusted by the following function on-the-fly
508 $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);