]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16596 support for total maxbytes per draft area - proper server-side validation...
authorskodak <skodak>
Sun, 21 Sep 2008 21:29:41 +0000 (21:29 +0000)
committerskodak <skodak>
Sun, 21 Sep 2008 21:29:41 +0000 (21:29 +0000)
files/draftfiles.php
lib/filelib.php
lib/form/areafiles.php
lib/formslib.php

index d07ee23abbd0958ab6dca4d24489b37b9a9c0a3b..1017cb39735b629c56b30bd68bc7d9e1fc2dc7b9 100644 (file)
@@ -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';
 
     $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.'&amp;filepath='.rawurlencode($newdirname).'&amp;subdirs='.$subdirs);
+        redirect('draftfiles.php?itemid='.$itemid.'&amp;filepath='.rawurlencode($newdirname).'&amp;subdirs='.$subdirs.'&amp;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.'&amp;filepath='.rawurlencode($filepath).'&amp;subdirs='.$subdirs.'&amp;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.'&amp;filepath='.rawurlencode($filepath).'&amp;subdirs='.$subdirs);
         }
     }
 
             $isdir = $file->is_directory();
             $file->delete();
             if ($isdir) {
-                redirect('draftfiles.php?itemid='.$itemid.'&amp;filepath='.rawurlencode($parent->get_filepath()).'&amp;subdirs='.$subdirs);
+                redirect('draftfiles.php?itemid='.$itemid.'&amp;filepath='.rawurlencode($parent->get_filepath()).'&amp;subdirs='.$subdirs.'&amp;maxbytes='.$maxbytes);
             } else {
-                redirect('draftfiles.php?itemid='.$itemid.'&amp;filepath='.rawurlencode($filepath).'&amp;subdirs='.$subdirs);
+                redirect('draftfiles.php?itemid='.$itemid.'&amp;filepath='.rawurlencode($filepath).'&amp;subdirs='.$subdirs.'&amp;maxbytes='.$maxbytes);
             }
         }
     }
 
     print_header();
 
+    if ($notice !== '') {
+        notify($notice);
+    }
+
     echo '<div class="areafiles">';
 
     $strfolder   = get_string('folder');
 
     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>';
     }
 
                 $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>';
             }
 
             $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').'" />';
index e4a8999fad90c98923edf13582bcfb969b4be69e..44a2224202e1e0496aa76927118358023bc12a62 100644 (file)
@@ -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
index 55c4da26fbbbc074cd24f0001cabc96bba84dd59..c71d3a84609e95d2357bf6c0435e85a4cfd14d14 100644 (file)
@@ -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&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.'" />';
index a7be9935168e8586f48a378d235588ca61f57506..69b2c4234ca2c9b722e80c31e4bf496703e9df6f 100644 (file)
@@ -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;
             }