Skip to content

Commit

Permalink
MDL-16596 support for total maxbytes per draft area - proper server-s…
Browse files Browse the repository at this point in the history
…ide validation still missing; some minor refactoring
  • Loading branch information
skodak committed Sep 21, 2008
1 parent 45d0b87 commit a83ad94
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 65 deletions.
85 changes: 60 additions & 25 deletions files/draftfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -18,6 +19,8 @@
print_error('invalidcontext');
}

$notice = '';

$contextid = $context->id;
$filearea = 'user_draft';

Expand All @@ -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);
}
}

Expand All @@ -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');
Expand All @@ -89,7 +109,7 @@

if ($parent) {
echo '<div class="folder">';
echo '<a href="draftfiles.php?itemid='.$itemid.'&amp;filepath='.$parent->get_filepath().'&amp;subdirs='.$subdirs.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" />&nbsp;'.get_string('parentfolder').'</a>';
echo '<a href="draftfiles.php?itemid='.$itemid.'&amp;filepath='.$parent->get_filepath().'&amp;subdirs='.$subdirs.'&amp;maxbytes='.$maxbytes.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" />&nbsp;'.get_string('parentfolder').'</a>';
echo '</div>';
}

Expand All @@ -107,8 +127,8 @@
$dirname = explode('/', trim($filepath, '/'));
$dirname = array_pop($dirname);
echo '<div class="folder">';
echo "<a href=\"draftfiles.php?itemid=$itemid&amp;filepath=$filepath&amp;subdirs=$subdirs\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" />&nbsp;".s($dirname)."</a> ";
echo "<a href=\"draftfiles.php?itemid=$itemid&amp;filepath=$filepath&amp;delete=$filenameurl&amp;subdirs=$subdirs\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
echo "<a href=\"draftfiles.php?itemid=$itemid&amp;filepath=$filepath&amp;subdirs=$subdirs&amp;maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" />&nbsp;".s($dirname)."</a> ";
echo "<a href=\"draftfiles.php?itemid=$itemid&amp;filepath=$filepath&amp;delete=$filenameurl&amp;subdirs=$subdirs&amp;maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
echo '</div>';
}

Expand All @@ -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\" />&nbsp;".s($filename)." ($filesize)</a> ";
echo "<a href=\"draftfiles.php?itemid=$itemid&amp;filepath=$filepath&amp;delete=$filenameurl&amp;subdirs=$subdirs\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
echo "<a href=\"draftfiles.php?itemid=$itemid&amp;filepath=$filepath&amp;delete=$filenameurl&amp;subdirs=$subdirs&amp;maxbytes=$maxbytes\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
echo '</div>';
}
}

echo '</div>';

echo '<form enctype="multipart/form-data" method="post" action="draftfiles.php"><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="sesskey" value="'.sesskey().'" />';
echo '<input name="newfile" type="file" />';
echo '<input type="submit" value="'.get_string('uploadafile').'" />';
echo '</div></form>';
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').'" />';
Expand Down
49 changes: 49 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions lib/form/areafiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -37,7 +39,8 @@ function getMaxbytes() {
}

function setMaxbytes($maxbytes) {
$this->_options['maxbytes'] = $maxbytes;
global $CFG;
$this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
}

function getSubdirs() {
Expand Down Expand Up @@ -90,6 +93,7 @@ function toHtml() {
$id = $this->_attributes['id'];
$elname = $this->_attributes['name'];
$subdirs = $this->_options['subdirs'];
$maxbytes = $this->_options['maxbytes'];
$draftitemid = $this->getValue();

if (empty($draftitemid)) {
Expand All @@ -99,7 +103,7 @@ function toHtml() {
$draftitemid = $this->getValue();
}

$editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid&amp;subdirs=$subdirs";
$editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid&amp;subdirs=$subdirs&amp;maxbytes=$maxbytes";

$str = $this->_getTabs();
$str .= '<input type="hidden" name="'.$elname.'" value="'.$draftitemid.'" />';
Expand Down
40 changes: 3 additions & 37 deletions lib/formslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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
Expand Down Expand Up @@ -215,42 +215,8 @@ function _validate_files(&$files) {
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;
}
Expand Down

0 comments on commit a83ad94

Please sign in to comment.