From: skodak Date: Sun, 21 Sep 2008 21:29:41 +0000 (+0000) Subject: MDL-16596 support for total maxbytes per draft area - proper server-side validation... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a83ad94625b2b3c62d0979801ed9c2c63b07d211;p=moodle.git MDL-16596 support for total maxbytes per draft area - proper server-side validation still missing; some minor refactoring --- diff --git a/files/draftfiles.php b/files/draftfiles.php index d07ee23abb..1017cb3973 100644 --- a/files/draftfiles.php +++ b/files/draftfiles.php @@ -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,25 +38,38 @@ $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()) { - $file = $_FILES['newfile']; - $newfilename = clean_param($file['name'], PARAM_FILE); - // TODO: some better error handling or use some upload manager - if (is_uploaded_file($_FILES['newfile']['tmp_name'])) { - if ($existingfile = $fs->get_file($contextid, $filearea, $itemid, $filepath, $newfilename)) { - $existingfile->delete(); + 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 + 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); } - $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); } } @@ -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 '
'; $strfolder = get_string('folder'); @@ -89,7 +109,7 @@ if ($parent) { echo ''; } @@ -107,8 +127,8 @@ $dirname = explode('/', trim($filepath, '/')); $dirname = array_pop($dirname); echo ''; } @@ -117,27 +137,42 @@ $viewurl = $browser->encodepath("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false); echo ''; } } 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 ''; diff --git a/lib/filelib.php b/lib/filelib.php index e4a8999fad..44a2224202 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -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 diff --git a/lib/form/areafiles.php b/lib/form/areafiles.php index 55c4da26fb..c71d3a8460 100644 --- a/lib/form/areafiles.php +++ b/lib/form/areafiles.php @@ -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 .= ''; diff --git a/lib/formslib.php b/lib/formslib.php index a7be993516..69b2c4234c 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -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; }