Fixed firstname lastname to call to fullname function
[moodle.git] / files / index.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3// Manage all uploaded files in a course file area
4
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
9
10 require("../config.php");
11
12 require_variable($id);
fb964e0c 13 optional_variable($file, "");
14 optional_variable($wdir, "");
15 optional_variable($action, "");
f9903ed0 16
5875b7d6 17 if (! $course = get_record("course", "id", $id) ) {
18 error("That's an invalid course id");
19 }
f9903ed0 20
5875b7d6 21 require_login($course->id);
f9903ed0 22
5875b7d6 23 if (! isteacheredit($course->id) ) {
24 error("You need to be a teacher with editing privileges");
f9903ed0 25 }
26
27 function html_footer() {
28 global $course;
29 echo "</td></tr></table></body></html>";
30 print_footer($course);
31 }
32
0c377177 33 function html_header($course, $wdir, $formfield=""){
8ce53f0f 34
65c59e30 35 global $CFG;
36
222ac91b 37 if ($course->id == SITEID) {
55e4b5f9 38 $strfiles = get_string("sitefiles");
39 } else {
40 $strfiles = get_string("files");
41 }
f9903ed0 42
0c377177 43 if ($wdir == "/") {
65c59e30 44 $fullnav = "$strfiles";
0c377177 45 } else {
46 $dirs = explode("/", $wdir);
47 $numdirs = count($dirs);
48 $link = "";
49 $navigation = "";
fdfc6269 50 for ($i=1; $i<$numdirs-1; $i++) {
0c377177 51 $navigation .= " -> ";
52 $link .= "/".urlencode($dirs[$i]);
839f2456 53 $navigation .= "<a href=\"index.php?id=$course->id&amp;wdir=$link\">".$dirs[$i]."</a>";
0c377177 54 }
839f2456 55 $fullnav = "<a href=\"index.php?id=$course->id&amp;wdir=/\">$strfiles</a> $navigation -> ".$dirs[$numdirs-1];
65c59e30 56 }
0c377177 57
222ac91b 58 if ($course->id == SITEID) {
65c59e30 59 print_header("$course->shortname: $strfiles", "$course->fullname",
60 "<a href=\"../$CFG->admin/index.php\">".get_string("administration").
61 "</a> -> $fullnav", $formfield);
55e4b5f9 62
63 print_heading(get_string("publicsitefileswarning"), "center", 2);
64
65c59e30 65 } else {
0c377177 66 print_header("$course->shortname: $strfiles", "$course->fullname",
65c59e30 67 "<a href=\"../course/view.php?id=$course->id\">$course->shortname".
68 "</a> -> $fullnav", $formfield);
0c377177 69 }
55e4b5f9 70
9a58f7cb 71 echo "<table border=\"0\" align=\"center\" cellspacing=\"3\" cellpadding=\"3\" width=\"640\">";
f9903ed0 72 echo "<tr>";
73 echo "<td colspan=\"2\">";
74 }
5875b7d6 75
76 if (! $basedir = make_upload_directory("$course->id")) {
6b174680 77 error("The site administrator needs to fix the file permissions");
f9903ed0 78 }
f9903ed0 79
f9903ed0 80 $baseweb = $CFG->wwwroot;
81
82// End of configuration and access control
83
f9903ed0 84 require("mimetypes.php");
85
86 $regexp="\\.\\.";
87 if (ereg( $regexp, $file, $regs )| ereg( $regexp, $wdir,$regs )) {
88 $message = "Error: Directories can not contain \"..\"";
89 $wdir = "/";
90 $action = "";
91 }
92
f9903ed0 93 if (!$wdir) {
94 $wdir="/";
95 }
96
fdfc6269 97 if ($wdir == "/backupdata") {
98 if (! make_upload_directory("$course->id/backupdata")) { // Backup folder
99 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
100 }
101 }
102
f9903ed0 103
f9903ed0 104 switch ($action) {
105
106 case "upload":
0c377177 107 html_header($course, $wdir);
3bb4d844 108 require_once($CFG->dirroot.'/lib/uploadlib.php');
109
3b7c1de9 110 if (!empty($save)) {
3bb4d844 111 $um = new upload_manager('userfile',false,false,$course,false,0);
112 $dir = "$basedir$wdir";
113 if ($um->process_file_uploads($dir)) {
114 notify(get_string('uploadedfile'));
f9903ed0 115 }
3bb4d844 116 // um will take care of error reporting.
f9903ed0 117 displaydir($wdir);
f9903ed0 118 } else {
241f6f30 119 $upload_max_filesize = get_max_upload_file_size($CFG->maxbytes); // Restricted by site setting
fedc147e 120 $filesize = display_size($upload_max_filesize);
6e40c33d 121
3b7c1de9 122 $struploadafile = get_string("uploadafile");
123 $struploadthisfile = get_string("uploadthisfile");
124 $strmaxsize = get_string("maxsize", "", $filesize);
125 $strcancel = get_string("cancel");
126
9a58f7cb 127 echo "<p>$struploadafile ($strmaxsize) --> <b>$wdir</b>";
128 echo "<table><tr><td colspan=\"2\">";
129 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"index.php\">";
9a58f7cb 130 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
131 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
132 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
3bb4d844 133 upload_print_form_fragment(1,array('userfile'),null,false,null,$course->maxbytes,0,false);
9a58f7cb 134 echo " </td><tr><td width=\"10\">";
135 echo " <input type=\"submit\" name=\"save\" value=\"$struploadthisfile\" />";
136 echo "</form>";
137 echo "</td><td width=\"100%\">";
138 echo "<form action=\"index.php\" method=\"get\">";
139 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
140 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
141 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
142 echo " <input type=\"submit\" value=\"$strcancel\" />";
143 echo "</form>";
144 echo "</td></tr></table>";
f9903ed0 145 }
146 html_footer();
147 break;
148
149 case "delete":
fb964e0c 150 if (!empty($confirm)) {
0c377177 151 html_header($course, $wdir);
f9903ed0 152 foreach ($USER->filelist as $file) {
153 $fullfile = $basedir.$file;
154 if (! fulldelete($fullfile)) {
9a58f7cb 155 echo "<br />Error: Could not delete: $fullfile";
f9903ed0 156 }
157 }
158 clearfilelist();
159 displaydir($wdir);
160 html_footer();
161
162 } else {
0c377177 163 html_header($course, $wdir);
607809b3 164 if (setfilelist($_POST)) {
9a58f7cb 165 echo "<p align=\"center\">".get_string("deletecheckwarning").":</p>";
f9903ed0 166 print_simple_box_start("center");
167 printfilelist($USER->filelist);
168 print_simple_box_end();
1efcfce8 169 echo "<br />";
170 notice_yesno (get_string("deletecheckfiles"),
839f2456 171 "index.php?id=$id&amp;wdir=$wdir&amp;action=delete&amp;confirm=1",
172 "index.php?id=$id&amp;wdir=$wdir&amp;action=cancel");
f9903ed0 173 } else {
174 displaydir($wdir);
175 }
176 html_footer();
177 }
178 break;
179
180 case "move":
0c377177 181 html_header($course, $wdir);
607809b3 182 if ($count = setfilelist($_POST)) {
f9903ed0 183 $USER->fileop = $action;
184 $USER->filesource = $wdir;
9a58f7cb 185 echo "<p align=\"center\">";
1efcfce8 186 print_string("selectednowmove", "moodle", $count);
187 echo "</p>";
f9903ed0 188 }
189 displaydir($wdir);
190 html_footer();
191 break;
192
193 case "paste":
0c377177 194 html_header($course, $wdir);
fb964e0c 195 if (isset($USER->fileop) and $USER->fileop == "move") {
f9903ed0 196 foreach ($USER->filelist as $file) {
197 $shortfile = basename($file);
198 $oldfile = $basedir.$file;
199 $newfile = $basedir.$wdir."/".$shortfile;
200 if (!rename($oldfile, $newfile)) {
9a58f7cb 201 echo "<p>Error: $shortfile not moved";
f9903ed0 202 }
203 }
204 }
205 clearfilelist();
206 displaydir($wdir);
207 html_footer();
208 break;
209
210 case "rename":
fb964e0c 211 if (!empty($name)) {
0c377177 212 html_header($course, $wdir);
8ef1e12f 213 $name = clean_filename($name);
214 $oldname = clean_filename($oldname);
f9903ed0 215 if (file_exists($basedir.$wdir."/".$name)) {
216 echo "Error: $name already exists!";
217 } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) {
218 echo "Error: could not rename $oldname to $name";
219 }
220 displaydir($wdir);
221
222 } else {
27c40942 223 $strrename = get_string("rename");
224 $strcancel = get_string("cancel");
225 $strrenamefileto = get_string("renamefileto", "moodle", $file);
0c377177 226 html_header($course, $wdir, "form.name");
9a58f7cb 227 echo "<p>$strrenamefileto:";
228 echo "<table><tr><td>";
229 echo "<form action=\"index.php\" method=\"post\" name=\"form\">";
230 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
231 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
232 echo " <input type=\"hidden\" name=\"action\" value=\"rename\" />";
233 echo " <input type=\"hidden\" name=\"oldname\" value=\"$file\" />";
234 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"$file\" />";
235 echo " <input type=\"submit\" value=\"$strrename\" />";
236 echo "</form>";
237 echo "</td><td>";
238 echo "<form action=\"index.php\" method=\"get\">";
239 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
240 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
241 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
242 echo " <input type=\"submit\" value=\"$strcancel\" />";
243 echo "</form>";
244 echo "</td></tr></table>";
f9903ed0 245 }
246 html_footer();
247 break;
248
249 case "mkdir":
fb964e0c 250 if (!empty($name)) {
0c377177 251 html_header($course, $wdir);
f9903ed0 252 $name = clean_filename($name);
fc613dff 253 if (file_exists("$basedir$wdir/$name")) {
f9903ed0 254 echo "Error: $name already exists!";
fc613dff 255 } else if (! make_upload_directory("$course->id/$wdir/$name")) {
f9903ed0 256 echo "Error: could not create $name";
257 }
258 displaydir($wdir);
259
260 } else {
27c40942 261 $strcreate = get_string("create");
262 $strcancel = get_string("cancel");
263 $strcreatefolder = get_string("createfolder", "moodle", $wdir);
0c377177 264 html_header($course, $wdir, "form.name");
9a58f7cb 265 echo "<p>$strcreatefolder:";
266 echo "<table><tr><td>";
267 echo "<form action=\"index.php\" method=\"post\" name=\"form\">";
268 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
269 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
270 echo " <input type=\"hidden\" name=\"action\" value=\"mkdir\" />";
271 echo " <input type=\"text\" name=\"name\" size=\"35\" />";
272 echo " <input type=\"submit\" value=\"$strcreate\" />";
273 echo "</form>";
274 echo "</td><td>";
275 echo "<form action=\"index.php\" method=\"get\">";
276 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
277 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
278 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
279 echo " <input type=\"submit\" value=\"$strcancel\" />";
280 echo "</form>";
281 echo "</td></tr></table>";
f9903ed0 282 }
283 html_footer();
284 break;
285
286 case "edit":
0c377177 287 html_header($course, $wdir);
f9903ed0 288 if (isset($text)) {
289 $fileptr = fopen($basedir.$file,"w");
290 fputs($fileptr, stripslashes($text));
291 fclose($fileptr);
292 displaydir($wdir);
293
294 } else {
9a58f7cb 295 $streditfile = get_string("edit", "", "<b>$file</b>");
f9903ed0 296 $fileptr = fopen($basedir.$file, "r");
297 $contents = fread($fileptr, filesize($basedir.$file));
298 fclose($fileptr);
299
04fa59d0 300 if (mimeinfo("type", $file) == "text/html") {
ffae3d46 301 $usehtmleditor = can_use_html_editor();
04fa59d0 302 } else {
303 $usehtmleditor = false;
04fa59d0 304 }
206a5075 305 $usehtmleditor = false; // Always keep it off for now
04fa59d0 306
307 print_heading("$streditfile");
308
9a58f7cb 309 echo "<table><tr><td colspan=\"2\">";
310 echo "<form action=\"index.php\" method=\"post\" name=\"form\">";
311 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
312 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
313 echo " <input type=\"hidden\" name=\"file\" value=\"$file\" />";
314 echo " <input type=\"hidden\" name=\"action\" value=\"edit\" />";
ffae3d46 315 print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents);
9a58f7cb 316 echo "</td></tr><tr><td>";
317 echo " <input type=\"submit\" value=\"".get_string("savechanges")."\" />";
318 echo "</form>";
319 echo "</td><td>";
320 echo "<form action=\"index.php\" method=\"get\">";
321 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
322 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
323 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
324 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
325 echo "</form>";
326 echo "</td></tr></table>";
04fa59d0 327
328 if ($usehtmleditor) {
ffae3d46 329 use_html_editor();
04fa59d0 330 }
331
332
f9903ed0 333 }
334 html_footer();
335 break;
336
337 case "zip":
fb964e0c 338 if (!empty($name)) {
0c377177 339 html_header($course, $wdir);
f9903ed0 340 $name = clean_filename($name);
e64984a0 341 if (empty($CFG->zip)) { // Use built-in php-based zip function
342 $files = array();
343 foreach ($USER->filelist as $file) {
7d064d87 344 $files[] = "$basedir/$file";
e64984a0 345 }
5151ae66 346 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
e64984a0 347 $archive = new PclZip("$basedir/$wdir/$name");
348 if (($list = $archive->create($files,'',"$basedir/$wdir/")) == 0) {
349 error($archive->errorInfo(true));
350 }
351 } else { // Use external zip program
352 $files = "";
353 foreach ($USER->filelist as $file) {
354 $files .= basename($file);
355 $files .= " ";
356 }
357 $command = "cd $basedir/$wdir ; $CFG->zip -r $name $files";
358 Exec($command);
f9903ed0 359 }
f9903ed0 360 clearfilelist();
361 displaydir($wdir);
362
363 } else {
0c377177 364 html_header($course, $wdir, "form.name");
27c40942 365
607809b3 366 if (setfilelist($_POST)) {
9a58f7cb 367 echo "<p align=\"center\">".get_string("youareabouttocreatezip").":</p>";
f9903ed0 368 print_simple_box_start("center");
369 printfilelist($USER->filelist);
370 print_simple_box_end();
9a58f7cb 371 echo "<br />";
372 echo "<p align=\"center\">".get_string("whattocallzip");
373 echo "<table><tr><td>";
374 echo "<form action=\"index.php\" method=\"post\" name=\"form\">";
375 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
376 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
377 echo " <input type=\"hidden\" name=\"action\" value=\"zip\" />";
378 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"new.zip\" />";
379 echo " <input type=\"submit\" value=\"".get_string("createziparchive")."\" />";
380 echo "</form>";
381 echo "</td><td>";
382 echo "<form action=\"index.php\" method=\"get\">";
383 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
384 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
385 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
386 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
387 echo "</form>";
388 echo "</td></tr></table>";
f9903ed0 389 } else {
390 displaydir($wdir);
391 clearfilelist();
392 }
393 }
394 html_footer();
395 break;
396
397 case "unzip":
0c377177 398 html_header($course, $wdir);
fb964e0c 399 if (!empty($file)) {
e64984a0 400 $strname = get_string("name");
401 $strsize = get_string("size");
402 $strmodified = get_string("modified");
403 $strstatus = get_string("status");
404 $strok = get_string("ok");
405 $strunpacking = get_string("unpacking", "", $file);
406
9a58f7cb 407 echo "<p align=\"center\">$strunpacking:</p>";
e64984a0 408
f9903ed0 409 $file = basename($file);
e64984a0 410
411 if (empty($CFG->unzip)) { // Use built-in php-based unzip function
5151ae66 412 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
e64984a0 413 $archive = new PclZip("$basedir/$wdir/$file");
414 if (!$list = $archive->extract("$basedir/$wdir")) {
415 error($archive->errorInfo(true));
416 } else { // print some output
9a58f7cb 417 echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\">";
418 echo "<tr><th align=\"left\">$strname</th>";
419 echo "<th align=\"right\">$strsize</th>";
420 echo "<th align=\"right\">$strmodified</th>";
421 echo "<th align=\"right\">$strstatus</th></tr>";
e64984a0 422 foreach ($list as $item) {
423 echo "<tr>";
424 $item['filename'] = str_replace("$basedir/$wdir/", "", $item['filename']);
425 print_cell("left", $item['filename']);
426 if (! $item['folder']) {
427 print_cell("right", display_size($item['size']));
428 } else {
429 echo "<td>&nbsp;</td>";
430 }
431 $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
432 print_cell("right", $filedate);
433 print_cell("right", $item['status']);
434 echo "</tr>";
435 }
436 echo "</table>";
437 }
438
439 } else { // Use external unzip program
440 print_simple_box_start("center");
9a58f7cb 441 echo "<pre>";
e64984a0 442 $command = "cd $basedir/$wdir ; $CFG->unzip -o $file 2>&1";
443 passthru($command);
9a58f7cb 444 echo "</pre>";
e64984a0 445 print_simple_box_end();
446 }
447
9a58f7cb 448 echo "<center><form action=\"index.php\" method=\"get\">";
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=\"$strok\" />";
453 echo "</form>";
454 echo "</center>";
f9903ed0 455 } else {
456 displaydir($wdir);
457 }
458 html_footer();
459 break;
460
e64984a0 461 case "listzip":
462 html_header($course, $wdir);
463 if (!empty($file)) {
464 $strname = get_string("name");
465 $strsize = get_string("size");
466 $strmodified = get_string("modified");
467 $strok = get_string("ok");
468 $strlistfiles = get_string("listfiles", "", $file);
469
9a58f7cb 470 echo "<p align=\"center\">$strlistfiles:</p>";
e64984a0 471 $file = basename($file);
472
5151ae66 473 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
e64984a0 474 $archive = new PclZip("$basedir/$wdir/$file");
475 if (!$list = $archive->listContent("$basedir/$wdir")) {
476 notify($archive->errorInfo(true));
477
478 } else {
9a58f7cb 479 echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\">";
480 echo "<tr><th align=\"left\">$strname</th><th align=\"right\">$strsize</th><th align=\"right\">$strmodified</th></tr>";
e64984a0 481 foreach ($list as $item) {
482 echo "<tr>";
483 print_cell("left", $item['filename']);
484 if (! $item['folder']) {
485 print_cell("right", display_size($item['size']));
486 } else {
487 echo "<td>&nbsp;</td>";
488 }
489 $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
490 print_cell("right", $filedate);
491 echo "</tr>";
492 }
493 echo "</table>";
494 }
9a58f7cb 495 echo "<br /><center><form action=\"index.php\" method=\"get\">";
496 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
497 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
498 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
499 echo " <input type=\"submit\" value=\"$strok\" />";
500 echo "</form>";
501 echo "</center>";
e64984a0 502 } else {
503 displaydir($wdir);
504 }
505 html_footer();
506 break;
507
476fd9c7 508 case "restore":
509 html_header($course, $wdir);
510 if (!empty($file)) {
9a58f7cb 511 echo "<p align=\"center\">".get_string("youaregoingtorestorefrom").":</p>";
476fd9c7 512 print_simple_box_start("center");
513 echo $file;
514 print_simple_box_end();
c79f9629 515 echo "<br />";
9a58f7cb 516 echo "<p align=\"center\">".get_string("areyousuretorestorethisinfo")."</p>";
70363f88 517 $restore_path = "../backup/restore.php";
476fd9c7 518 notice_yesno (get_string("areyousuretorestorethis"),
839f2456 519 $restore_path."?id=".$id."&amp;file=".$id.$wdir."/".$file,
520 "index.php?id=$id&amp;wdir=$wdir&amp;action=cancel");
476fd9c7 521 } else {
522 displaydir($wdir);
523 }
524 html_footer();
525 break;
526
f9903ed0 527 case "cancel";
528 clearfilelist();
529
530 default:
0c377177 531 html_header($course, $wdir);
f9903ed0 532 displaydir($wdir);
533 html_footer();
534 break;
535}
536
537
538/// FILE FUNCTIONS ///////////////////////////////////////////////////////////
539
540
541function fulldelete($location) {
542 if (is_dir($location)) {
543 $currdir = opendir($location);
544 while ($file = readdir($currdir)) {
545 if ($file <> ".." && $file <> ".") {
546 $fullfile = $location."/".$file;
547 if (is_dir($fullfile)) {
548 if (!fulldelete($fullfile)) {
549 return false;
550 }
551 } else {
552 if (!unlink($fullfile)) {
553 return false;
554 }
555 }
556 }
557 }
558 closedir($currdir);
559 if (! rmdir($location)) {
560 return false;
561 }
562
563 } else {
564 if (!unlink($location)) {
565 return false;
566 }
567 }
568 return true;
569}
570
f9903ed0 571
572
573function setfilelist($VARS) {
574 global $USER;
575
576 $USER->filelist = array ();
577 $USER->fileop = "";
578
579 $count = 0;
580 foreach ($VARS as $key => $val) {
581 if (substr($key,0,4) == "file") {
582 $count++;
583 $USER->filelist[] = rawurldecode($val);
584 }
585 }
f9903ed0 586 return $count;
587}
588
589function clearfilelist() {
590 global $USER;
591
592 $USER->filelist = array ();
593 $USER->fileop = "";
594}
595
596
597function printfilelist($filelist) {
241f6f30 598 global $CFG, $basedir;
f9903ed0 599
600 foreach ($filelist as $file) {
601 if (is_dir($basedir.$file)) {
839f2456 602 echo "<img src=\"$CFG->pixpath/f/folder.gif\" height=\"16\" width=\"16\" alt=\"\" /> $file<br />";
f9903ed0 603 $subfilelist = array();
604 $currdir = opendir($basedir.$file);
605 while ($subfile = readdir($currdir)) {
606 if ($subfile <> ".." && $subfile <> ".") {
607 $subfilelist[] = $file."/".$subfile;
608 }
609 }
610 printfilelist($subfilelist);
611
612 } else {
613 $icon = mimeinfo("icon", $file);
839f2456 614 echo "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"\" /> $file<br />";
f9903ed0 615 }
616 }
617}
618
619
f9903ed0 620function print_cell($alignment="center", $text="&nbsp;") {
9a58f7cb 621 echo "<td align=\"$alignment\" nowrap=\"nowrap\">";
622 echo "<font size=\"-1\" face=\"Arial, Helvetica\">";
f9903ed0 623 echo "$text";
9a58f7cb 624 echo "</font>";
625 echo "</td>\n";
f9903ed0 626}
627
628function displaydir ($wdir) {
629// $wdir == / or /a or /a/b/c/d etc
630
631 global $basedir;
632 global $id;
6e4c09db 633 global $USER, $CFG;
f9903ed0 634
635 $fullpath = $basedir.$wdir;
636
637 $directory = opendir($fullpath); // Find all files
638 while ($file = readdir($directory)) {
5875b7d6 639 if ($file == "." || $file == "..") {
f9903ed0 640 continue;
641 }
642
643 if (is_dir($fullpath."/".$file)) {
644 $dirlist[] = $file;
645 } else {
646 $filelist[] = $file;
647 }
648 }
649 closedir($directory);
650
0c377177 651 $strname = get_string("name");
652 $strsize = get_string("size");
653 $strmodified = get_string("modified");
654 $straction = get_string("action");
655 $strmakeafolder = get_string("makeafolder");
656 $struploadafile = get_string("uploadafile");
657 $strwithchosenfiles = get_string("withchosenfiles");
658 $strmovetoanotherfolder = get_string("movetoanotherfolder");
659 $strmovefilestohere = get_string("movefilestohere");
660 $strdeletecompletely = get_string("deletecompletely");
661 $strcreateziparchive = get_string("createziparchive");
662 $strrename = get_string("rename");
663 $stredit = get_string("edit");
664 $strunzip = get_string("unzip");
e64984a0 665 $strlist = get_string("list");
476fd9c7 666 $strrestore= get_string("restore");
0c377177 667
f9903ed0 668
9a58f7cb 669 echo "<form action=\"index.php\" method=\"post\" name=\"dirform\">";
670 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
671 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
672 echo "<tr>";
673 echo "<th width=\"5\"></th>";
674 echo "<th align=\"left\">$strname</th>";
675 echo "<th align=\"right\">$strsize</th>";
676 echo "<th align=\"right\">$strmodified</th>";
677 echo "<th align=\"right\">$straction</th>";
678 echo "</tr>\n";
f9903ed0 679
680 if ($wdir == "/") {
681 $wdir = "";
f9903ed0 682 }
683
f9903ed0 684 $count = 0;
685
fb964e0c 686 if (!empty($dirlist)) {
f9903ed0 687 asort($dirlist);
688 foreach ($dirlist as $dir) {
689
690 $count++;
691
692 $filename = $fullpath."/".$dir;
693 $fileurl = rawurlencode($wdir."/".$dir);
694 $filesafe = rawurlencode($dir);
16a5602c 695 $filesize = display_size(get_directory_size("$fullpath/$dir"));
45111451 696 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
f9903ed0 697
9a58f7cb 698 echo "<tr>";
f9903ed0 699
9a58f7cb 700 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />");
839f2456 701 print_cell("left", "<a href=\"index.php?id=$id&amp;wdir=$fileurl\"><img src=\"$CFG->pixpath/f/folder.gif\" height=\"16\" width=\"16\" border=\"0\" alt=\"Folder\" /></a> <a href=\"index.php?id=$id&amp;wdir=$fileurl\">".htmlspecialchars($dir)."</a>");
16a5602c 702 print_cell("right", "<b>$filesize</b>");
f9903ed0 703 print_cell("right", $filedate);
839f2456 704 print_cell("right", "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename\">$strrename</a>");
f9903ed0 705
9a58f7cb 706 echo "</tr>";
f9903ed0 707 }
708 }
709
710
fb964e0c 711 if (!empty($filelist)) {
f9903ed0 712 asort($filelist);
713 foreach ($filelist as $file) {
714
715 $icon = mimeinfo("icon", $file);
716
717 $count++;
718 $filename = $fullpath."/".$file;
719 $fileurl = "$wdir/$file";
720 $filesafe = rawurlencode($file);
721 $fileurlsafe = rawurlencode($fileurl);
45111451 722 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
f9903ed0 723
55e4b5f9 724 echo "<tr>";
f9903ed0 725
9a58f7cb 726 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />");
727 echo "<td align=\"left\" nowrap=\"nowrap\">";
3f8247c2 728 if ($CFG->slasharguments) {
729 $ffurl = "/file.php/$id$fileurl";
730 } else {
731 $ffurl = "/file.php?file=/$id$fileurl";
732 }
733 link_to_popup_window ($ffurl, "display",
839f2456 734 "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" border=\"0\" alt=\"File\" />",
f9903ed0 735 480, 640);
55e4b5f9 736 echo "<font size=\"-1\" face=\"Arial, Helvetica\">";
3f8247c2 737 link_to_popup_window ($ffurl, "display",
f9903ed0 738 htmlspecialchars($file),
739 480, 640);
55e4b5f9 740 echo "</font></td>";
f9903ed0 741
fedc147e 742 $file_size = filesize($filename);
743 print_cell("right", display_size($file_size));
f9903ed0 744 print_cell("right", $filedate);
745 if ($icon == "text.gif" || $icon == "html.gif") {
839f2456 746 $edittext = "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=edit\">$stredit</a>";
f9903ed0 747 } else if ($icon == "zip.gif") {
839f2456 748 $edittext = "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=unzip\">$strunzip</a>&nbsp;";
749 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=listzip\">$strlist</a> ";
862810dc 750 if (!empty($CFG->backup_version) and isteacheredit($id)) {
839f2456 751 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=restore\">$strrestore</a> ";
476fd9c7 752 }
f9903ed0 753 } else {
754 $edittext = "";
755 }
839f2456 756 print_cell("right", "$edittext <a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename\">$strrename</a>");
f9903ed0 757
9a58f7cb 758 echo "</tr>";
f9903ed0 759 }
760 }
9a58f7cb 761 echo "</table>";
762 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
f9903ed0 763
fb964e0c 764 if (empty($wdir)) {
f9903ed0 765 $wdir = "/";
766 }
767
9a58f7cb 768 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
769 echo "<tr><td>";
770 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
771 echo "<input type=\"hidden\" name=\"wdir\" value=\"$wdir\" /> ";
f9903ed0 772 $options = array (
0c377177 773 "move" => "$strmovetoanotherfolder",
774 "delete" => "$strdeletecompletely",
775 "zip" => "$strcreateziparchive"
f9903ed0 776 );
fb964e0c 777 if (!empty($count)) {
0c377177 778 choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:document.dirform.submit()");
f9903ed0 779 }
780
9a58f7cb 781 echo "</form>";
782 echo "<td align=\"center\">";
c897eac5 783 if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) {
9a58f7cb 784 echo "<form action=\"index.php\" method=\"get\">";
785 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
786 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
787 echo " <input type=\"hidden\" name=\"action\" value=\"paste\" />";
788 echo " <input type=\"submit\" value=\"$strmovefilestohere\" />";
789 echo "</form>";
f9903ed0 790 }
9a58f7cb 791 echo "<td align=\"right\">";
792 echo "<form action=\"index.php\" method=\"get\">";
793 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
794 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
795 echo " <input type=\"hidden\" name=\"action\" value=\"mkdir\" />";
796 echo " <input type=\"submit\" value=\"$strmakeafolder\" />";
797 echo "</form>";
798 echo "</td>";
799 echo "<td align=\"right\">";
800 echo "<form action=\"index.php\" method=\"get\">";
801 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
802 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
803 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
804 echo " <input type=\"submit\" value=\"$struploadafile\" />";
805 echo "</form>";
806 echo "</td></tr>";
807 echo "</table>";
808 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
f9903ed0 809
810}
811
812?>