. /** * Draft files management script used when javascript not available. * * @package moodlecore * @subpackage file * @copyright 2008 Petr Skoda (http://skodak.org) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require('../config.php'); require_once($CFG->libdir.'/filelib.php'); $itemid = required_param('itemid', PARAM_INT); $filepath = optional_param('filepath', '/', PARAM_PATH); $newdirname = optional_param('newdirname', '', PARAM_FILE); $delete = optional_param('delete', '', PARAM_PATH); $subdirs = optional_param('subdirs', 0, PARAM_BOOL); $maxbytes = optional_param('maxbytes', 0, PARAM_INT); require_login(); if (isguestuser()) { print_error('noguest'); } if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) { print_error('invalidcontext'); } $notice = ''; $contextid = $context->id; $filearea = 'user_draft'; $browser = get_file_browser(); $fs = get_file_storage(); if (!$subdirs) { $filepath = '/'; } if (!$directory = $fs->get_file($context->id, 'user_draft', $itemid, $filepath, '.')) { $directory = new virtual_root_file($context->id, 'user_draft', $itemid); $filepath = $directory->get_filepath(); } $files = $fs->get_directory_files($context->id, 'user_draft', $itemid, $directory->get_filepath()); $parent = $directory->get_parent_directory(); $totalbytes = 0; foreach ($files as $hash=>$file) { if (!$subdirs and $file->get_filepath() !== '/') { unset($files[$hash]); continue; } $totalbytes += $file->get_filesize(); } /// process actions if ($newdirname !== '' and data_submitted() and confirm_sesskey()) { $newdirname = $directory->get_filepath().$newdirname.'/'; $fs->create_directory($contextid, $filearea, $itemid, $newdirname, $USER->id); redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($newdirname).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); } if (isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) { if (!empty($_FILES['newfile']['error'])) { $notice = file_get_upload_error($_FILES['newfile']['error']); } else { $file = $_FILES['newfile']; $newfilename = clean_param($file['name'], PARAM_FILE); if (is_uploaded_file($_FILES['newfile']['tmp_name'])) { if ($existingfile = $fs->get_file($contextid, $filearea, $itemid, $filepath, $newfilename)) { $existingfile->delete(); } $filerecord = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$newfilename, 'userid'=>$USER->id); $newfile = $fs->create_file_from_pathname($filerecord, $_FILES['newfile']['tmp_name']); redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); } } } if ($delete !== '' and $file = $fs->get_file($contextid, $filearea, $itemid, $filepath, $delete)) { if (!data_submitted() or !confirm_sesskey()) { echo $OUTPUT->header(); echo $OUTPUT->notification(get_string('deletecheckwarning').': '.s($file->get_filepath().$file->get_filename())); $optionsno = array('itemid'=>$itemid, 'filepath'=>$filepath, 'subdirs'=>$subdirs); $optionsyes = array('itemid'=>$itemid, 'filepath'=>$filepath, 'delete'=>$delete, 'sesskey'=>sesskey(), 'subdirs'=>$subdirs); echo $OUTPUT->confirm(get_string('deletecheckfiles'), new moodle_url('draftfiles.php', $optionsyes), new moodle_url('draftfiles.php', $optionsno)); echo $OUTPUT->footer(); die; } else { $isdir = $file->is_directory(); $file->delete(); if ($isdir) { redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($parent->get_filepath()).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); } else { redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); } } } echo $OUTPUT->header(); if ($notice !== '') { echo $OUTPUT->notification($notice); } echo '