diff --git a/files/renderer.php b/files/renderer.php index 98c8431..c9d640e 100644 --- a/files/renderer.php +++ b/files/renderer.php @@ -454,14 +454,21 @@ class core_files_renderer extends plugin_renderer_base { */ private function fm_print_restrictions($fm) { $maxbytes = display_size($fm->options->maxbytes); - if (empty($fm->options->maxfiles) || $fm->options->maxfiles == -1) { - $maxsize = get_string('maxfilesize', 'moodle', $maxbytes); - } else { - $strparam = (object)array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles); + $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles, + 'areasize' => display_size($fm->options->areamaxbytes)); + $hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0; + $hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1; + if ($hasmaxfiles && $hasarealimit) { + $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam); + } else if ($hasmaxfiles) { $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam); + } else if ($hasarealimit) { + $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam); + } else { + $maxsize = get_string('maxfilesize', 'moodle', $maxbytes); } // TODO MDL-32020 also should say about 'File types accepted' - return ''. $maxsize. ''; + return ''. $maxsize . ''; } /** diff --git a/lang/en/error.php b/lang/en/error.php index b9e5707..bf9e294 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -346,6 +346,7 @@ $string['logfilenotavailable'] = 'Logs not available'; $string['loginasnoenrol'] = 'You cannot use enrol or unenrol when in course "Login as" session'; $string['loginasonecourse'] = 'You cannot enter this course.
You have to terminate the "Login as" session before entering any other course.'; $string['maxbytes'] = 'This file is bigger than the maximum size'; +$string['maxareabytes'] = 'Not enough available space to store this file'; $string['messagingdisable'] = 'Messaging is disabled on this site'; $string['mimetexisnotexist'] = 'Your system is not configured to run mimeTeX. You need to download the appropriate executable for you PHP_OS platform from http://moodle.org/download/mimetex/, or obtain the C source from http://www.forkosh.com/mimetex.zip, compile it and put the executable into your moodle/filter/tex/ directory.'; $string['mimetexnotexecutable'] = 'Custom mimetex is not executable!'; diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 39ab807..4560718 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -985,6 +985,8 @@ $string['markedthistopic'] = 'This topic is highlighted as the current topic'; $string['markthistopic'] = 'Highlight this topic as the current topic'; $string['matchingsearchandrole'] = 'Matching \'{$a->search}\' and {$a->role}'; $string['maxsizeandattachments'] = 'Maximum size for new files: {$a->size}, maximum attachments: {$a->attachments}'; +$string['maxsizeandattachmentsandareasize'] = 'Maximum size for new files: {$a->size}, maximum attachments: {$a->attachments}, overall limit: {$a->areasize}'; +$string['maxsizeandareasize'] = 'Maximum size for new files: {$a->size}, overall limit: {$a->areasize}'; $string['maxfilesreached'] = 'You are allowed to attach a maximum of {$a} file(s) to this item'; $string['maximumgrade'] = 'Maximum grade'; $string['maximumgradex'] = 'Maximum grade: {$a}'; diff --git a/lang/en/role.php b/lang/en/role.php index 61f7e4b..67590fe 100644 --- a/lang/en/role.php +++ b/lang/en/role.php @@ -366,6 +366,7 @@ $string['user:editmessageprofile'] = 'Edit user messaging profile'; $string['user:editownmessageprofile'] = 'Edit own user messaging profile'; $string['user:editownprofile'] = 'Edit own user profile'; $string['user:editprofile'] = 'Edit user profile'; +$string['user:ignoreuserquota'] = 'Ignore user quota limit'; $string['user:loginas'] = 'Login as other users'; $string['user:manageblocks'] = 'Manage blocks on user profile of other users'; $string['user:manageownblocks'] = 'Manage blocks on own public user profile'; diff --git a/lib/db/access.php b/lib/db/access.php index 297083a..1ad3a58 100644 --- a/lib/db/access.php +++ b/lib/db/access.php @@ -574,6 +574,15 @@ $capabilities = array( ) ), + // Can the user ignore the setting userquota? + // The permissions are cloned from ignorefilesizelimits as it was partly used for that purpose. + 'moodle/user:ignoreuserquota' => array( + 'riskbitmap' => RISK_SPAM, + 'captype' => 'write', + 'contextlevel' => CONTEXT_SYSTEM, + 'clonepermissionsfrom' => 'moodle/course:ignorefilesizelimits' + ), + // can the user manage the system default dashboard page? 'moodle/my:configsyspages' => array( diff --git a/lib/filelib.php b/lib/filelib.php index a9a49d9..ee5d1e5 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -29,6 +29,11 @@ defined('MOODLE_INTERNAL') || die(); */ define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); +/** + * Unlimited area size constant + */ +define('FILE_AREA_MAX_BYTES_UNLIMITED', -1); + require_once("$CFG->libdir/filestorage/file_exceptions.php"); require_once("$CFG->libdir/filestorage/file_storage.php"); require_once("$CFG->libdir/filestorage/zip_packer.php"); @@ -482,6 +487,27 @@ function file_get_draft_area_info($draftitemid) { } /** + * Returns whether a draft area has exceeded/will exceed its size limit. + * + * Please note that the unlimited value for $areamaxbytes is -1 {@link FILE_AREA_MAX_BYTES_UNLIMITED}, not 0. + * + * @param int $draftitemid the draft area item id. + * @param int $areamaxbytes the maximum size allowed in this draft area. + * @param int $newfilesize the size that would be added to the current area. + * @return bool true if the area will/has exceeded its limit. + * @since 2.4 + */ +function file_is_draft_area_limit_reached($draftitemid, $areamaxbytes, $newfilesize = 0) { + if ($areamaxbytes != FILE_AREA_MAX_BYTES_UNLIMITED) { + $draftinfo = file_get_draft_area_info($draftitemid); + if ($draftinfo['filesize'] + $newfilesize > $areamaxbytes) { + return true; + } + } + return false; +} + +/** * Get used space of files * @global moodle_database $DB * @global stdClass $USER @@ -724,6 +750,9 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea if (!isset($options['maxbytes']) || $options['maxbytes'] == USER_CAN_IGNORE_FILE_SIZE_LIMITS) { $options['maxbytes'] = 0; // unlimited } + if (!isset($options['areamaxbytes'])) { + $options['areamaxbytes'] = FILE_AREA_MAX_BYTES_UNLIMITED; // Unlimited. + } $allowreferences = true; if (isset($options['return_types']) && !($options['return_types'] & FILE_REFERENCE)) { // we assume that if $options['return_types'] is NOT specified, we DO allow references. @@ -732,6 +761,13 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea $allowreferences = false; } + // Check if the draft area has exceeded the authorised limit. This should never happen as validation + // should have taken place before, unless the user is doing something nauthly. If so, let's just not save + // anything at all in the next area. + if (file_is_draft_area_limit_reached($draftitemid, $options['areamaxbytes'])) { + return null; + } + $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id'); $oldfiles = $fs->get_area_files($contextid, $component, $filearea, $itemid, 'id'); diff --git a/lib/form/dndupload.js b/lib/form/dndupload.js index bbadd5c..96ea8da 100644 --- a/lib/form/dndupload.js +++ b/lib/form/dndupload.js @@ -38,6 +38,8 @@ M.form_dndupload.init = function(Y, options) { acceptedtypes: [], // maximum size of files allowed in this form maxbytes: 0, + // Maximum combined size of files allowed in this form. {@link FILE_AREA_MAX_BYTES_UNLIMITED} + areamaxbytes: -1, // unqiue id of this form field used for html elements clientid: '', // upload repository id, used for upload @@ -63,6 +65,7 @@ M.form_dndupload.init = function(Y, options) { * acceptdtypes: accepted filetypes by this form * maxfiles: maximum number of files this form allows * maxbytes: maximum size of files allowed in this form + * areamaxbytes: maximum combined size of files allowed in this form * clientid: unqiue id of this form field used for html elements * containerid: htmlid of container * repositories: array of repository objects passed from filepicker @@ -90,6 +93,7 @@ M.form_dndupload.init = function(Y, options) { this.acceptedtypes = options.acceptedtypes; this.clientid = options.clientid; this.maxbytes = options.maxbytes; + this.areamaxbytes = options.areamaxbytes; this.itemid = options.itemid; this.author = options.author; this.container = this.Y.one('#'+options.containerid); @@ -406,10 +410,14 @@ M.form_dndupload.init = function(Y, options) { currentfiles: null, // Total number of files already uploaded (to check for exceeding limits). currentfilecount: 0, + // Total size of the files present in the area. + currentareasize: 0, // The list of files to upload. uploadqueue: [], // This list of files with name clashes. renamequeue: [], + // Size of the current queue. + queuesize: 0, // Set to true if the user has clicked on 'overwrite all'. overwriteall: false, // Set to true if the user has clicked on 'rename all'. @@ -432,6 +440,12 @@ M.form_dndupload.init = function(Y, options) { this.callback = params.callback; this.currentfiles = params.currentfiles; this.currentfilecount = params.currentfilecount; + this.currentareasize = 0; + + // Retrieve the current size of the area. + for (var i = 0; i < this.currentfiles.length; i++) { + this.currentareasize += this.currentfiles[i].size; + }; this.initialise_queue(params.files); }, @@ -489,8 +503,9 @@ M.form_dndupload.init = function(Y, options) { initialise_queue: function(files) { this.uploadqueue = []; this.renamequeue = []; + this.queuesize = 0; - // Loop through the files and find any name clashes with existing files + // Loop through the files and find any name clashes with existing files. var i; for (i=0; i 0 && files[i].size > this.options.maxbytes) { @@ -507,6 +522,7 @@ M.form_dndupload.init = function(Y, options) { return; } } + this.queuesize += files[i].size; } }, @@ -528,6 +544,17 @@ M.form_dndupload.init = function(Y, options) { this.print_msg(M.util.get_string('maxfilesreached', 'moodle', this.options.maxfiles), 'error'); return false; } + // The new file will cause the area to reach its limit, we cancel the upload of all files. + // -1 is the value defined by FILE_AREA_MAX_BYTES_UNLIMITED. + if (this.options.areamaxbytes > -1) { + var sizereached = this.currentareasize + this.queuesize + file.size; + if (sizereached > this.options.areamaxbytes) { + this.uploadqueue = []; + this.renamequeue = []; + this.print_msg(M.util.get_string('uploadformlimit', 'moodle', file.name), 'error'); + return false; + } + } this.uploadqueue.push({file:file, filename:filename, overwrite:overwrite}); return true; }, diff --git a/lib/form/editor.php b/lib/form/editor.php index d922b09..bd04b22 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -51,8 +51,9 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { public $_type = 'editor'; /** @var array options provided to initalize filepicker */ - protected $_options = array('subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>0, 'changeformat'=>0, - 'context'=>null, 'noclean'=>0, 'trusttext'=>0, 'return_types'=>7); + protected $_options = array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 0, 'changeformat' => 0, + 'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED, 'context' => null, 'noclean' => 0, 'trusttext' => 0, + 'return_types' => 7); // $_options['return_types'] = FILE_INTERNAL | FILE_EXTERNAL | FILE_REFERENCE /** @var array values for editor */ @@ -153,6 +154,24 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes); } + /** + * Returns the maximum size of the area. + * + * @return int + */ + function getAreamaxbytes() { + return $this->_options['areamaxbytes']; + } + + /** + * Sets the maximum size of the area. + * + * @param int $areamaxbytes size limit + */ + function setAreamaxbytes($areamaxbytes) { + $this->_options['areamaxbytes'] = $areamaxbytes; + } + /** * Returns maximum number of files which can be uploaded * @@ -274,6 +293,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $subdirs = $this->_options['subdirs']; $maxbytes = $this->_options['maxbytes']; + $areamaxbytes = $this->_options['areamaxbytes']; $maxfiles = $this->_options['maxfiles']; $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls @@ -318,6 +338,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $image_options->context = $ctx; $image_options->client_id = uniqid(); $image_options->maxbytes = $this->_options['maxbytes']; + $image_options->areamaxbytes = $this->_options['areamaxbytes']; $image_options->env = 'editor'; $image_options->itemid = $draftitemid; @@ -327,6 +348,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $media_options->context = $ctx; $media_options->client_id = uniqid(); $media_options->maxbytes = $this->_options['maxbytes']; + $media_options->areamaxbytes = $this->_options['areamaxbytes']; $media_options->env = 'editor'; $media_options->itemid = $draftitemid; @@ -336,6 +358,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $link_options->context = $ctx; $link_options->client_id = uniqid(); $link_options->maxbytes = $this->_options['maxbytes']; + $link_options->areamaxbytes = $this->_options['areamaxbytes']; $link_options->env = 'editor'; $link_options->itemid = $draftitemid; @@ -389,6 +412,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { 'itemid'=>$draftitemid, 'subdirs'=>$subdirs, 'maxbytes'=>$maxbytes, + 'areamaxbytes' => $areamaxbytes, 'maxfiles'=>$maxfiles, 'ctx_id'=>$ctx->id, 'course'=>$PAGE->course->id, diff --git a/lib/form/filemanager.js b/lib/form/filemanager.js index cbd528a..c5bd2b6 100644 --- a/lib/form/filemanager.js +++ b/lib/form/filemanager.js @@ -26,6 +26,7 @@ * this.filecount, how many files in this filemanager * this.maxfiles * this.maxbytes + * this.areamaxbytes, the maximum size of the area * this.filemanager, contains reference to filemanager Node * this.selectnode, contains referenct to select-file Node * this.selectui, YUI Panel to select the file @@ -68,6 +69,7 @@ M.form_filemanager.init = function(Y, options) { this.currentpath = '/'; this.maxfiles = options.maxfiles; this.maxbytes = options.maxbytes; + this.areamaxbytes = options.areamaxbytes; this.emptycallback = null; // Used by drag and drop upload this.filepicker_options = options.filepicker?options.filepicker:{}; @@ -75,6 +77,7 @@ M.form_filemanager.init = function(Y, options) { this.filepicker_options.context = options.context; this.filepicker_options.maxfiles = this.maxfiles; this.filepicker_options.maxbytes = this.maxbytes; + this.filepicker_options.areamaxbytes = this.areamaxbytes; this.filepicker_options.env = 'filemanager'; this.filepicker_options.itemid = options.itemid; @@ -1003,6 +1006,7 @@ M.form_filemanager.init = function(Y, options) { author: options.author, maxfiles: options.maxfiles, maxbytes: options.maxbytes, + areamaxbytes: options.areamaxbytes, itemid: options.itemid, repositories: manager.filepicker_options.repositories, containerid: manager.dndcontainer.get('id') diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php index c5daa9e..c67cfd9 100644 --- a/lib/form/filemanager.php +++ b/lib/form/filemanager.php @@ -48,7 +48,8 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { // PHP doesn't support 'key' => $value1 | $value2 in class definition // We cannot do $_options = array('return_types'=> FILE_INTERNAL | FILE_REFERENCE); // So I have to set null here, and do it in constructor - protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=> null); + protected $_options = array('mainfile' => '', 'subdirs' => 1, 'maxbytes' => -1, 'maxfiles' => -1, + 'accepted_types' => '*', 'return_types' => null, 'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED); /** * Constructor @@ -134,6 +135,24 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { } /** + * Returns the maximum size of the area. + * + * @return int + */ + function getAreamaxbytes() { + return $this->_options['areamaxbytes']; + } + + /** + * Sets the maximum size of the area. + * + * @param int $areamaxbytes size limit + */ + function setAreamaxbytes($areamaxbytes) { + $this->_options['areamaxbytes'] = $areamaxbytes; + } + + /** * Returns true if subdirectoy can be created, else false * * @return bool @@ -237,6 +256,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { $options->accepted_types = $accepted_types; $options->return_types = $this->_options['return_types']; $options->context = $PAGE->context; + $options->areamaxbytes = $this->_options['areamaxbytes']; $html = $this->_getTabs(); $fm = new form_filemanager($options); @@ -271,6 +291,7 @@ class form_filemanager implements renderable { * @param stdClass $options options for filemanager * default options are: * maxbytes=>-1, + * areamaxbytes => FILE_AREA_MAX_BYTES_UNLIMITED, * maxfiles=>-1, * itemid=>0, * subdirs=>false, @@ -287,6 +308,7 @@ class form_filemanager implements renderable { require_once($CFG->dirroot. '/repository/lib.php'); $defaults = array( 'maxbytes'=>-1, + 'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED, 'maxfiles'=>-1, 'itemid'=>0, 'subdirs'=>0, @@ -362,6 +384,7 @@ class form_filemanager implements renderable { 'itemid'=>$this->options->itemid, 'subdirs'=>$this->options->subdirs, 'maxbytes'=>$this->options->maxbytes, + 'areamaxbytes' => $this->options->areamaxbytes, 'maxfiles'=>$this->options->maxfiles, 'ctx_id'=>$PAGE->context->id, // TODO ? 'course'=>$PAGE->course->id, // TODO ? diff --git a/repository/draftfiles_manager.php b/repository/draftfiles_manager.php index abe84f5..89ce1e6 100644 --- a/repository/draftfiles_manager.php +++ b/repository/draftfiles_manager.php @@ -55,6 +55,7 @@ $targetpath = optional_param('targetpath', '', PARAM_PATH); $maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes $subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes +$areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); // Area maxbytes. // draft area $newdirname = optional_param('newdirname', '', PARAM_FILE); @@ -70,7 +71,7 @@ $PAGE->set_context($user_context); $fs = get_file_storage(); -$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey()); +$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'areamaxbytes'=>$areamaxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey()); $PAGE->set_url('/repository/draftfiles_manager.php', $params); $filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params); diff --git a/repository/filepicker.js b/repository/filepicker.js index f51cb05..6952c0c 100644 --- a/repository/filepicker.js +++ b/repository/filepicker.js @@ -544,6 +544,8 @@ M.core_filepicker.init = function(Y, options) { params['client_id'] = args.client_id; params['itemid'] = this.options.itemid?this.options.itemid:0; params['maxbytes'] = this.options.maxbytes?this.options.maxbytes:-1; + // The unlimited value of areamaxbytes is -1, it is defined by FILE_AREA_MAX_BYTES_UNLIMITED. + params['areamaxbytes'] = this.options.areamaxbytes ? this.options.areamaxbytes : -1; if (this.options.context && this.options.context.id) { params['ctx_id'] = this.options.context.id; } diff --git a/repository/filepicker.php b/repository/filepicker.php index 1b67331..6821103 100644 --- a/repository/filepicker.php +++ b/repository/filepicker.php @@ -59,6 +59,7 @@ $search_text = optional_param('s', '', PARAM_CLEANHTML); $maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes $subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes +$areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); // Area maxbytes. $accepted_types = optional_param_array('accepted_types', '*', PARAM_RAW); // the path to save files @@ -93,7 +94,7 @@ $context = context::instance_by_id($contextid); // Make sure maxbytes passed is within site filesize limits. $maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $course->maxbytes, $maxbytes); -$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey()); +$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'areamaxbytes'=>$areamaxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey()); $params['action'] = 'browse'; $params['draftpath'] = $draftpath; $home_url = new moodle_url('/repository/draftfiles_manager.php', $params); @@ -318,7 +319,7 @@ case 'download': $record->sortorder = 0; if ($repo->has_moodle_files()) { - $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes); + $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes, $areamaxbytes); redirect($home_url, get_string('downloadsucc', 'repository')); } else { $thefile = $repo->get_file($reference, $filename); @@ -328,6 +329,11 @@ case 'download': unlink($thefile['path']); print_error('maxbytes'); } + // Ensure the file will not make the area exceed its size limit. + if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, $filesize)) { + unlink($thefile['path']); + print_error('maxareabytes'); + } try { $info = repository::move_to_filepool($thefile['path'], $record); redirect($home_url, get_string('downloadsucc', 'repository')); diff --git a/repository/lib.php b/repository/lib.php index 0997fb9..3a7d54b 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -706,9 +706,11 @@ abstract class repository { * attributes of the new file * @param int $maxbytes maximum allowed size of file, -1 if unlimited. If size of file exceeds * the limit, the file_exception is thrown. + * @param int $areamaxbytes the maximum size of the area. A file_exception is thrown if the + * new file will reach the limit. * @return array The information about the created file */ - public function copy_to_area($source, $filerecord, $maxbytes = -1) { + public function copy_to_area($source, $filerecord, $maxbytes = -1, $areamaxbytes = FILE_AREA_MAX_BYTES_UNLIMITED) { global $USER; $fs = get_file_storage(); @@ -732,6 +734,10 @@ abstract class repository { if ($maxbytes != -1 && $stored_file->get_filesize() > $maxbytes) { throw new file_exception('maxbytes'); } + // Validate the size of the draft area. + if (file_is_draft_area_limit_reached($draftitemid, $areamaxbytes, $stored_file->get_filesize())) { + throw new file_exception('maxareabytes'); + } if (repository::draftfile_exists($draftitemid, $new_filepath, $new_filename)) { // create new file diff --git a/repository/repository_ajax.php b/repository/repository_ajax.php index 7e5d99d..d2fccd9 100644 --- a/repository/repository_ajax.php +++ b/repository/repository_ajax.php @@ -46,6 +46,7 @@ $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // Maxbytes $req_path = optional_param('p', '', PARAM_RAW); // Path $accepted_types = optional_param_array('accepted_types', '*', PARAM_RAW); $saveas_filename = optional_param('title', '', PARAM_FILE); // save as file name +$areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); // Area max bytes. $saveas_path = optional_param('savepath', '/', PARAM_PATH); // save as file path $search_text = optional_param('s', '', PARAM_CLEANHTML); $linkexternal = optional_param('linkexternal', '', PARAM_ALPHA); @@ -289,7 +290,7 @@ switch ($action) { // If the moodle file is an alias we copy this alias, otherwise we copy the file // {@link repository::copy_to_area()}. - $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes); + $fileinfo = $repo->copy_to_area($reference, $record, $maxbytes, $areamaxbytes); echo json_encode($fileinfo); die; @@ -301,6 +302,11 @@ switch ($action) { die(json_encode($err)); } + // Check if we exceed the max bytes of the area. + if (file_is_draft_area_limit_reached($itemid, $areamaxbytes, filesize($downloadedfile['path']))) { + throw new file_exception('maxareabytes'); + } + // Check if exceed maxbytes. if ($maxbytes != -1 && filesize($downloadedfile['path']) > $maxbytes) { throw new file_exception('maxbytes'); diff --git a/repository/upgrade.txt b/repository/upgrade.txt index ac82eee..7cd84ac 100644 --- a/repository/upgrade.txt +++ b/repository/upgrade.txt @@ -3,6 +3,14 @@ information provided here is intended especially for developers. Full details of the repository API are available on Moodle docs: http://docs.moodle.org/dev/Repository_API +=== 2.4 === + +* copy_to_area() can receive a new parameter called $areamaxbytes which controls the maximum + size of the area the files will be stored in. + +* the repositories using the upload() method should implement a check for $areamaxbytes, + see repository/upload/lib.php upload() and process_upload() for an example on how handling it. + === 2.3 === * instance_config_form() must now be declared static for php5.4 compatibility. diff --git a/repository/upload/lib.php b/repository/upload/lib.php index e7c4ae4..d2c1fdd 100644 --- a/repository/upload/lib.php +++ b/repository/upload/lib.php @@ -57,9 +57,10 @@ class repository_upload extends repository { $itemid = optional_param('itemid', 0, PARAM_INT); $license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT); $author = optional_param('author', '', PARAM_TEXT); + $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); $overwriteexisting = optional_param('overwrite', false, PARAM_BOOL); - return $this->process_upload($saveas_filename, $maxbytes, $types, $savepath, $itemid, $license, $author, $overwriteexisting); + return $this->process_upload($saveas_filename, $maxbytes, $types, $savepath, $itemid, $license, $author, $overwriteexisting, $areamaxbytes); } /** @@ -72,9 +73,11 @@ class repository_upload extends repository { * @param string $license optional the license to use for this file * @param string $author optional the name of the author of this file * @param bool $overwriteexisting optional user has asked to overwrite the existing file + * @param int $areamaxbytes maximum size of the file area. * @return object containing details of the file uploaded */ - public function process_upload($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0, $license = null, $author = '', $overwriteexisting = false) { + public function process_upload($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0, + $license = null, $author = '', $overwriteexisting = false, $areamaxbytes = FILE_AREA_MAX_BYTES_UNLIMITED) { global $USER, $CFG; if ((is_array($types) and in_array('*', $types)) or $types == '*') { @@ -192,6 +195,10 @@ class repository_upload extends repository { $record->itemid = 0; } + if (file_is_draft_area_limit_reached($record->itemid, $areamaxbytes, filesize($_FILES[$elname]['tmp_name']))) { + throw new file_exception('maxareabytes'); + } + if (($maxbytes!==-1) && (filesize($_FILES[$elname]['tmp_name']) > $maxbytes)) { throw new file_exception('maxbytes'); } diff --git a/user/files.php b/user/files.php index 973bbf5..d4ec489 100644 --- a/user/files.php +++ b/user/files.php @@ -51,9 +51,15 @@ $PAGE->set_heading($title); $PAGE->set_pagelayout('mydashboard'); $PAGE->set_pagetype('user-files'); +$maxareabytes = $CFG->userquota; +if (has_capability('moodle/user:ignoreuserquota', $context)) { + $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED; +} + $data = new stdClass(); $data->returnurl = $returnurl; -$options = array('subdirs'=>1, 'maxbytes'=>$CFG->userquota, 'maxfiles'=>-1, 'accepted_types'=>'*'); +$options = array('subdirs' => 1, 'maxbytes' => $CFG->userquota, 'maxfiles' => -1, 'accepted_types' => '*', + 'areamaxbytes' => $maxareabytes); file_prepare_standard_filemanager($data, 'files', $options, $context, 'user', 'private', 0); $mform = new user_files_form(null, array('data'=>$data, 'options'=>$options)); diff --git a/user/files_form.php b/user/files_form.php index 55451da..1cb3715 100644 --- a/user/files_form.php +++ b/user/files_form.php @@ -46,8 +46,7 @@ class user_files_form extends moodleform { $errors = array(); $draftitemid = $data['files_filemanager']; - $fileinfo = file_get_draft_area_info($draftitemid); - if ($fileinfo['filesize'] > $CFG->userquota) { + if (file_is_draft_area_limit_reached($draftitemid, $this->_customdata['options']['areamaxbytes'])) { $errors['files_filemanager'] = get_string('userquotalimit', 'error'); } diff --git a/version.php b/version.php index 4904073..55556f9 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2012110101.00; // YYYYMMDD = weekly release date of this DEV branch +$version = 2012110101.01; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes