id); if (! isteacher($course->id) ) { error("Only teachers can edit files"); } if (!$CFG->zip) { $CFG->zip = "/usr/bin/zip"; } if (!$CFG->unzip) { $CFG->unzip = "/usr/bin/unzip"; } function html_footer() { global $course; echo ""; print_footer($course); } function html_header($course, $wdir, $formfield=""){ $strfiles = get_string("files"); if ($wdir == "/") { print_header("$course->shortname: $strfiles", "$course->fullname", "id\">$course->shortname -> $strfiles", $formfield); } else { $dirs = explode("/", $wdir); $numdirs = count($dirs); $link = ""; $navigation = ""; for ($i=1; $i<$numdirs; $i++) { $navigation .= " -> "; $link .= "/".urlencode($dirs[$i]); $navigation .= "id&wdir=$link\">".$dirs[$i].""; } print_header("$course->shortname: $strfiles", "$course->fullname", "id\">$course->shortname -> id&wdir=/\">$strfiles $navigation", $formfield); } echo ""; echo ""; echo "\n"; } function displaydir ($wdir) { // $wdir == / or /a or /a/b/c/d etc global $basedir; global $id; global $USER, $CFG; $fullpath = $basedir.$wdir; $directory = opendir($fullpath); // Find all files while ($file = readdir($directory)) { if ($file == "." || $file == "..") { continue; } if (is_dir($fullpath."/".$file)) { $dirlist[] = $file; } else { $filelist[] = $file; } } closedir($directory); $strname = get_string("name"); $strsize = get_string("size"); $strmodified = get_string("modified"); $straction = get_string("action"); $strmakeafolder = get_string("makeafolder"); $struploadafile = get_string("uploadafile"); $strwithchosenfiles = get_string("withchosenfiles"); $strmovetoanotherfolder = get_string("movetoanotherfolder"); $strmovefilestohere = get_string("movefilestohere"); $strdeletecompletely = get_string("deletecompletely"); $strcreateziparchive = get_string("createziparchive"); $strrename = get_string("rename"); $stredit = get_string("edit"); $strunzip = get_string("unzip"); echo ""; echo "
"; echo "
"; } if (! $basedir = make_upload_directory("$course->id")) { error("The site administrator needs to fix the file permissions"); } $baseweb = $CFG->wwwroot; // End of configuration and access control require("mimetypes.php"); $regexp="\\.\\."; if (ereg( $regexp, $file, $regs )| ereg( $regexp, $wdir,$regs )) { $message = "Error: Directories can not contain \"..\""; $wdir = "/"; $action = ""; } if (!$wdir) { $wdir="/"; } switch ($action) { case "upload": html_header($course, $wdir); if ($save) { if (!is_uploaded_file($userfile['tmp_name']) and $userfile['size'] > 0) { echo "

Error: That was not a valid file."; } else { $userfile_name = clean_filename($userfile['name']); if ($userfile_name) { $newfile = "$basedir$wdir/$userfile_name"; if (move_uploaded_file($userfile['tmp_name'], $newfile)) { echo "Uploaded $userfile_name (".$userfile['type'].") to $wdir"; } else { echo "A problem occurred while uploading '$userfile_name'"; echo " (possibly it was too large)"; } } } displaydir($wdir); } else { $upload_max_filesize = get_max_upload_file_size(); $filesize = display_size($upload_max_filesize); echo "

Upload a file (maximum size $filesize) into $wdir:"; echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo " "; echo "
"; echo " "; echo ""; echo ""; echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; } html_footer(); break; case "delete": if (!empty($confirm)) { html_header($course, $wdir); foreach ($USER->filelist as $file) { $fullfile = $basedir.$file; if (! fulldelete($fullfile)) { echo "
Error: Could not delete: $fullfile"; } } clearfilelist(); displaydir($wdir); html_footer(); } else { html_header($course, $wdir); if (setfilelist($HTTP_POST_VARS)) { echo "

You are about to delete:

"; print_simple_box_start("center"); printfilelist($USER->filelist); print_simple_box_end(); echo "
"; notice_yesno ("Are you sure you want to delete these?", "index.php?id=$id&wdir=$wdir&action=delete&confirm=1", "index.php?id=$id&wdir=$wdir&action=cancel"); } else { displaydir($wdir); } html_footer(); } break; case "move": html_header($course, $wdir); if ($count = setfilelist($HTTP_POST_VARS)) { $USER->fileop = $action; $USER->filesource = $wdir; save_session("USER"); echo "

$count files selected for moving. Now go to the destination and press \"Move files to here\".

"; } displaydir($wdir); html_footer(); break; case "paste": html_header($course, $wdir); if (isset($USER->fileop) and $USER->fileop == "move") { foreach ($USER->filelist as $file) { $shortfile = basename($file); $oldfile = $basedir.$file; $newfile = $basedir.$wdir."/".$shortfile; if (!rename($oldfile, $newfile)) { echo "

Error: $shortfile not moved"; } } } clearfilelist(); displaydir($wdir); html_footer(); break; case "rename": if (!empty($name)) { html_header($course, $wdir); $name = clean_filename($name); if (file_exists($basedir.$wdir."/".$name)) { echo "Error: $name already exists!"; } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) { echo "Error: could not rename $oldname to $name"; } displaydir($wdir); } else { html_header($course, $wdir, "form.name"); echo "

Rename $file to:"; echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; } html_footer(); break; case "mkdir": if (!empty($name)) { html_header($course, $wdir); $name = clean_filename($name); if (file_exists($basedir.$wdir."/".$name)) { echo "Error: $name already exists!"; } else if (!mkdir($basedir.$wdir."/".$name, 0750)) { echo "Error: could not create $name"; } displaydir($wdir); } else { html_header($course, $wdir, "form.name"); echo "

Create folder in $wdir:"; echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; } html_footer(); break; case "edit": html_header($course, $wdir); if (isset($text)) { $fileptr = fopen($basedir.$file,"w"); fputs($fileptr, stripslashes($text)); fclose($fileptr); displaydir($wdir); } else { $streditfile = get_string("edit", "", "$file"); $fileptr = fopen($basedir.$file, "r"); $contents = fread($fileptr, filesize($basedir.$file)); fclose($fileptr); if (mimeinfo("type", $file) == "text/html") { if ($usehtmleditor = can_use_richtext_editor()) { $onsubmit = "onsubmit=\"copyrichtext(document.form.text);\""; } else { $onsubmit = ""; } } else { $usehtmleditor = false; $onsubmit = ""; } print_heading("$streditfile"); echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; print_textarea($usehtmleditor, 25, 80, 680, 400, "text", $contents); echo "
"; echo " "; echo ""; echo ""; echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; if ($usehtmleditor) { print_richedit_javascript("form", "text", "yes"); } } html_footer(); break; case "zip": if (!empty($name)) { html_header($course, $wdir); $name = clean_filename($name); $files = ""; foreach ($USER->filelist as $file) { $files .= basename($file); $files .= " "; } $command = "cd $basedir/$wdir ; $CFG->zip -r $name $files"; Exec($command); clearfilelist(); displaydir($wdir); } else { html_header($course, $wdir, "form.name"); if (setfilelist($HTTP_POST_VARS)) { echo "

You are about create a zip file containing:

"; print_simple_box_start("center"); printfilelist($USER->filelist); print_simple_box_end(); echo "
"; echo "

What do you want to call the zip file?"; echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; } else { displaydir($wdir); clearfilelist(); } } html_footer(); break; case "unzip": html_header($course, $wdir); if (!empty($file)) { echo "

Unzipping $file:

"; print_simple_box_start("center"); echo "
";
                $file = basename($file);
                $command = "cd $basedir/$wdir ; $CFG->unzip -o $file 2>&1";
                passthru($command);
                echo "
"; print_simple_box_end(); echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; } else { displaydir($wdir); } html_footer(); break; case "cancel"; clearfilelist(); default: html_header($course, $wdir); displaydir($wdir); html_footer(); break; } /// FILE FUNCTIONS /////////////////////////////////////////////////////////// function fulldelete($location) { if (is_dir($location)) { $currdir = opendir($location); while ($file = readdir($currdir)) { if ($file <> ".." && $file <> ".") { $fullfile = $location."/".$file; if (is_dir($fullfile)) { if (!fulldelete($fullfile)) { return false; } } else { if (!unlink($fullfile)) { return false; } } } } closedir($currdir); if (! rmdir($location)) { return false; } } else { if (!unlink($location)) { return false; } } return true; } function setfilelist($VARS) { global $USER; $USER->filelist = array (); $USER->fileop = ""; $count = 0; foreach ($VARS as $key => $val) { if (substr($key,0,4) == "file") { $count++; $USER->filelist[] = rawurldecode($val); } } save_session("USER"); return $count; } function clearfilelist() { global $USER; $USER->filelist = array (); $USER->fileop = ""; save_session("USER"); } function printfilelist($filelist) { global $basedir; foreach ($filelist as $file) { if (is_dir($basedir.$file)) { echo " $file
"; $subfilelist = array(); $currdir = opendir($basedir.$file); while ($subfile = readdir($currdir)) { if ($subfile <> ".." && $subfile <> ".") { $subfilelist[] = $file."/".$subfile; } } printfilelist($subfilelist); } else { $icon = mimeinfo("icon", $file); echo " $file
"; } } } function print_cell($alignment="center", $text=" ") { echo "
"; echo ""; echo "$text"; echo ""; echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "\n"; if ($wdir == "/") { $wdir = ""; } $count = 0; if (!empty($dirlist)) { asort($dirlist); foreach ($dirlist as $dir) { $count++; $filename = $fullpath."/".$dir; $fileurl = rawurlencode($wdir."/".$dir); $filesafe = rawurlencode($dir); $filedate = userdate(filectime($filename), "%d %b %Y, %I:%M %p"); echo ""; print_cell("center", ""); print_cell("left", "\"Folder\"".htmlspecialchars($dir).""); print_cell("right", "-"); print_cell("right", $filedate); print_cell("right", "$strrename"); echo ""; } } if (!empty($filelist)) { asort($filelist); foreach ($filelist as $file) { $icon = mimeinfo("icon", $file); $count++; $filename = $fullpath."/".$file; $fileurl = "$wdir/$file"; $filesafe = rawurlencode($file); $fileurlsafe = rawurlencode($fileurl); $filedate = userdate(filectime($filename), "%d %b %Y, %I:%M %p"); echo ""; print_cell("center", ""); echo ""; $file_size = filesize($filename); print_cell("right", display_size($file_size)); print_cell("right", $filedate); if ($icon == "text.gif" || $icon == "html.gif") { $edittext = "$stredit"; } else if ($icon == "zip.gif") { $edittext = "$strunzip"; } else { $edittext = ""; } print_cell("right", "$edittext $strrename"); echo ""; } } echo "
$strname$strsize$strmodified$straction
"; if ($CFG->slasharguments) { $ffurl = "/file.php/$id$fileurl"; } else { $ffurl = "/file.php?file=/$id$fileurl"; } link_to_popup_window ($ffurl, "display", "\"File\"", 480, 640); echo ""; link_to_popup_window ($ffurl, "display", htmlspecialchars($file), 480, 640); echo "
"; echo "
"; if (empty($wdir)) { $wdir = "/"; } echo ""; echo ""; echo ""; echo "
"; echo ""; echo " "; $options = array ( "move" => "$strmovetoanotherfolder", "delete" => "$strdeletecompletely", "zip" => "$strcreateziparchive" ); if (!empty($count)) { choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:document.dirform.submit()"); } echo ""; echo ""; if (($USER->fileop == "move") && $USER->filesource <> $wdir) { echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; } echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; echo "
"; echo " "; echo " "; echo " "; echo " "; echo "
"; echo "
"; echo "
"; } ?>