mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-16596 support for total maxbytes per draft area - proper server-side validation still missing; some minor refactoring
This commit is contained in:
parent
45d0b8729c
commit
a83ad94625
@ -8,6 +8,7 @@
|
||||
$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()) {
|
||||
@ -18,6 +19,8 @@
|
||||
print_error('invalidcontext');
|
||||
}
|
||||
|
||||
$notice = '';
|
||||
|
||||
$contextid = $context->id;
|
||||
$filearea = 'user_draft';
|
||||
|
||||
@ -35,14 +38,26 @@
|
||||
$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);
|
||||
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);
|
||||
// TODO: some better error handling or use some upload manager
|
||||
@ -53,7 +68,8 @@
|
||||
$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);
|
||||
redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,15 +87,19 @@
|
||||
$isdir = $file->is_directory();
|
||||
$file->delete();
|
||||
if ($isdir) {
|
||||
redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($parent->get_filepath()).'&subdirs='.$subdirs);
|
||||
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);
|
||||
redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath).'&subdirs='.$subdirs.'&maxbytes='.$maxbytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print_header();
|
||||
|
||||
if ($notice !== '') {
|
||||
notify($notice);
|
||||
}
|
||||
|
||||
echo '<div class="areafiles">';
|
||||
|
||||
$strfolder = get_string('folder');
|
||||
@ -89,7 +109,7 @@
|
||||
|
||||
if ($parent) {
|
||||
echo '<div class="folder">';
|
||||
echo '<a href="draftfiles.php?itemid='.$itemid.'&filepath='.$parent->get_filepath().'&subdirs='.$subdirs.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" /> '.get_string('parentfolder').'</a>';
|
||||
echo '<a href="draftfiles.php?itemid='.$itemid.'&filepath='.$parent->get_filepath().'&subdirs='.$subdirs.'&maxbytes='.$maxbytes.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" /> '.get_string('parentfolder').'</a>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
@ -107,8 +127,8 @@
|
||||
$dirname = explode('/', trim($filepath, '/'));
|
||||
$dirname = array_pop($dirname);
|
||||
echo '<div class="folder">';
|
||||
echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&subdirs=$subdirs\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($dirname)."</a> ";
|
||||
echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&delete=$filenameurl&subdirs=$subdirs\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
|
||||
echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&subdirs=$subdirs&maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($dirname)."</a> ";
|
||||
echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&delete=$filenameurl&subdirs=$subdirs&maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
@ -117,27 +137,42 @@
|
||||
$viewurl = $browser->encodepath("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false);
|
||||
echo '<div class="file">';
|
||||
echo "<a href=\"$viewurl\"><img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".s($filename)." ($filesize)</a> ";
|
||||
echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&delete=$filenameurl&subdirs=$subdirs\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
|
||||
echo "<a href=\"draftfiles.php?itemid=$itemid&filepath=$filepath&delete=$filenameurl&subdirs=$subdirs&maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
|
||||
echo '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
|
||||
if ($maxbytes == 0 or $maxbytes > $totalbytes) {
|
||||
echo '<form enctype="multipart/form-data" method="post" action="draftfiles.php"><div>';
|
||||
if ($maxbytes) {
|
||||
echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxbytes-$totalbytes).'" />';
|
||||
}
|
||||
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
|
||||
echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
|
||||
echo '<input type="hidden" name="subdirs" value="'.$subdirs.'" />';
|
||||
echo '<input type="hidden" name="maxbytes" value="'.$maxbytes.'" />';
|
||||
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
|
||||
echo '<input name="newfile" type="file" />';
|
||||
echo '<input type="submit" value="'.get_string('uploadafile').'" />';
|
||||
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 '</div></form>';
|
||||
} else {
|
||||
//TODO: notify upload limit reached here
|
||||
echo get_string('maxsize', '', display_size(get_max_upload_file_size($CFG->maxbytes, $maxbytes)));
|
||||
}
|
||||
|
||||
if ($subdirs) {
|
||||
echo '<form action="draftfiles.php" method="post"><div>';
|
||||
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
|
||||
echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
|
||||
echo '<input type="hidden" name="subdirs" value="'.$subdirs.'" />';
|
||||
echo '<input type="hidden" name="maxbytes" value="'.$maxbytes.'" />';
|
||||
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
|
||||
echo '<input type="text" name="newdirname" value="" />';
|
||||
echo '<input type="submit" value="'.get_string('makeafolder').'" />';
|
||||
|
@ -225,6 +225,55 @@ function file_convert_draftarea($draftitemid, $contextid, $filearea, $itemid, $s
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of upload error
|
||||
* @param int $errorcode found in $_FILES['filename.ext']['error']
|
||||
* @return error description string, '' if ok
|
||||
*/
|
||||
function file_get_upload_error($errorcode) {
|
||||
|
||||
switch ($errorcode) {
|
||||
case 0: // UPLOAD_ERR_OK - no error
|
||||
$errmessage = '';
|
||||
break;
|
||||
|
||||
case 1: // UPLOAD_ERR_INI_SIZE
|
||||
$errmessage = get_string('uploadserverlimit');
|
||||
break;
|
||||
|
||||
case 2: // UPLOAD_ERR_FORM_SIZE
|
||||
$errmessage = get_string('uploadformlimit');
|
||||
break;
|
||||
|
||||
case 3: // UPLOAD_ERR_PARTIAL
|
||||
$errmessage = get_string('uploadpartialfile');
|
||||
break;
|
||||
|
||||
case 4: // UPLOAD_ERR_NO_FILE
|
||||
$errmessage = get_string('uploadnofilefound');
|
||||
break;
|
||||
|
||||
// Note: there is no error with a value of 5
|
||||
|
||||
case 6: // UPLOAD_ERR_NO_TMP_DIR
|
||||
$errmessage = get_string('uploadnotempdir');
|
||||
break;
|
||||
|
||||
case 7: // UPLOAD_ERR_CANT_WRITE
|
||||
$errmessage = get_string('uploadcantwrite');
|
||||
break;
|
||||
|
||||
case 8: // UPLOAD_ERR_EXTENSION
|
||||
$errmessage = get_string('uploadextension');
|
||||
break;
|
||||
|
||||
default:
|
||||
$errmessage = get_string('uploadproblem');
|
||||
}
|
||||
|
||||
return $errmessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds occurences of a link to "draftfile.php" in text and replaces the
|
||||
* address based on passed information. Matching is performed using the given
|
||||
|
@ -7,11 +7,13 @@ class MoodleQuickForm_areafiles extends HTML_QuickForm_element {
|
||||
protected $_options = array('subdirs'=>0, 'maxbytes'=>0);
|
||||
|
||||
function MoodleQuickForm_areafiles($elementName=null, $elementLabel=null, $options=null) {
|
||||
global $CFG;
|
||||
|
||||
if (!empty($options['subdirs'])) {
|
||||
$this->_options['subdirs'] = 1;
|
||||
}
|
||||
if (!empty($options['maxbytes'])) {
|
||||
$this->_options['maxbytes'] = $options['maxbytes'];
|
||||
$this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
|
||||
}
|
||||
parent::HTML_QuickForm_element($elementName, $elementLabel);
|
||||
}
|
||||
@ -37,7 +39,8 @@ class MoodleQuickForm_areafiles extends HTML_QuickForm_element {
|
||||
}
|
||||
|
||||
function setMaxbytes($maxbytes) {
|
||||
$this->_options['maxbytes'] = $maxbytes;
|
||||
global $CFG;
|
||||
$this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
|
||||
}
|
||||
|
||||
function getSubdirs() {
|
||||
@ -90,6 +93,7 @@ class MoodleQuickForm_areafiles extends HTML_QuickForm_element {
|
||||
$id = $this->_attributes['id'];
|
||||
$elname = $this->_attributes['name'];
|
||||
$subdirs = $this->_options['subdirs'];
|
||||
$maxbytes = $this->_options['maxbytes'];
|
||||
$draftitemid = $this->getValue();
|
||||
|
||||
if (empty($draftitemid)) {
|
||||
@ -99,7 +103,7 @@ class MoodleQuickForm_areafiles extends HTML_QuickForm_element {
|
||||
$draftitemid = $this->getValue();
|
||||
}
|
||||
|
||||
$editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid&subdirs=$subdirs";
|
||||
$editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid&subdirs=$subdirs&maxbytes=$maxbytes";
|
||||
|
||||
$str = $this->_getTabs();
|
||||
$str .= '<input type="hidden" name="'.$elname.'" value="'.$draftitemid.'" />';
|
||||
|
@ -26,7 +26,7 @@ require_once 'HTML/QuickForm.php';
|
||||
require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
|
||||
require_once 'HTML/QuickForm/Renderer/Tableless.php';
|
||||
|
||||
require_once $CFG->libdir.'/uploadlib.php'; // TODO: remove
|
||||
require_once $CFG->libdir.'/filelib.php';
|
||||
|
||||
/**
|
||||
* Callback called when PEAR throws an error
|
||||
@ -215,42 +215,8 @@ class moodleform {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($file['error'] > 0) {
|
||||
switch ($file['error']) {
|
||||
case 1: // UPLOAD_ERR_INI_SIZE
|
||||
$errmessage = get_string('uploadserverlimit');
|
||||
break;
|
||||
|
||||
case 2: // UPLOAD_ERR_FORM_SIZE
|
||||
$errmessage = get_string('uploadformlimit');
|
||||
break;
|
||||
|
||||
case 3: // UPLOAD_ERR_PARTIAL
|
||||
$errmessage = get_string('uploadpartialfile');
|
||||
break;
|
||||
|
||||
case 4: // UPLOAD_ERR_NO_FILE
|
||||
$errmessage = get_string('uploadnofilefound');
|
||||
break;
|
||||
|
||||
// Note: there is no error with a value of 5
|
||||
|
||||
case 6: // UPLOAD_ERR_NO_TMP_DIR
|
||||
$errmessage = get_string('uploadnotempdir');
|
||||
break;
|
||||
|
||||
case 7: // UPLOAD_ERR_CANT_WRITE
|
||||
$errmessage = get_string('uploadcantwrite');
|
||||
break;
|
||||
|
||||
case 8: // UPLOAD_ERR_EXTENSION
|
||||
$errmessage = get_string('uploadextension');
|
||||
break;
|
||||
|
||||
default:
|
||||
$errmessage = get_string('uploadproblem', $file['name']);
|
||||
}
|
||||
$errors[$elname] = $errmessage;
|
||||
if (!empty($file['error'])) {
|
||||
$errors[$elname] = file_get_upload_error($file['error']);
|
||||
unset($_FILES[$elname]);
|
||||
continue;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user