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