improved legacy file migration - now processing default directory indexes too
[moodle.git] / lib / resourcelib.php
CommitLineData
aa54ed7b 1<?php
2
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.
14//
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/>.
17
18/**
19 * Recourse module like helper functions
20 *
78bfb562
PS
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
aa54ed7b 25 */
26
78bfb562
PS
27defined('MOODLE_INTERNAL') || die();
28
aa54ed7b 29/** Try the best way */
30define('RESOURCELIB_DISPLAY_AUTO', 0);
31/** Display using object tag */
32define('RESOURCELIB_DISPLAY_EMBED', 1);
33/** Display inside frame */
34define('RESOURCELIB_DISPLAY_FRAME', 2);
35/** Display normal link in new window */
36define('RESOURCELIB_DISPLAY_NEW', 3);
37/** Force download of file instead of display */
38define('RESOURCELIB_DISPLAY_DOWNLOAD', 4);
39/** Open directly */
40define('RESOURCELIB_DISPLAY_OPEN', 5);
41/** Open in "emulated" pop-up without navigation */
42define('RESOURCELIB_DISPLAY_POPUP', 6);
43
44/** Legacy files not needed or new resource */
45define('RESOURCELIB_LEGACYFILES_NO', 0);
46/** Legacy files conversion marked as completed */
42439c05 47define('RESOURCELIB_LEGACYFILES_DONE', 1);
aa54ed7b 48/** Legacy files conversion in progress*/
49define('RESOURCELIB_LEGACYFILES_ACTIVE', 2);
50
51
9b56a34f
PS
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
64f93798 57 * @param string $component
9b56a34f
PS
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
61 */
64f93798 62function resourcelib_try_file_migration($filepath, $cmid, $courseid, $component, $filearea, $itemid) {
9b56a34f
PS
63 $fs = get_file_storage();
64
65 if (stripos($filepath, '/backupdata/') === 0 or stripos($filepath, '/moddata/') === 0) {
66 // do not steal protected files!
67 return false;
68 }
69
70 if (!$context = get_context_instance(CONTEXT_MODULE, $cmid)) {
71 return false;
72 }
73 if (!$coursecontext = get_context_instance(CONTEXT_COURSE, $courseid)) {
74 return false;
75 }
76
aa0dd423
PS
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;
83 }
84 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
85 break;
86 }
87 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
88 break;
89 }
90 }
91 return false;
92 }
93 } while (false);
9b56a34f
PS
94
95 // copy and keep the same path, name, etc.
64f93798 96 $file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid);
9b56a34f
PS
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;
102 }
103}
104
aa54ed7b 105/**
106 * Returns list of available display options
107 * @param array $enabled list of options enabled in module configuration
ed149942 108 * @param int $current current display options for existing instances
aa54ed7b 109 * @return array of key=>name pairs
110 */
111function resourcelib_get_displayoptions(array $enabled, $current=null) {
112 if (is_number($current)) {
113 $enabled[] = $current;
114 }
115
9b56a34f
PS
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'));
aa54ed7b 123
124 $result = array();
125
126 foreach ($options as $key=>$value) {
127 if (in_array($key, $enabled)) {
128 $result[$key] = $value;
129 }
130 }
131
132 if (empty($result)) {
133 // there should be always something in case admin misconfigures module
134 $result[RESOURCELIB_DISPLAY_OPEN] = $options[RESOURCELIB_DISPLAY_OPEN];
135 }
136
137 return $result;
138}
139
140/**
141 * Tries to guess correct mimetype for arbitrary URL
142 * @param string $fullurl
143 * @return string mimetype
144 */
145function resourcelib_guess_url_mimetype($fullurl) {
146 global $CFG;
147 require_once("$CFG->libdir/filelib.php");
148
149 if (preg_match("|^(.*)/[a-z]*file.php(\?file=)?(/[^&\?]*)|", $fullurl, $matches)) {
150 // remove the special moodle file serving hacks so that the *file.php is ignored
151 $fullurl = $matches[1].$matches[3];
152 }
153
154 if (strpos($fullurl, '.php')){
155 // we do not really know what is in general php script
156 return 'text/html';
157
158 } else if (substr($fullurl, -1) === '/') {
159 // directory index (http://example.com/smaples/)
160 return 'text/html';
161
162 } else if (strpos($fullurl, '//') !== false and substr_count($fullurl, '/') == 2) {
163 // just a host name (http://example.com), solves Australian servers "audio" problem too
164 return 'text/html';
165
166 } else {
167 // ok, this finally looks like a real file
168 return mimeinfo('type', $fullurl);
169 }
170}
171
172/**
173 * Returns image embedding html.
174 * @param string $fullurl
175 * @param string $title
176 * @return string html
177 */
178function resourcelib_embed_image($fullurl, $title) {
179 $code = '';
180 $code .= '<div class="resourcecontent resourceimg">';
181 $code .= "<img title=\"".strip_tags(format_string($title))."\" class=\"resourceimage\" src=\"$fullurl\" alt=\"\" />";
182 $code .= '</div>';
183
184 return $code;
185}
186
187/**
188 * Returns mp3 embedding html.
189 * @param string $fullurl
190 * @param string $title
191 * @param string $clicktoopen
192 * @return string html
193 */
194function resourcelib_embed_mp3($fullurl, $title, $clicktoopen) {
088ccb43
PS
195 global $CFG, $OUTPUT, $PAGE;
196
197 $c = $OUTPUT->resource_mp3player_colors(); // You can set this up in your theme/xxx/config.php
3a42ad12
RW
198 $colors = explode('&', $c);
199 $playercolors = array();
200 foreach ($colors as $color) {
201 $color = explode('=', $color);
202 $playercolors[$color[0]] = $color[1];
203 }
aa54ed7b 204
aa54ed7b 205 $id = 'filter_mp3_'.time(); //we need something unique because it might be stored in text cache
206
3a42ad12
RW
207 $playerpath = $CFG->wwwroot .'/filter/mediaplugin/mp3player.swf';
208 $audioplayerpath = $CFG->wwwroot .'/filter/mediaplugin/flowplayer.audio.swf';
aa54ed7b 209
aa54ed7b 210 $code = <<<OET
211<div class="resourcecontent resourcemp3">
3a42ad12 212 <span class="resourcemediaplugin resourcemediaplugin_mp3" id="$id"></span>
ac9889d4 213 <noscript><div>
3a42ad12
RW
214 <object width="251" height="25" id="nonjsmp3plugin" name="undefined" data="$playerpath" type="application/x-shockwave-flash">
215 <param name="movie" value="$playerpath" />
216 <param name="allowfullscreen" value="false" />
217 <param name="allowscriptaccess" value="always" />
218 <param name="flashvars" value='config={"plugins": {"controls": {
219 "fullscreen": false,
220 "height": 25,
221 "autoHide": false
222 }
223 },
224 "clip":{"url":"$fullurl",
225 "autoPlay": false},
226 "content":{"url":"$playerpath"}}}' />
aa54ed7b 227 </object>
ac9889d4 228 </div></noscript>
aa54ed7b 229</div>
230OET;
3a42ad12
RW
231 $PAGE->requires->js('/lib/flowplayer.js');
232 $code .= $PAGE->requires->js_function_call('M.util.init_mp3flowplayer', array('id'=>$id, 'playerpath'=>$playerpath, 'audioplayerpath'=>$audioplayerpath, 'fileurl'=>$fullurl, 'color'=>$playercolors));
aa54ed7b 233
aa54ed7b 234 return $code;
235}
236
237/**
238 * Returns flash video embedding html.
239 * @param string $fullurl
240 * @param string $title
241 * @param string $clicktoopen
242 * @return string html
243 */
244function resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen) {
245 global $CFG, $PAGE;
246
247 $id = 'filter_flv_'.time(); //we need something unique because it might be stored in text cache
248
3a42ad12 249 $playerpath = $CFG->wwwroot .'/filter/mediaplugin/flvplayer.swf';
aa54ed7b 250
251 $code = <<<EOT
252<div class="resourcecontent resourceflv">
253 <span class="mediaplugin mediaplugin_flv" id="$id"></span>
ac9889d4 254 <noscript><div>
3a42ad12
RW
255 <object width="800" height="600" id="undefined" name="undefined" data="$playerpath" type="application/x-shockwave-flash">
256 <param name="movie" value="$playerpath" />
257 <param name="allowfullscreen" value="true" />
258 <param name="allowscriptaccess" value="always" />
259 <param name="flashvars" value='config={"clip":{"url":"$fullurl",
260 "autoPlay": false},
261 "content":{"url":"$playerpath"}}}' />
aa54ed7b 262 </object>
ac9889d4 263 </div></noscript>
aa54ed7b 264</div>
265EOT;
266
3a42ad12
RW
267 $PAGE->requires->js('/lib/flowplayer.js');
268 $code .= $PAGE->requires->js_function_call('M.util.init_flvflowplayer', array('id'=>$id, 'playerpath'=>$playerpath, 'fileurl'=>$fullurl));
aa54ed7b 269 return $code;
270}
271
272/**
273 * Returns flash embedding html.
274 * @param string $fullurl
275 * @param string $title
276 * @param string $clicktoopen
277 * @return string html
278 */
279function resourcelib_embed_flash($fullurl, $title, $clicktoopen) {
280 $code = <<<EOT
281<div class="resourcecontent resourceswf">
282 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
283 <param name="movie" value="$fullurl" />
284 <param name="autoplay" value="true" />
285 <param name="loop" value="true" />
286 <param name="controller" value="true" />
287 <param name="scale" value="aspect" />
288 <param name="base" value="." />
289<!--[if !IE]>-->
290 <object type="application/x-shockwave-flash" data="$fullurl">
291 <param name="controller" value="true" />
292 <param name="autoplay" value="true" />
293 <param name="loop" value="true" />
294 <param name="scale" value="aspect" />
295 <param name="base" value="." />
296<!--<![endif]-->
297$clicktoopen
298<!--[if !IE]>-->
299 </object>
300<!--<![endif]-->
301 </object>
302</div>
303EOT;
304
305 return $code;
306}
307
308/**
309 * Returns ms media embedding html.
310 * @param string $fullurl
311 * @param string $title
312 * @param string $clicktoopen
313 * @return string html
314 */
315function resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen) {
316 $code = <<<EOT
317<div class="resourcecontent resourcewmv">
318 <object type="video/x-ms-wmv" data="$fullurl">
319 <param name="controller" value="true" />
320 <param name="autostart" value="true" />
321 <param name="src" value="$fullurl" />
322 <param name="scale" value="noScale" />
323 $clicktoopen
324 </object>
325</div>
326EOT;
327
328 return $code;
329}
330
331/**
332 * Returns quicktime embedding html.
333 * @param string $fullurl
334 * @param string $title
335 * @param string $clicktoopen
336 * @return string html
337 */
338function resourcelib_embed_quicktime($fullurl, $title, $clicktoopen) {
339 $code = <<<EOT
340<div class="resourcecontent resourceqt">
341 <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">
342 <param name="src" value="$fullurl" />
343 <param name="autoplay" value="true" />
344 <param name="loop" value="true" />
345 <param name="controller" value="true" />
346 <param name="scale" value="aspect" />
347<!--[if !IE]>-->
348 <object type="video/quicktime" data="$fullurl">
349 <param name="controller" value="true" />
350 <param name="autoplay" value="true" />
351 <param name="loop" value="true" />
352 <param name="scale" value="aspect" />
353<!--<![endif]-->
354$clicktoopen
355<!--[if !IE]>-->
356 </object>
357<!--<![endif]-->
358 </object>
359</div>
360EOT;
361
362 return $code;
363}
364
365/**
366 * Returns mpeg embedding html.
367 * @param string $fullurl
368 * @param string $title
369 * @param string $clicktoopen
370 * @return string html
371 */
372function resourcelib_embed_mpeg($fullurl, $title, $clicktoopen) {
373 $code = <<<EOT
374<div class="resourcecontent resourcempeg">
375 <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">
376 <param name="fileName" value="$fullurl" />
377 <param name="autoStart" value="true" />
378 <param name="animationatStart" value="true" />
379 <param name="transparentatStart" value="true" />
380 <param name="showControls" value="true" />
381 <param name="Volume" value="-450" />
382<!--[if !IE]>-->
383 <object type="video/mpeg" data="$fullurl">
384 <param name="controller" value="true" />
385 <param name="autostart" value="true" />
386 <param name="src" value="$fullurl" />
387<!--<![endif]-->
388$clicktoopen
389<!--[if !IE]>-->
390 </object>
391<!--<![endif]-->
392 </object>
393</div>
394EOT;
395
396 return $code;
397}
398
399/**
400 * Returns real media embedding html.
401 * @param string $fullurl
402 * @param string $title
403 * @param string $clicktoopen
404 * @return string html
405 */
406function resourcelib_embed_real($fullurl, $title, $clicktoopen) {
407 $code = <<<EOT
408<div class="resourcecontent resourcerm">
409 <object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="320" height="240">
410 <param name="src" value="$fullurl" />
411 <param name="controls" value="All" />
412<!--[if !IE]>-->
413 <object type="audio/x-pn-realaudio-plugin" data="$fullurl" width="320" height="240">
414 <param name="controls" value="All" />
415<!--<![endif]-->
416$clicktoopen
417<!--[if !IE]>-->
418 </object>
419<!--<![endif]-->
420 </object>
421</div>
422EOT;
423
424 return $code;
425}
426
b8037d73
MD
427/**
428 * Returns general link or pdf embedding html.
429 * @param string $fullurl
430 * @param string $title
431 * @param string $clicktoopen
432 * @return string html
433 */
434function resourcelib_embed_pdf($fullurl, $title, $clicktoopen) {
435 global $CFG, $PAGE;
436
437 $code = <<<EOT
438<div class="resourcecontent resourcepdf">
439 <object id="resourceobject" data="$fullurl" type="application/pdf">
440 <param name="src" value="$fullurl" />
441 $clicktoopen
442 </object>
443</div>
444EOT;
445 //$PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
446
447 return $code;
448}
449
450
aa54ed7b 451/**
452 * Returns general link or file embedding html.
453 * @param string $fullurl
454 * @param string $title
455 * @param string $clicktoopen
456 * @return string html
457 */
458function resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype) {
459 global $CFG, $PAGE;
460
461 $iframe = false;
462 // IE can not embed stuff properly if stored on different server
463 // that is why we use iframe instead, unfortunately this tag does not validate
464 // in xhtml strict mode
465 if ($mimetype === 'text/html' and check_browser_version('MSIE', 5)) {
466 if (preg_match('(^https?://[^/]*)', $fullurl, $matches)) {
467 if (strpos($CFG->wwwroot, $matches[0]) !== 0) {
468 $iframe = true;
469 }
470 }
471 }
472
473 if ($iframe) {
474 $code = <<<EOT
475<div class="resourcecontent resourcegeneral">
476 <iframe id="resourceobject" src="$fullurl">
477 $clicktoopen
478 </iframe>
479</div>
480EOT;
481 } else {
482 $code = <<<EOT
483<div class="resourcecontent resourcegeneral">
484 <object id="resourceobject" data="$fullurl" type="$mimetype">
485 <param name="src" value="$fullurl" />
486 $clicktoopen
487 </object>
488</div>
489EOT;
490 }
491
05967638
PS
492 $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
493
aa54ed7b 494 return $code;
495}