Putting SCORM into maintree CVS, to make it easier to test.
[moodle.git] / mod / scorm / coursefiles.php
1 <?PHP // $Id$
3 //  Manage ONLY ZIP & PIF uploaded files in a course file area
5 //  This file is a hack to files/index.php that removes
6 //  the headers and adds some controls so that images
7 //  can be selected within the Richtext editor.  
9 //  All the Moodle-specific stuff is in this top section
10 //  Configuration and access control occurs here.
11 //  Must define:  USER, basedir, baseweb, html_header and html_footer
12 //  USER is a persistent variable using sessions
14     require("../../config.php");
15     require("../../files/mimetypes.php");
17     require_variable($id);
18     optional_variable($file, "");
19     optional_variable($wdir, "");
20     optional_variable($action, "");
22     if (! $course = get_record("course", "id", $id) ) {
23         error("That's an invalid course id");
24     }
26     require_login($course->id);
28     if (! isteacher($course->id) ) {
29         error("Only teachers can edit files");
30     }
32     function html_footer() {
33         echo "</td></tr></table></body></html>";
34     }
35     
36     function html_header($course, $wdir, $formfield=""){
38         global $CFG,$THEME;
40         if (! $site = get_site()) {
41             error("Invalid site!");
42         }
44         if ($course->id == $site->id) {
45             $strfiles = get_string("sitefiles");
46         } else {
47             $strfiles = get_string("files");
48         }
49     
50         if ($wdir == "/") {
51             $fullnav = "$strfiles";
52         } else {
53             $dirs = explode("/", $wdir);
54             $numdirs = count($dirs);
55             $link = "";
56             $navigation = "";
57             for ($i=1; $i<$numdirs; $i++) {
58                $navigation .= " -> ";
59                $link .= "/".urlencode($dirs[$i]);
60                $navigation .= "<a href=\"".$_SERVER['PHP_SELF']."?id=$course->id&wdir=$link\">".$dirs[$i]."</a>";
61             }
62             $fullnav = "<a href=\"".$_SERVER['PHP_SELF']."?id=$course->id&wdir=/\">$strfiles</a> $navigation";
63         }
65         print_header();
66         ?>
67             <script language="javascript" type="text/javascript">
68             <!--
69             function set_value(txt) {
70                     opener.document.forms['form'].reference.value = txt;
71                     window.close();
72             }
73         -->
74             </script>
75         <?php
77         echo '<table border="0" cellpadding="3" cellspacing="0" width="100%">';
78         echo '<tr>';
79         echo '<td bgcolor="'.$THEME->cellheading.'" class="navbar">';
80         echo '<font size="2"><b>'."$course->shortname -> $fullnav".'</b></font>';
81         echo '</td>';
82         echo '</tr>';
83         echo '</table>';
85         if ($course->id == $site->id) {
86             print_heading(get_string("publicsitefileswarning"), "center", 2);
87         }
89         echo "<table border=0 align=center cellspacing=3 cellpadding=3 width=640>";
90         echo "<tr>";
91         echo "<td colspan=\"2\">";
92     }
94     if (! $basedir = make_upload_directory("$course->id")) {
95         error("The site administrator needs to fix the file permissions");
96     }
98     $baseweb = $CFG->wwwroot;
100 //  End of configuration and access control
103     $regexp="\\.\\.";
104     if (ereg( $regexp, $file, $regs )| ereg( $regexp, $wdir,$regs )) {           
105         $message = "Error: Directories can not contain \"..\"";
106         $wdir = "/";
107         $action = "";
108     }    
110     if (!$wdir) {
111         $wdir="/";
112     }
115     switch ($action) {
117         case "upload":
118             html_header($course, $wdir);
120             if (!empty($_FILES['userfile'])) {
121                 $userfile = $_FILES['userfile'];
122             } else {
123                 $save = false;
124             }
125             if (!empty($save)) {
126                 if (!is_uploaded_file($userfile['tmp_name']) or $userfile['size'] == 0) {
127                     notify(get_string("uploadnofilefound"));
128                 } else {
129                     $userfile_name = clean_filename($userfile['name']);
130                     if ($userfile_name) {
131                         $newfile = "$basedir$wdir/$userfile_name";
132                         if (move_uploaded_file($userfile['tmp_name'], $newfile)) {
133                             chmod($newfile, 0666);
134                             $a = NULL;
135                             $a->file = "$userfile_name (".$userfile['type'].")";
136                             $a->directory = $wdir;
137                             print_string("uploadedfileto", "", $a);
138                         } else {
139                             notify(get_string("uploadproblem", "", $userfile_name));
140                         }
141                     }
142                 }
143                 displaydir($wdir);
144                     
145             } else {
146                 $upload_max_filesize = get_max_upload_file_size($CFG->maxbytes);
147                 $filesize = display_size($upload_max_filesize);
149                 $struploadafile = get_string("uploadafile");
150                 $struploadthisfile = get_string("uploadthisfile");
151                 $strmaxsize = get_string("maxsize", "", $filesize);
152                 $strcancel = get_string("cancel");
154                 echo "<P>$struploadafile ($strmaxsize) --> <B>$wdir</B>";
155                 echo "<TABLE><TR><TD COLSPAN=2>";
156                 echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"post\" ACTION=\"".$_SERVER['PHP_SELF']."\">";
157                 echo " <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=\"$upload_max_filesize\">";
158                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
159                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
160                 echo " <INPUT TYPE=hidden NAME=action VALUE=upload>";
161                 echo " <INPUT NAME=\"userfile\" TYPE=\"file\" size=\"60\">";
162                 echo " </TD><TR><TD WIDTH=10>";
163                 echo " <INPUT TYPE=submit NAME=save VALUE=\"$struploadthisfile\">";
164                 echo "</FORM>";
165                 echo "</TD><TD WIDTH=100%>";
166                 echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=\"get\">";
167                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
168                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
169                 echo " <INPUT TYPE=hidden NAME=action VALUE=cancel>";
170                 echo " <INPUT TYPE=submit VALUE=\"$strcancel\">";
171                 echo "</FORM>";
172                 echo "</TD></TR></TABLE>";
173             }
174             html_footer();
175             break;
177         case "delete":
178             if (!empty($confirm)) {
179                 html_header($course, $wdir);
180                 foreach ($USER->filelist as $file) {
181                     $fullfile = $basedir.$file;
182                     if (! fulldelete($fullfile)) {
183                         echo "<BR>Error: Could not delete: $fullfile";
184                     }
185                 }
186                 clearfilelist();
187                 displaydir($wdir);
188                 html_footer();
190             } else {
191                 html_header($course, $wdir);
192                 if (setfilelist($_POST)) {
193                     echo "<p align=center>".get_string("deletecheckwarning").":</p>";
194                     print_simple_box_start("center");
195                     printfilelist($USER->filelist);
196                     print_simple_box_end();
197                     echo "<br />";
198                     notice_yesno (get_string("deletecheckfiles"), 
199                                 "".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&action=delete&confirm=1",
200                                 "".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&action=cancel");
201                 } else {
202                     displaydir($wdir);
203                 }
204                 html_footer();
205             }
206             break;
208         case "move":
209             html_header($course, $wdir);
210             if ($count = setfilelist($_POST)) {
211                 $USER->fileop     = $action;
212                 $USER->filesource = $wdir;
213                 echo "<p align=center>";
214                 print_string("selectednowmove", "moodle", $count);
215                 echo "</p>";
216             }
217             displaydir($wdir);
218             html_footer();
219             break;
221         case "paste":
222             html_header($course, $wdir);
223             if (isset($USER->fileop) and $USER->fileop == "move") {
224                 foreach ($USER->filelist as $file) {
225                     $shortfile = basename($file);
226                     $oldfile = $basedir.$file;
227                     $newfile = $basedir.$wdir."/".$shortfile;
228                     if (!rename($oldfile, $newfile)) {
229                         echo "<P>Error: $shortfile not moved";
230                     }
231                 }
232             }
233             clearfilelist();
234             displaydir($wdir);
235             html_footer();
236             break;
238         case "rename":
239             if (!empty($name)) {
240                 html_header($course, $wdir);
241                 $name = clean_filename($name);
242                 if (file_exists($basedir.$wdir."/".$name)) {
243                     echo "Error: $name already exists!";
244                 } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) {
245                     echo "Error: could not rename $oldname to $name";
246                 }
247                 displaydir($wdir);
248                     
249             } else {
250                 $strrename = get_string("rename");
251                 $strcancel = get_string("cancel");
252                 $strrenamefileto = get_string("renamefileto", "moodle", $file);
253                 html_header($course, $wdir, "form.name");
254                 echo "<P>$strrenamefileto:";
255                 echo "<TABLE><TR><TD>";
256                 echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=\"post\" NAME=\"form\">";
257                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
258                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
259                 echo " <INPUT TYPE=hidden NAME=action VALUE=rename>";
260                 echo " <INPUT TYPE=hidden NAME=oldname VALUE=\"$file\">";
261                 echo " <INPUT TYPE=text NAME=name SIZE=35 VALUE=\"$file\">";
262                 echo " <INPUT TYPE=submit VALUE=\"$strrename\">";
263                 echo "</FORM>";
264                 echo "</TD><TD>";
265                 echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=get>";
266                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
267                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
268                 echo " <INPUT TYPE=hidden NAME=action VALUE=cancel>";
269                 echo " <INPUT TYPE=submit VALUE=\"$strcancel\">";
270                 echo "</FORM>";
271                 echo "</TD></TR></TABLE>";
272             }
273             html_footer();
274             break;
276         case "mkdir":
277             if (!empty($name)) {
278                 html_header($course, $wdir);
279                 $name = clean_filename($name);
280                 if (file_exists("$basedir$wdir/$name")) {
281                     echo "Error: $name already exists!";
282                 } else if (! make_upload_directory("$course->id/$wdir/$name")) {
283                     echo "Error: could not create $name";
284                 }
285                 displaydir($wdir);
286                     
287             } else {
288                 $strcreate = get_string("create");
289                 $strcancel = get_string("cancel");
290                 $strcreatefolder = get_string("createfolder", "moodle", $wdir);
291                 html_header($course, $wdir, "form.name");
292                 echo "<P>$strcreatefolder:";
293                 echo "<TABLE><TR><TD>";
294                 echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=post NAME=form>";
295                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
296                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
297                 echo " <INPUT TYPE=hidden NAME=action VALUE=mkdir>";
298                 echo " <INPUT TYPE=text NAME=name SIZE=35>";
299                 echo " <INPUT TYPE=submit VALUE=\"$strcreate\">";
300                 echo "</FORM>";
301                 echo "</TD><TD>";
302                 echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=get>";
303                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
304                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
305                 echo " <INPUT TYPE=hidden NAME=action VALUE=cancel>";
306                 echo " <INPUT TYPE=submit VALUE=\"$strcancel\">";
307                 echo "</FORM>";
308                 echo "</TD></TR></TABLE>";
309             }
310             html_footer();
311             break;
313         case "edit":
314             html_header($course, $wdir);
315             if (isset($text)) {
316                 $fileptr = fopen($basedir.$file,"w");
317                 fputs($fileptr, stripslashes($text));
318                 fclose($fileptr);
319                 displaydir($wdir);
320                     
321             } else {
322                 $streditfile = get_string("edit", "", "<B>$file</B>");
323                 $fileptr  = fopen($basedir.$file, "r");
324                 $contents = fread($fileptr, filesize($basedir.$file));
325                 fclose($fileptr);
327                 if (mimeinfo("type", $file) == "text/html") {
328                     if ($usehtmleditor = can_use_richtext_editor()) {
329                         $onsubmit = "onsubmit=\"copyrichtext(document.form.text);\"";
330                     } else {
331                         $onsubmit = "";
332                     }
333                 } else {
334                     $usehtmleditor = false;
335                     $onsubmit = "";
336                 }
338                 print_heading("$streditfile");
340                 echo "<TABLE><TR><TD COLSPAN=2>";
341                 echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=\"post\" NAME=\"form\" $onsubmit>";
342                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
343                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=\"$wdir\">";
344                 echo " <INPUT TYPE=hidden NAME=file VALUE=\"$file\">";
345                 echo " <INPUT TYPE=hidden NAME=action VALUE=edit>";
346                 print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents);
347                 echo "</TD></TR><TR><TD>";
348                 echo " <INPUT TYPE=submit VALUE=\"".get_string("savechanges")."\">";
349                 echo "</FORM>";
350                 echo "</TD><TD>";
351                 echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=get>";
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=\"".get_string("cancel")."\">";
356                 echo "</FORM>";
357                 echo "</TD></TR></TABLE>";
359                 if ($usehtmleditor) { 
360                     print_richedit_javascript("form", "text", "yes");
361                 }
364             }
365             html_footer();
366             break;
368         case "zip":
369             if (!empty($name)) {
370                 html_header($course, $wdir);
371                 $name = clean_filename($name);
372                 if (empty($CFG->zip)) {    // Use built-in php-based zip function
373                     $files = array();
374                     foreach ($USER->filelist as $file) {
375                         $files[] = "$basedir/$file";
376                     }
377                     include_once($CFG->libdir.'/pclzip/pclzip.lib.php');
378                     $archive = new PclZip("$basedir/$wdir/$name");
379                     if (($list = $archive->create($files,'',"$basedir/$wdir/")) == 0) {
380                         error($archive->errorInfo(true));
381                     }
382                 } else {                   // Use external zip program
383                     $files = "";
384                     foreach ($USER->filelist as $file) {
385                         $files .= basename($file);
386                         $files .= " ";
387                     }
388                     $command = "cd $basedir/$wdir ; $CFG->zip -r $name $files";
389                     Exec($command);
390                 }
391                 clearfilelist();
392                 displaydir($wdir);
393                     
394             } else {
395                 html_header($course, $wdir, "form.name");
397                 if (setfilelist($_POST)) {
398                     echo "<P ALIGN=CENTER>".get_string("youareabouttocreatezip").":</P>";
399                     print_simple_box_start("center");
400                     printfilelist($USER->filelist);
401                     print_simple_box_end();
402                     echo "<BR>";
403                     echo "<P ALIGN=CENTER>".get_string("whattocallzip");
404                     echo "<TABLE><TR><TD>";
405                     echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=post NAME=form>";
406                     echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
407                     echo " <INPUT TYPE=hidden NAME=wdir VALUE=\"$wdir\">";
408                     echo " <INPUT TYPE=hidden NAME=action VALUE=zip>";
409                     echo " <INPUT TYPE=text NAME=name SIZE=35 VALUE=\"new.zip\">";
410                     echo " <INPUT TYPE=submit VALUE=\"".get_string("createziparchive")."\">";
411                     echo "</FORM>";
412                     echo "</TD><TD>";
413                     echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=get>";
414                     echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
415                     echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
416                     echo " <INPUT TYPE=hidden NAME=action VALUE=cancel>";
417                     echo " <INPUT TYPE=submit VALUE=\"".get_string("cancel")."\">";
418                     echo "</FORM>";
419                     echo "</TD></TR></TABLE>";
420                 } else {
421                     displaydir($wdir);
422                     clearfilelist();
423                 }
424             }
425             html_footer();
426             break;
428         case "unzip":
429             html_header($course, $wdir);
430             if (!empty($file)) {
431                 $strname = get_string("name");
432                 $strsize = get_string("size");
433                 $strmodified = get_string("modified");
434                 $strstatus = get_string("status");
435                 $strok = get_string("ok");
436                 $strunpacking = get_string("unpacking", "", $file);
438                 echo "<P ALIGN=CENTER>$strunpacking:</P>";
440                 $file = basename($file);
442                 if (empty($CFG->unzip)) {    // Use built-in php-based unzip function
443                     include_once($CFG->libdir.'/pclzip/pclzip.lib.php');
444                     $archive = new PclZip("$basedir/$wdir/$file");
445                     if (!$list = $archive->extract("$basedir/$wdir")) {
446                         error($archive->errorInfo(true));
447                     } else {  // print some output
448                         echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=640>";
449                         echo "<tr><th align=left>$strname</th>";
450                         echo "<th align=right>$strsize</th>";
451                         echo "<th align=right>$strmodified</th>";
452                         echo "<th align=right>$strstatus</th></tr>";
453                         foreach ($list as $item) {
454                             echo "<tr>";
455                             $item['filename'] = str_replace("$basedir/$wdir/", "", $item['filename']);
456                             print_cell("left", $item['filename']);
457                             if (! $item['folder']) {
458                                 print_cell("right", display_size($item['size']));
459                             } else {
460                                 echo "<td>&nbsp;</td>";
461                             }
462                             $filedate  = userdate($item['mtime'], get_string("strftimedatetime"));
463                             print_cell("right", $filedate);
464                             print_cell("right", $item['status']);
465                             echo "</tr>";
466                         }
467                         echo "</table>";
468                     }
469                     
470                 } else {                     // Use external unzip program
471                     print_simple_box_start("center");
472                     echo "<PRE>";
473                     $command = "cd $basedir/$wdir ; $CFG->unzip -o $file 2>&1";
474                     passthru($command);
475                     echo "</PRE>";
476                     print_simple_box_end();
477                 }
479                 echo "<CENTER><FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=get>";
480                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
481                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
482                 echo " <INPUT TYPE=hidden NAME=action VALUE=cancel>";
483                 echo " <INPUT TYPE=submit VALUE=\"$strok\">";
484                 echo "</FORM>";
485                 echo "</CENTER>";
486             } else {
487                 displaydir($wdir);
488             }
489             html_footer();
490             break;
492         case "listzip":
493             html_header($course, $wdir);
494             if (!empty($file)) {
495                 $strname = get_string("name");
496                 $strsize = get_string("size");
497                 $strmodified = get_string("modified");
498                 $strok = get_string("ok");
499                 $strlistfiles = get_string("listfiles", "", $file);
501                 echo "<P ALIGN=CENTER>$strlistfiles:</P>";
502                 $file = basename($file);
504                 include_once($CFG->libdir.'/pclzip/pclzip.lib.php');
505                 $archive = new PclZip("$basedir/$wdir/$file");
506                 if (!$list = $archive->listContent("$basedir/$wdir")) {
507                     notify($archive->errorInfo(true));
509                 } else {
510                     echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=640>";
511                     echo "<tr><th align=left>$strname</th><th align=right>$strsize</th><th align=right>$strmodified</th></tr>";
512                     foreach ($list as $item) {
513                         echo "<tr>";
514                         print_cell("left", $item['filename']);
515                         if (! $item['folder']) {
516                             print_cell("right", display_size($item['size']));
517                         } else {
518                             echo "<td>&nbsp;</td>";
519                         }
520                         $filedate  = userdate($item['mtime'], get_string("strftimedatetime"));
521                         print_cell("right", $filedate);
522                         echo "</tr>";
523                     }
524                     echo "</table>";
525                 }
526                 echo "<br><center><form action=\"".$_SERVER['PHP_SELF']."\" method=get>";
527                 echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
528                 echo " <INPUT TYPE=hidden NAME=wdir VALUE=$wdir>";
529                 echo " <INPUT TYPE=hidden NAME=action VALUE=cancel>";
530                 echo " <INPUT TYPE=submit VALUE=\"$strok\">";
531                 echo "</FORM>";
532                 echo "</CENTER>";
533             } else {
534                 displaydir($wdir);
535             }
536             html_footer();
537             break;
539         case "torte":
540                 if($_POST)
541                 {
542                         while(list($key, $val) = each($_POST))
543                         {
544                                 if(ereg("file([0-9]+)", $key, $regs))
545                                 {
546                                         $file = $val;
547                                 }
548                         }
549                         if(@filetype($CFG->dataroot ."/". $course->id . $file) == "file")
550                         {
551                                 if(mimeinfo("icon", $file) == "image.gif")
552                                 {
553                                         $url = $CFG->wwwroot ."/file.php?file=/" .$course->id . $file;
554                                         runjavascript($url);
555                                 }
556                                 else
557                                 {
558                                         print "File is not a image!";
559                                 }
560                         }
561                         else
562                         {
563                                 print "You cannot insert FOLDER into richtext editor!!!";
564                         }
565                 }
566                 break;
567         case "cancel";
568             clearfilelist();
570         default:
571             html_header($course, $wdir);
572             displaydir($wdir);
573             html_footer();
574             break;
578 /// FILE FUNCTIONS ///////////////////////////////////////////////////////////
581 function fulldelete($location) { 
582     if (is_dir($location)) {
583         $currdir = opendir($location);
584         while ($file = readdir($currdir)) { 
585             if ($file <> ".." && $file <> ".") {
586                 $fullfile = $location."/".$file;
587                 if (is_dir($fullfile)) { 
588                     if (!fulldelete($fullfile)) {
589                         return false;
590                     }
591                 } else {
592                     if (!unlink($fullfile)) {
593                         return false;
594                     }
595                 } 
596             }
597         } 
598         closedir($currdir);
599         if (! rmdir($location)) {
600             return false;
601         }
603     } else {
604         if (!unlink($location)) {
605             return false;
606         }
607     }
608     return true;
613 function setfilelist($VARS) {
614     global $USER;
616     $USER->filelist = array ();
617     $USER->fileop = "";
619     $count = 0;
620     foreach ($VARS as $key => $val) {
621         if (substr($key,0,4) == "file") {
622             $count++;
623             $USER->filelist[] = rawurldecode($val);
624         }
625     }
626     return $count;
629 function clearfilelist() {
630     global $USER;
632     $USER->filelist = array ();
633     $USER->fileop = "";
637 function printfilelist($filelist) {
638     global $basedir, $CFG;
640     foreach ($filelist as $file) {
641         if (is_dir($basedir.$file)) {
642             echo "<img src=\"$CFG->pixpath/f/folder.gif\" height=16 width=16> $file<br>";
643             $subfilelist = array();
644             $currdir = opendir($basedir.$file);
645             while ($subfile = readdir($currdir)) { 
646                 if ($subfile <> ".." && $subfile <> ".") {
647                     $subfilelist[] = $file."/".$subfile;
648                 }
649             }
650             printfilelist($subfilelist);
652         } else { 
653             $icon = mimeinfo("icon", $file);
654             echo "<img src=\"$CFG->pixpath/f/$icon\"  height=16 width=16> $file<br>";
655         }
656     }
660 function print_cell($alignment="center", $text="&nbsp;") {
661     echo "<TD ALIGN=\"$alignment\" NOWRAP>";
662     echo "<FONT SIZE=\"-1\" FACE=\"Arial, Helvetica\">";
663     echo "$text";
664     echo "</FONT>";
665     echo "</TD>\n";
668 function displaydir ($wdir) {
669 //  $wdir == / or /a or /a/b/c/d  etc
671     global $basedir;
672     global $id;
673     global $USER, $CFG;
675     $fullpath = $basedir.$wdir;
677     $directory = opendir($fullpath);             // Find all files
678     while ($file = readdir($directory)) {
679         if ($file == "." || $file == "..") {
680             continue;
681         }
682         
683         if (is_dir($fullpath."/".$file)) {
684             $dirlist[] = $file;
685         } else {
686             if ((basename(strtolower($file),".zip") != basename(strtolower($file))) || (basename(strtolower($file),".pif") != basename(strtolower($file))))
687                 $filelist[] = $file;
688         }
689     }
690     closedir($directory);
692     $strname = get_string("name");
693     $strsize = get_string("size");
694     $strmodified = get_string("modified");
695     $straction = get_string("action");
696     $strmakeafolder = get_string("makeafolder");
697     $struploadafile = get_string("uploadafile");
698     $strwithchosenfiles = get_string("withchosenfiles");
699     $strmovetoanotherfolder = get_string("movetoanotherfolder");
700     $strmovefilestohere = get_string("movefilestohere");
701     $strdeletecompletely = get_string("deletecompletely");
702     $strcreateziparchive = get_string("createziparchive");
703     $strrename = get_string("rename");
704     $stredit   = get_string("edit");
705     $strunzip  = get_string("unzip");
706     $strlist   = get_string("list");
707     $strchoose   = get_string("choose");
710     echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=post NAME=dirform>";
711     echo "<TABLE BORDER=0 cellspacing=2 cellpadding=2 width=640>";    
712     echo "<TR>";
713     echo "<TH WIDTH=5></TH>";
714     echo "<TH ALIGN=left>$strname</TH>";
715     echo "<TH ALIGN=right>$strsize</TH>";
716     echo "<TH ALIGN=right>$strmodified</TH>";
717     echo "<TH ALIGN=right>$straction</TH>";
718     echo "</TR>\n";
720     if ($wdir == "/") {
721         $wdir = "";
722     }
724     $count = 0;
726     if (!empty($dirlist)) {
727         asort($dirlist);
728         foreach ($dirlist as $dir) {
730             $count++;
732             $filename = $fullpath."/".$dir;
733             $fileurl  = rawurlencode($wdir."/".$dir);
734             $filesafe = rawurlencode($dir);
735             $filedate = userdate(filectime($filename), "%d %b %Y, %I:%M %p");
736     
737             echo "<TR>";
739             print_cell("center", "<INPUT TYPE=checkbox NAME=\"file$count\" VALUE=\"$fileurl\">");
740             print_cell("left", "<A HREF=\"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$fileurl\"><IMG SRC=\"$CFG->pixpath/f/folder.gif\" HEIGHT=16 WIDTH=16 BORDER=0 ALT=\"Folder\"></A> <A HREF=\"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$fileurl\">".htmlspecialchars($dir)."</A>");
741             print_cell("right", "-");
742             print_cell("right", $filedate);
743             print_cell("right", "<A HREF=\"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&file=$filesafe&action=rename\">$strrename</A>");
744     
745             echo "</TR>";
746         }
747     }
750     if (!empty($filelist)) {
751         asort($filelist);
752         foreach ($filelist as $file) {
754             $icon = mimeinfo("icon", $file);
756             $count++;
757             $filename    = "$fullpath/$file";
758             $fileurl     = "$wdir/$file";
759             $filesafe    = rawurlencode($file);
760             $fileurlsafe = rawurlencode($fileurl);
761             $filedate    = userdate(filectime($filename), "%d %b %Y, %I:%M %p");
763             if (substr($fileurl,0,1) == '/') {
764                 $selectfile = substr($fileurl,1);
765             } else {
766                 $selectfile = $fileurl;
767             }
768             if ($CFG->slasharguments) {
769                 $ffurl = "/file.php/$id$fileurl";
770             } else {
771                 $ffurl = "/file.php?file=/$id$fileurl";
772             }
774             echo "<tr>";
776             print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\">");
778             echo "<td align=left nowrap>";
779             link_to_popup_window ($ffurl, "display", 
780                                   "<img src=\"$CFG->pixpath/f/$icon\" height=16 width=16 border=0 alt=\"file\">", 
781                                   480, 640);
782             echo "<font size=\"-1\" face=\"Arial, Helvetica\">";
783             link_to_popup_window ($ffurl, "display", htmlspecialchars($file), 480, 640);
784             echo "</font></td>";
786             $file_size = filesize($filename);
787             print_cell("right", display_size($file_size));
788             print_cell("right", $filedate);
790             $edittext = "<b><a onMouseDown=\"return set_value('$selectfile')\" href=\"\">$strchoose</a></b>&nbsp;";
792             if ($icon == "text.gif" || $icon == "html.gif") {
793                 $edittext .= "<a href=\"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&file=$fileurl&action=edit\">$stredit</a>";
794             } else if ($icon == "zip.gif") {
795                 $edittext .= "<a href=\"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&file=$fileurl&action=unzip\">$strunzip</a>&nbsp;";
796                 $edittext .= "<a href=\"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&file=$fileurl&action=listzip\">$strlist</a> ";
797             }
799             print_cell("right", "$edittext <A HREF=\"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&file=$filesafe&action=rename\">$strrename</A>");
800     
801             echo "</tr>";
802         }
803     }
804     echo "</table>";
805     echo "<hr width=640 align=center noshade size=1>";
807     if (empty($wdir)) {
808         $wdir = "/";
809     }
811     echo "<TABLE BORDER=0 cellspacing=2 cellpadding=2 width=640>";    
812     echo "<TR><TD>";
813     echo "<INPUT TYPE=hidden NAME=id VALUE=\"$id\">";
814     echo "<INPUT TYPE=hidden NAME=wdir VALUE=\"$wdir\"> ";
815     $options = array (
816                    "move" => "$strmovetoanotherfolder",
817                    "delete" => "$strdeletecompletely",
818                    "zip" => "$strcreateziparchive"
819                );
820     if (!empty($count)) {
821         choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:document.dirform.submit()");
822     }
824     echo "</FORM>";
825     echo "<TD ALIGN=center>";
826     if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) {
827         echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=get>";
828         echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
829         echo " <INPUT TYPE=hidden NAME=wdir VALUE=\"$wdir\">";
830         echo " <INPUT TYPE=hidden NAME=action VALUE=paste>";
831         echo " <INPUT TYPE=submit VALUE=\"$strmovefilestohere\">";
832         echo "</FORM>";
833     }
834     echo "<TD ALIGN=right>";
835         echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=get>";
836         echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
837         echo " <INPUT TYPE=hidden NAME=wdir VALUE=\"$wdir\">";
838         echo " <INPUT TYPE=hidden NAME=action VALUE=mkdir>";
839         echo " <INPUT TYPE=submit VALUE=\"$strmakeafolder\">";
840         echo "</FORM>";
841     echo "</TD>";
842     echo "<TD ALIGN=right>";
843         echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=get>";
844         echo " <INPUT TYPE=hidden NAME=id VALUE=$id>";
845         echo " <INPUT TYPE=hidden NAME=wdir VALUE=\"$wdir\">";
846         echo " <INPUT TYPE=hidden NAME=action VALUE=upload>";
847         echo " <INPUT TYPE=submit VALUE=\"$struploadafile\">";
848         echo "</FORM>";
849     echo "</TD></TR>";
850     echo "</TABLE>";
851     echo "<HR WIDTH=640 ALIGN=CENTER NOSHADE SIZE=1>";
855 ?>