From be19e367857625984de50516a8baca3376308eda Mon Sep 17 00:00:00 2001 From: skodak Date: Thu, 21 May 2009 10:12:53 +0000 Subject: [PATCH] MDL-18111 improving file api comments and docs, fixing license header --- files/draftfiles.php | 342 +++++++++++++++++++++++-------------------- files/index.php | 266 ++++++++++++++++++--------------- 2 files changed, 328 insertions(+), 280 deletions(-) diff --git a/files/draftfiles.php b/files/draftfiles.php index c6371d2f21..61befdb279 100644 --- a/files/draftfiles.php +++ b/files/draftfiles.php @@ -1,183 +1,207 @@ -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(); +. + +/** + * 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 ($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()) { + print_header(); + notify(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); + notice_yesno (get_string('deletecheckfiles'), 'draftfiles.php', 'draftfiles.php', $optionsyes, $optionsno, 'post', 'get'); + print_footer('empty'); + die; - if ($delete !== '' and $file = $fs->get_file($contextid, $filearea, $itemid, $filepath, $delete)) { - if (!data_submitted() or !confirm_sesskey()) { - print_header(); - notify(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); - notice_yesno (get_string('deletecheckfiles'), 'draftfiles.php', 'draftfiles.php', $optionsyes, $optionsno, 'post', 'get'); - print_footer('empty'); - 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 { - $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); - } + redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes); } } +} - print_header(); - - if ($notice !== '') { - notify($notice); - } +print_header(); - echo '
'; +if ($notice !== '') { + notify($notice); +} - $strfolder = get_string('folder'); - $strfile = get_string('file'); - $strdownload = get_string('download'); - $strdelete = get_string('delete'); - - if ($parent) { - echo '
'; - echo ' '.get_string('parentfolder').''; - echo '
'; - } +echo '
'; - foreach ($files as $file) { - $filename = $file->get_filename(); - $filenameurl = rawurlencode($filename); - $filepath = $file->get_filepath(); - $filesize = $file->get_filesize(); - $filesize = $filesize ? display_size($filesize) : ''; - - $mimetype = $file->get_mimetype(); - - if ($file->is_directory()) { - if ($subdirs) { - $dirname = explode('/', trim($filepath, '/')); - $dirname = array_pop($dirname); - echo ''; - } +$strfolder = get_string('folder'); +$strfile = get_string('file'); +$strdownload = get_string('download'); +$strdelete = get_string('delete'); - } else { - $icon = mimeinfo_from_type('icon', $mimetype); - $viewurl = file_encode_url("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false); - echo '
'; - echo "pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".s($filename)." ($filesize) "; - echo "pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" />";; +if ($parent) { + echo '
'; + echo ' '.get_string('parentfolder').''; + echo '
'; +} + +foreach ($files as $file) { + $filename = $file->get_filename(); + $filenameurl = rawurlencode($filename); + $filepath = $file->get_filepath(); + $filesize = $file->get_filesize(); + $filesize = $filesize ? display_size($filesize) : ''; + + $mimetype = $file->get_mimetype(); + + if ($file->is_directory()) { + if ($subdirs) { + $dirname = explode('/', trim($filepath, '/')); + $dirname = array_pop($dirname); + echo ''; } - } - echo '
'; - - if ($maxbytes == 0 or $maxbytes > $totalbytes) { - echo '
'; - if ($maxbytes) { - echo ''; - } - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - if ($maxbytes) { - echo ' ('.get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes-$totalbytes))).')'; - } else { - echo ' ('.get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes))).')'; - } - echo '
'; } else { - //TODO: notify upload limit reached here - echo get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes))); + $icon = mimeinfo_from_type('icon', $mimetype); + $viewurl = file_encode_url("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false); + echo ''; } +} + +echo '
'; - if ($subdirs) { - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'; +if ($maxbytes == 0 or $maxbytes > $totalbytes) { + echo '
'; + if ($maxbytes) { + echo ''; + } + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + if ($maxbytes) { + echo ' ('.get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes-$totalbytes))).')'; + } else { + echo ' ('.get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes))).')'; } + echo '
'; +} else { + //TODO: notify upload limit reached here + echo get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes))); +} + +if ($subdirs) { + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; +} + +print_footer('empty'); - print_footer('empty'); -?> diff --git a/files/index.php b/files/index.php index a7c65290fc..d4006cd756 100644 --- a/files/index.php +++ b/files/index.php @@ -1,154 +1,178 @@ -libdir.'/filelib.php'); - require_once($CFG->libdir.'/adminlib.php'); - - $courseid = optional_param('id', 0, PARAM_INT); - - $contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT); - $filearea = optional_param('filearea', '', PARAM_ALPHAEXT); - $itemid = optional_param('itemid', -1, PARAM_INT); - $filepath = optional_param('filepath', '', PARAM_PATH); - $filename = optional_param('filename', '', PARAM_FILE); - - $newdirname = optional_param('newdirname', '', PARAM_FILE); - $delete = optional_param('delete', 0, PARAM_BOOL); - - if ($courseid) { - if (!$course = $DB->get_record('course', array('id'=>$courseid))) { - print_error('invalidcourseid'); - } - if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) { - print_error('invalidcontext'); - } - redirect('index.php?contextid='.$context->id.'&itemid=0&filearea=course_content'); +. + +/** + * Temporary file manager for all moodle files. To be replaced by something much better. + * + * @package moodlecore + * @subpackage file + * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require('../config.php'); +require_once($CFG->libdir.'/filelib.php'); +require_once($CFG->libdir.'/adminlib.php'); + +$courseid = optional_param('id', 0, PARAM_INT); + +$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT); +$filearea = optional_param('filearea', '', PARAM_ALPHAEXT); +$itemid = optional_param('itemid', -1, PARAM_INT); +$filepath = optional_param('filepath', '', PARAM_PATH); +$filename = optional_param('filename', '', PARAM_FILE); + +$newdirname = optional_param('newdirname', '', PARAM_FILE); +$delete = optional_param('delete', 0, PARAM_BOOL); + +if ($courseid) { + if (!$course = $DB->get_record('course', array('id'=>$courseid))) { + print_error('invalidcourseid'); } - - if (!$context = get_context_instance_by_id($contextid)) { + if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) { print_error('invalidcontext'); } + redirect('index.php?contextid='.$context->id.'&itemid=0&filearea=course_content'); +} + +if (!$context = get_context_instance_by_id($contextid)) { + print_error('invalidcontext'); +} - require_login(); - require_capability('moodle/course:managefiles', $context); +require_login(); +require_capability('moodle/course:managefiles', $context); - if ($filearea === '') { - $filearea = null; - } +if ($filearea === '') { + $filearea = null; +} - if ($itemid < 0) { - $itemid = null; - } +if ($itemid < 0) { + $itemid = null; +} - if ($filepath === '') { - $filepath = null; - } +if ($filepath === '') { + $filepath = null; +} - if ($filename === '') { - $filename = null; - } +if ($filename === '') { + $filename = null; +} - $error = ''; +$error = ''; - $browser = get_file_browser(); +$browser = get_file_browser(); - $file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename); +$file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename); /// process actions - if ($file_info and $file_info->is_directory() and $file_info->is_writable() and $newdirname !== '' and data_submitted() and confirm_sesskey()) { - if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) { - $params = $newdir_info->get_params_rawencoded(); - $params = implode('&', $params); - redirect("index.php?$params"); - } else { - $error = "Could not create new dir"; // TODO: localise - } +if ($file_info and $file_info->is_directory() and $file_info->is_writable() and $newdirname !== '' and data_submitted() and confirm_sesskey()) { + if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) { + $params = $newdir_info->get_params_rawencoded(); + $params = implode('&', $params); + redirect("index.php?$params"); + } else { + $error = "Could not create new dir"; // TODO: localise } +} - if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) { - $file = $_FILES['newfile']; - $newfilename = clean_param($file['name'], PARAM_FILE); - if (is_uploaded_file($_FILES['newfile']['tmp_name'])) { - try { - if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) { - $params = $file_info->get_params_rawencoded(); - $params = implode('&', $params); - redirect("index.php?$params"); +if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) { + $file = $_FILES['newfile']; + $newfilename = clean_param($file['name'], PARAM_FILE); + if (is_uploaded_file($_FILES['newfile']['tmp_name'])) { + try { + if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) { + $params = $file_info->get_params_rawencoded(); + $params = implode('&', $params); + redirect("index.php?$params"); - } else { - $error = "Could not create upload file"; // TODO: localise - } - } catch (file_exception $e) { - $error = "Exception: Could not create upload file"; // TODO: localise + } else { + $error = "Could not create upload file"; // TODO: localise } + } catch (file_exception $e) { + $error = "Exception: Could not create upload file"; // TODO: localise } } +} - if ($file_info and $delete) { - if (!data_submitted() or !confirm_sesskey()) { - print_header(); - notify(get_string('deletecheckwarning').': '.$file_info->get_visible_name()); - $parent_info = $file_info->get_parent(); +if ($file_info and $delete) { + if (!data_submitted() or !confirm_sesskey()) { + print_header(); + notify(get_string('deletecheckwarning').': '.$file_info->get_visible_name()); + $parent_info = $file_info->get_parent(); - $optionsno = $parent_info->get_params(); - $optionsyes = $file_info->get_params(); - $optionsyes['delete'] = 1; - $optionsyes['sesskey'] = sesskey(); + $optionsno = $parent_info->get_params(); + $optionsyes = $file_info->get_params(); + $optionsyes['delete'] = 1; + $optionsyes['sesskey'] = sesskey(); - notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get'); - print_footer(); - die; - } + notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get'); + print_footer(); + die; + } - if ($parent_info = $file_info->get_parent() and $parent_info->is_writable()) { - if (!$file_info->delete()) { - $error = "Could not delete file!"; // TODO: localise - } - $params = $parent_info->get_params_rawencoded(); - $params = implode('&', $params); - redirect("index.php?$params", $error); + if ($parent_info = $file_info->get_parent() and $parent_info->is_writable()) { + if (!$file_info->delete()) { + $error = "Could not delete file!"; // TODO: localise } + $params = $parent_info->get_params_rawencoded(); + $params = implode('&', $params); + redirect("index.php?$params", $error); } +} /// print dir listing - html_header($context, $file_info); +html_header($context, $file_info); - if ($error !== '') { - notify($error); - } +if ($error !== '') { + notify($error); +} - displaydir($file_info); - - if ($file_info and $file_info->is_directory() and $file_info->is_writable()) { - echo '
'; - - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'; - - echo '
'; - - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'; - } +displaydir($file_info); + +if ($file_info and $file_info->is_directory() and $file_info->is_writable()) { + echo '
'; + + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; + + echo '
'; + + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; +} - html_footer(); +html_footer(); /// UI functions ///////////////////////// @@ -309,4 +333,4 @@ function displaydir($file_info) { } -?> + -- 2.39.5