Should be $COURSE not $course
[moodle-linuxchix.git] / files / index.php
blob8f91ffc0ea14986727ec389d0d2a07cfa477502c
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 if ($course->id == SITEID) {
53 $strfiles = get_string("sitefiles");
54 } else {
55 $strfiles = get_string("files");
58 if ($wdir == "/") {
59 $fullnav = "$strfiles";
60 } else {
61 $dirs = explode("/", $wdir);
62 $numdirs = count($dirs);
63 $link = "";
64 $navigation = "";
65 for ($i=1; $i<$numdirs-1; $i++) {
66 $navigation .= " -> ";
67 $link .= "/".urlencode($dirs[$i]);
68 $navigation .= "<a href=\"".$ME."?id=$course->id&amp;wdir=$link&amp;choose=$choose\">".$dirs[$i]."</a>";
70 $fullnav = "<a href=\"".$ME."?id=$course->id&amp;wdir=/&amp;choose=$choose\">$strfiles</a> $navigation -> ".$dirs[$numdirs-1];
74 if ($choose) {
75 print_header();
77 $chooseparts = explode('.', $choose);
78 if (count($chooseparts)==2){
80 <script type="text/javascript">
81 //<![CDATA[
82 function set_value(txt) {
83 opener.document.forms['<?php echo $chooseparts[0]."'].".$chooseparts[1] ?>.value = txt;
84 window.close();
86 //]]>
87 </script>
89 <?php
90 } elseif (count($chooseparts)==1){
92 <script type="text/javascript">
93 //<![CDATA[
94 function set_value(txt) {
95 opener.document.getElementById('<?php echo $chooseparts[0] ?>').value = txt;
96 window.close();
98 //]]>
99 </script>
101 <?php
104 $fullnav = str_replace('->', '&raquo;', format_string($course->shortname) . " -> $fullnav");
105 echo '<div id="nav-bar">'.$fullnav.'</div>';
107 if ($course->id == SITEID and $wdir != "/backupdata") {
108 print_heading(get_string("publicsitefileswarning"), "center", 2);
111 } else {
113 if ($course->id == SITEID) {
115 if ($wdir == "/backupdata") {
116 admin_externalpage_setup('frontpagerestore');
117 admin_externalpage_print_header();
118 } else {
119 admin_externalpage_setup('sitefiles');
120 admin_externalpage_print_header();
122 print_heading(get_string("publicsitefileswarning"), "center", 2);
126 } else {
127 print_header("$course->shortname: $strfiles", $course->fullname,
128 "<a href=\"../course/view.php?id=$course->id\">$course->shortname".
129 "</a> -> $fullnav", $formfield);
134 echo "<table border=\"0\" style=\"margin-left:auto;margin-right:auto\" cellspacing=\"3\" cellpadding=\"3\" width=\"640\">";
135 echo "<tr>";
136 echo "<td colspan=\"2\">";
141 if (! $basedir = make_upload_directory("$course->id")) {
142 error("The site administrator needs to fix the file permissions");
145 $baseweb = $CFG->wwwroot;
147 // End of configuration and access control
150 if ($wdir == '') {
151 $wdir = "/";
154 if ($wdir{0} != '/') { //make sure $wdir starts with slash
155 $wdir = "/".$wdir;
158 if ($wdir == "/backupdata") {
159 if (! make_upload_directory("$course->id/backupdata")) { // Backup folder
160 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
164 if (!is_dir($basedir.$wdir)) {
165 html_header($course, $wdir);
166 error("Requested directory does not exist.", "$CFG->wwwroot/files/index.php?id=$id");
169 switch ($action) {
171 case "upload":
172 html_header($course, $wdir);
173 require_once($CFG->dirroot.'/lib/uploadlib.php');
175 if ($save and confirm_sesskey()) {
176 $course->maxbytes = 0; // We are ignoring course limits
177 $um = new upload_manager('userfile',false,false,$course,false,0);
178 $dir = "$basedir$wdir";
179 if ($um->process_file_uploads($dir)) {
180 notify(get_string('uploadedfile'));
182 // um will take care of error reporting.
183 displaydir($wdir);
184 } else {
185 $upload_max_filesize = get_max_upload_file_size($CFG->maxbytes);
186 $filesize = display_size($upload_max_filesize);
188 $struploadafile = get_string("uploadafile");
189 $struploadthisfile = get_string("uploadthisfile");
190 $strmaxsize = get_string("maxsize", "", $filesize);
191 $strcancel = get_string("cancel");
193 echo "<p>$struploadafile ($strmaxsize) --> <b>$wdir</b></p>";
194 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"index.php\">";
195 echo "<div>";
196 echo "<table><tr><td colspan=\"2\">";
197 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
198 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
199 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
200 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
201 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
202 upload_print_form_fragment(1,array('userfile'),null,false,null,$upload_max_filesize,0,false);
203 echo " </td></tr></table>";
204 echo " <input type=\"submit\" name=\"save\" value=\"$struploadthisfile\" />";
205 echo "</div>";
206 echo "</form>";
207 echo "<form action=\"index.php\" method=\"get\">";
208 echo "<div>";
209 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
210 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
211 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
212 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
213 echo " <input type=\"submit\" value=\"$strcancel\" />";
214 echo "</div>";
215 echo "</form>";
217 html_footer();
218 break;
220 case "delete":
221 if ($confirm and confirm_sesskey()) {
222 html_header($course, $wdir);
223 if (!empty($USER->filelist)) {
224 foreach ($USER->filelist as $file) {
225 $fullfile = $basedir.'/'.$file;
226 if (! fulldelete($fullfile)) {
227 echo "<br />Error: Could not delete: $fullfile";
231 clearfilelist();
232 displaydir($wdir);
233 html_footer();
235 } else {
236 html_header($course, $wdir);
238 if (setfilelist($_POST)) {
239 notify(get_string('deletecheckwarning').':');
240 print_simple_box_start("center");
241 printfilelist($USER->filelist);
242 print_simple_box_end();
243 echo "<br />";
245 require_once($CFG->dirroot.'/mod/resource/lib.php');
246 $block = resource_delete_warning($course, $USER->filelist);
248 if (empty($CFG->resource_blockdeletingfile) or $block == '') {
249 $optionsyes = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'choose'=>$choose);
250 $optionsno = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
251 notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
252 } else {
254 notify(get_string('warningblockingdelete', 'resource'));
255 $options = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
256 print_continue("index.php?id=$id&amp;wdir=$wdir&amp;action=cancel&amp;choose=$choose");
258 } else {
259 displaydir($wdir);
261 html_footer();
263 break;
265 case "move":
266 html_header($course, $wdir);
267 if (($count = setfilelist($_POST)) and confirm_sesskey()) {
268 $USER->fileop = $action;
269 $USER->filesource = $wdir;
270 echo "<p class=\"centerpara\">";
271 print_string("selectednowmove", "moodle", $count);
272 echo "</p>";
274 displaydir($wdir);
275 html_footer();
276 break;
278 case "paste":
279 html_header($course, $wdir);
280 if (isset($USER->fileop) and ($USER->fileop == "move") and confirm_sesskey()) {
281 foreach ($USER->filelist as $file) {
282 $shortfile = basename($file);
283 $oldfile = $basedir.'/'.$file;
284 $newfile = $basedir.$wdir."/".$shortfile;
285 if (!rename($oldfile, $newfile)) {
286 echo "<p>Error: $shortfile not moved</p>";
290 clearfilelist();
291 displaydir($wdir);
292 html_footer();
293 break;
295 case "rename":
296 if (($name != '') and confirm_sesskey()) {
297 html_header($course, $wdir);
298 $name = clean_filename($name);
299 if (file_exists($basedir.$wdir."/".$name)) {
300 echo "<center>Error: $name already exists!</center>";
301 } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) {
302 echo "<p align=\"center\">Error: could not rename $oldname to $name</p>";
303 } else {
304 //file was renamed now update resources if needed
305 require_once($CFG->dirroot.'/mod/resource/lib.php');
306 resource_renamefiles($course, $wdir, $oldname, $name);
308 displaydir($wdir);
310 } else {
311 $strrename = get_string("rename");
312 $strcancel = get_string("cancel");
313 $strrenamefileto = get_string("renamefileto", "moodle", $file);
314 html_header($course, $wdir, "form.name");
315 echo "<p>$strrenamefileto:</p>";
316 echo "<table><tr><td>";
317 echo "<form action=\"index.php\" method=\"post\">";
318 echo "<fieldset class=\"invisiblefieldset\">";
319 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
320 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
321 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
322 echo " <input type=\"hidden\" name=\"action\" value=\"rename\" />";
323 echo " <input type=\"hidden\" name=\"oldname\" value=\"$file\" />";
324 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
325 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"$file\" />";
326 echo " <input type=\"submit\" value=\"$strrename\" />";
327 echo "</fieldset>";
328 echo "</form>";
329 echo "</td><td>";
330 echo "<form action=\"index.php\" method=\"get\">";
331 echo "<div>";
332 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
333 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
334 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
335 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
336 echo " <input type=\"submit\" value=\"$strcancel\" />";
337 echo "</div>";
338 echo "</form>";
339 echo "</td></tr></table>";
341 html_footer();
342 break;
344 case "makedir":
345 if (($name != '') and confirm_sesskey()) {
346 html_header($course, $wdir);
347 $name = clean_filename($name);
348 if (file_exists("$basedir$wdir/$name")) {
349 echo "Error: $name already exists!";
350 } else if (! make_upload_directory("$course->id$wdir/$name")) {
351 echo "Error: could not create $name";
353 displaydir($wdir);
355 } else {
356 $strcreate = get_string("create");
357 $strcancel = get_string("cancel");
358 $strcreatefolder = get_string("createfolder", "moodle", $wdir);
359 html_header($course, $wdir, "form.name");
360 echo "<p>$strcreatefolder:</p>";
361 echo "<table><tr><td>";
362 echo "<form action=\"index.php\" method=\"post\">";
363 echo "<fieldset class=\"invisiblefieldset\">";
364 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
365 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
366 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
367 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
368 echo " <input type=\"text\" name=\"name\" size=\"35\" />";
369 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
370 echo " <input type=\"submit\" value=\"$strcreate\" />";
371 echo "</fieldset>";
372 echo "</form>";
373 echo "</td><td>";
374 echo "<form action=\"index.php\" method=\"get\">";
375 echo "<div>";
376 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
377 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
378 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
379 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
380 echo " <input type=\"submit\" value=\"$strcancel\" />";
381 echo "</div>";
382 echo "</form>";
383 echo "</td></tr></table>";
385 html_footer();
386 break;
388 case "edit":
389 html_header($course, $wdir);
390 if (($text != '') and confirm_sesskey()) {
391 $fileptr = fopen($basedir.'/'.$file,"w");
392 $text = preg_replace('/\x0D/', '', $text); // http://moodle.org/mod/forum/discuss.php?d=38860
393 fputs($fileptr, stripslashes($text));
394 fclose($fileptr);
395 displaydir($wdir);
397 } else {
398 $streditfile = get_string("edit", "", "<b>$file</b>");
399 $fileptr = fopen($basedir.'/'.$file, "r");
400 $contents = fread($fileptr, filesize($basedir.'/'.$file));
401 fclose($fileptr);
403 if (mimeinfo("type", $file) == "text/html") {
404 $usehtmleditor = can_use_html_editor();
405 } else {
406 $usehtmleditor = false;
408 $usehtmleditor = false; // Always keep it off for now
410 print_heading("$streditfile");
412 echo "<table><tr><td colspan=\"2\">";
413 echo "<form action=\"index.php\" method=\"post\">";
414 echo "<div>";
415 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
416 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
417 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
418 echo " <input type=\"hidden\" name=\"file\" value=\"$file\" />";
419 echo " <input type=\"hidden\" name=\"action\" value=\"edit\" />";
420 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
421 print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents);
422 echo "</td></tr><tr><td>";
423 echo " <input type=\"submit\" value=\"".get_string("savechanges")."\" />";
424 echo "</div>";
425 echo "</form>";
426 echo "</td><td>";
427 echo "<form action=\"index.php\" method=\"get\">";
428 echo "<div>";
429 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
430 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
431 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
432 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
433 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
434 echo "</div>";
435 echo "</form>";
436 echo "</td></tr></table>";
438 if ($usehtmleditor) {
439 use_html_editor();
444 html_footer();
445 break;
447 case "zip":
448 if (($name != '') and confirm_sesskey()) {
449 html_header($course, $wdir);
450 $name = clean_filename($name);
452 $files = array();
453 foreach ($USER->filelist as $file) {
454 $files[] = "$basedir/$file";
457 if (!zip_files($files,"$basedir$wdir/$name")) {
458 error(get_string("zipfileserror","error"));
461 clearfilelist();
462 displaydir($wdir);
464 } else {
465 html_header($course, $wdir, "form.name");
467 if (setfilelist($_POST)) {
468 echo "<p align=\"center\">".get_string("youareabouttocreatezip").":</p>";
469 print_simple_box_start("center");
470 printfilelist($USER->filelist);
471 print_simple_box_end();
472 echo "<br />";
473 echo "<p align=\"center\">".get_string("whattocallzip")."</p>";
474 echo "<table><tr><td>";
475 echo "<form action=\"index.php\" method=\"post\">";
476 echo "<fieldset class=\"invisiblefieldset\">";
477 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
478 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
479 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
480 echo " <input type=\"hidden\" name=\"action\" value=\"zip\" />";
481 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"new.zip\" />";
482 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
483 echo " <input type=\"submit\" value=\"".get_string("createziparchive")."\" />";
484 echo "<fieldset>";
485 echo "</form>";
486 echo "</td><td>";
487 echo "<form action=\"index.php\" method=\"get\">";
488 echo "<div>";
489 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
490 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
491 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
492 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
493 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
494 echo "</div>";
495 echo "</form>";
496 echo "</td></tr></table>";
497 } else {
498 displaydir($wdir);
499 clearfilelist();
502 html_footer();
503 break;
505 case "unzip":
506 html_header($course, $wdir);
507 if (($file != '') and confirm_sesskey()) {
508 $strok = get_string("ok");
509 $strunpacking = get_string("unpacking", "", $file);
511 echo "<p align=\"center\">$strunpacking:</p>";
513 $file = basename($file);
515 if (!unzip_file("$basedir$wdir/$file")) {
516 error(get_string("unzipfileserror","error"));
519 echo "<div style=\"text-align:center\"><form action=\"index.php\" method=\"get\">";
520 echo "<div>";
521 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
522 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
523 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
524 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
525 echo " <input type=\"submit\" value=\"$strok\" />";
526 echo "</div>";
527 echo "</form>";
528 echo "</div>";
529 } else {
530 displaydir($wdir);
532 html_footer();
533 break;
535 case "listzip":
536 html_header($course, $wdir);
537 if (($file != '') and confirm_sesskey()) {
538 $strname = get_string("name");
539 $strsize = get_string("size");
540 $strmodified = get_string("modified");
541 $strok = get_string("ok");
542 $strlistfiles = get_string("listfiles", "", $file);
544 echo "<p align=\"center\">$strlistfiles:</p>";
545 $file = basename($file);
547 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
548 $archive = new PclZip(cleardoubleslashes("$basedir$wdir/$file"));
549 if (!$list = $archive->listContent(cleardoubleslashes("$basedir$wdir"))) {
550 notify($archive->errorInfo(true));
552 } else {
553 echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\" class=\"files\">";
554 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>";
555 foreach ($list as $item) {
556 echo "<tr>";
557 print_cell("left", s($item['filename']), 'name');
558 if (! $item['folder']) {
559 print_cell("right", display_size($item['size']), 'size');
560 } else {
561 echo "<td>&nbsp;</td>";
563 $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
564 print_cell("right", $filedate, 'date');
565 echo "</tr>";
567 echo "</table>";
569 echo "<br /><center><form action=\"index.php\" method=\"get\">";
570 echo "<div>";
571 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
572 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
573 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
574 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
575 echo " <input type=\"submit\" value=\"$strok\" />";
576 echo "</div>";
577 echo "</form>";
578 echo "</center>";
579 } else {
580 displaydir($wdir);
582 html_footer();
583 break;
585 case "restore":
586 html_header($course, $wdir);
587 if (($file != '') and confirm_sesskey()) {
588 echo "<p align=\"center\">".get_string("youaregoingtorestorefrom").":</p>";
589 print_simple_box_start("center");
590 echo $file;
591 print_simple_box_end();
592 echo "<br />";
593 echo "<p align=\"center\">".get_string("areyousuretorestorethisinfo")."</p>";
594 $restore_path = "$CFG->wwwroot/backup/restore.php";
595 notice_yesno (get_string("areyousuretorestorethis"),
596 $restore_path."?id=".$id."&amp;file=".cleardoubleslashes($id.$wdir."/".$file)."&amp;method=manual",
597 "index.php?id=$id&amp;wdir=$wdir&amp;action=cancel");
598 } else {
599 displaydir($wdir);
601 html_footer();
602 break;
604 case "cancel":
605 clearfilelist();
607 default:
608 html_header($course, $wdir);
609 displaydir($wdir);
610 html_footer();
611 break;
615 /// FILE FUNCTIONS ///////////////////////////////////////////////////////////
618 function setfilelist($VARS) {
619 global $USER;
621 $USER->filelist = array ();
622 $USER->fileop = "";
624 $count = 0;
625 foreach ($VARS as $key => $val) {
626 if (substr($key,0,4) == "file") {
627 $count++;
628 $val = rawurldecode($val);
629 $USER->filelist[] = clean_param($val, PARAM_PATH);
632 return $count;
635 function clearfilelist() {
636 global $USER;
638 $USER->filelist = array ();
639 $USER->fileop = "";
643 function printfilelist($filelist) {
644 global $CFG, $basedir;
646 $strfolder = get_string("folder");
647 $strfile = get_string("file");
649 foreach ($filelist as $file) {
650 if (is_dir($basedir.'/'.$file)) {
651 echo "<img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> $file<br />";
652 $subfilelist = array();
653 $currdir = opendir($basedir.'/'.$file);
654 while (false !== ($subfile = readdir($currdir))) {
655 if ($subfile <> ".." && $subfile <> ".") {
656 $subfilelist[] = $file."/".$subfile;
659 printfilelist($subfilelist);
661 } else {
662 $icon = mimeinfo("icon", $file);
663 echo "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> $file<br />";
669 function print_cell($alignment='center', $text='&nbsp;', $class='') {
670 if ($class) {
671 $class = ' class="'.$class.'"';
673 echo '<td align="'.$alignment.'" style="white-space:nowrap "'.$class.'>'.$text.'</td>';
676 function displaydir ($wdir) {
677 // $wdir == / or /a or /a/b/c/d etc
679 global $basedir;
680 global $id;
681 global $USER, $CFG;
682 global $choose;
684 $fullpath = $basedir.$wdir;
685 $dirlist = array();
687 $directory = opendir($fullpath); // Find all files
688 while (false !== ($file = readdir($directory))) {
689 if ($file == "." || $file == "..") {
690 continue;
693 if (is_dir($fullpath."/".$file)) {
694 $dirlist[] = $file;
695 } else {
696 $filelist[] = $file;
699 closedir($directory);
701 $strname = get_string("name");
702 $strsize = get_string("size");
703 $strmodified = get_string("modified");
704 $straction = get_string("action");
705 $strmakeafolder = get_string("makeafolder");
706 $struploadafile = get_string("uploadafile");
707 $strselectall = get_string("selectall");
708 $strselectnone = get_string("deselectall");
709 $strwithchosenfiles = get_string("withchosenfiles");
710 $strmovetoanotherfolder = get_string("movetoanotherfolder");
711 $strmovefilestohere = get_string("movefilestohere");
712 $strdeletecompletely = get_string("deletecompletely");
713 $strcreateziparchive = get_string("createziparchive");
714 $strrename = get_string("rename");
715 $stredit = get_string("edit");
716 $strunzip = get_string("unzip");
717 $strlist = get_string("list");
718 $strrestore= get_string("restore");
719 $strchoose = get_string("choose");
720 $strfolder = get_string("folder");
721 $strfile = get_string("file");
724 echo "<form action=\"index.php\" method=\"post\" id=\"dirform\">";
725 echo "<div>";
726 echo '<input type="hidden" name="choose" value="'.$choose.'" />';
727 // echo "<hr align=\"center\" noshade=\"noshade\" size=\"1\" />";
728 echo "<hr/>";
729 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\" class=\"files\">";
730 echo "<tr>";
731 echo "<th scope=\"col\"></th>";
732 echo "<th class=\"header name\" scope=\"col\">$strname</th>";
733 echo "<th class=\"header size\" scope=\"col\">$strsize</th>";
734 echo "<th class=\"header date\" scope=\"col\">$strmodified</th>";
735 echo "<th class=\"header commands\" scope=\"col\">$straction</th>";
736 echo "</tr>\n";
738 if ($wdir != "/") {
739 $dirlist[] = '..';
742 $count = 0;
744 if (!empty($dirlist)) {
745 asort($dirlist);
746 foreach ($dirlist as $dir) {
747 echo "<tr class=\"folder\">";
749 if ($dir == '..') {
750 $fileurl = rawurlencode(dirname($wdir));
751 print_cell();
752 // alt attribute intentionally empty to prevent repetition in screen reader
753 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');
754 print_cell();
755 print_cell();
756 print_cell();
758 } else {
759 $count++;
760 $filename = $fullpath."/".$dir;
761 $fileurl = rawurlencode($wdir."/".$dir);
762 $filesafe = rawurlencode($dir);
763 $filesize = display_size(get_directory_size("$fullpath/$dir"));
764 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
765 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
766 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');
767 print_cell("right", $filesize, 'size');
768 print_cell("right", $filedate, 'date');
769 print_cell("right", "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename&amp;choose=$choose\">$strrename</a>", 'commands');
772 echo "</tr>";
777 if (!empty($filelist)) {
778 asort($filelist);
779 foreach ($filelist as $file) {
781 $icon = mimeinfo("icon", $file);
783 $count++;
784 $filename = $fullpath."/".$file;
785 $fileurl = trim($wdir, "/")."/$file";
786 $filesafe = rawurlencode($file);
787 $fileurlsafe = rawurlencode($fileurl);
788 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
790 $selectfile = trim($fileurl, "/");
792 echo "<tr class=\"file\">";
794 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
795 echo "<td align=\"left\" style=\"white-space:nowrap\" class=\"name\">";
796 if ($CFG->slasharguments) {
797 $ffurl = str_replace('//', '/', "/file.php/$id/$fileurl");
798 } else {
799 $ffurl = str_replace('//', '/', "/file.php?file=/$id/$fileurl");
801 link_to_popup_window ($ffurl, "display",
802 "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".htmlspecialchars($file),
803 480, 640);
804 echo "</td>";
806 $file_size = filesize($filename);
807 print_cell("right", display_size($file_size), 'size');
808 print_cell("right", $filedate, 'date');
810 if ($choose) {
811 $edittext = "<strong><a onclick=\"return set_value('$selectfile')\" href=\"#\">$strchoose</a></strong>&nbsp;";
812 } else {
813 $edittext = '';
817 if ($icon == "text.gif" || $icon == "html.gif") {
818 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=edit&amp;choose=$choose\">$stredit</a>";
819 } else if ($icon == "zip.gif") {
820 $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;";
821 $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> ";
822 if (!empty($CFG->backup_version) and has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
823 $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> ";
827 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');
829 echo "</tr>";
832 echo "</table>";
833 echo "<hr />";
834 //echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
836 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
837 echo "<tr><td>";
838 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
839 echo '<input type="hidden" name="choose" value="'.$choose.'" />';
840 echo "<input type=\"hidden\" name=\"wdir\" value=\"$wdir\" /> ";
841 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
842 $options = array (
843 "move" => "$strmovetoanotherfolder",
844 "delete" => "$strdeletecompletely",
845 "zip" => "$strcreateziparchive"
847 if (!empty($count)) {
848 choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:getElementById('dirform').submit()");
850 echo "</td></tr></table>";
851 echo "</div>";
852 echo "</form>";
853 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\"><tr>";
854 echo "<td align=\"center\">";
855 if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) {
856 echo "<form action=\"index.php\" method=\"get\">";
857 echo "<div>";
858 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
859 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
860 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
861 echo " <input type=\"hidden\" name=\"action\" value=\"paste\" />";
862 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
863 echo " <input type=\"submit\" value=\"$strmovefilestohere\" />";
864 echo "</div>";
865 echo "</form>";
867 echo "</td>";
868 echo "<td align=\"right\">";
869 echo "<form action=\"index.php\" method=\"get\">";
870 echo "<div>";
871 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
872 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
873 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
874 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
875 echo " <input type=\"submit\" value=\"$strmakeafolder\" />";
876 echo "</div>";
877 echo "</form>";
878 echo "</td>";
879 echo "<td align=\"right\">";
880 echo "<form action=\"index.php\" method=\"get\">"; //dummy form - alignment only
881 echo "<fieldset class=\"invisiblefieldset\">";
882 echo " <input type=\"button\" value=\"$strselectall\" onclick=\"checkall();\" />";
883 echo " <input type=\"button\" value=\"$strselectnone\" onclick=\"uncheckall();\" />";
884 echo "</fieldset>";
885 echo "</form>";
886 echo "</td>";
887 echo "<td align=\"right\">";
888 echo "<form action=\"index.php\" method=\"get\">";
889 echo "<div>";
890 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
891 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
892 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
893 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
894 echo " <input type=\"submit\" value=\"$struploadafile\" />";
895 echo "</div>";
896 echo "</form>";
897 echo "</td></tr>";
898 echo "</table>";
899 echo "<hr/>";
900 //echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";