Updated the 19 build version to 20100915
[moodle.git] / files / index.php
blobdff961d22d4e3a9fadaf18f77539d51766ecbb94
1 <?php // $Id$
3 // Manage all uploaded files in a course file area
5 // All the Moodle-specific stuff is in this top section
6 // Configuration and access control occurs here.
7 // Must define: USER, basedir, baseweb, html_header and html_footer
8 // USER is a persistent variable using sessions
10 require('../config.php');
11 require($CFG->libdir.'/filelib.php');
12 require($CFG->libdir.'/adminlib.php');
14 $id = required_param('id', PARAM_INT);
15 $file = optional_param('file', '', PARAM_PATH);
16 $wdir = optional_param('wdir', '', PARAM_PATH);
17 $action = optional_param('action', '', PARAM_ACTION);
18 $name = optional_param('name', '', PARAM_FILE);
19 $oldname = optional_param('oldname', '', PARAM_FILE);
20 $choose = optional_param('choose', '', PARAM_FILE); //in fact it is always 'formname.inputname'
21 $userfile= optional_param('userfile','',PARAM_FILE);
22 $save = optional_param('save', 0, PARAM_BOOL);
23 $text = optional_param('text', '', PARAM_RAW);
24 $confirm = optional_param('confirm', 0, PARAM_BOOL);
26 if ($choose) {
27 if (count(explode('.', $choose)) > 2) {
28 error('Incorrect format for choose parameter');
33 if (! $course = get_record("course", "id", $id) ) {
34 error("That's an invalid course id");
37 require_login($course);
39 require_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $course->id));
41 function html_footer() {
42 global $COURSE, $choose;
44 echo '</td></tr></table>';
46 print_footer($COURSE);
49 function html_header($course, $wdir, $formfield=""){
50 global $CFG, $ME, $choose;
52 $navlinks = array();
53 // $navlinks[] = array('name' => $course->shortname, 'link' => "../course/view.php?id=$course->id", 'type' => 'misc');
55 if ($course->id == SITEID) {
56 $strfiles = get_string("sitefiles");
57 } else {
58 $strfiles = get_string("files");
61 if ($wdir == "/") {
62 $navlinks[] = array('name' => $strfiles, 'link' => null, 'type' => 'misc');
63 } else {
64 $dirs = explode("/", $wdir);
65 $numdirs = count($dirs);
66 $link = "";
67 $navlinks[] = array('name' => $strfiles,
68 'link' => $ME."?id=$course->id&amp;wdir=/&amp;choose=$choose",
69 'type' => 'misc');
71 for ($i=1; $i<$numdirs-1; $i++) {
72 $link .= "/".urlencode($dirs[$i]);
73 $navlinks[] = array('name' => $dirs[$i],
74 'link' => $ME."?id=$course->id&amp;wdir=$link&amp;choose=$choose",
75 'type' => 'misc');
77 $navlinks[] = array('name' => $dirs[$numdirs-1], 'link' => null, 'type' => 'misc');
80 $navigation = build_navigation($navlinks);
82 if ($choose) {
83 print_header();
85 $chooseparts = explode('.', $choose);
86 if (count($chooseparts)==2){
88 <script type="text/javascript">
89 //<![CDATA[
90 function set_value(txt) {
91 opener.document.forms['<?php echo $chooseparts[0]."'].".$chooseparts[1] ?>.value = txt;
92 window.close();
94 //]]>
95 </script>
97 <?php
98 } elseif (count($chooseparts)==1){
100 <script type="text/javascript">
101 //<![CDATA[
102 function set_value(txt) {
103 opener.document.getElementById('<?php echo $chooseparts[0] ?>').value = txt;
104 window.close();
106 //]]>
107 </script>
109 <?php
112 $fullnav = '';
113 $i = 0;
114 foreach ($navlinks as $navlink) {
115 // If this is the last link do not link
116 if ($i == count($navlinks) - 1) {
117 $fullnav .= $navlink['name'];
118 } else {
119 $fullnav .= '<a href="'.$navlink['link'].'">'.$navlink['name'].'</a>';
121 $fullnav .= ' -> ';
122 $i++;
124 $fullnav = substr($fullnav, 0, -4);
125 $fullnav = str_replace('->', '&raquo;', format_string($course->shortname) . " -> " . $fullnav);
126 echo '<div id="nav-bar">'.$fullnav.'</div>';
128 if ($course->id == SITEID and $wdir != "/backupdata") {
129 print_heading(get_string("publicsitefileswarning3"), "center", 2);
132 } else {
134 if ($course->id == SITEID) {
136 if ($wdir == "/backupdata") {
137 admin_externalpage_setup('frontpagerestore');
138 admin_externalpage_print_header();
139 } else {
140 admin_externalpage_setup('sitefiles');
141 admin_externalpage_print_header();
143 print_heading(get_string("publicsitefileswarning3"), "center", 2);
147 } else {
148 print_header("$course->shortname: $strfiles", $course->fullname, $navigation, $formfield);
153 echo "<table border=\"0\" style=\"margin-left:auto;margin-right:auto\" cellspacing=\"3\" cellpadding=\"3\" width=\"640\">";
154 echo "<tr>";
155 echo "<td colspan=\"2\">";
160 if (! $basedir = make_upload_directory("$course->id")) {
161 error("The site administrator needs to fix the file permissions");
164 // make sure site files contain the backupdata or else people put backups into public area!!
165 if ($course->id == SITEID) {
166 if (!file_exists("$CFG->dataroot/$course->id/backupdata")) {
167 make_upload_directory("$course->id/backupdata");
171 $baseweb = $CFG->wwwroot;
173 // End of configuration and access control
176 if ($wdir == '') {
177 $wdir = "/";
180 if ($wdir{0} != '/') { //make sure $wdir starts with slash
181 $wdir = "/".$wdir;
184 if ($wdir == "/backupdata") {
185 if (! make_upload_directory("$course->id/backupdata")) { // Backup folder
186 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
190 if (!is_dir($basedir.$wdir)) {
191 html_header($course, $wdir);
192 error("Requested directory does not exist.", "$CFG->wwwroot/files/index.php?id=$id");
195 switch ($action) {
197 case "upload":
198 html_header($course, $wdir);
199 require_once($CFG->dirroot.'/lib/uploadlib.php');
201 if ($save and confirm_sesskey()) {
202 $course->maxbytes = 0; // We are ignoring course limits
203 $um = new upload_manager('userfile',false,false,$course,false,0);
204 $dir = "$basedir$wdir";
205 if ($um->process_file_uploads($dir)) {
206 notify(get_string('uploadedfile'));
208 // um will take care of error reporting.
209 displaydir($wdir);
210 } else {
211 $upload_max_filesize = get_max_upload_file_size($CFG->maxbytes);
212 $filesize = display_size($upload_max_filesize);
214 $struploadafile = get_string("uploadafile");
215 $struploadthisfile = get_string("uploadthisfile");
216 $strmaxsize = get_string("maxsize", "", $filesize);
217 $strcancel = get_string("cancel");
219 echo "<p>$struploadafile ($strmaxsize) --> <b>$wdir</b></p>";
220 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"index.php\">";
221 echo "<div>";
222 echo "<table><tr><td colspan=\"2\">";
223 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
224 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
225 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
226 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
227 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
228 upload_print_form_fragment(1,array('userfile'),null,false,null,$upload_max_filesize,0,false);
229 echo " </td></tr></table>";
230 echo " <input type=\"submit\" name=\"save\" value=\"$struploadthisfile\" />";
231 echo "</div>";
232 echo "</form>";
233 echo "<form action=\"index.php\" method=\"get\">";
234 echo "<div>";
235 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
236 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
237 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
238 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
239 echo " <input type=\"submit\" value=\"$strcancel\" />";
240 echo "</div>";
241 echo "</form>";
243 html_footer();
244 break;
246 case "delete":
247 if ($confirm and confirm_sesskey()) {
248 html_header($course, $wdir);
249 if (!empty($USER->filelist)) {
250 foreach ($USER->filelist as $file) {
251 $fullfile = $basedir.'/'.$file;
252 if (! fulldelete($fullfile)) {
253 echo "<br />Error: Could not delete: $fullfile";
257 clearfilelist();
258 displaydir($wdir);
259 html_footer();
261 } else {
262 html_header($course, $wdir);
264 if (setfilelist($_POST)) {
265 notify(get_string('deletecheckwarning').':');
266 print_simple_box_start("center");
267 printfilelist($USER->filelist);
268 print_simple_box_end();
269 echo "<br />";
271 require_once($CFG->dirroot.'/mod/resource/lib.php');
272 $block = resource_delete_warning($course, $USER->filelist);
274 if (empty($CFG->resource_blockdeletingfile) or $block == '') {
275 $optionsyes = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'choose'=>$choose);
276 $optionsno = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
277 notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
278 } else {
280 notify(get_string('warningblockingdelete', 'resource'));
281 $options = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
282 print_continue("index.php?id=$id&amp;wdir=$wdir&amp;action=cancel&amp;choose=$choose");
284 } else {
285 displaydir($wdir);
287 html_footer();
289 break;
291 case "move":
292 html_header($course, $wdir);
293 if (($count = setfilelist($_POST)) and confirm_sesskey()) {
294 $USER->fileop = $action;
295 $USER->filesource = $wdir;
296 echo "<p class=\"centerpara\">";
297 print_string("selectednowmove", "moodle", $count);
298 echo "</p>";
300 displaydir($wdir);
301 html_footer();
302 break;
304 case "paste":
305 html_header($course, $wdir);
306 if (isset($USER->fileop) and ($USER->fileop == "move") and confirm_sesskey()) {
307 foreach ($USER->filelist as $file) {
308 $shortfile = basename($file);
309 $oldfile = $basedir.'/'.$file;
310 $newfile = $basedir.$wdir."/".$shortfile;
311 if (!rename($oldfile, $newfile)) {
312 echo "<p>Error: $shortfile not moved</p>";
316 clearfilelist();
317 displaydir($wdir);
318 html_footer();
319 break;
321 case "rename":
322 if (($name != '') and confirm_sesskey()) {
323 html_header($course, $wdir);
324 $name = clean_filename($name);
325 if (file_exists($basedir.$wdir."/".$name)) {
326 echo "<center>Error: $name already exists!</center>";
327 } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) {
328 echo "<p align=\"center\">Error: could not rename $oldname to $name</p>";
329 } else {
330 //file was renamed now update resources if needed
331 require_once($CFG->dirroot.'/mod/resource/lib.php');
332 resource_renamefiles($course, $wdir, $oldname, $name);
334 displaydir($wdir);
336 } else {
337 $strrename = get_string("rename");
338 $strcancel = get_string("cancel");
339 $strrenamefileto = get_string("renamefileto", "moodle", $file);
340 html_header($course, $wdir, "form.name");
341 echo "<p>$strrenamefileto:</p>";
342 echo "<table><tr><td>";
343 echo "<form action=\"index.php\" method=\"post\">";
344 echo "<fieldset class=\"invisiblefieldset\">";
345 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
346 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
347 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
348 echo " <input type=\"hidden\" name=\"action\" value=\"rename\" />";
349 echo " <input type=\"hidden\" name=\"oldname\" value=\"$file\" />";
350 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
351 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"$file\" />";
352 echo " <input type=\"submit\" value=\"$strrename\" />";
353 echo "</fieldset>";
354 echo "</form>";
355 echo "</td><td>";
356 echo "<form action=\"index.php\" method=\"get\">";
357 echo "<div>";
358 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
359 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
360 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
361 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
362 echo " <input type=\"submit\" value=\"$strcancel\" />";
363 echo "</div>";
364 echo "</form>";
365 echo "</td></tr></table>";
367 html_footer();
368 break;
370 case "makedir":
371 if (($name != '') and confirm_sesskey()) {
372 html_header($course, $wdir);
373 $name = clean_filename($name);
374 if (file_exists("$basedir$wdir/$name")) {
375 echo "Error: $name already exists!";
376 } else if (! make_upload_directory("$course->id$wdir/$name")) {
377 echo "Error: could not create $name";
379 displaydir($wdir);
381 } else {
382 $strcreate = get_string("create");
383 $strcancel = get_string("cancel");
384 $strcreatefolder = get_string("createfolder", "moodle", $wdir);
385 html_header($course, $wdir, "form.name");
386 echo "<p>$strcreatefolder:</p>";
387 echo "<table><tr><td>";
388 echo "<form action=\"index.php\" method=\"post\">";
389 echo "<fieldset class=\"invisiblefieldset\">";
390 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
391 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
392 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
393 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
394 echo " <input type=\"text\" name=\"name\" size=\"35\" />";
395 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
396 echo " <input type=\"submit\" value=\"$strcreate\" />";
397 echo "</fieldset>";
398 echo "</form>";
399 echo "</td><td>";
400 echo "<form action=\"index.php\" method=\"get\">";
401 echo "<div>";
402 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
403 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
404 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
405 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
406 echo " <input type=\"submit\" value=\"$strcancel\" />";
407 echo "</div>";
408 echo "</form>";
409 echo "</td></tr></table>";
411 html_footer();
412 break;
414 case "edit":
415 html_header($course, $wdir);
416 if (($text != '') and confirm_sesskey()) {
417 $fileptr = fopen($basedir.'/'.$file,"w");
418 $text = preg_replace('/\x0D/', '', $text); // http://moodle.org/mod/forum/discuss.php?d=38860
419 fputs($fileptr, stripslashes($text));
420 fclose($fileptr);
421 displaydir($wdir);
423 } else {
424 $streditfile = get_string("edit", "", "<b>$file</b>");
425 $fileptr = fopen($basedir.'/'.$file, "r");
426 $contents = fread($fileptr, filesize($basedir.'/'.$file));
427 fclose($fileptr);
429 if (mimeinfo("type", $file) == "text/html") {
430 $usehtmleditor = can_use_html_editor();
431 } else {
432 $usehtmleditor = false;
434 $usehtmleditor = false; // Always keep it off for now
436 print_heading("$streditfile");
438 echo "<table><tr><td colspan=\"2\">";
439 echo "<form action=\"index.php\" method=\"post\">";
440 echo "<div>";
441 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
442 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
443 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
444 echo " <input type=\"hidden\" name=\"file\" value=\"$file\" />";
445 echo " <input type=\"hidden\" name=\"action\" value=\"edit\" />";
446 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
447 print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents);
448 echo "</td></tr><tr><td>";
449 echo " <input type=\"submit\" value=\"".get_string("savechanges")."\" />";
450 echo "</div>";
451 echo "</form>";
452 echo "</td><td>";
453 echo "<form action=\"index.php\" method=\"get\">";
454 echo "<div>";
455 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
456 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
457 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
458 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
459 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
460 echo "</div>";
461 echo "</form>";
462 echo "</td></tr></table>";
464 if ($usehtmleditor) {
465 use_html_editor();
470 html_footer();
471 break;
473 case "zip":
474 if (($name != '') and confirm_sesskey()) {
475 html_header($course, $wdir);
476 $name = clean_filename($name);
478 $files = array();
479 foreach ($USER->filelist as $file) {
480 $files[] = "$basedir/$file";
483 if (!zip_files($files,"$basedir$wdir/$name")) {
484 print_error("zipfileserror","error");
487 clearfilelist();
488 displaydir($wdir);
490 } else {
491 html_header($course, $wdir, "form.name");
493 if (setfilelist($_POST)) {
494 echo "<p align=\"center\">".get_string("youareabouttocreatezip").":</p>";
495 print_simple_box_start("center");
496 printfilelist($USER->filelist);
497 print_simple_box_end();
498 echo "<br />";
499 echo "<p align=\"center\">".get_string("whattocallzip")."</p>";
500 echo "<table><tr><td>";
501 echo "<form action=\"index.php\" method=\"post\">";
502 echo "<fieldset class=\"invisiblefieldset\">";
503 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
504 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
505 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
506 echo " <input type=\"hidden\" name=\"action\" value=\"zip\" />";
507 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"new.zip\" />";
508 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
509 echo " <input type=\"submit\" value=\"".get_string("createziparchive")."\" />";
510 echo "<fieldset>";
511 echo "</form>";
512 echo "</td><td>";
513 echo "<form action=\"index.php\" method=\"get\">";
514 echo "<div>";
515 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
516 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
517 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
518 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
519 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
520 echo "</div>";
521 echo "</form>";
522 echo "</td></tr></table>";
523 } else {
524 displaydir($wdir);
525 clearfilelist();
528 html_footer();
529 break;
531 case "unzip":
532 html_header($course, $wdir);
533 if (($file != '') and confirm_sesskey()) {
534 $strok = get_string("ok");
535 $strunpacking = get_string("unpacking", "", $file);
537 echo "<p align=\"center\">$strunpacking:</p>";
539 $file = basename($file);
541 if (!unzip_file("$basedir$wdir/$file")) {
542 print_error("unzipfileserror","error");
545 echo "<div style=\"text-align:center\"><form action=\"index.php\" method=\"get\">";
546 echo "<div>";
547 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
548 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
549 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
550 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
551 echo " <input type=\"submit\" value=\"$strok\" />";
552 echo "</div>";
553 echo "</form>";
554 echo "</div>";
555 } else {
556 displaydir($wdir);
558 html_footer();
559 break;
561 case "listzip":
562 html_header($course, $wdir);
563 if (($file != '') and confirm_sesskey()) {
564 $strname = get_string("name");
565 $strsize = get_string("size");
566 $strmodified = get_string("modified");
567 $strok = get_string("ok");
568 $strlistfiles = get_string("listfiles", "", $file);
570 echo "<p align=\"center\">$strlistfiles:</p>";
571 $file = basename($file);
573 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
574 $archive = new PclZip(cleardoubleslashes("$basedir$wdir/$file"));
575 if (!$list = $archive->listContent(cleardoubleslashes("$basedir$wdir"))) {
576 notify($archive->errorInfo(true));
578 } else {
579 echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\" class=\"files\">";
580 echo "<tr class=\"file\"><th align=\"left\" class=\"header name\" scope=\"col\">$strname</th><th align=\"right\" class=\"header size\" scope=\"col\">$strsize</th><th align=\"right\" class=\"header date\" scope=\"col\">$strmodified</th></tr>";
581 foreach ($list as $item) {
582 echo "<tr>";
583 print_cell("left", s($item['filename']), 'name');
584 if (! $item['folder']) {
585 print_cell("right", display_size($item['size']), 'size');
586 } else {
587 echo "<td>&nbsp;</td>";
589 $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
590 print_cell("right", $filedate, 'date');
591 echo "</tr>";
593 echo "</table>";
595 echo "<br /><center><form action=\"index.php\" method=\"get\">";
596 echo "<div>";
597 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
598 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
599 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
600 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
601 echo " <input type=\"submit\" value=\"$strok\" />";
602 echo "</div>";
603 echo "</form>";
604 echo "</center>";
605 } else {
606 displaydir($wdir);
608 html_footer();
609 break;
611 case "restore":
612 html_header($course, $wdir);
613 if (($file != '') and confirm_sesskey()) {
614 echo "<p align=\"center\">".get_string("youaregoingtorestorefrom").":</p>";
615 print_simple_box_start("center");
616 echo $file;
617 print_simple_box_end();
618 echo "<br />";
619 echo "<p align=\"center\">".get_string("areyousuretorestorethisinfo")."</p>";
620 $restore_path = "$CFG->wwwroot/backup/restore.php";
621 notice_yesno (get_string("areyousuretorestorethis"),
622 $restore_path."?id=".$id."&amp;file=".cleardoubleslashes($id.$wdir."/".$file)."&amp;method=manual",
623 "index.php?id=$id&amp;wdir=$wdir&amp;action=cancel");
624 } else {
625 displaydir($wdir);
627 html_footer();
628 break;
630 case "cancel":
631 clearfilelist();
633 default:
634 html_header($course, $wdir);
635 displaydir($wdir);
636 html_footer();
637 break;
641 /// FILE FUNCTIONS ///////////////////////////////////////////////////////////
644 function setfilelist($VARS) {
645 global $USER;
647 $USER->filelist = array ();
648 $USER->fileop = "";
650 $count = 0;
651 foreach ($VARS as $key => $val) {
652 if (substr($key,0,4) == "file") {
653 $count++;
654 $val = rawurldecode($val);
655 $USER->filelist[] = clean_param($val, PARAM_PATH);
658 return $count;
661 function clearfilelist() {
662 global $USER;
664 $USER->filelist = array ();
665 $USER->fileop = "";
669 function printfilelist($filelist) {
670 global $CFG, $basedir;
672 $strfolder = get_string("folder");
673 $strfile = get_string("file");
675 foreach ($filelist as $file) {
676 if (is_dir($basedir.'/'.$file)) {
677 echo '<img src="'. $CFG->pixpath .'/f/folder.gif" class="icon" alt="'. $strfolder .'" /> '. htmlspecialchars($file) .'<br />';
678 $subfilelist = array();
679 $currdir = opendir($basedir.'/'.$file);
680 while (false !== ($subfile = readdir($currdir))) {
681 if ($subfile <> ".." && $subfile <> ".") {
682 $subfilelist[] = $file."/".$subfile;
685 printfilelist($subfilelist);
687 } else {
688 $icon = mimeinfo("icon", $file);
689 echo '<img src="'. $CFG->pixpath .'/f/'. $icon .'" class="icon" alt="'. $strfile .'" /> '. htmlspecialchars($file) .'<br />';
695 function print_cell($alignment='center', $text='&nbsp;', $class='') {
696 if ($class) {
697 $class = ' class="'.$class.'"';
699 echo '<td align="'.$alignment.'" style="white-space:nowrap "'.$class.'>'.$text.'</td>';
702 function displaydir ($wdir) {
703 // $wdir == / or /a or /a/b/c/d etc
705 global $basedir;
706 global $id;
707 global $USER, $CFG;
708 global $choose;
710 $fullpath = $basedir.$wdir;
711 $dirlist = array();
713 $directory = opendir($fullpath); // Find all files
714 while (false !== ($file = readdir($directory))) {
715 if ($file == "." || $file == "..") {
716 continue;
719 if (is_dir($fullpath."/".$file)) {
720 $dirlist[] = $file;
721 } else {
722 $filelist[] = $file;
725 closedir($directory);
727 $strname = get_string("name");
728 $strsize = get_string("size");
729 $strmodified = get_string("modified");
730 $straction = get_string("action");
731 $strmakeafolder = get_string("makeafolder");
732 $struploadafile = get_string("uploadafile");
733 $strselectall = get_string("selectall");
734 $strselectnone = get_string("deselectall");
735 $strwithchosenfiles = get_string("withchosenfiles");
736 $strmovetoanotherfolder = get_string("movetoanotherfolder");
737 $strmovefilestohere = get_string("movefilestohere");
738 $strdeletecompletely = get_string("deletecompletely");
739 $strcreateziparchive = get_string("createziparchive");
740 $strrename = get_string("rename");
741 $stredit = get_string("edit");
742 $strunzip = get_string("unzip");
743 $strlist = get_string("list");
744 $strrestore= get_string("restore");
745 $strchoose = get_string("choose");
746 $strfolder = get_string("folder");
747 $strfile = get_string("file");
750 echo "<form action=\"index.php\" method=\"post\" id=\"dirform\">";
751 echo "<div>";
752 echo '<input type="hidden" name="choose" value="'.$choose.'" />';
753 // echo "<hr align=\"center\" noshade=\"noshade\" size=\"1\" />";
754 echo "<hr/>";
755 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\" class=\"files\">";
756 echo "<tr>";
757 echo "<th class=\"header\" scope=\"col\"></th>";
758 echo "<th class=\"header name\" scope=\"col\">$strname</th>";
759 echo "<th class=\"header size\" scope=\"col\">$strsize</th>";
760 echo "<th class=\"header date\" scope=\"col\">$strmodified</th>";
761 echo "<th class=\"header commands\" scope=\"col\">$straction</th>";
762 echo "</tr>\n";
764 if ($wdir != "/") {
765 $dirlist[] = '..';
768 $count = 0;
770 if (!empty($dirlist)) {
771 asort($dirlist);
772 foreach ($dirlist as $dir) {
773 echo "<tr class=\"folder\">";
775 if ($dir == '..') {
776 $fileurl = rawurlencode(dirname($wdir));
777 print_cell();
778 // alt attribute intentionally empty to prevent repetition in screen reader
779 print_cell('left', '<a href="index.php?id='.$id.'&amp;wdir='.$fileurl.'&amp;choose='.$choose.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" />&nbsp;'.get_string('parentfolder').'</a>', 'name');
780 print_cell();
781 print_cell();
782 print_cell();
784 } else {
785 $count++;
786 $filename = $fullpath."/".$dir;
787 $fileurl = rawurlencode($wdir."/".$dir);
788 $filesafe = rawurlencode($dir);
789 $filesize = display_size(get_directory_size("$fullpath/$dir"));
790 $filedate = userdate(filemtime($filename), get_string("strftimedatetime"));
791 if ($wdir.$dir === '/moddata') {
792 print_cell();
793 } else {
794 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
796 print_cell("left", "<a href=\"index.php?id=$id&amp;wdir=$fileurl&amp;choose=$choose\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" />&nbsp;".htmlspecialchars($dir)."</a>", 'name');
797 print_cell("right", $filesize, 'size');
798 print_cell("right", $filedate, 'date');
799 if ($wdir.$dir === '/moddata') {
800 print_cell();
801 } else {
802 print_cell("right", "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename&amp;choose=$choose\">$strrename</a>", 'commands');
806 echo "</tr>";
811 if (!empty($filelist)) {
812 asort($filelist);
813 foreach ($filelist as $file) {
815 $icon = mimeinfo("icon", $file);
817 $count++;
818 $filename = $fullpath."/".$file;
819 $fileurl = trim($wdir, "/")."/$file";
820 $filesafe = rawurlencode($file);
821 $fileurlsafe = rawurlencode($fileurl);
822 $filedate = userdate(filemtime($filename), get_string("strftimedatetime"));
824 $selectfile = trim($fileurl, "/");
826 echo "<tr class=\"file\">";
828 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
829 echo "<td align=\"left\" style=\"white-space:nowrap\" class=\"name\">";
831 $ffurl = get_file_url($id.'/'.$fileurl);
832 link_to_popup_window ($ffurl, "display",
833 "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".htmlspecialchars($file),
834 480, 640);
835 echo "</td>";
837 $file_size = filesize($filename);
838 print_cell("right", display_size($file_size), 'size');
839 print_cell("right", $filedate, 'date');
841 if ($choose) {
842 $edittext = "<strong><a onclick=\"return set_value('$selectfile')\" href=\"#\">$strchoose</a></strong>&nbsp;";
843 } else {
844 $edittext = '';
848 if ($icon == "text.gif" || $icon == "html.gif") {
849 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=edit&amp;choose=$choose\">$stredit</a>";
850 } else if ($icon == "zip.gif") {
851 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=unzip&amp;sesskey=$USER->sesskey&amp;choose=$choose\">$strunzip</a>&nbsp;";
852 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=listzip&amp;sesskey=$USER->sesskey&amp;choose=$choose\">$strlist</a> ";
853 if (!empty($CFG->backup_version) and has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
854 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=restore&amp;sesskey=$USER->sesskey&amp;choose=$choose\">$strrestore</a> ";
858 print_cell("right", "$edittext <a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename&amp;choose=$choose\">$strrename</a>", 'commands');
860 echo "</tr>";
863 echo "</table>";
864 echo "<hr />";
865 //echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
867 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
868 echo "<tr><td>";
869 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
870 echo '<input type="hidden" name="choose" value="'.$choose.'" />';
871 echo "<input type=\"hidden\" name=\"wdir\" value=\"$wdir\" /> ";
872 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
873 $options = array (
874 "move" => "$strmovetoanotherfolder",
875 "delete" => "$strdeletecompletely",
876 "zip" => "$strcreateziparchive"
878 if (!empty($count)) {
880 choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:getElementById('dirform').submit()");
881 echo '<div id="noscriptgo" style="display: inline;">';
882 echo '<input type="submit" value="'.get_string('go').'" />';
883 echo '<script type="text/javascript">'.
884 "\n//<![CDATA[\n".
885 'document.getElementById("noscriptgo").style.display = "none";'.
886 "\n//]]>\n".'</script>';
887 echo '</div>';
890 echo "</td></tr></table>";
891 echo "</div>";
892 echo "</form>";
893 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\"><tr>";
894 echo "<td align=\"center\">";
895 if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) {
896 echo "<form action=\"index.php\" method=\"get\">";
897 echo "<div>";
898 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
899 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
900 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
901 echo " <input type=\"hidden\" name=\"action\" value=\"paste\" />";
902 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
903 echo " <input type=\"submit\" value=\"$strmovefilestohere\" />";
904 echo "</div>";
905 echo "</form>";
907 echo "</td>";
908 echo "<td align=\"right\">";
909 echo "<form action=\"index.php\" method=\"get\">";
910 echo "<div>";
911 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
912 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
913 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
914 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
915 echo " <input type=\"submit\" value=\"$strmakeafolder\" />";
916 echo "</div>";
917 echo "</form>";
918 echo "</td>";
919 echo "<td align=\"right\">";
920 echo "<form action=\"index.php\" method=\"get\">"; //dummy form - alignment only
921 echo "<fieldset class=\"invisiblefieldset\">";
922 echo " <input type=\"button\" value=\"$strselectall\" onclick=\"checkall();\" />";
923 echo " <input type=\"button\" value=\"$strselectnone\" onclick=\"uncheckall();\" />";
924 echo "</fieldset>";
925 echo "</form>";
926 echo "</td>";
927 echo "<td align=\"right\">";
928 echo "<form action=\"index.php\" method=\"get\">";
929 echo "<div>";
930 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
931 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
932 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
933 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
934 echo " <input type=\"submit\" value=\"$struploadafile\" />";
935 echo "</div>";
936 echo "</form>";
937 echo "</td></tr>";
938 echo "</table>";
939 echo "<hr/>";
940 //echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";