Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / files / index.php
blob87b83c1b38f24f652438d6f2a8a7c3762c804efd
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');
13 $id = required_param('id', PARAM_INT);
14 $file = optional_param('file', '', PARAM_PATH);
15 $wdir = optional_param('wdir', '', PARAM_PATH);
16 $action = optional_param('action', '', PARAM_ACTION);
17 $name = optional_param('name', '', PARAM_FILE);
18 $oldname = optional_param('oldname', '', PARAM_FILE);
19 $choose = optional_param('choose', '', PARAM_FILE); //in fact it is always 'formname.inputname'
20 $userfile= optional_param('userfile','',PARAM_FILE);
21 $save = optional_param('save', 0, PARAM_BOOL);
22 $text = optional_param('text', '', PARAM_RAW);
23 $confirm = optional_param('confirm', 0, PARAM_BOOL);
25 if ($choose) {
26 if (count(explode('.', $choose)) > 2) {
27 error('Incorrect format for choose parameter');
32 if (! $course = get_record("course", "id", $id) ) {
33 error("That's an invalid course id");
36 require_login($course->id);
38 require_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $course->id));
40 function html_footer() {
41 global $course, $choose;
43 echo '</td></tr></table>';
45 print_footer($course);
48 function html_header($course, $wdir, $formfield=""){
49 global $CFG, $ME, $choose;
51 if (! $site = get_site()) {
52 error("Invalid site!");
55 if ($course->id == $site->id) {
56 $strfiles = get_string("sitefiles");
57 } else {
58 $strfiles = get_string("files");
61 if ($wdir == "/") {
62 $fullnav = "$strfiles";
63 } else {
64 $dirs = explode("/", $wdir);
65 $numdirs = count($dirs);
66 $link = "";
67 $navigation = "";
68 for ($i=1; $i<$numdirs-1; $i++) {
69 $navigation .= " -> ";
70 $link .= "/".urlencode($dirs[$i]);
71 $navigation .= "<a href=\"".$ME."?id=$course->id&amp;wdir=$link&amp;choose=$choose\">".$dirs[$i]."</a>";
73 $fullnav = "<a href=\"".$ME."?id=$course->id&amp;wdir=/&amp;choose=$choose\">$strfiles</a> $navigation -> ".$dirs[$numdirs-1];
77 if ($choose) {
78 print_header();
80 $chooseparts = explode('.', $choose);
81 if (count($chooseparts)==2){
83 <script type="text/javascript">
84 //<![CDATA[
85 function set_value(txt) {
86 opener.document.forms['<?php echo $chooseparts[0]."'].".$chooseparts[1] ?>.value = txt;
87 window.close();
89 //]]>
90 </script>
92 <?php
93 } elseif (count($chooseparts)==1){
95 <script type="text/javascript">
96 //<![CDATA[
97 function set_value(txt) {
98 opener.document.getElementById('<?php echo $chooseparts[0] ?>').value = txt;
99 window.close();
101 //]]>
102 </script>
104 <?php
107 $fullnav = str_replace('->', '&raquo;', "$course->shortname -> $fullnav");
108 echo '<div id="nav-bar">'.$fullnav.'</div>';
110 if ($course->id == $site->id) {
111 print_heading(get_string("publicsitefileswarning"), "center", 2);
114 } else {
116 if ($course->id == $site->id) {
117 print_header("$course->shortname: $strfiles", "$course->fullname",
118 "<a href=\"../$CFG->admin/index.php\">".get_string("administration").
119 "</a> -> $fullnav", $formfield);
121 print_heading(get_string("publicsitefileswarning"), "center", 2);
123 } else {
124 print_header("$course->shortname: $strfiles", "$course->fullname",
125 "<a href=\"../course/view.php?id=$course->id\">$course->shortname".
126 "</a> -> $fullnav", $formfield);
131 echo "<table border=\"0\" align=\"center\" cellspacing=\"3\" cellpadding=\"3\" width=\"640\">";
132 echo "<tr>";
133 echo "<td colspan=\"2\">";
138 if (! $basedir = make_upload_directory("$course->id")) {
139 error("The site administrator needs to fix the file permissions");
142 $baseweb = $CFG->wwwroot;
144 // End of configuration and access control
147 if ($wdir == '') {
148 $wdir = "/";
151 if ($wdir{0} != '/') { //make sure $wdir starts with slash
152 $wdir = "/".$wdir;
155 if ($wdir == "/backupdata") {
156 if (! make_upload_directory("$course->id/backupdata")) { // Backup folder
157 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
161 if (!is_dir($basedir.$wdir)) {
162 html_header($course, $wdir);
163 error("Requested directory does not exist.", "$CFG->wwwroot/files/index.php?id=$id");
166 switch ($action) {
168 case "upload":
169 html_header($course, $wdir);
170 require_once($CFG->dirroot.'/lib/uploadlib.php');
172 if ($save and confirm_sesskey()) {
173 $course->maxbytes = 0; // We are ignoring course limits
174 $um = new upload_manager('userfile',false,false,$course,false,0);
175 $dir = "$basedir$wdir";
176 if ($um->process_file_uploads($dir)) {
177 notify(get_string('uploadedfile'));
179 // um will take care of error reporting.
180 displaydir($wdir);
181 } else {
182 $upload_max_filesize = get_max_upload_file_size($CFG->maxbytes);
183 $filesize = display_size($upload_max_filesize);
185 $struploadafile = get_string("uploadafile");
186 $struploadthisfile = get_string("uploadthisfile");
187 $strmaxsize = get_string("maxsize", "", $filesize);
188 $strcancel = get_string("cancel");
190 echo "<p>$struploadafile ($strmaxsize) --> <b>$wdir</b></p>";
191 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"index.php\">";
192 echo "<table><tr><td colspan=\"2\">";
193 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
194 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
195 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
196 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
197 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
198 upload_print_form_fragment(1,array('userfile'),null,false,null,$upload_max_filesize,0,false);
199 echo " </td></tr></table>";
200 echo " <input type=\"submit\" name=\"save\" value=\"$struploadthisfile\" />";
201 echo "</form>";
202 echo "<form action=\"index.php\" method=\"get\">";
203 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
204 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
205 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
206 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
207 echo " <input type=\"submit\" value=\"$strcancel\" />";
208 echo "</form>";
210 html_footer();
211 break;
213 case "delete":
214 if ($confirm and confirm_sesskey()) {
215 html_header($course, $wdir);
216 if (!empty($USER->filelist)) {
217 foreach ($USER->filelist as $file) {
218 $fullfile = $basedir.'/'.$file;
219 if (! fulldelete($fullfile)) {
220 echo "<br />Error: Could not delete: $fullfile";
224 clearfilelist();
225 displaydir($wdir);
226 html_footer();
228 } else {
229 html_header($course, $wdir);
231 if (setfilelist($_POST)) {
232 notify(get_string('deletecheckwarning').':');
233 print_simple_box_start("center");
234 printfilelist($USER->filelist);
235 print_simple_box_end();
236 echo "<br />";
238 require_once($CFG->dirroot.'/mod/resource/lib.php');
239 $block = resource_delete_warning($course, $USER->filelist);
241 if (empty($CFG->resource_blockdeletingfile) or $block == '') {
242 $optionsyes = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'choose'=>$choose);
243 $optionsno = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
244 notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
245 } else {
247 notify(get_string('warningblockingdelete', 'resource'));
248 $options = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
249 print_continue("index.php?id=$id&amp;wdir=$wdir&amp;action=cancel&amp;choose=$choose");
251 } else {
252 displaydir($wdir);
254 html_footer();
256 break;
258 case "move":
259 html_header($course, $wdir);
260 if (($count = setfilelist($_POST)) and confirm_sesskey()) {
261 $USER->fileop = $action;
262 $USER->filesource = $wdir;
263 echo "<p align=\"center\">";
264 print_string("selectednowmove", "moodle", $count);
265 echo "</p>";
267 displaydir($wdir);
268 html_footer();
269 break;
271 case "paste":
272 html_header($course, $wdir);
273 if (isset($USER->fileop) and ($USER->fileop == "move") and confirm_sesskey()) {
274 foreach ($USER->filelist as $file) {
275 $shortfile = basename($file);
276 $oldfile = $basedir.'/'.$file;
277 $newfile = $basedir.$wdir."/".$shortfile;
278 if (!rename($oldfile, $newfile)) {
279 echo "<p>Error: $shortfile not moved</p>";
283 clearfilelist();
284 displaydir($wdir);
285 html_footer();
286 break;
288 case "rename":
289 if (($name != '') and confirm_sesskey()) {
290 html_header($course, $wdir);
291 $name = clean_filename($name);
292 if (file_exists($basedir.$wdir."/".$name)) {
293 echo "<center>Error: $name already exists!</center>";
294 } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) {
295 echo "<p align=\"center\">Error: could not rename $oldname to $name</p>";
296 } else {
297 //file was renamed now update resources if needed
298 require_once($CFG->dirroot.'/mod/resource/lib.php');
299 resource_renamefiles($course, $wdir, $oldname, $name);
301 displaydir($wdir);
303 } else {
304 $strrename = get_string("rename");
305 $strcancel = get_string("cancel");
306 $strrenamefileto = get_string("renamefileto", "moodle", $file);
307 html_header($course, $wdir, "form.name");
308 echo "<p>$strrenamefileto:</p>";
309 echo "<table><tr><td>";
310 echo "<form action=\"index.php\" method=\"post\">";
311 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
312 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
313 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
314 echo " <input type=\"hidden\" name=\"action\" value=\"rename\" />";
315 echo " <input type=\"hidden\" name=\"oldname\" value=\"$file\" />";
316 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
317 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"$file\" />";
318 echo " <input type=\"submit\" value=\"$strrename\" />";
319 echo "</form>";
320 echo "</td><td>";
321 echo "<form action=\"index.php\" method=\"get\">";
322 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
323 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
324 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
325 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
326 echo " <input type=\"submit\" value=\"$strcancel\" />";
327 echo "</form>";
328 echo "</td></tr></table>";
330 html_footer();
331 break;
333 case "makedir":
334 if (($name != '') and confirm_sesskey()) {
335 html_header($course, $wdir);
336 $name = clean_filename($name);
337 if (file_exists("$basedir$wdir/$name")) {
338 echo "Error: $name already exists!";
339 } else if (! make_upload_directory("$course->id$wdir/$name")) {
340 echo "Error: could not create $name";
342 displaydir($wdir);
344 } else {
345 $strcreate = get_string("create");
346 $strcancel = get_string("cancel");
347 $strcreatefolder = get_string("createfolder", "moodle", $wdir);
348 html_header($course, $wdir, "form.name");
349 echo "<p>$strcreatefolder:</p>";
350 echo "<table><tr><td>";
351 echo "<form action=\"index.php\" method=\"post\">";
352 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
353 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
354 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
355 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
356 echo " <input type=\"text\" name=\"name\" size=\"35\" />";
357 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
358 echo " <input type=\"submit\" value=\"$strcreate\" />";
359 echo "</form>";
360 echo "</td><td>";
361 echo "<form action=\"index.php\" method=\"get\">";
362 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
363 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
364 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
365 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
366 echo " <input type=\"submit\" value=\"$strcancel\" />";
367 echo "</form>";
368 echo "</td></tr></table>";
370 html_footer();
371 break;
373 case "edit":
374 html_header($course, $wdir);
375 if (($text != '') and confirm_sesskey()) {
376 $fileptr = fopen($basedir.'/'.$file,"w");
377 $text = preg_replace('/\x0D/', '', $text); // http://moodle.org/mod/forum/discuss.php?d=38860
378 fputs($fileptr, stripslashes($text));
379 fclose($fileptr);
380 displaydir($wdir);
382 } else {
383 $streditfile = get_string("edit", "", "<b>$file</b>");
384 $fileptr = fopen($basedir.'/'.$file, "r");
385 $contents = fread($fileptr, filesize($basedir.'/'.$file));
386 fclose($fileptr);
388 if (mimeinfo("type", $file) == "text/html") {
389 $usehtmleditor = can_use_html_editor();
390 } else {
391 $usehtmleditor = false;
393 $usehtmleditor = false; // Always keep it off for now
395 print_heading("$streditfile");
397 echo "<table><tr><td colspan=\"2\">";
398 echo "<form action=\"index.php\" method=\"post\">";
399 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
400 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
401 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
402 echo " <input type=\"hidden\" name=\"file\" value=\"$file\" />";
403 echo " <input type=\"hidden\" name=\"action\" value=\"edit\" />";
404 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
405 print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents);
406 echo "</td></tr><tr><td>";
407 echo " <input type=\"submit\" value=\"".get_string("savechanges")."\" />";
408 echo "</form>";
409 echo "</td><td>";
410 echo "<form action=\"index.php\" method=\"get\">";
411 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
412 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
413 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
414 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
415 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
416 echo "</form>";
417 echo "</td></tr></table>";
419 if ($usehtmleditor) {
420 use_html_editor();
425 html_footer();
426 break;
428 case "zip":
429 if (($name != '') and confirm_sesskey()) {
430 html_header($course, $wdir);
431 $name = clean_filename($name);
433 $files = array();
434 foreach ($USER->filelist as $file) {
435 $files[] = "$basedir/$file";
438 if (!zip_files($files,"$basedir$wdir/$name")) {
439 error(get_string("zipfileserror","error"));
442 clearfilelist();
443 displaydir($wdir);
445 } else {
446 html_header($course, $wdir, "form.name");
448 if (setfilelist($_POST)) {
449 echo "<p align=\"center\">".get_string("youareabouttocreatezip").":</p>";
450 print_simple_box_start("center");
451 printfilelist($USER->filelist);
452 print_simple_box_end();
453 echo "<br />";
454 echo "<p align=\"center\">".get_string("whattocallzip")."</p>";
455 echo "<table><tr><td>";
456 echo "<form action=\"index.php\" method=\"post\">";
457 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
458 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
459 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
460 echo " <input type=\"hidden\" name=\"action\" value=\"zip\" />";
461 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"new.zip\" />";
462 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
463 echo " <input type=\"submit\" value=\"".get_string("createziparchive")."\" />";
464 echo "</form>";
465 echo "</td><td>";
466 echo "<form action=\"index.php\" method=\"get\">";
467 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
468 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
469 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
470 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
471 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
472 echo "</form>";
473 echo "</td></tr></table>";
474 } else {
475 displaydir($wdir);
476 clearfilelist();
479 html_footer();
480 break;
482 case "unzip":
483 html_header($course, $wdir);
484 if (($file != '') and confirm_sesskey()) {
485 $strok = get_string("ok");
486 $strunpacking = get_string("unpacking", "", $file);
488 echo "<p align=\"center\">$strunpacking:</p>";
490 $file = basename($file);
492 if (!unzip_file("$basedir$wdir/$file")) {
493 error(get_string("unzipfileserror","error"));
496 echo "<center><form action=\"index.php\" method=\"get\">";
497 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
498 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
499 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
500 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
501 echo " <input type=\"submit\" value=\"$strok\" />";
502 echo "</form>";
503 echo "</center>";
504 } else {
505 displaydir($wdir);
507 html_footer();
508 break;
510 case "listzip":
511 html_header($course, $wdir);
512 if (($file != '') and confirm_sesskey()) {
513 $strname = get_string("name");
514 $strsize = get_string("size");
515 $strmodified = get_string("modified");
516 $strok = get_string("ok");
517 $strlistfiles = get_string("listfiles", "", $file);
519 echo "<p align=\"center\">$strlistfiles:</p>";
520 $file = basename($file);
522 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
523 $archive = new PclZip(cleardoubleslashes("$basedir$wdir/$file"));
524 if (!$list = $archive->listContent(cleardoubleslashes("$basedir$wdir"))) {
525 notify($archive->errorInfo(true));
527 } else {
528 echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\" class=\"files\">";
529 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>";
530 foreach ($list as $item) {
531 echo "<tr>";
532 print_cell("left", s($item['filename']), 'name');
533 if (! $item['folder']) {
534 print_cell("right", display_size($item['size']), 'size');
535 } else {
536 echo "<td>&nbsp;</td>";
538 $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
539 print_cell("right", $filedate, 'date');
540 echo "</tr>";
542 echo "</table>";
544 echo "<br /><center><form action=\"index.php\" method=\"get\">";
545 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
546 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
547 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
548 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
549 echo " <input type=\"submit\" value=\"$strok\" />";
550 echo "</form>";
551 echo "</center>";
552 } else {
553 displaydir($wdir);
555 html_footer();
556 break;
558 case "restore":
559 html_header($course, $wdir);
560 if (($file != '') and confirm_sesskey()) {
561 echo "<p align=\"center\">".get_string("youaregoingtorestorefrom").":</p>";
562 print_simple_box_start("center");
563 echo $file;
564 print_simple_box_end();
565 echo "<br />";
566 echo "<p align=\"center\">".get_string("areyousuretorestorethisinfo")."</p>";
567 $restore_path = "$CFG->wwwroot/backup/restore.php";
568 notice_yesno (get_string("areyousuretorestorethis"),
569 $restore_path."?id=".$id."&amp;file=".cleardoubleslashes($id.$wdir."/".$file)."&amp;method=manual",
570 "index.php?id=$id&amp;wdir=$wdir&amp;action=cancel");
571 } else {
572 displaydir($wdir);
574 html_footer();
575 break;
577 case "cancel":
578 clearfilelist();
580 default:
581 html_header($course, $wdir);
582 displaydir($wdir);
583 html_footer();
584 break;
588 /// FILE FUNCTIONS ///////////////////////////////////////////////////////////
591 function setfilelist($VARS) {
592 global $USER;
594 $USER->filelist = array ();
595 $USER->fileop = "";
597 $count = 0;
598 foreach ($VARS as $key => $val) {
599 if (substr($key,0,4) == "file") {
600 $count++;
601 $val = rawurldecode($val);
602 $USER->filelist[] = clean_param($val, PARAM_PATH);
605 return $count;
608 function clearfilelist() {
609 global $USER;
611 $USER->filelist = array ();
612 $USER->fileop = "";
616 function printfilelist($filelist) {
617 global $CFG, $basedir;
619 $strfolder = get_string("folder");
620 $strfile = get_string("file");
622 foreach ($filelist as $file) {
623 if (is_dir($basedir.'/'.$file)) {
624 echo "<img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> $file<br />";
625 $subfilelist = array();
626 $currdir = opendir($basedir.'/'.$file);
627 while (false !== ($subfile = readdir($currdir))) {
628 if ($subfile <> ".." && $subfile <> ".") {
629 $subfilelist[] = $file."/".$subfile;
632 printfilelist($subfilelist);
634 } else {
635 $icon = mimeinfo("icon", $file);
636 echo "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> $file<br />";
642 function print_cell($alignment='center', $text='&nbsp;', $class='') {
643 if ($class) {
644 $class = ' class="'.$class.'"';
646 echo '<td align="'.$alignment.'" nowrap="nowrap"'.$class.'>'.$text.'</td>';
649 function displaydir ($wdir) {
650 // $wdir == / or /a or /a/b/c/d etc
652 global $basedir;
653 global $id;
654 global $USER, $CFG;
655 global $choose;
657 $fullpath = $basedir.$wdir;
658 $dirlist = array();
660 $directory = opendir($fullpath); // Find all files
661 while (false !== ($file = readdir($directory))) {
662 if ($file == "." || $file == "..") {
663 continue;
666 if (is_dir($fullpath."/".$file)) {
667 $dirlist[] = $file;
668 } else {
669 $filelist[] = $file;
672 closedir($directory);
674 $strname = get_string("name");
675 $strsize = get_string("size");
676 $strmodified = get_string("modified");
677 $straction = get_string("action");
678 $strmakeafolder = get_string("makeafolder");
679 $struploadafile = get_string("uploadafile");
680 $strselectall = get_string("selectall");
681 $strselectnone = get_string("deselectall");
682 $strwithchosenfiles = get_string("withchosenfiles");
683 $strmovetoanotherfolder = get_string("movetoanotherfolder");
684 $strmovefilestohere = get_string("movefilestohere");
685 $strdeletecompletely = get_string("deletecompletely");
686 $strcreateziparchive = get_string("createziparchive");
687 $strrename = get_string("rename");
688 $stredit = get_string("edit");
689 $strunzip = get_string("unzip");
690 $strlist = get_string("list");
691 $strrestore= get_string("restore");
692 $strchoose = get_string("choose");
693 $strfolder = get_string("folder");
694 $strfile = get_string("file");
697 echo "<form action=\"index.php\" method=\"post\" id=\"dirform\">";
698 echo '<input type="hidden" name="choose" value="'.$choose.'" />';
699 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
700 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\" class=\"files\">";
701 echo "<tr>";
702 echo "<th width=\"5\" scope=\"col\"></th>";
703 echo "<th align=\"left\" class=\"header name\" scope=\"col\">$strname</th>";
704 echo "<th align=\"right\" class=\"header size\" scope=\"col\">$strsize</th>";
705 echo "<th align=\"right\" class=\"header date\" scope=\"col\">$strmodified</th>";
706 echo "<th align=\"right\" class=\"header commands\" scope=\"col\">$straction</th>";
707 echo "</tr>\n";
709 if ($wdir != "/") {
710 $dirlist[] = '..';
713 $count = 0;
715 if (!empty($dirlist)) {
716 asort($dirlist);
717 foreach ($dirlist as $dir) {
718 echo "<tr class=\"folder\">";
720 if ($dir == '..') {
721 $fileurl = rawurlencode(dirname($wdir));
722 print_cell();
723 // alt attribute intentionally empty to prevent repetition in screen reader
724 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');
725 print_cell();
726 print_cell();
727 print_cell();
729 } else {
730 $count++;
731 $filename = $fullpath."/".$dir;
732 $fileurl = rawurlencode($wdir."/".$dir);
733 $filesafe = rawurlencode($dir);
734 $filesize = display_size(get_directory_size("$fullpath/$dir"));
735 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
736 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
737 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');
738 print_cell("right", $filesize, 'size');
739 print_cell("right", $filedate, 'date');
740 print_cell("right", "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename&amp;choose=$choose\">$strrename</a>", 'commands');
743 echo "</tr>";
748 if (!empty($filelist)) {
749 asort($filelist);
750 foreach ($filelist as $file) {
752 $icon = mimeinfo("icon", $file);
754 $count++;
755 $filename = $fullpath."/".$file;
756 $fileurl = trim($wdir, "/")."/$file";
757 $filesafe = rawurlencode($file);
758 $fileurlsafe = rawurlencode($fileurl);
759 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
761 $selectfile = trim($fileurl, "/");
763 echo "<tr class=\"file\">";
765 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
766 echo "<td align=\"left\" nowrap=\"nowrap\" class=\"name\">";
767 if ($CFG->slasharguments) {
768 $ffurl = str_replace('//', '/', "/file.php/$id/$fileurl");
769 } else {
770 $ffurl = str_replace('//', '/', "/file.php?file=/$id/$fileurl");
772 link_to_popup_window ($ffurl, "display",
773 "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".htmlspecialchars($file),
774 480, 640);
775 echo "</td>";
777 $file_size = filesize($filename);
778 print_cell("right", display_size($file_size), 'size');
779 print_cell("right", $filedate, 'date');
781 if ($choose) {
782 $edittext = "<strong><a onclick=\"return set_value('$selectfile')\" href=\"#\">$strchoose</a></strong>&nbsp;";
783 } else {
784 $edittext = '';
788 if ($icon == "text.gif" || $icon == "html.gif") {
789 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=edit&amp;choose=$choose\">$stredit</a>";
790 } else if ($icon == "zip.gif") {
791 $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;";
792 $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> ";
793 if (!empty($CFG->backup_version) and has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
794 $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> ";
798 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');
800 echo "</tr>";
803 echo "</table>";
804 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
806 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
807 echo "<tr><td>";
808 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
809 echo '<input type="hidden" name="choose" value="'.$choose.'" />';
810 echo "<input type=\"hidden\" name=\"wdir\" value=\"$wdir\" /> ";
811 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
812 $options = array (
813 "move" => "$strmovetoanotherfolder",
814 "delete" => "$strdeletecompletely",
815 "zip" => "$strcreateziparchive"
817 if (!empty($count)) {
818 choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:getElementById('dirform').submit()");
820 echo "</td></tr></table>";
821 echo "</form>";
822 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\"><tr>";
823 echo "<td align=\"center\">";
824 if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) {
825 echo "<form action=\"index.php\" method=\"get\">";
826 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
827 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
828 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
829 echo " <input type=\"hidden\" name=\"action\" value=\"paste\" />";
830 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
831 echo " <input type=\"submit\" value=\"$strmovefilestohere\" />";
832 echo "</form>";
834 echo "</td>";
835 echo "<td align=\"right\">";
836 echo "<form action=\"index.php\" method=\"get\">";
837 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
838 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
839 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
840 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
841 echo " <input type=\"submit\" value=\"$strmakeafolder\" />";
842 echo "</form>";
843 echo "</td>";
844 echo "<td align=\"right\">";
845 echo "<form action=\"index.php\" method=\"get\">"; //dummy form - alignment only
846 echo " <input type=\"button\" value=\"$strselectall\" onclick=\"checkall();\" />";
847 echo " <input type=\"button\" value=\"$strselectnone\" onclick=\"uncheckall();\" />";
848 echo "</form>";
849 echo "</td>";
850 echo "<td align=\"right\">";
851 echo "<form action=\"index.php\" method=\"get\">";
852 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
853 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
854 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
855 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
856 echo " <input type=\"submit\" value=\"$struploadafile\" />";
857 echo "</form>";
858 echo "</td></tr>";
859 echo "</table>";
860 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";