Now the zip_files() function is working better:
[moodle.git] / files / index.php
CommitLineData
ad4e0fcc 1<?php // $Id$
f9903ed0 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
ad4e0fcc 12 $id = required_param('id', PARAM_INT);
13 $file = optional_param('file', '');
14 $wdir = optional_param('wdir', '');
15 $action = optional_param('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
ad4e0fcc 86 if (!$wdir) {
87 $wdir="/";
88 }
89
90 if (($wdir != '/' and detect_munged_arguments($wdir, 0))
91 or ($file != '' and detect_munged_arguments($file, 0))) {
f9903ed0 92 $message = "Error: Directories can not contain \"..\"";
93 $wdir = "/";
94 $action = "";
f9903ed0 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) {
ad4e0fcc 344 $files[] = cleardoubleslashes("$basedir/$file"); // no doubleslashes!
e64984a0 345 }
5151ae66 346 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
ad4e0fcc 347 $archive = new PclZip(cleardoubleslashes("$basedir/$wdir/$name"));
348 if (($list = $archive->create($files, PCLZIP_OPT_REMOVE_PATH,
349 rtrim(cleardoubleslashes("$basedir/$wdir"), "/"))) == 0) { // no double slashes and trailing slash!
e64984a0 350 error($archive->errorInfo(true));
351 }
352 } else { // Use external zip program
353 $files = "";
354 foreach ($USER->filelist as $file) {
355 $files .= basename($file);
356 $files .= " ";
357 }
358 $command = "cd $basedir/$wdir ; $CFG->zip -r $name $files";
359 Exec($command);
f9903ed0 360 }
f9903ed0 361 clearfilelist();
362 displaydir($wdir);
363
364 } else {
0c377177 365 html_header($course, $wdir, "form.name");
27c40942 366
607809b3 367 if (setfilelist($_POST)) {
9a58f7cb 368 echo "<p align=\"center\">".get_string("youareabouttocreatezip").":</p>";
f9903ed0 369 print_simple_box_start("center");
370 printfilelist($USER->filelist);
371 print_simple_box_end();
9a58f7cb 372 echo "<br />";
373 echo "<p align=\"center\">".get_string("whattocallzip");
374 echo "<table><tr><td>";
375 echo "<form action=\"index.php\" method=\"post\" name=\"form\">";
376 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
377 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
378 echo " <input type=\"hidden\" name=\"action\" value=\"zip\" />";
379 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"new.zip\" />";
380 echo " <input type=\"submit\" value=\"".get_string("createziparchive")."\" />";
381 echo "</form>";
382 echo "</td><td>";
383 echo "<form action=\"index.php\" method=\"get\">";
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=\"cancel\" />";
387 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
388 echo "</form>";
389 echo "</td></tr></table>";
f9903ed0 390 } else {
391 displaydir($wdir);
392 clearfilelist();
393 }
394 }
395 html_footer();
396 break;
397
398 case "unzip":
0c377177 399 html_header($course, $wdir);
fb964e0c 400 if (!empty($file)) {
e64984a0 401 $strname = get_string("name");
402 $strsize = get_string("size");
403 $strmodified = get_string("modified");
404 $strstatus = get_string("status");
405 $strok = get_string("ok");
406 $strunpacking = get_string("unpacking", "", $file);
407
9a58f7cb 408 echo "<p align=\"center\">$strunpacking:</p>";
e64984a0 409
f9903ed0 410 $file = basename($file);
e64984a0 411
412 if (empty($CFG->unzip)) { // Use built-in php-based unzip function
5151ae66 413 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
ad4e0fcc 414 $archive = new PclZip(cleardoubleslashes("$basedir/$wdir/$file"));
415 if (!$list = $archive->extract(PCLZIP_OPT_PATH, cleardoubleslashes("$basedir/$wdir"),
416 PCLZIP_CB_PRE_EXTRACT, 'approvefile')) {
e64984a0 417 error($archive->errorInfo(true));
418 } else { // print some output
9a58f7cb 419 echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\">";
420 echo "<tr><th align=\"left\">$strname</th>";
421 echo "<th align=\"right\">$strsize</th>";
422 echo "<th align=\"right\">$strmodified</th>";
423 echo "<th align=\"right\">$strstatus</th></tr>";
e64984a0 424 foreach ($list as $item) {
425 echo "<tr>";
ad4e0fcc 426 $item['filename'] = str_replace(cleardoubleslashes("$basedir/$wdir/"), "", $item['filename']);
e64984a0 427 print_cell("left", $item['filename']);
428 if (! $item['folder']) {
429 print_cell("right", display_size($item['size']));
430 } else {
431 echo "<td>&nbsp;</td>";
432 }
433 $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
434 print_cell("right", $filedate);
435 print_cell("right", $item['status']);
436 echo "</tr>";
437 }
438 echo "</table>";
439 }
440
441 } else { // Use external unzip program
442 print_simple_box_start("center");
9a58f7cb 443 echo "<pre>";
e64984a0 444 $command = "cd $basedir/$wdir ; $CFG->unzip -o $file 2>&1";
445 passthru($command);
9a58f7cb 446 echo "</pre>";
e64984a0 447 print_simple_box_end();
448 }
449
9a58f7cb 450 echo "<center><form action=\"index.php\" method=\"get\">";
451 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
452 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
453 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
454 echo " <input type=\"submit\" value=\"$strok\" />";
455 echo "</form>";
456 echo "</center>";
f9903ed0 457 } else {
458 displaydir($wdir);
459 }
460 html_footer();
461 break;
462
e64984a0 463 case "listzip":
464 html_header($course, $wdir);
465 if (!empty($file)) {
466 $strname = get_string("name");
467 $strsize = get_string("size");
468 $strmodified = get_string("modified");
469 $strok = get_string("ok");
470 $strlistfiles = get_string("listfiles", "", $file);
471
9a58f7cb 472 echo "<p align=\"center\">$strlistfiles:</p>";
e64984a0 473 $file = basename($file);
474
5151ae66 475 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
ad4e0fcc 476 $archive = new PclZip(cleardoubleslashes("$basedir/$wdir/$file"));
477 if (!$list = $archive->listContent(cleardoubleslashes("$basedir/$wdir"))) {
e64984a0 478 notify($archive->errorInfo(true));
479
480 } else {
9a58f7cb 481 echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\">";
482 echo "<tr><th align=\"left\">$strname</th><th align=\"right\">$strsize</th><th align=\"right\">$strmodified</th></tr>";
e64984a0 483 foreach ($list as $item) {
484 echo "<tr>";
485 print_cell("left", $item['filename']);
486 if (! $item['folder']) {
487 print_cell("right", display_size($item['size']));
488 } else {
489 echo "<td>&nbsp;</td>";
490 }
491 $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
492 print_cell("right", $filedate);
493 echo "</tr>";
494 }
495 echo "</table>";
496 }
9a58f7cb 497 echo "<br /><center><form action=\"index.php\" method=\"get\">";
498 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
499 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
500 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
501 echo " <input type=\"submit\" value=\"$strok\" />";
502 echo "</form>";
503 echo "</center>";
e64984a0 504 } else {
505 displaydir($wdir);
506 }
507 html_footer();
508 break;
509
476fd9c7 510 case "restore":
511 html_header($course, $wdir);
512 if (!empty($file)) {
9a58f7cb 513 echo "<p align=\"center\">".get_string("youaregoingtorestorefrom").":</p>";
476fd9c7 514 print_simple_box_start("center");
515 echo $file;
516 print_simple_box_end();
c79f9629 517 echo "<br />";
ad4e0fcc 518 echo "<p align=center>".get_string("areyousuretorestorethisinfo")."</p>";
519 $restore_path = "$CFG->wwwroot/backup/restore.php";
476fd9c7 520 notice_yesno (get_string("areyousuretorestorethis"),
ad4e0fcc 521 $restore_path."?id=".$id."&file=".cleardoubleslashes($id.$wdir."/".$file),
522 "index.php?id=$id&wdir=$wdir&action=cancel");
476fd9c7 523 } else {
524 displaydir($wdir);
525 }
526 html_footer();
527 break;
528
f9903ed0 529 case "cancel";
530 clearfilelist();
531
532 default:
0c377177 533 html_header($course, $wdir);
f9903ed0 534 displaydir($wdir);
535 html_footer();
536 break;
537}
538
539
540/// FILE FUNCTIONS ///////////////////////////////////////////////////////////
541
ad4e0fcc 542function approvefile($p_event, &$p_header){
543 if (detect_munged_arguments($p_header['filename'], 0)) {
544 return 0; // do not extract file!!
545 } else {
546 return 1;
547 }
548}
f9903ed0 549
550function fulldelete($location) {
551 if (is_dir($location)) {
552 $currdir = opendir($location);
553 while ($file = readdir($currdir)) {
554 if ($file <> ".." && $file <> ".") {
555 $fullfile = $location."/".$file;
556 if (is_dir($fullfile)) {
557 if (!fulldelete($fullfile)) {
558 return false;
559 }
560 } else {
561 if (!unlink($fullfile)) {
562 return false;
563 }
564 }
565 }
566 }
567 closedir($currdir);
568 if (! rmdir($location)) {
569 return false;
570 }
571
572 } else {
573 if (!unlink($location)) {
574 return false;
575 }
576 }
577 return true;
578}
579
f9903ed0 580
581
582function setfilelist($VARS) {
583 global $USER;
584
585 $USER->filelist = array ();
586 $USER->fileop = "";
587
588 $count = 0;
589 foreach ($VARS as $key => $val) {
590 if (substr($key,0,4) == "file") {
591 $count++;
ad4e0fcc 592 $val = rawurldecode($val);
593 if (!detect_munged_arguments($val, 0)) {
594 $USER->filelist[] = rawurldecode($val);
595 }
f9903ed0 596 }
597 }
f9903ed0 598 return $count;
599}
600
601function clearfilelist() {
602 global $USER;
603
604 $USER->filelist = array ();
605 $USER->fileop = "";
606}
607
608
609function printfilelist($filelist) {
241f6f30 610 global $CFG, $basedir;
f9903ed0 611
612 foreach ($filelist as $file) {
613 if (is_dir($basedir.$file)) {
839f2456 614 echo "<img src=\"$CFG->pixpath/f/folder.gif\" height=\"16\" width=\"16\" alt=\"\" /> $file<br />";
f9903ed0 615 $subfilelist = array();
616 $currdir = opendir($basedir.$file);
617 while ($subfile = readdir($currdir)) {
618 if ($subfile <> ".." && $subfile <> ".") {
619 $subfilelist[] = $file."/".$subfile;
620 }
621 }
622 printfilelist($subfilelist);
623
624 } else {
625 $icon = mimeinfo("icon", $file);
839f2456 626 echo "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"\" /> $file<br />";
f9903ed0 627 }
628 }
629}
630
631
f9903ed0 632function print_cell($alignment="center", $text="&nbsp;") {
9a58f7cb 633 echo "<td align=\"$alignment\" nowrap=\"nowrap\">";
634 echo "<font size=\"-1\" face=\"Arial, Helvetica\">";
f9903ed0 635 echo "$text";
9a58f7cb 636 echo "</font>";
637 echo "</td>\n";
f9903ed0 638}
639
640function displaydir ($wdir) {
641// $wdir == / or /a or /a/b/c/d etc
642
643 global $basedir;
644 global $id;
6e4c09db 645 global $USER, $CFG;
f9903ed0 646
647 $fullpath = $basedir.$wdir;
648
649 $directory = opendir($fullpath); // Find all files
650 while ($file = readdir($directory)) {
5875b7d6 651 if ($file == "." || $file == "..") {
f9903ed0 652 continue;
653 }
654
655 if (is_dir($fullpath."/".$file)) {
656 $dirlist[] = $file;
657 } else {
658 $filelist[] = $file;
659 }
660 }
661 closedir($directory);
662
0c377177 663 $strname = get_string("name");
664 $strsize = get_string("size");
665 $strmodified = get_string("modified");
666 $straction = get_string("action");
667 $strmakeafolder = get_string("makeafolder");
668 $struploadafile = get_string("uploadafile");
669 $strwithchosenfiles = get_string("withchosenfiles");
670 $strmovetoanotherfolder = get_string("movetoanotherfolder");
671 $strmovefilestohere = get_string("movefilestohere");
672 $strdeletecompletely = get_string("deletecompletely");
673 $strcreateziparchive = get_string("createziparchive");
674 $strrename = get_string("rename");
675 $stredit = get_string("edit");
676 $strunzip = get_string("unzip");
e64984a0 677 $strlist = get_string("list");
476fd9c7 678 $strrestore= get_string("restore");
0c377177 679
f9903ed0 680
9a58f7cb 681 echo "<form action=\"index.php\" method=\"post\" name=\"dirform\">";
682 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
683 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
684 echo "<tr>";
685 echo "<th width=\"5\"></th>";
686 echo "<th align=\"left\">$strname</th>";
687 echo "<th align=\"right\">$strsize</th>";
688 echo "<th align=\"right\">$strmodified</th>";
689 echo "<th align=\"right\">$straction</th>";
690 echo "</tr>\n";
f9903ed0 691
692 if ($wdir == "/") {
693 $wdir = "";
f9903ed0 694 }
695
f9903ed0 696 $count = 0;
697
fb964e0c 698 if (!empty($dirlist)) {
f9903ed0 699 asort($dirlist);
700 foreach ($dirlist as $dir) {
701
702 $count++;
703
704 $filename = $fullpath."/".$dir;
705 $fileurl = rawurlencode($wdir."/".$dir);
706 $filesafe = rawurlencode($dir);
16a5602c 707 $filesize = display_size(get_directory_size("$fullpath/$dir"));
45111451 708 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
f9903ed0 709
9a58f7cb 710 echo "<tr>";
f9903ed0 711
9a58f7cb 712 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />");
839f2456 713 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 714 print_cell("right", "<b>$filesize</b>");
f9903ed0 715 print_cell("right", $filedate);
839f2456 716 print_cell("right", "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename\">$strrename</a>");
f9903ed0 717
9a58f7cb 718 echo "</tr>";
f9903ed0 719 }
720 }
721
722
fb964e0c 723 if (!empty($filelist)) {
f9903ed0 724 asort($filelist);
725 foreach ($filelist as $file) {
726
727 $icon = mimeinfo("icon", $file);
728
729 $count++;
730 $filename = $fullpath."/".$file;
731 $fileurl = "$wdir/$file";
732 $filesafe = rawurlencode($file);
733 $fileurlsafe = rawurlencode($fileurl);
45111451 734 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
f9903ed0 735
55e4b5f9 736 echo "<tr>";
f9903ed0 737
9a58f7cb 738 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />");
739 echo "<td align=\"left\" nowrap=\"nowrap\">";
3f8247c2 740 if ($CFG->slasharguments) {
741 $ffurl = "/file.php/$id$fileurl";
742 } else {
743 $ffurl = "/file.php?file=/$id$fileurl";
744 }
745 link_to_popup_window ($ffurl, "display",
839f2456 746 "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" border=\"0\" alt=\"File\" />",
f9903ed0 747 480, 640);
55e4b5f9 748 echo "<font size=\"-1\" face=\"Arial, Helvetica\">";
3f8247c2 749 link_to_popup_window ($ffurl, "display",
f9903ed0 750 htmlspecialchars($file),
751 480, 640);
55e4b5f9 752 echo "</font></td>";
f9903ed0 753
fedc147e 754 $file_size = filesize($filename);
755 print_cell("right", display_size($file_size));
f9903ed0 756 print_cell("right", $filedate);
757 if ($icon == "text.gif" || $icon == "html.gif") {
839f2456 758 $edittext = "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=edit\">$stredit</a>";
f9903ed0 759 } else if ($icon == "zip.gif") {
839f2456 760 $edittext = "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=unzip\">$strunzip</a>&nbsp;";
761 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=listzip\">$strlist</a> ";
862810dc 762 if (!empty($CFG->backup_version) and isteacheredit($id)) {
839f2456 763 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=restore\">$strrestore</a> ";
476fd9c7 764 }
f9903ed0 765 } else {
766 $edittext = "";
767 }
839f2456 768 print_cell("right", "$edittext <a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename\">$strrename</a>");
f9903ed0 769
9a58f7cb 770 echo "</tr>";
f9903ed0 771 }
772 }
9a58f7cb 773 echo "</table>";
774 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
f9903ed0 775
fb964e0c 776 if (empty($wdir)) {
f9903ed0 777 $wdir = "/";
778 }
779
9a58f7cb 780 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
781 echo "<tr><td>";
782 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
783 echo "<input type=\"hidden\" name=\"wdir\" value=\"$wdir\" /> ";
f9903ed0 784 $options = array (
0c377177 785 "move" => "$strmovetoanotherfolder",
786 "delete" => "$strdeletecompletely",
787 "zip" => "$strcreateziparchive"
f9903ed0 788 );
fb964e0c 789 if (!empty($count)) {
0c377177 790 choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:document.dirform.submit()");
f9903ed0 791 }
792
9a58f7cb 793 echo "</form>";
794 echo "<td align=\"center\">";
c897eac5 795 if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) {
9a58f7cb 796 echo "<form action=\"index.php\" method=\"get\">";
797 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
798 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
799 echo " <input type=\"hidden\" name=\"action\" value=\"paste\" />";
800 echo " <input type=\"submit\" value=\"$strmovefilestohere\" />";
801 echo "</form>";
f9903ed0 802 }
9a58f7cb 803 echo "<td align=\"right\">";
804 echo "<form action=\"index.php\" method=\"get\">";
805 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
806 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
807 echo " <input type=\"hidden\" name=\"action\" value=\"mkdir\" />";
808 echo " <input type=\"submit\" value=\"$strmakeafolder\" />";
809 echo "</form>";
810 echo "</td>";
811 echo "<td align=\"right\">";
812 echo "<form action=\"index.php\" method=\"get\">";
813 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
814 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
815 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
816 echo " <input type=\"submit\" value=\"$struploadafile\" />";
817 echo "</form>";
818 echo "</td></tr>";
819 echo "</table>";
820 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
f9903ed0 821
822}
823
824?>