Merged from 1.7.
[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
f1e0649c 10 require('../config.php');
11 require($CFG->libdir.'/filelib.php');
84c8ede0 12 require($CFG->libdir.'/adminlib.php');
f9903ed0 13
2515cd16 14 $id = required_param('id', PARAM_INT);
15 $file = optional_param('file', '', PARAM_PATH);
16 $wdir = optional_param('wdir', '', PARAM_PATH);
17 $action = optional_param('action', '', PARAM_ACTION);
18 $name = optional_param('name', '', PARAM_FILE);
19 $oldname = optional_param('oldname', '', PARAM_FILE);
61212e3f 20 $choose = optional_param('choose', '', PARAM_FILE); //in fact it is always 'formname.inputname'
4c71d8b2 21 $userfile= optional_param('userfile','',PARAM_FILE);
e77ea640 22 $save = optional_param('save', 0, PARAM_BOOL);
70cedbfd 23 $text = optional_param('text', '', PARAM_RAW);
e77ea640 24 $confirm = optional_param('confirm', 0, PARAM_BOOL);
8e73aa97 25
26 if ($choose) {
f1e6550b 27 if (count(explode('.', $choose)) > 2) {
8e73aa97 28 error('Incorrect format for choose parameter');
29 }
30 }
31
f9903ed0 32
5875b7d6 33 if (! $course = get_record("course", "id", $id) ) {
34 error("That's an invalid course id");
35 }
f9903ed0 36
84c8ede0 37 require_login($course);
f9903ed0 38
07637b9e 39 require_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $course->id));
f9903ed0 40
41 function html_footer() {
84c8ede0 42 global $course, $choose, $adminroot;
8e73aa97 43
d639dbf7 44 echo '</td></tr></table>';
45
84c8ede0 46 if ($course->id == SITEID) {
47 admin_externalpage_print_footer($adminroot);
48 } else {
49 print_footer($course);
50 }
f9903ed0 51 }
70cedbfd 52
0c377177 53 function html_header($course, $wdir, $formfield=""){
7c5829f4 54 global $CFG, $ME, $choose;
8ce53f0f 55
84c8ede0 56 if ($course->id == SITEID) {
55e4b5f9 57 $strfiles = get_string("sitefiles");
58 } else {
59 $strfiles = get_string("files");
60 }
8e73aa97 61
0c377177 62 if ($wdir == "/") {
65c59e30 63 $fullnav = "$strfiles";
0c377177 64 } else {
65 $dirs = explode("/", $wdir);
66 $numdirs = count($dirs);
67 $link = "";
68 $navigation = "";
fdfc6269 69 for ($i=1; $i<$numdirs-1; $i++) {
0c377177 70 $navigation .= " -> ";
71 $link .= "/".urlencode($dirs[$i]);
8e73aa97 72 $navigation .= "<a href=\"".$ME."?id=$course->id&amp;wdir=$link&amp;choose=$choose\">".$dirs[$i]."</a>";
0c377177 73 }
8e73aa97 74 $fullnav = "<a href=\"".$ME."?id=$course->id&amp;wdir=/&amp;choose=$choose\">$strfiles</a> $navigation -> ".$dirs[$numdirs-1];
65c59e30 75 }
0c377177 76
55e4b5f9 77
8e73aa97 78 if ($choose) {
79 print_header();
80
81 $chooseparts = explode('.', $choose);
f1e6550b 82 if (count($chooseparts)==2){
8e73aa97 83 ?>
f4ba7e1a 84 <script type="text/javascript">
85 //<![CDATA[
8e73aa97 86 function set_value(txt) {
87 opener.document.forms['<?php echo $chooseparts[0]."'].".$chooseparts[1] ?>.value = txt;
88 window.close();
89 }
f4ba7e1a 90 //]]>
8e73aa97 91 </script>
92
93 <?php
f1e6550b 94 } elseif (count($chooseparts)==1){
95 ?>
f4ba7e1a 96 <script type="text/javascript">
97 //<![CDATA[
f1e6550b 98 function set_value(txt) {
99 opener.document.getElementById('<?php echo $chooseparts[0] ?>').value = txt;
100 window.close();
101 }
f4ba7e1a 102 //]]>
f1e6550b 103 </script>
104
105 <?php
106
107 }
8e73aa97 108 $fullnav = str_replace('->', '&raquo;', "$course->shortname -> $fullnav");
7c5829f4 109 echo '<div id="nav-bar">'.$fullnav.'</div>';
8e73aa97 110
84c8ede0 111 if ($course->id == SITEID and $wdir != "/backupdata") {
8e73aa97 112 print_heading(get_string("publicsitefileswarning"), "center", 2);
113 }
55e4b5f9 114
65c59e30 115 } else {
8e73aa97 116
84c8ede0 117 if ($course->id == SITEID) {
8e73aa97 118
84c8ede0 119 if ($wdir == "/backupdata") {
120 $adminroot = admin_get_root();
121 admin_externalpage_setup('frontpagerestore', $adminroot);
122 admin_externalpage_print_header($adminroot);
123 } else {
124 $adminroot = admin_get_root();
125 admin_externalpage_setup('sitefiles', $adminroot);
126 admin_externalpage_print_header($adminroot);
127
128 print_heading(get_string("publicsitefileswarning"), "center", 2);
129
130 }
8e73aa97 131
132 } else {
133 print_header("$course->shortname: $strfiles", "$course->fullname",
134 "<a href=\"../course/view.php?id=$course->id\">$course->shortname".
135 "</a> -> $fullnav", $formfield);
136 }
0c377177 137 }
55e4b5f9 138
8e73aa97 139
d8ffb057 140 echo "<table border=\"0\" align=\"center\" cellspacing=\"3\" cellpadding=\"3\" width=\"640\">";
f9903ed0 141 echo "<tr>";
142 echo "<td colspan=\"2\">";
8e73aa97 143
f9903ed0 144 }
5875b7d6 145
8e73aa97 146
5875b7d6 147 if (! $basedir = make_upload_directory("$course->id")) {
6b174680 148 error("The site administrator needs to fix the file permissions");
f9903ed0 149 }
f9903ed0 150
f9903ed0 151 $baseweb = $CFG->wwwroot;
152
153// End of configuration and access control
154
f9903ed0 155
70cedbfd 156 if ($wdir == '') {
157 $wdir = "/";
ad4e0fcc 158 }
159
70cedbfd 160 if ($wdir{0} != '/') { //make sure $wdir starts with slash
161 $wdir = "/".$wdir;
f9903ed0 162 }
163
fdfc6269 164 if ($wdir == "/backupdata") {
165 if (! make_upload_directory("$course->id/backupdata")) { // Backup folder
166 error("Could not create backupdata folder. The site administrator needs to fix the file permissions");
167 }
168 }
169
70cedbfd 170 if (!is_dir($basedir.$wdir)) {
171 html_header($course, $wdir);
172 error("Requested directory does not exist.", "$CFG->wwwroot/files/index.php?id=$id");
173 }
f9903ed0 174
f9903ed0 175 switch ($action) {
176
177 case "upload":
0c377177 178 html_header($course, $wdir);
3bb4d844 179 require_once($CFG->dirroot.'/lib/uploadlib.php');
70cedbfd 180
e77ea640 181 if ($save and confirm_sesskey()) {
6f61241c 182 $course->maxbytes = 0; // We are ignoring course limits
3bb4d844 183 $um = new upload_manager('userfile',false,false,$course,false,0);
184 $dir = "$basedir$wdir";
185 if ($um->process_file_uploads($dir)) {
186 notify(get_string('uploadedfile'));
f9903ed0 187 }
3bb4d844 188 // um will take care of error reporting.
f9903ed0 189 displaydir($wdir);
f9903ed0 190 } else {
1dab3c83 191 $upload_max_filesize = get_max_upload_file_size($CFG->maxbytes);
fedc147e 192 $filesize = display_size($upload_max_filesize);
6e40c33d 193
3b7c1de9 194 $struploadafile = get_string("uploadafile");
195 $struploadthisfile = get_string("uploadthisfile");
196 $strmaxsize = get_string("maxsize", "", $filesize);
197 $strcancel = get_string("cancel");
198
d639dbf7 199 echo "<p>$struploadafile ($strmaxsize) --> <b>$wdir</b></p>";
c5bb2770 200 echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"index.php\">";
9a58f7cb 201 echo "<table><tr><td colspan=\"2\">";
755a1c4b 202 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 203 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
204 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
205 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
2515cd16 206 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
6f61241c 207 upload_print_form_fragment(1,array('userfile'),null,false,null,$upload_max_filesize,0,false);
c5bb2770 208 echo " </td></tr></table>";
9a58f7cb 209 echo " <input type=\"submit\" name=\"save\" value=\"$struploadthisfile\" />";
210 echo "</form>";
9a58f7cb 211 echo "<form action=\"index.php\" method=\"get\">";
755a1c4b 212 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 213 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
214 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
215 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
216 echo " <input type=\"submit\" value=\"$strcancel\" />";
217 echo "</form>";
f9903ed0 218 }
219 html_footer();
220 break;
221
222 case "delete":
e77ea640 223 if ($confirm and confirm_sesskey()) {
0c377177 224 html_header($course, $wdir);
22060485 225 if (!empty($USER->filelist)) {
226 foreach ($USER->filelist as $file) {
607a6aa8 227 $fullfile = $basedir.'/'.$file;
22060485 228 if (! fulldelete($fullfile)) {
229 echo "<br />Error: Could not delete: $fullfile";
230 }
f9903ed0 231 }
232 }
233 clearfilelist();
234 displaydir($wdir);
235 html_footer();
236
237 } else {
0c377177 238 html_header($course, $wdir);
d27a5b25 239
607809b3 240 if (setfilelist($_POST)) {
22060485 241 notify(get_string('deletecheckwarning').':');
242 print_simple_box_start("center");
243 printfilelist($USER->filelist);
244 print_simple_box_end();
245 echo "<br />";
246
a69be0d8 247 require_once($CFG->dirroot.'/mod/resource/lib.php');
248 $block = resource_delete_warning($course, $USER->filelist);
249
250 if (empty($CFG->resource_blockdeletingfile) or $block == '') {
251 $optionsyes = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'choose'=>$choose);
252 $optionsno = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
253 notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
254 } else {
255
256 notify(get_string('warningblockingdelete', 'resource'));
257 $options = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose);
258 print_continue("index.php?id=$id&amp;wdir=$wdir&amp;action=cancel&amp;choose=$choose");
22060485 259 }
f9903ed0 260 } else {
261 displaydir($wdir);
262 }
263 html_footer();
264 }
265 break;
266
267 case "move":
0c377177 268 html_header($course, $wdir);
e101802e 269 if (($count = setfilelist($_POST)) and confirm_sesskey()) {
f9903ed0 270 $USER->fileop = $action;
271 $USER->filesource = $wdir;
9a58f7cb 272 echo "<p align=\"center\">";
1efcfce8 273 print_string("selectednowmove", "moodle", $count);
274 echo "</p>";
f9903ed0 275 }
276 displaydir($wdir);
277 html_footer();
278 break;
279
280 case "paste":
0c377177 281 html_header($course, $wdir);
e101802e 282 if (isset($USER->fileop) and ($USER->fileop == "move") and confirm_sesskey()) {
f9903ed0 283 foreach ($USER->filelist as $file) {
284 $shortfile = basename($file);
607a6aa8 285 $oldfile = $basedir.'/'.$file;
f9903ed0 286 $newfile = $basedir.$wdir."/".$shortfile;
287 if (!rename($oldfile, $newfile)) {
5c8ea9d9 288 echo "<p>Error: $shortfile not moved</p>";
f9903ed0 289 }
290 }
291 }
292 clearfilelist();
293 displaydir($wdir);
294 html_footer();
295 break;
296
297 case "rename":
70cedbfd 298 if (($name != '') and confirm_sesskey()) {
0c377177 299 html_header($course, $wdir);
2515cd16 300 $name = clean_filename($name);
f9903ed0 301 if (file_exists($basedir.$wdir."/".$name)) {
d27a5b25 302 echo "<center>Error: $name already exists!</center>";
f9903ed0 303 } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) {
d27a5b25 304 echo "<p align=\"center\">Error: could not rename $oldname to $name</p>";
a69be0d8 305 } else {
306 //file was renamed now update resources if needed
307 require_once($CFG->dirroot.'/mod/resource/lib.php');
308 resource_renamefiles($course, $wdir, $oldname, $name);
f9903ed0 309 }
310 displaydir($wdir);
d27a5b25 311
f9903ed0 312 } else {
27c40942 313 $strrename = get_string("rename");
314 $strcancel = get_string("cancel");
315 $strrenamefileto = get_string("renamefileto", "moodle", $file);
0c377177 316 html_header($course, $wdir, "form.name");
5c8ea9d9 317 echo "<p>$strrenamefileto:</p>";
9a58f7cb 318 echo "<table><tr><td>";
b7dc2256 319 echo "<form action=\"index.php\" method=\"post\">";
755a1c4b 320 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 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=\"rename\" />";
324 echo " <input type=\"hidden\" name=\"oldname\" value=\"$file\" />";
2515cd16 325 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
9a58f7cb 326 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"$file\" />";
327 echo " <input type=\"submit\" value=\"$strrename\" />";
328 echo "</form>";
329 echo "</td><td>";
330 echo "<form action=\"index.php\" method=\"get\">";
755a1c4b 331 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 332 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
333 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
334 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
335 echo " <input type=\"submit\" value=\"$strcancel\" />";
336 echo "</form>";
337 echo "</td></tr></table>";
f9903ed0 338 }
339 html_footer();
340 break;
341
bf9fd1b2 342 case "makedir":
70cedbfd 343 if (($name != '') and confirm_sesskey()) {
0c377177 344 html_header($course, $wdir);
f9903ed0 345 $name = clean_filename($name);
fc613dff 346 if (file_exists("$basedir$wdir/$name")) {
f9903ed0 347 echo "Error: $name already exists!";
70cedbfd 348 } else if (! make_upload_directory("$course->id$wdir/$name")) {
f9903ed0 349 echo "Error: could not create $name";
350 }
351 displaydir($wdir);
70cedbfd 352
f9903ed0 353 } else {
27c40942 354 $strcreate = get_string("create");
355 $strcancel = get_string("cancel");
356 $strcreatefolder = get_string("createfolder", "moodle", $wdir);
0c377177 357 html_header($course, $wdir, "form.name");
d639dbf7 358 echo "<p>$strcreatefolder:</p>";
9a58f7cb 359 echo "<table><tr><td>";
b7dc2256 360 echo "<form action=\"index.php\" method=\"post\">";
755a1c4b 361 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 362 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
363 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
bf9fd1b2 364 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
9a58f7cb 365 echo " <input type=\"text\" name=\"name\" size=\"35\" />";
2515cd16 366 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
9a58f7cb 367 echo " <input type=\"submit\" value=\"$strcreate\" />";
368 echo "</form>";
369 echo "</td><td>";
370 echo "<form action=\"index.php\" method=\"get\">";
755a1c4b 371 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 372 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
373 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
374 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
375 echo " <input type=\"submit\" value=\"$strcancel\" />";
376 echo "</form>";
377 echo "</td></tr></table>";
f9903ed0 378 }
379 html_footer();
380 break;
381
382 case "edit":
0c377177 383 html_header($course, $wdir);
70cedbfd 384 if (($text != '') and confirm_sesskey()) {
607a6aa8 385 $fileptr = fopen($basedir.'/'.$file,"w");
c64633f6 386 $text = preg_replace('/\x0D/', '', $text); // http://moodle.org/mod/forum/discuss.php?d=38860
f9903ed0 387 fputs($fileptr, stripslashes($text));
388 fclose($fileptr);
389 displaydir($wdir);
70cedbfd 390
f9903ed0 391 } else {
9a58f7cb 392 $streditfile = get_string("edit", "", "<b>$file</b>");
607a6aa8 393 $fileptr = fopen($basedir.'/'.$file, "r");
394 $contents = fread($fileptr, filesize($basedir.'/'.$file));
f9903ed0 395 fclose($fileptr);
396
04fa59d0 397 if (mimeinfo("type", $file) == "text/html") {
ffae3d46 398 $usehtmleditor = can_use_html_editor();
04fa59d0 399 } else {
400 $usehtmleditor = false;
04fa59d0 401 }
206a5075 402 $usehtmleditor = false; // Always keep it off for now
04fa59d0 403
404 print_heading("$streditfile");
405
9a58f7cb 406 echo "<table><tr><td colspan=\"2\">";
b7dc2256 407 echo "<form action=\"index.php\" method=\"post\">";
755a1c4b 408 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 409 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
410 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
411 echo " <input type=\"hidden\" name=\"file\" value=\"$file\" />";
412 echo " <input type=\"hidden\" name=\"action\" value=\"edit\" />";
e101802e 413 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
ffae3d46 414 print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents);
9a58f7cb 415 echo "</td></tr><tr><td>";
416 echo " <input type=\"submit\" value=\"".get_string("savechanges")."\" />";
417 echo "</form>";
418 echo "</td><td>";
419 echo "<form action=\"index.php\" method=\"get\">";
755a1c4b 420 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 421 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
422 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
423 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
424 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
425 echo "</form>";
426 echo "</td></tr></table>";
04fa59d0 427
70cedbfd 428 if ($usehtmleditor) {
ffae3d46 429 use_html_editor();
04fa59d0 430 }
431
432
f9903ed0 433 }
434 html_footer();
435 break;
436
437 case "zip":
70cedbfd 438 if (($name != '') and confirm_sesskey()) {
0c377177 439 html_header($course, $wdir);
f9903ed0 440 $name = clean_filename($name);
1a11dfd1 441
442 $files = array();
443 foreach ($USER->filelist as $file) {
444 $files[] = "$basedir/$file";
445 }
446
70cedbfd 447 if (!zip_files($files,"$basedir$wdir/$name")) {
1a11dfd1 448 error(get_string("zipfileserror","error"));
f9903ed0 449 }
1a11dfd1 450
f9903ed0 451 clearfilelist();
452 displaydir($wdir);
70cedbfd 453
f9903ed0 454 } else {
0c377177 455 html_header($course, $wdir, "form.name");
27c40942 456
607809b3 457 if (setfilelist($_POST)) {
9a58f7cb 458 echo "<p align=\"center\">".get_string("youareabouttocreatezip").":</p>";
f9903ed0 459 print_simple_box_start("center");
460 printfilelist($USER->filelist);
461 print_simple_box_end();
9a58f7cb 462 echo "<br />";
d639dbf7 463 echo "<p align=\"center\">".get_string("whattocallzip")."</p>";
9a58f7cb 464 echo "<table><tr><td>";
b7dc2256 465 echo "<form action=\"index.php\" method=\"post\">";
755a1c4b 466 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 467 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
468 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
469 echo " <input type=\"hidden\" name=\"action\" value=\"zip\" />";
470 echo " <input type=\"text\" name=\"name\" size=\"35\" value=\"new.zip\" />";
2515cd16 471 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
9a58f7cb 472 echo " <input type=\"submit\" value=\"".get_string("createziparchive")."\" />";
473 echo "</form>";
474 echo "</td><td>";
475 echo "<form action=\"index.php\" method=\"get\">";
755a1c4b 476 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 477 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
478 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
479 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
480 echo " <input type=\"submit\" value=\"".get_string("cancel")."\" />";
481 echo "</form>";
482 echo "</td></tr></table>";
f9903ed0 483 } else {
484 displaydir($wdir);
485 clearfilelist();
486 }
487 }
488 html_footer();
489 break;
490
491 case "unzip":
0c377177 492 html_header($course, $wdir);
70cedbfd 493 if (($file != '') and confirm_sesskey()) {
e64984a0 494 $strok = get_string("ok");
495 $strunpacking = get_string("unpacking", "", $file);
496
9a58f7cb 497 echo "<p align=\"center\">$strunpacking:</p>";
e64984a0 498
f9903ed0 499 $file = basename($file);
e64984a0 500
70cedbfd 501 if (!unzip_file("$basedir$wdir/$file")) {
343fc727 502 error(get_string("unzipfileserror","error"));
e64984a0 503 }
504
9a58f7cb 505 echo "<center><form action=\"index.php\" method=\"get\">";
755a1c4b 506 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 507 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
508 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
509 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
510 echo " <input type=\"submit\" value=\"$strok\" />";
511 echo "</form>";
512 echo "</center>";
f9903ed0 513 } else {
514 displaydir($wdir);
515 }
516 html_footer();
517 break;
518
e64984a0 519 case "listzip":
520 html_header($course, $wdir);
70cedbfd 521 if (($file != '') and confirm_sesskey()) {
e64984a0 522 $strname = get_string("name");
523 $strsize = get_string("size");
524 $strmodified = get_string("modified");
525 $strok = get_string("ok");
526 $strlistfiles = get_string("listfiles", "", $file);
527
9a58f7cb 528 echo "<p align=\"center\">$strlistfiles:</p>";
e64984a0 529 $file = basename($file);
530
5151ae66 531 include_once("$CFG->libdir/pclzip/pclzip.lib.php");
70cedbfd 532 $archive = new PclZip(cleardoubleslashes("$basedir$wdir/$file"));
533 if (!$list = $archive->listContent(cleardoubleslashes("$basedir$wdir"))) {
e64984a0 534 notify($archive->errorInfo(true));
535
536 } else {
6b091c94 537 echo "<table cellpadding=\"4\" cellspacing=\"2\" border=\"0\" width=\"640\" class=\"files\">";
54926e78 538 echo "<tr class=\"file\"><th align=\"left\" class=\"header name\" scope=\"col\">$strname</th><th align=\"right\" class=\"header size\" scope=\"col\">$strsize</th><th align=\"right\" class=\"header date\" scope=\"col\">$strmodified</th></tr>";
e64984a0 539 foreach ($list as $item) {
540 echo "<tr>";
18478666 541 print_cell("left", s($item['filename']), 'name');
e64984a0 542 if (! $item['folder']) {
6b091c94 543 print_cell("right", display_size($item['size']), 'size');
e64984a0 544 } else {
545 echo "<td>&nbsp;</td>";
546 }
547 $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
6b091c94 548 print_cell("right", $filedate, 'date');
e64984a0 549 echo "</tr>";
550 }
551 echo "</table>";
552 }
9a58f7cb 553 echo "<br /><center><form action=\"index.php\" method=\"get\">";
755a1c4b 554 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 555 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
556 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
557 echo " <input type=\"hidden\" name=\"action\" value=\"cancel\" />";
558 echo " <input type=\"submit\" value=\"$strok\" />";
559 echo "</form>";
560 echo "</center>";
e64984a0 561 } else {
562 displaydir($wdir);
563 }
564 html_footer();
565 break;
566
476fd9c7 567 case "restore":
568 html_header($course, $wdir);
70cedbfd 569 if (($file != '') and confirm_sesskey()) {
9a58f7cb 570 echo "<p align=\"center\">".get_string("youaregoingtorestorefrom").":</p>";
476fd9c7 571 print_simple_box_start("center");
572 echo $file;
573 print_simple_box_end();
c79f9629 574 echo "<br />";
d8ffb057 575 echo "<p align=\"center\">".get_string("areyousuretorestorethisinfo")."</p>";
ad4e0fcc 576 $restore_path = "$CFG->wwwroot/backup/restore.php";
476fd9c7 577 notice_yesno (get_string("areyousuretorestorethis"),
69179494 578 $restore_path."?id=".$id."&amp;file=".cleardoubleslashes($id.$wdir."/".$file)."&amp;method=manual",
d8ffb057 579 "index.php?id=$id&amp;wdir=$wdir&amp;action=cancel");
476fd9c7 580 } else {
581 displaydir($wdir);
582 }
583 html_footer();
584 break;
70cedbfd 585
ea7a672d 586 case "cancel":
f9903ed0 587 clearfilelist();
588
589 default:
0c377177 590 html_header($course, $wdir);
f9903ed0 591 displaydir($wdir);
592 html_footer();
593 break;
594}
595
596
597/// FILE FUNCTIONS ///////////////////////////////////////////////////////////
598
f9903ed0 599
600function setfilelist($VARS) {
601 global $USER;
602
603 $USER->filelist = array ();
604 $USER->fileop = "";
605
606 $count = 0;
607 foreach ($VARS as $key => $val) {
608 if (substr($key,0,4) == "file") {
609 $count++;
e101802e 610 $val = rawurldecode($val);
70cedbfd 611 $USER->filelist[] = clean_param($val, PARAM_PATH);
f9903ed0 612 }
613 }
f9903ed0 614 return $count;
615}
616
617function clearfilelist() {
618 global $USER;
619
620 $USER->filelist = array ();
621 $USER->fileop = "";
622}
623
624
625function printfilelist($filelist) {
241f6f30 626 global $CFG, $basedir;
f9903ed0 627
2bbaf749 628 $strfolder = get_string("folder");
629 $strfile = get_string("file");
630
f9903ed0 631 foreach ($filelist as $file) {
607a6aa8 632 if (is_dir($basedir.'/'.$file)) {
0d905d9f 633 echo "<img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> $file<br />";
f9903ed0 634 $subfilelist = array();
607a6aa8 635 $currdir = opendir($basedir.'/'.$file);
0219eb2b 636 while (false !== ($subfile = readdir($currdir))) {
f9903ed0 637 if ($subfile <> ".." && $subfile <> ".") {
638 $subfilelist[] = $file."/".$subfile;
639 }
640 }
641 printfilelist($subfilelist);
642
70cedbfd 643 } else {
f9903ed0 644 $icon = mimeinfo("icon", $file);
0d905d9f 645 echo "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> $file<br />";
f9903ed0 646 }
647 }
648}
649
650
6b091c94 651function print_cell($alignment='center', $text='&nbsp;', $class='') {
652 if ($class) {
653 $class = ' class="'.$class.'"';
654 }
655 echo '<td align="'.$alignment.'" nowrap="nowrap"'.$class.'>'.$text.'</td>';
f9903ed0 656}
657
658function displaydir ($wdir) {
659// $wdir == / or /a or /a/b/c/d etc
660
661 global $basedir;
662 global $id;
6e4c09db 663 global $USER, $CFG;
8e73aa97 664 global $choose;
f9903ed0 665
666 $fullpath = $basedir.$wdir;
70cedbfd 667 $dirlist = array();
f9903ed0 668
669 $directory = opendir($fullpath); // Find all files
211ea3e9 670 while (false !== ($file = readdir($directory))) {
5875b7d6 671 if ($file == "." || $file == "..") {
f9903ed0 672 continue;
673 }
70cedbfd 674
f9903ed0 675 if (is_dir($fullpath."/".$file)) {
676 $dirlist[] = $file;
677 } else {
678 $filelist[] = $file;
679 }
680 }
681 closedir($directory);
682
0c377177 683 $strname = get_string("name");
684 $strsize = get_string("size");
685 $strmodified = get_string("modified");
686 $straction = get_string("action");
687 $strmakeafolder = get_string("makeafolder");
688 $struploadafile = get_string("uploadafile");
82c85992 689 $strselectall = get_string("selectall");
690 $strselectnone = get_string("deselectall");
0c377177 691 $strwithchosenfiles = get_string("withchosenfiles");
692 $strmovetoanotherfolder = get_string("movetoanotherfolder");
693 $strmovefilestohere = get_string("movefilestohere");
694 $strdeletecompletely = get_string("deletecompletely");
695 $strcreateziparchive = get_string("createziparchive");
696 $strrename = get_string("rename");
697 $stredit = get_string("edit");
698 $strunzip = get_string("unzip");
e64984a0 699 $strlist = get_string("list");
476fd9c7 700 $strrestore= get_string("restore");
2bbaf749 701 $strchoose = get_string("choose");
702 $strfolder = get_string("folder");
703 $strfile = get_string("file");
0c377177 704
f9903ed0 705
b7dc2256 706 echo "<form action=\"index.php\" method=\"post\" id=\"dirform\">";
755a1c4b 707 echo '<input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 708 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
70cedbfd 709 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\" class=\"files\">";
9a58f7cb 710 echo "<tr>";
54926e78 711 echo "<th width=\"5\" scope=\"col\"></th>";
712 echo "<th align=\"left\" class=\"header name\" scope=\"col\">$strname</th>";
713 echo "<th align=\"right\" class=\"header size\" scope=\"col\">$strsize</th>";
714 echo "<th align=\"right\" class=\"header date\" scope=\"col\">$strmodified</th>";
715 echo "<th align=\"right\" class=\"header commands\" scope=\"col\">$straction</th>";
9a58f7cb 716 echo "</tr>\n";
f9903ed0 717
70cedbfd 718 if ($wdir != "/") {
f4b351a3 719 $dirlist[] = '..';
720 }
f9903ed0 721
f9903ed0 722 $count = 0;
723
fb964e0c 724 if (!empty($dirlist)) {
f9903ed0 725 asort($dirlist);
726 foreach ($dirlist as $dir) {
6b091c94 727 echo "<tr class=\"folder\">";
f9903ed0 728
6b091c94 729 if ($dir == '..') {
f4b351a3 730 $fileurl = rawurlencode(dirname($wdir));
731 print_cell();
2bbaf749 732 // alt attribute intentionally empty to prevent repetition in screen reader
0d905d9f 733 print_cell('left', '<a href="index.php?id='.$id.'&amp;wdir='.$fileurl.'&amp;choose='.$choose.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" />&nbsp;'.get_string('parentfolder').'</a>', 'name');
f4b351a3 734 print_cell();
735 print_cell();
736 print_cell();
6b091c94 737
738 } else {
f4b351a3 739 $count++;
740 $filename = $fullpath."/".$dir;
741 $fileurl = rawurlencode($wdir."/".$dir);
742 $filesafe = rawurlencode($dir);
743 $filesize = display_size(get_directory_size("$fullpath/$dir"));
744 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
6b091c94 745 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
0d905d9f 746 print_cell("left", "<a href=\"index.php?id=$id&amp;wdir=$fileurl&amp;choose=$choose\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" />&nbsp;".htmlspecialchars($dir)."</a>", 'name');
6b091c94 747 print_cell("right", $filesize, 'size');
748 print_cell("right", $filedate, 'date');
749 print_cell("right", "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename&amp;choose=$choose\">$strrename</a>", 'commands');
f4b351a3 750 }
70cedbfd 751
9a58f7cb 752 echo "</tr>";
f9903ed0 753 }
754 }
755
756
fb964e0c 757 if (!empty($filelist)) {
f9903ed0 758 asort($filelist);
759 foreach ($filelist as $file) {
760
761 $icon = mimeinfo("icon", $file);
762
763 $count++;
764 $filename = $fullpath."/".$file;
b2fcca27 765 $fileurl = trim($wdir, "/")."/$file";
f9903ed0 766 $filesafe = rawurlencode($file);
767 $fileurlsafe = rawurlencode($fileurl);
45111451 768 $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");
f9903ed0 769
b2fcca27 770 $selectfile = trim($fileurl, "/");
8e73aa97 771
6b091c94 772 echo "<tr class=\"file\">";
f9903ed0 773
6b091c94 774 print_cell("center", "<input type=\"checkbox\" name=\"file$count\" value=\"$fileurl\" />", 'checkbox');
775 echo "<td align=\"left\" nowrap=\"nowrap\" class=\"name\">";
3f8247c2 776 if ($CFG->slasharguments) {
79b37009 777 $ffurl = str_replace('//', '/', "/file.php/$id/$fileurl");
3f8247c2 778 } else {
79b37009 779 $ffurl = str_replace('//', '/', "/file.php?file=/$id/$fileurl");
3f8247c2 780 }
70cedbfd 781 link_to_popup_window ($ffurl, "display",
0d905d9f 782 "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".htmlspecialchars($file),
f9903ed0 783 480, 640);
6b091c94 784 echo "</td>";
f9903ed0 785
fedc147e 786 $file_size = filesize($filename);
6b091c94 787 print_cell("right", display_size($file_size), 'size');
788 print_cell("right", $filedate, 'date');
8e73aa97 789
790 if ($choose) {
5294b1d3 791 $edittext = "<strong><a onclick=\"return set_value('$selectfile')\" href=\"#\">$strchoose</a></strong>&nbsp;";
8e73aa97 792 } else {
793 $edittext = '';
794 }
795
796
f9903ed0 797 if ($icon == "text.gif" || $icon == "html.gif") {
8e73aa97 798 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=edit&amp;choose=$choose\">$stredit</a>";
f9903ed0 799 } else if ($icon == "zip.gif") {
8e73aa97 800 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=unzip&amp;sesskey=$USER->sesskey&amp;choose=$choose\">$strunzip</a>&nbsp;";
801 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$fileurl&amp;action=listzip&amp;sesskey=$USER->sesskey&amp;choose=$choose\">$strlist</a> ";
17d6a25e 802 if (!empty($CFG->backup_version) and has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
8e73aa97 803 $edittext .= "<a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=restore&amp;sesskey=$USER->sesskey&amp;choose=$choose\">$strrestore</a> ";
476fd9c7 804 }
f9903ed0 805 }
8e73aa97 806
6b091c94 807 print_cell("right", "$edittext <a href=\"index.php?id=$id&amp;wdir=$wdir&amp;file=$filesafe&amp;action=rename&amp;choose=$choose\">$strrename</a>", 'commands');
70cedbfd 808
9a58f7cb 809 echo "</tr>";
f9903ed0 810 }
811 }
9a58f7cb 812 echo "</table>";
813 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
f9903ed0 814
70cedbfd 815 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\">";
9a58f7cb 816 echo "<tr><td>";
817 echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
755a1c4b 818 echo '<input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 819 echo "<input type=\"hidden\" name=\"wdir\" value=\"$wdir\" /> ";
e101802e 820 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
f9903ed0 821 $options = array (
0c377177 822 "move" => "$strmovetoanotherfolder",
823 "delete" => "$strdeletecompletely",
824 "zip" => "$strcreateziparchive"
f9903ed0 825 );
fb964e0c 826 if (!empty($count)) {
d2ce367f 827 choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:getElementById('dirform').submit()");
f9903ed0 828 }
cef55421 829 echo "</td></tr></table>";
9a58f7cb 830 echo "</form>";
cef55421 831 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"640\"><tr>";
9a58f7cb 832 echo "<td align=\"center\">";
c897eac5 833 if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) {
9a58f7cb 834 echo "<form action=\"index.php\" method=\"get\">";
755a1c4b 835 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 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=\"paste\" />";
e101802e 839 echo " <input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
9a58f7cb 840 echo " <input type=\"submit\" value=\"$strmovefilestohere\" />";
841 echo "</form>";
f9903ed0 842 }
954ec82f 843 echo "</td>";
9a58f7cb 844 echo "<td align=\"right\">";
845 echo "<form action=\"index.php\" method=\"get\">";
755a1c4b 846 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 847 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
848 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
bf9fd1b2 849 echo " <input type=\"hidden\" name=\"action\" value=\"makedir\" />";
9a58f7cb 850 echo " <input type=\"submit\" value=\"$strmakeafolder\" />";
851 echo "</form>";
852 echo "</td>";
82c85992 853 echo "<td align=\"right\">";
70cedbfd 854 echo "<form action=\"index.php\" method=\"get\">"; //dummy form - alignment only
d639dbf7 855 echo " <input type=\"button\" value=\"$strselectall\" onclick=\"checkall();\" />";
856 echo " <input type=\"button\" value=\"$strselectnone\" onclick=\"uncheckall();\" />";
70cedbfd 857 echo "</form>";
82c85992 858 echo "</td>";
9a58f7cb 859 echo "<td align=\"right\">";
860 echo "<form action=\"index.php\" method=\"get\">";
755a1c4b 861 echo ' <input type="hidden" name="choose" value="'.$choose.'" />';
9a58f7cb 862 echo " <input type=\"hidden\" name=\"id\" value=\"$id\" />";
863 echo " <input type=\"hidden\" name=\"wdir\" value=\"$wdir\" />";
864 echo " <input type=\"hidden\" name=\"action\" value=\"upload\" />";
865 echo " <input type=\"submit\" value=\"$struploadafile\" />";
866 echo "</form>";
867 echo "</td></tr>";
868 echo "</table>";
869 echo "<hr width=\"640\" align=\"center\" noshade=\"noshade\" size=\"1\" />";
f9903ed0 870
871}
872
873?>