"MDL-14129, fix print_error"
[moodle.git] / files / index.php
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             print_error('invalidformatpara');
29         }
30     }
33     if (! $course = get_record("course", "id", $id) ) {
34         print_error('invalidcourseid');
35     }
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);
47     }
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");
59         }
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');
76             }
77             $navlinks[] = array('name' => $dirs[$numdirs-1], 'link' => null, 'type' => 'misc');
78         }
80         $navigation = build_navigation($navlinks);
82         if ($choose) {
83             print_header();
85             $chooseparts = explode('.', $choose);
86             if (count($chooseparts)==2){
87             ?>
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();
93             }
94             //]]>
95             </script>
97             <?php
98             } elseif (count($chooseparts)==1){
99             ?>
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();
105             }
106             //]]>
107             </script>
109             <?php
111             }
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>';
120                 }
121                 $fullnav .= ' -> ';
122                 $i++;
123             }
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("publicsitefileswarning"), "center", 2);
130             }
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("publicsitefileswarning"), "center", 2);
145                 }
147             } else {
148                 print_header("$course->shortname: $strfiles", $course->fullname, $navigation,  $formfield);
149             }
150         }
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\">";
157     }
160     if (! $basedir = make_upload_directory("$course->id")) {
161         print_error("nopermissiontomkdir");
162     }
164     $baseweb = $CFG->wwwroot;
166 //  End of configuration and access control
169     if ($wdir == '') {
170         $wdir = "/";
171     }
173     if ($wdir{0} != '/') {  //make sure $wdir starts with slash
174         $wdir = "/".$wdir;
175     }
177     if ($wdir == "/backupdata") {
178         if (! make_upload_directory("$course->id/backupdata")) {   // Backup folder
179             print_error('cannotcreatebackupdir');
180         }
181     }
183     if (!is_dir($basedir.$wdir)) {
184         html_header($course, $wdir);
185         print_error('nofolder', '', "$CFG->wwwroot/files/index.php?id=$id");
186     }
188     switch ($action) {
190         case "upload":
191             html_header($course, $wdir);
192             require_once($CFG->dirroot.'/lib/uploadlib.php');
194             if ($save and confirm_sesskey()) {
195                 $course->maxbytes = 0;  // We are ignoring course limits
196                 $um = new upload_manager('userfile',false,false,$course,false,0);
197                 $dir = "$basedir$wdir";
198                 if ($um->process_file_uploads($dir)) {
199                     notify(get_string('uploadedfile'));
200                 }
201                 // um will take care of error reporting.
202                 displaydir($wdir);
203             } else {
204                 $upload_max_filesize = get_max_upload_file_size($CFG->maxbytes);
205                 $filesize = display_size($upload_max_filesize);
207                 $struploadafile = get_string("uploadafile");
208                 $struploadthisfile = get_string("uploadthisfile");
209                 $strmaxsize = get_string("maxsize", "", $filesize);
210                 $strcancel = get_string("cancel");
212                 echo "<p>$struploadafile ($strmaxsize) --> <b>$wdir</b></p>";
213                 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"index.php\">";
214                 echo "<div>";
215                 echo "<table><tr><td colspan=\"2\">";
216                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
217                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
218                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
219                 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
220                 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
221                 upload_print_form_fragment(1,array('userfile'),null,false,null,$upload_max_filesize,0,false);
222                 echo " </td></tr></table>";
223                 echo " <input type=\"submit\" name=\"save\" value=\"$struploadthisfile\" />";
224                 echo "</div>";
225                 echo "</form>";
226                 echo "<form action=\"index.php\" method=\"get\">";
227                 echo "<div>";
228                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
229                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
230                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
231                 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
232                 echo " <input type=\"submit\" value=\"$strcancel\" />";
233                 echo "</div>";
234                 echo "</form>";
235             }
236             html_footer();
237             break;
239         case "delete":
240             if ($confirm and confirm_sesskey()) {
241                 html_header($course, $wdir);
242                 if (!empty($USER->filelist)) {
243                     foreach ($USER->filelist as $file) {
244                         $fullfile = $basedir.'/'.$file;
245                         if (! fulldelete($fullfile)) {
246                             echo "<br />Error: Could not delete: $fullfile";
247                         }
248                     }
249                 }
250                 clearfilelist();
251                 displaydir($wdir);
252                 html_footer();
254             } else {
255                 html_header($course, $wdir);
257                 if (setfilelist($_POST)) {
258                     notify(get_string('deletecheckwarning').':');
259                     print_simple_box_start("center");
260                     printfilelist($USER->filelist);
261                     print_simple_box_end();
262                     echo "<br />";
264                     require_once($CFG->dirroot.'/mod/resource/lib.php');
265                     $block = resource_delete_warning($course, $USER->filelist);
267                     if (empty($CFG->resource_blockdeletingfile) or $block == '') {
268                         $optionsyes = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'choose'=>$choose);
269                         $optionsno  = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
270                         notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
271                     } else {
273                         notify(get_string('warningblockingdelete', 'resource'));
274                         $options  = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
275                         print_continue("index.php?id=$id&amp;wdir=$wdir&amp;action=cancel&amp;choose=$choose");
276                     }
277                 } else {
278                     displaydir($wdir);
279                 }
280                 html_footer();
281             }
282             break;
284         case "move":
285             html_header($course, $wdir);
286             if (($count = setfilelist($_POST)) and confirm_sesskey()) {
287                 $USER->fileop     = $action;
288                 $USER->filesource = $wdir;
289                 echo "<p class=\"centerpara\">";
290                 print_string("selectednowmove", "moodle", $count);
291                 echo "</p>";
292             }
293             displaydir($wdir);
294             html_footer();
295             break;
297         case "paste":
298             html_header($course, $wdir);
299             if (isset($USER->fileop) and ($USER->fileop == "move") and confirm_sesskey()) {
300                 foreach ($USER->filelist as $file) {
301                     $shortfile = basename($file);
302                     $oldfile = $basedir.'/'.$file;
303                     $newfile = $basedir.$wdir."/".$shortfile;
304                     if (!rename($oldfile, $newfile)) {
305                         echo "<p>Error: $shortfile not moved</p>";
306                     }
307                 }
308             }
309             clearfilelist();
310             displaydir($wdir);
311             html_footer();
312             break;
314         case "rename":
315             if (($name != '') and confirm_sesskey()) {
316                 html_header($course, $wdir);
317                 $name = clean_filename($name);
318                 if (file_exists($basedir.$wdir."/".$name)) {
319                     echo "<center>Error: $name already exists!</center>";
320                 } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) {
321                     echo "<p align=\"center\">Error: could not rename $oldname to $name</p>";
322                 } else {
323                     //file was renamed now update resources if needed
324                     require_once($CFG->dirroot.'/mod/resource/lib.php');
325                     resource_renamefiles($course, $wdir, $oldname, $name);
326                 }
327                 displaydir($wdir);
329             } else {
330                 $strrename = get_string("rename");
331                 $strcancel = get_string("cancel");
332                 $strrenamefileto = get_string("renamefileto", "moodle", $file);
333                 html_header($course, $wdir, "form.name");
334                 echo "<p>$strrenamefileto:</p>";
335                 echo "<table><tr><td>";
336                 echo "<form action=\"index.php\" method=\"post\">";
337                 echo "<fieldset class=\"invisiblefieldset\">";
338                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
339                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
340                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
341                 echo " <input type=\"hidden\" name=\"action\" value=\"rename\" />";
342                 echo " <input type=\"hidden\" name=\"oldname\" value=\"$file\" />";
343                 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
344                 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"$file\" />";
345                 echo " <input type=\"submit\" value=\"$strrename\" />";
346                 echo "</fieldset>";
347                 echo "</form>";
348                 echo "</td><td>";
349                 echo "<form action=\"index.php\" method=\"get\">";
350                 echo "<div>";
351                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
352                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
353                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
354                 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
355                 echo " <input type=\"submit\" value=\"$strcancel\" />";
356                 echo "</div>";
357                 echo "</form>";
358                 echo "</td></tr></table>";
359             }
360             html_footer();
361             break;
363         case "makedir":
364             if (($name != '') and confirm_sesskey()) {
365                 html_header($course, $wdir);
366                 $name = clean_filename($name);
367                 if (file_exists("$basedir$wdir/$name")) {
368                     echo "Error: $name already exists!";
369                 } else if (! make_upload_directory("$course->id$wdir/$name")) {
370                     echo "Error: could not create $name";
371                 }
372                 displaydir($wdir);
374             } else {
375                 $strcreate = get_string("create");
376                 $strcancel = get_string("cancel");
377                 $strcreatefolder = get_string("createfolder", "moodle", $wdir);
378                 html_header($course, $wdir, "form.name");
379                 echo "<p>$strcreatefolder:</p>";
380                 echo "<table><tr><td>";
381                 echo "<form action=\"index.php\" method=\"post\">";
382                 echo "<fieldset class=\"invisiblefieldset\">";
383                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
384                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
385                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
386                 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
387                 echo " <input type=\"text\" name=\"name\" size=\"35\" />";
388                 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
389                 echo " <input type=\"submit\" value=\"$strcreate\" />";
390                 echo "</fieldset>";
391                 echo "</form>";
392                 echo "</td><td>";
393                 echo "<form action=\"index.php\" method=\"get\">";
394                 echo "<div>";
395                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
396                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
397                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
398                 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
399                 echo " <input type=\"submit\" value=\"$strcancel\" />";
400                 echo "</div>";
401                 echo "</form>";
402                 echo "</td></tr></table>";
403             }
404             html_footer();
405             break;
407         case "edit":
408             html_header($course, $wdir);
409             if (($text != '') and confirm_sesskey()) {
410                 $fileptr = fopen($basedir.'/'.$file,"w");
411                 $text = preg_replace('/\x0D/', '', $text);  // http://moodle.org/mod/forum/discuss.php?d=38860
412                 fputs($fileptr, stripslashes($text));
413                 fclose($fileptr);
414                 displaydir($wdir);
416             } else {
417                 $streditfile = get_string("edit", "", "<b>$file</b>");
418                 $fileptr  = fopen($basedir.'/'.$file, "r");
419                 $contents = fread($fileptr, filesize($basedir.'/'.$file));
420                 fclose($fileptr);
422                 if (mimeinfo("type", $file) == "text/html") {
423                     $usehtmleditor = can_use_html_editor();
424                 } else {
425                     $usehtmleditor = false;
426                 }
427                 $usehtmleditor = false;    // Always keep it off for now
429                 print_heading("$streditfile");
431                 echo "<table><tr><td colspan=\"2\">";
432                 echo "<form action=\"index.php\" method=\"post\">";
433                 echo "<div>";
434                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
435                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
436                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
437                 echo " <input type=\"hidden\" name=\"file\" value=\"$file\" />";
438                 echo " <input type=\"hidden\" name=\"action\" value=\"edit\" />";
439                 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
440                 print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents);
441                 echo "</td></tr><tr><td>";
442                 echo " <input type=\"submit\" value=\"".get_string("savechanges")."\" />";
443                 echo "</div>";
444                 echo "</form>";
445                 echo "</td><td>";
446                 echo "<form action=\"index.php\" method=\"get\">";
447                 echo "<div>";
448                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
449                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
450                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
451                 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
452                 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
453                 echo "</div>";
454                 echo "</form>";
455                 echo "</td></tr></table>";
457                 if ($usehtmleditor) {
458                     use_html_editor();
459                 }
462             }
463             html_footer();
464             break;
466         case "zip":
467             if (($name != '') and confirm_sesskey()) {
468                 html_header($course, $wdir);
469                 $name = clean_filename($name);
471                 $files = array();
472                 foreach ($USER->filelist as $file) {
473                    $files[] = "$basedir/$file";
474                 }
476                 if (!zip_files($files,"$basedir$wdir/$name")) {
477                     print_error("zipfileserror", "error");
478                 }
480                 clearfilelist();
481                 displaydir($wdir);
483             } else {
484                 html_header($course, $wdir, "form.name");
486                 if (setfilelist($_POST)) {
487                     echo "<p align=\"center\">".get_string("youareabouttocreatezip").":</p>";
488                     print_simple_box_start("center");
489                     printfilelist($USER->filelist);
490                     print_simple_box_end();
491                     echo "<br />";
492                     echo "<p align=\"center\">".get_string("whattocallzip")."</p>";
493                     echo "<table><tr><td>";
494                     echo "<form action=\"index.php\" method=\"post\">";
495                     echo "<fieldset class=\"invisiblefieldset\">";
496                     echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
497                     echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
498                     echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
499                     echo " <input type=\"hidden\" name=\"action\" value=\"zip\" />";
500                     echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"new.zip\" />";
501                     echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
502                     echo " <input type=\"submit\" value=\"".get_string("createziparchive")."\" />";
503                     echo "<fieldset>";
504                     echo "</form>";
505                     echo "</td><td>";
506                     echo "<form action=\"index.php\" method=\"get\">";
507                     echo "<div>";
508                     echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
509                     echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
510                     echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
511                     echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
512                     echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
513                     echo "</div>";
514                     echo "</form>";
515                     echo "</td></tr></table>";
516                 } else {
517                     displaydir($wdir);
518                     clearfilelist();
519                 }
520             }
521             html_footer();
522             break;
524         case "unzip":
525             html_header($course, $wdir);
526             if (($file != '') and confirm_sesskey()) {
527                 $strok = get_string("ok");
528                 $strunpacking = get_string("unpacking", "", $file);
530                 echo "<p align=\"center\">$strunpacking:</p>";
532                 $file = basename($file);
534                 if (!unzip_file("$basedir$wdir/$file")) {
535                     print_error("unzipfileserror", "error");
536                 }
538                 echo "<div style=\"text-align:center\"><form action=\"index.php\" method=\"get\">";
539                 echo "<div>";
540                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
541                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
542                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
543                 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
544                 echo " <input type=\"submit\" value=\"$strok\" />";
545                 echo "</div>";
546                 echo "</form>";
547                 echo "</div>";
548             } else {
549                 displaydir($wdir);
550             }
551             html_footer();
552             break;
554         case "listzip":
555             html_header($course, $wdir);
556             if (($file != '') and confirm_sesskey()) {
557                 $strname = get_string("name");
558                 $strsize = get_string("size");
559                 $strmodified = get_string("modified");
560                 $strok = get_string("ok");
561                 $strlistfiles = get_string("listfiles", "", $file);
563                 echo "<p align=\"center\">$strlistfiles:</p>";
564                 $file = basename($file);
566                 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
567                 $archive = new PclZip(cleardoubleslashes("$basedir$wdir/$file"));
568                 if (!$list = $archive->listContent(cleardoubleslashes("$basedir$wdir"))) {
569                     notify($archive->errorInfo(true));
571                 } else {
572                     echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\" class=\"files\">";
573                     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>";
574                     foreach ($list as $item) {
575                         echo "<tr>";
576                         print_cell("left", s($item['filename']), 'name');
577                         if (! $item['folder']) {
578                             print_cell("right", display_size($item['size']), 'size');
579                         } else {
580                             echo "<td>&nbsp;</td>";
581                         }
582                         $filedate  = userdate($item['mtime'], get_string("strftimedatetime"));
583                         print_cell("right", $filedate, 'date');
584                         echo "</tr>";
585                     }
586                     echo "</table>";
587                 }
588                 echo "<br /><center><form action=\"index.php\" method=\"get\">";
589                 echo "<div>";
590                 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
591                 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
592                 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
593                 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
594                 echo " <input type=\"submit\" value=\"$strok\" />";
595                 echo "</div>";
596                 echo "</form>";
597                 echo "</center>";
598             } else {
599                 displaydir($wdir);
600             }
601             html_footer();
602             break;
604         case "restore":
605             html_header($course, $wdir);
606             if (($file != '') and confirm_sesskey()) {
607                 echo "<p align=\"center\">".get_string("youaregoingtorestorefrom").":</p>";
608                 print_simple_box_start("center");
609                 echo $file;
610                 print_simple_box_end();
611                 echo "<br />";
612                 echo "<p align=\"center\">".get_string("areyousuretorestorethisinfo")."</p>";
613                 $restore_path = "$CFG->wwwroot/backup/restore.php";
614                 notice_yesno (get_string("areyousuretorestorethis"),
615                                 $restore_path."?id=".$id."&amp;file=".cleardoubleslashes($id.$wdir."/".$file)."&amp;method=manual",
616                                 "index.php?id=$id&amp;wdir=$wdir&amp;action=cancel");
617             } else {
618                 displaydir($wdir);
619             }
620             html_footer();
621             break;
623         case "cancel":
624             clearfilelist();
626         default:
627             html_header($course, $wdir);
628             displaydir($wdir);
629             html_footer();
630             break;
634 /// FILE FUNCTIONS ///////////////////////////////////////////////////////////
637 function setfilelist($VARS) {
638     global $USER;
640     $USER->filelist = array ();
641     $USER->fileop = "";
643     $count = 0;
644     foreach ($VARS as $key => $val) {
645         if (substr($key,0,4) == "file") {
646             $count++;
647             $val = rawurldecode($val);
648             $USER->filelist[] = clean_param($val, PARAM_PATH);
649         }
650     }
651     return $count;
654 function clearfilelist() {
655     global $USER;
657     $USER->filelist = array ();
658     $USER->fileop = "";
662 function printfilelist($filelist) {
663     global $CFG, $basedir;
665     $strfolder = get_string("folder");
666     $strfile   = get_string("file");
668     foreach ($filelist as $file) {
669         if (is_dir($basedir.'/'.$file)) {
670             echo '<img src="'. $CFG->pixpath .'/f/folder.gif" class="icon" alt="'. $strfolder .'" /> '. htmlspecialchars($file) .'<br />';
671             $subfilelist = array();
672             $currdir = opendir($basedir.'/'.$file);
673             while (false !== ($subfile = readdir($currdir))) {
674                 if ($subfile <> ".." && $subfile <> ".") {
675                     $subfilelist[] = $file."/".$subfile;
676                 }
677             }
678             printfilelist($subfilelist);
680         } else {
681             $icon = mimeinfo("icon", $file);
682             echo '<img src="'. $CFG->pixpath .'/f/'. $icon .'" class="icon" alt="'. $strfile .'" /> '. htmlspecialchars($file) .'<br />';
683         }
684     }
688 function print_cell($alignment='center', $text='&nbsp;', $class='') {
689     if ($class) {
690         $class = ' class="'.$class.'"';
691     }
692     echo '<td align="'.$alignment.'" style="white-space:nowrap "'.$class.'>'.$text.'</td>';
695 function displaydir ($wdir) {
696 //  $wdir == / or /a or /a/b/c/d  etc
698     global $basedir;
699     global $id;
700     global $USER, $CFG;
701     global $choose;
703     $fullpath = $basedir.$wdir;
704     $dirlist = array();
706     $directory = opendir($fullpath);             // Find all files
707     while (false !== ($file = readdir($directory))) {
708         if ($file == "." || $file == "..") {
709             continue;
710         }
712         if (is_dir($fullpath."/".$file)) {
713             $dirlist[] = $file;
714         } else {
715             $filelist[] = $file;
716         }
717     }
718     closedir($directory);
720     $strname = get_string("name");
721     $strsize = get_string("size");
722     $strmodified = get_string("modified");
723     $straction = get_string("action");
724     $strmakeafolder = get_string("makeafolder");
725     $struploadafile = get_string("uploadafile");
726     $strselectall = get_string("selectall");
727     $strselectnone = get_string("deselectall");
728     $strwithchosenfiles = get_string("withchosenfiles");
729     $strmovetoanotherfolder = get_string("movetoanotherfolder");
730     $strmovefilestohere = get_string("movefilestohere");
731     $strdeletecompletely = get_string("deletecompletely");
732     $strcreateziparchive = get_string("createziparchive");
733     $strrename = get_string("rename");
734     $stredit   = get_string("edit");
735     $strunzip  = get_string("unzip");
736     $strlist   = get_string("list");
737     $strrestore= get_string("restore");
738     $strchoose = get_string("choose");
739     $strfolder = get_string("folder");
740     $strfile   = get_string("file");
743     echo "<form action=\"index.php\" method=\"post\" id=\"dirform\">";
744     echo "<div>";
745     echo '<input type="hidden" name="choose" value="'.$choose.'" />';
746     // echo "<hr align=\"center\" noshade=\"noshade\" size=\"1\" />";
747     echo "<hr/>";
748     echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\" class=\"files\">";
749     echo "<tr>";
750     echo "<th class=\"header\" scope=\"col\"></th>";
751     echo "<th class=\"header name\" scope=\"col\">$strname</th>";
752     echo "<th class=\"header size\" scope=\"col\">$strsize</th>";
753     echo "<th class=\"header date\" scope=\"col\">$strmodified</th>";
754     echo "<th class=\"header commands\" scope=\"col\">$straction</th>";
755     echo "</tr>\n";
757     if ($wdir != "/") {
758         $dirlist[] = '..';
759     }
761     $count = 0;
763     if (!empty($dirlist)) {
764         asort($dirlist);
765         foreach ($dirlist as $dir) {
766             echo "<tr class=\"folder\">";
768             if ($dir == '..') {
769                 $fileurl = rawurlencode(dirname($wdir));
770                 print_cell();
771                 // alt attribute intentionally empty to prevent repetition in screen reader
772                 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');
773                 print_cell();
774                 print_cell();
775                 print_cell();
777             } else {
778                 $count++;
779                 $filename = $fullpath."/".$dir;
780                 $fileurl  = rawurlencode($wdir."/".$dir);
781                 $filesafe = rawurlencode($dir);
782                 $filesize = display_size(get_directory_size("$fullpath/$dir"));
783                 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
784                 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
785                 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');
786                 print_cell("right", $filesize, 'size');
787                 print_cell("right", $filedate, 'date');
788                 print_cell("right", "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename&amp;choose=$choose\">$strrename</a>", 'commands');
789             }
791             echo "</tr>";
792         }
793     }
796     if (!empty($filelist)) {
797         asort($filelist);
798         foreach ($filelist as $file) {
800             $icon = mimeinfo("icon", $file);
802             $count++;
803             $filename    = $fullpath."/".$file;
804             $fileurl     = trim($wdir, "/")."/$file";
805             $filesafe    = rawurlencode($file);
806             $fileurlsafe = rawurlencode($fileurl);
807             $filedate    = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
809             $selectfile = trim($fileurl, "/");
811             echo "<tr class=\"file\">";
813             print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
814             echo "<td align=\"left\" style=\"white-space:nowrap\" class=\"name\">";
815             if ($CFG->slasharguments) {
816                 $ffurl = str_replace('//', '/', "/file.php/$id/$fileurl");
817             } else {
818                 $ffurl = str_replace('//', '/', "/file.php?file=/$id/$fileurl");
819             }
820             link_to_popup_window ($ffurl, "display",
821                                   "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".htmlspecialchars($file),
822                                   480, 640);
823             echo "</td>";
825             $file_size = filesize($filename);
826             print_cell("right", display_size($file_size), 'size');
827             print_cell("right", $filedate, 'date');
829             if ($choose) {
830                 $edittext = "<strong><a onclick=\"return set_value('$selectfile')\" href=\"#\">$strchoose</a></strong>&nbsp;";
831             } else {
832                 $edittext = '';
833             }
836             if ($icon == "text.gif" || $icon == "html.gif") {
837                 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=edit&amp;choose=$choose\">$stredit</a>";
838             } else if ($icon == "zip.gif") {
839                 $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;";
840                 $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> ";
841                 if (!empty($CFG->backup_version) and has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
842                     $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> ";
843                 }
844             }
846             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');
848             echo "</tr>";
849         }
850     }
851     echo "</table>";
852     echo "<hr />";
853     //echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
855     echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
856     echo "<tr><td>";
857     echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
858     echo '<input type="hidden" name="choose" value="'.$choose.'" />';
859     echo "<input type=\"hidden\" name=\"wdir\" value=\"$wdir\" /> ";
860     echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
861     $options = array (
862                    "move" => "$strmovetoanotherfolder",
863                    "delete" => "$strdeletecompletely",
864                    "zip" => "$strcreateziparchive"
865                );
866     if (!empty($count)) {
868         choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:getElementById('dirform').submit()");
869         echo '<div id="noscriptgo" style="display: inline;">';
870         echo '<input type="submit" value="'.get_string('go').'" /></div>';
871         echo '<script type="text/javascript">'.
872                "\n//<![CDATA[\n".
873                'document.getElementById("noscriptgo").style.display = "none";'.
874                "\n//]]>\n".'</script>';
875         echo '</div>';
877     }
878     echo "</td></tr></table>";
879     echo "</div>";
880     echo "</form>";
881     echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\"><tr>";
882     echo "<td align=\"center\">";
883     if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) {
884         echo "<form action=\"index.php\" method=\"get\">";
885         echo "<div>";
886         echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
887         echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
888         echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
889         echo " <input type=\"hidden\" name=\"action\" value=\"paste\" />";
890         echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
891         echo " <input type=\"submit\" value=\"$strmovefilestohere\" />";
892         echo "</div>";
893         echo "</form>";
894     }
895     echo "</td>";
896     echo "<td align=\"right\">";
897         echo "<form action=\"index.php\" method=\"get\">";
898         echo "<div>";
899         echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
900         echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
901         echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
902         echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
903         echo " <input type=\"submit\" value=\"$strmakeafolder\" />";
904         echo "</div>";
905         echo "</form>";
906     echo "</td>";
907     echo "<td align=\"right\">";
908         echo "<form action=\"index.php\" method=\"get\">"; //dummy form - alignment only
909         echo "<fieldset class=\"invisiblefieldset\">";
910         echo " <input type=\"button\" value=\"$strselectall\" onclick=\"checkall();\" />";
911         echo " <input type=\"button\" value=\"$strselectnone\" onclick=\"uncheckall();\" />";
912         echo "</fieldset>";
913         echo "</form>";
914     echo "</td>";
915     echo "<td align=\"right\">";
916         echo "<form action=\"index.php\" method=\"get\">";
917         echo "<div>";
918         echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
919         echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
920         echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
921         echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
922         echo " <input type=\"submit\" value=\"$struploadafile\" />";
923         echo "</div>";
924         echo "</form>";
925     echo "</td></tr>";
926     echo "</table>";
927     echo "<hr/>";
928     //echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
932 ?>