libdir.'/filelib.php'); $id = required_param('id', PARAM_INT); $file = optional_param('file', '', PARAM_PATH); $wdir = optional_param('wdir', '', PARAM_PATH); $action = optional_param('action', '', PARAM_ACTION); $name = optional_param('name', '', PARAM_FILE); $oldname = optional_param('oldname', '', PARAM_FILE); $usecheckboxes = optional_param('usecheckboxes', 1, PARAM_INT); $save = optional_param('save', 0, PARAM_BOOL); $text = optional_param('text', '', PARAM_RAW); $confirm = optional_param('confirm', 0, PARAM_BOOL); if (! $course = get_record("course", "id", $id) ) { error("That's an invalid course id"); } require_login($course); require_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $id)); function html_footer() { echo "\n\n\n"; } function html_header($course, $wdir, $formfield=""){ global $CFG; if (!empty($_SERVER['HTTPS']) and $_SERVER['HTTPS'] != 'off') { $url = preg_replace('|https?://[^/]+|', '', $CFG->wwwroot).'/lib/editor/htmlarea/'; } else { $url = $CFG->wwwroot.'/lib/editor/htmlarea/'; } ?> coursefiles id")) { error("The site administrator needs to fix the file permissions"); } $baseweb = $CFG->wwwroot; // End of configuration and access control if ($wdir == '') { $wdir='/'; } switch ($action) { case "upload": html_header($course, $wdir); require_once($CFG->dirroot.'/lib/uploadlib.php'); if ($save and confirm_sesskey()) { $um = new upload_manager('userfile',false,false,$course,false,0); $dir = "$basedir$wdir"; if ($um->process_file_uploads($dir)) { notify(get_string('uploadedfile')); } // um will take care of error reporting. displaydir($wdir); } else { $upload_max_filesize = get_max_upload_file_size($CFG->maxbytes); $filesize = display_size($upload_max_filesize); $struploadafile = get_string("uploadafile"); $struploadthisfile = get_string("uploadthisfile"); $strmaxsize = get_string("maxsize", "", $filesize); $strcancel = get_string("cancel"); echo "

$struploadafile ($strmaxsize) --> $wdir"; echo "\n\n\n
\n"; echo "
\n"; upload_print_form_fragment(1,array('userfile'),null,false,null,$course->maxbytes,0,false); echo " \n"; echo " \n"; echo " \n"; echo " sesskey\" />\n"; echo "
"; echo " \n"; echo "\n"; echo "\n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "
\n"; echo "
\n"; } html_footer(); break; case "delete": if ($confirm and confirm_sesskey()) { 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($_POST)) { echo "

".get_string("deletecheckwarning").":

"; print_simple_box_start("center"); printfilelist($USER->filelist); print_simple_box_end(); echo "
"; $frameold = $CFG->framename; $CFG->framename = "ibrowser"; notice_yesno (get_string("deletecheckfiles"), "coursefiles.php?id=$id&wdir=$wdir&action=delete&confirm=1&sesskey=$USER->sesskey", "coursefiles.php?id=$id&wdir=$wdir&action=cancel"); $CFG->framename = $frameold; } else { displaydir($wdir); } html_footer(); } break; case "move": html_header($course, $wdir); if ($count = setfilelist($_POST) and confirm_sesskey()) { $USER->fileop = $action; $USER->filesource = $wdir; echo "

"; print_string("selectednowmove", "moodle", $count); echo "

"; } displaydir($wdir); html_footer(); break; case "paste": html_header($course, $wdir); if (isset($USER->fileop) and $USER->fileop == "move" and confirm_sesskey()) { 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) and confirm_sesskey()) { 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 { $strrename = get_string("rename"); $strcancel = get_string("cancel"); $strrenamefileto = get_string("renamefileto", "moodle", $file); html_header($course, $wdir, "form.name"); echo "

$strrenamefileto:"; echo "\n\n\n
\n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " sesskey\" />\n"; echo " \n"; echo " \n"; echo " \n"; echo "
\n"; echo "
\n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "
"; echo "
\n"; } html_footer(); break; case "mkdir": if (!empty($name) and confirm_sesskey()) { html_header($course, $wdir); $name = clean_filename($name); if (file_exists("$basedir$wdir/$name")) { echo "Error: $name already exists!"; } else if (! make_upload_directory("$course->id/$wdir/$name")) { echo "Error: could not create $name"; } displaydir($wdir); } else { $strcreate = get_string("create"); $strcancel = get_string("cancel"); $strcreatefolder = get_string("createfolder", "moodle", $wdir); html_header($course, $wdir, "form.name"); echo "

$strcreatefolder:"; echo "\n\n\n
\n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " sesskey\" />\n"; echo " \n"; echo " \n"; echo "
\n"; echo "
\n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "
\n"; echo "
\n"; } html_footer(); break; case "edit": html_header($course, $wdir); if (($text != '') and confirm_sesskey()) { $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); print_heading("$streditfile"); echo "\n\n\n\n
\n"; echo "
\n"; echo " \n"; echo " \n"; echo " "; echo " \n"; echo " sesskey\" />\n"; print_textarea(false, 25, 80, 680, 400, "text", $contents); echo "
\n"; echo " \n"; echo "\n"; echo "\n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "
\n"; echo "
\n"; if ($usehtmleditor) { use_html_editor("text"); } } html_footer(); break; case "zip": if (!empty($name) and confirm_sesskey()) { html_header($course, $wdir); $name = clean_filename($name); $files = array(); foreach ($USER->filelist as $file) { $files[] = "$basedir/$file"; } if (!zip_files($files,"$basedir/$wdir/$name")) { print_error("zipfileserror","error"); } clearfilelist(); displaydir($wdir); } else { html_header($course, $wdir, "form.name"); if (setfilelist($_POST)) { echo "

".get_string("youareabouttocreatezip").":

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

".get_string("whattocallzip"); echo "\n\n\n\n\n
\n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " sesskey\" />\n"; echo " \n"; echo " "; echo "
\n"; echo "
\n"; echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "
\n"; echo "
\n"; } else { displaydir($wdir); clearfilelist(); } } html_footer(); break; case "unzip": html_header($course, $wdir); if (!empty($file) and confirm_sesskey()) { $strok = get_string("ok"); $strunpacking = get_string("unpacking", "", $file); echo "

$strunpacking:

"; $file = basename($file); if (!unzip_file("$basedir/$wdir/$file")) { print_error("unzipfileserror","error"); } echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "
\n"; echo "
\n"; } else { displaydir($wdir); } html_footer(); break; case "listzip": html_header($course, $wdir); if (!empty($file) and confirm_sesskey()) { $strname = get_string("name"); $strsize = get_string("size"); $strmodified = get_string("modified"); $strok = get_string("ok"); $strlistfiles = get_string("listfiles", "", $file); echo "

$strlistfiles:

"; $file = basename($file); require_once($CFG->libdir.'/pclzip/pclzip.lib.php'); $archive = new PclZip("$basedir/$wdir/$file"); if (!$list = $archive->listContent("$basedir/$wdir")) { notify($archive->errorInfo(true)); } else { echo "\n"; echo "\n"; foreach ($list as $item) { echo ""; print_cell("left", $item['filename']); if (! $item['folder']) { print_cell("right", display_size($item['size'])); } else { echo "\n"; } $filedate = userdate($item['mtime'], get_string("strftimedatetime")); print_cell("right", $filedate); echo "\n"; } echo "
$strname$strsize$strmodified
 
\n"; } echo "
\n"; echo " \n"; echo " \n"; echo " \n"; echo " sesskey\" />\n"; echo " \n"; echo "
\n"; echo "
\n"; } else { displaydir($wdir); } html_footer(); break; case "cancel": clearfilelist(); default: html_header($course, $wdir); displaydir($wdir); html_footer(); break; } /// FILE FUNCTIONS /////////////////////////////////////////////////////////// function setfilelist($VARS) { global $USER; $USER->filelist = array (); $USER->fileop = ""; $count = 0; foreach ($VARS as $key => $val) { if (substr($key,0,4) == "file") { $count++; $val = rawurldecode($val); if (!detect_munged_arguments($val, 0)) { $USER->filelist[] = $val; } } } return $count; } function clearfilelist() { global $USER; $USER->filelist = array (); $USER->fileop = ""; } function printfilelist($filelist) { global $basedir, $CFG; foreach ($filelist as $file) { if (is_dir($basedir.$file)) { echo "pixpath/f/folder.gif\" class=\"icon\" alt=\"".get_string('folder')."\" /> $file
"; $subfilelist = array(); $currdir = opendir($basedir.$file); while (false !== ($subfile = readdir($currdir))) { if ($subfile <> ".." && $subfile <> ".") { $subfilelist[] = $file."/".$subfile; } } printfilelist($subfilelist); } else { $icon = mimeinfo("icon", $file); echo "pixpath/f/$icon\" class=\"icon\" alt=\"".get_string('file')."\" /> $file
"; } } } function print_cell($alignment="center", $text=" ") { echo "\n"; echo "$text"; echo "\n"; } function get_image_size($filepath) { /// This function get's the image size /// Check if file exists if(!file_exists($filepath)) { return false; } else { /// Get the mime type so it really an image. if(mimeinfo("icon", basename($filepath)) != "image.gif") { return false; } else { $array_size = getimagesize($filepath); return $array_size; } } unset($filepath,$array_size); } function displaydir ($wdir) { // $wdir == / or /a or /a/b/c/d etc global $basedir; global $usecheckboxes; global $id; global $USER, $CFG; $fullpath = $basedir.$wdir; $directory = opendir($fullpath); // Find all files while (false !== ($file = readdir($directory))) { if ($file == "." || $file == "..") { continue; } if (is_dir($fullpath."/".$file)) { $dirlist[] = $file; } else { $filelist[] = $file; } } closedir($directory); $strfile = get_string("file"); $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"); $strlist = get_string("list"); $strchoose = get_string("choose"); echo "
\n"; echo "\n"; if ($wdir == "/") { $wdir = ""; } else { $bdir = str_replace("/".basename($wdir),"",$wdir); if($bdir == "/") { $bdir = ""; } print "\n\n\n"; } $count = 0; if (!empty($dirlist)) { asort($dirlist); foreach ($dirlist as $dir) { $count++; $filename = $fullpath."/".$dir; $fileurl = $wdir."/".$dir; $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p"); echo ""; if ($usecheckboxes) { if ($fileurl === '/moddata') { print_cell(); } else { print_cell("center", ""); } } print_cell("left", "pixpath/f/folder.gif\" class=\"icon\" alt=\"".get_string('folder')."\" />".htmlspecialchars($dir).""); print_cell("right", " "); print_cell("right", $filedate); echo ""; } } if (!empty($filelist)) { asort($filelist); foreach ($filelist as $file) { $icon = mimeinfo("icon", $file); $imgtype = mimeinfo("type",$file); $count++; $filename = $fullpath."/".$file; $fileurl = "$wdir/$file"; $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p"); $dimensions = get_image_size($filename); if($dimensions) { $imgwidth = $dimensions[0]; $imgheight = $dimensions[1]; } else { $imgwidth = "Unknown"; $imgheight = "Unknown"; } unset($dimensions); echo "\n"; if ($usecheckboxes) { print_cell("center", ""); } echo "\n"; if ($icon == "zip.gif") { $edittext = "sesskey\">$strunzip "; $edittext .= "sesskey\">$strlist "; } else { $edittext = " "; } print_cell("right", "$edittext "); print_cell("right", $filedate); echo "\n"; } } echo "
"; print ""; print "wwwroot/lib/editor/htmlarea/images/folderup.gif\" height=\"14\" width=\"24\" border=\"0\" alt=\"".get_string('parentfolder')."\" />"; print "
"; $ffurl = get_file_url($id.$fileurl); link_to_popup_window ($ffurl, "display", "pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />", 480, 640); $file_size = filesize($filename); echo "$file"; echo "
\n"; if (empty($wdir)) { $wdir = "/"; } echo "\n"; echo "\n\n"; echo "
"; echo "\n"; echo "\n"; echo "sesskey\" />\n"; $options = array ( "move" => "$strmovetoanotherfolder", "delete" => "$strdeletecompletely", "zip" => "$strcreateziparchive" ); if (!empty($count)) { choose_from_menu ($options, "action", "", "$strwithchosenfiles...", "javascript:getElementById('dirform').submit()"); } if (!empty($USER->fileop) and ($USER->fileop == "move") and ($USER->filesource <> $wdir)) { echo "\n"; echo " \n"; echo " \n"; echo " \n"; echo " sesskey\" />\n"; echo " \n"; echo ""; } echo "
\n"; echo "\n"; } ?>