From: moodler Date: Mon, 6 Oct 2003 18:02:35 +0000 (+0000) Subject: These changes implement some better controls for controlling uploads. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=4909e17632f78e0b62e97c1783dd747d2b41f4bf;p=moodle.git These changes implement some better controls for controlling uploads. 1) There is a new site-wide configuration variable called maxbytes, which provides an upper filesize limit for all (student) uploads. 2) There is a new course-level variable called maxbytes, which further limits uploads within a course. 3) There is a new maxbytes field for forums, which further restricts the size allowed in a particular forum. There is also a new config variable in the module configuration to specify a default size to use when defining a new forum. 4) Assignments already had a limit, but this is now aware of the other limits, and like forums there is now a settable default value. 5) Finally, the sizes of files (Gb, Mb, Kb, bytes) is now translatable in the language packs. --- diff --git a/admin/config.html b/admin/config.html index b8a79c71f8..2848bbd299 100644 --- a/admin/config.html +++ b/admin/config.html @@ -300,6 +300,18 @@ + +

maxbytes: + + maxbytes, ""); + ?> + + + + + diff --git a/admin/module.php b/admin/module.php index 5710e8f4f8..5d4231ba6f 100644 --- a/admin/module.php +++ b/admin/module.php @@ -37,7 +37,7 @@ $strmanagemodules = get_string("managemodules"); $strmodulename = get_string("modulename", $module); - print_header("$site->shortname: $strconfigvariables", $site->fullname, + print_header("$site->shortname: $strmodulename: $strconfiguration", $site->fullname, "$stradmin -> ". "$strconfiguration -> ". "$strmanagemodules -> $strmodulename", $focus); diff --git a/course/edit.html b/course/edit.html index e8e3163965..0d9d7d2dc8 100644 --- a/course/edit.html +++ b/course/edit.html @@ -127,6 +127,14 @@ helpbutton("coursegrades", get_string("grades")); ?> + +

: + maxbytes); + choose_from_menu ($choices, "maxbytes", $form->maxbytes, ""); + helpbutton("courseuploadsize", get_string("maximumupload")); ?> + +

: diff --git a/course/scales.php b/course/scales.php index 8ce76a4c62..6f41f39d33 100644 --- a/course/scales.php +++ b/course/scales.php @@ -52,6 +52,13 @@ if ($scales = get_records("scale", "courseid", "$course->id", "name ASC")) { print_heading($strcustomscales); + + if (isteacheredit($course->id)) { + echo "

("; + print_string("scalestip"); + echo ")

"; + } + foreach ($scales as $scale) { $scalemenu = make_menu_from_list($scale->scale); @@ -64,6 +71,13 @@ print_simple_box_end(); echo "
"; } + + } else { + if (isteacheredit($course->id)) { + echo "

("; + print_string("scalestip"); + echo ")

"; + } } if ($scales = get_records("scale", "courseid", "0", "name ASC")) { diff --git a/lib/db/mysql.php b/lib/db/mysql.php index d5b6ef0918..3152c56f18 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -505,6 +505,10 @@ function main_upgrade($oldversion=0) { table_column("course_modules", "", "indent", "integer", "5", "unsigned", "0", "", "score"); } + if ($oldversion < 2003092900) { + table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker"); + } + return $result; } diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index 9b8b2a9b3b..d9da0b3d58 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -48,6 +48,7 @@ CREATE TABLE `prefix_course` ( `numsections` smallint(5) unsigned NOT NULL default '1', `showrecent` smallint(5) unsigned NOT NULL default '1', `marker` int(10) unsigned NOT NULL default '0', + `maxbytes` int(10) unsigned NOT NULL default '0', `visible` int(10) unsigned NOT NULL default '1', `timecreated` int(10) unsigned NOT NULL default '0', `timemodified` int(10) unsigned NOT NULL default '0', diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index 89e6a89a03..e7e09b7ce1 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -254,6 +254,9 @@ function main_upgrade($oldversion=0) { table_column("course_modules", "", "indent", "integer", "5", "unsigned", "0", "", "score"); } + if ($oldversion < 2003092900) { + table_column("course", "", "maxbytes", "integer", "10", "unsigned", "0", "", "marker"); + } return $result; } diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index 5e4fe2d321..30a5f4141e 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -26,6 +26,7 @@ CREATE TABLE prefix_course ( numsections integer NOT NULL default '1', showrecent integer NOT NULL default '1', marker integer NOT NULL default '0', + maxbytes integer NOT NULL default '0', visible integer NOT NULL default '1', timecreated integer NOT NULL default '0', timemodified integer NOT NULL default '0' diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 8a57b0350e..ec9d6bfccc 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -1059,12 +1059,64 @@ function valid_uploaded_file($newfile) { } } -function get_max_upload_file_size() { +function get_max_upload_file_size($sitebytes=0, $coursebytes=0, $modulebytes=0) { /// Returns the maximum size for uploading files +/// There are six possible upload limits: +/// +/// 1) in Apache using LimitRequestBody (no way of checking or changing this) +/// 2) in php.ini for 'upload_max_filesize' (can not be changed inside PHP) +/// 3) in .htaccess for 'upload_max_filesize' (can not be changed inside PHP) +/// 4) by the Moodle admin in $CFG->maxbytes +/// 5) by the teacher in the current course $course->maxbytes +/// 6) by the teacher for the current module, eg $assignment->maxbytes +/// +/// These last two are passed to this function as arguments (in bytes). +/// Anything defined as 0 is ignored. +/// The smallest of all the non-zero numbers is returned. + if (! $filesize = ini_get("upload_max_filesize")) { $filesize = "5M"; } - return get_real_size($filesize); + $minimumsize = get_real_size($filesize); + + if ($sitebytes and $sitebytes < $minimumsize) { + $minimumsize = $sitebytes; + } + + if ($coursebytes and $coursebytes < $minimumsize) { + $minimumsize = $coursebytes; + } + + if ($modulebytes and $modulebytes < $minimumsize) { + $minimumsize = $modulebytes; + } + + return $minimumsize; +} + +function get_max_upload_sizes($sitebytes=0, $coursebytes=0, $modulebytes=0) { +/// Related to the above function - this function returns an +/// array of possible sizes in an array, translated to the +/// local language. + + if (!$maxsize = get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes)) { + return array(); + } + + $filesize[$maxsize] = display_size($maxsize); + + $sizelist = array(10240, 51200, 102400, 512000, 1048576, 2097152, + 5242880, 10485760, 20971520, 52428800, 104857600); + + foreach ($sizelist as $sizebytes) { + if ($sizebytes < $maxsize) { + $filesize[$sizebytes] = display_size($sizebytes); + } + } + + krsort($filesize, SORT_NUMERIC); + + return $filesize; } function get_directory_list($rootdir, $excludefile="", $descend=true) { @@ -1127,14 +1179,24 @@ function get_real_size($size=0) { function display_size($size) { /// Converts bytes into display form + + static $gb,$mb,$kb,$b; + + if (empty($gb)) { + $gb = get_string('sizegb'); + $mb = get_string('sizemb'); + $kb = get_string('sizekb'); + $b = get_string('sizeb'); + } + if ($size >= 1073741824) { - $size = round($size / 1073741824 * 10) / 10 . "Gb"; + $size = round($size / 1073741824 * 10) / 10 . $gb; } else if ($size >= 1048576) { - $size = round($size / 1048576 * 10) / 10 . "Mb"; + $size = round($size / 1048576 * 10) / 10 . $mb; } else if ($size >= 1024) { - $size = round($size / 1024 * 10) / 10 . "Kb"; + $size = round($size / 1024 * 10) / 10 . $kb; } else { - $size = $size . "b"; + $size = $size ." $b"; } return $size; } diff --git a/mod/assignment/config.html b/mod/assignment/config.html new file mode 100644 index 0000000000..26248960d4 --- /dev/null +++ b/mod/assignment/config.html @@ -0,0 +1,22 @@ +
+ + + + + + + + + + + +

assignment_maxbytes:

maxbytes); + choose_from_menu ($choices, "assignment_maxbytes", $CFG->assignment_maxbytes, ""); + ?> + + +
+ ">
+ +
diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 8dfacf800c..37dab00ec6 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -8,6 +8,10 @@ define("UPLOADSINGLE", "1"); $ASSIGNMENT_TYPE = array (OFFLINE => get_string("typeoffline", "assignment"), UPLOADSINGLE => get_string("typeuploadsingle", "assignment") ); +if (!isset($CFG->assignment_maxbytes)) { + set_config("assignment_maxbytes", 1024000); // Default maximum size for all assignments +} + function assignment_add_instance($assignment) { // Given an object containing all the necessary data, diff --git a/mod/assignment/mod.html b/mod/assignment/mod.html index 224c612f2d..031bcf2b37 100644 --- a/mod/assignment/mod.html +++ b/mod/assignment/mod.html @@ -25,7 +25,7 @@ $form->grade = 100; } if (empty($form->maxbytes)) { - $form->maxbytes = ""; + $form->maxbytes = $CFG->assignment_maxbytes; } if (empty($form->timedue)) { $form->timedue = ""; @@ -104,28 +104,11 @@

:

- - maxbytes == $sizebytes) { - $sizeinlist = true; - } - } - $filesize[$maxsize] = display_size($maxsize); - if (!$sizeinlist) { - $form->maxbytes = get_real_size("500K"); - } - ksort($filesize, SORT_NUMERIC); - choose_from_menu($filesize, "maxbytes", "$form->maxbytes", ""); - ?> - + maxbytes, $course->maxbytes); + choose_from_menu ($choices, "maxbytes", $form->maxbytes, ""); + ?> +

: diff --git a/mod/forum/config.html b/mod/forum/config.html index 6686c4be6f..a374d2eeb6 100644 --- a/mod/forum/config.html +++ b/mod/forum/config.html @@ -37,6 +37,18 @@ + +

forum_maxbytes: + maxbytes); + choose_from_menu ($choices, "forum_maxbytes", $CFG->forum_maxbytes, ""); + ?> + + + + + + "> diff --git a/mod/forum/db/mysql.php b/mod/forum/db/mysql.php index e0f2d5c101..69f7f1090b 100644 --- a/mod/forum/db/mysql.php +++ b/mod/forum/db/mysql.php @@ -75,6 +75,10 @@ function forum_upgrade($oldversion) { table_column("forum", "scale", "scale", "integer", "10", "", "0"); execute_sql("UPDATE {$CFG->prefix}forum SET scale = (- scale)"); } + + if ($oldversion < 2003100600) { + table_column("forum", "", "maxbytes", "integer", "10", "unsigned", "0", "", "scale"); + } return true; diff --git a/mod/forum/db/mysql.sql b/mod/forum/db/mysql.sql index ece465b13a..ac03778360 100644 --- a/mod/forum/db/mysql.sql +++ b/mod/forum/db/mysql.sql @@ -13,6 +13,7 @@ CREATE TABLE prefix_forum ( assesstimestart int(10) unsigned NOT NULL default '0', assesstimefinish int(10) unsigned NOT NULL default '0', scale int(10) NOT NULL default '0', + maxbytes int(10) unsigned NOT NULL default '0', forcesubscribe tinyint(1) unsigned NOT NULL default '0', timemodified int(10) unsigned NOT NULL default '0', PRIMARY KEY (id), diff --git a/mod/forum/db/oci8po.sql b/mod/forum/db/oci8po.sql index 6bad0b8ace..d12718d50d 100755 --- a/mod/forum/db/oci8po.sql +++ b/mod/forum/db/oci8po.sql @@ -14,6 +14,7 @@ CREATE TABLE prefix_forum ( open number(2) default '2' not null, assessed number(10) default '0' NOT NULL, scale number(10) default '0' NOT NULL, + maxbytes number(10) default '0' NOT NULL, forcesubscribe number(1) default '0' NOT NULL, timemodified number(10) default '0' NOT NULL ); diff --git a/mod/forum/db/postgres7.php b/mod/forum/db/postgres7.php index bc985d952f..149b8af391 100644 --- a/mod/forum/db/postgres7.php +++ b/mod/forum/db/postgres7.php @@ -19,6 +19,10 @@ function forum_upgrade($oldversion) { execute_sql("UPDATE {$CFG->prefix}forum SET scale = (- scale)"); } + if ($oldversion < 2003100600) { + table_column("forum", "", "maxbytes", "integer", "10", "unsigned", "0", "", "scale"); + } + return true; } diff --git a/mod/forum/db/postgres7.sql b/mod/forum/db/postgres7.sql index 7ac576c528..c77bffccef 100644 --- a/mod/forum/db/postgres7.sql +++ b/mod/forum/db/postgres7.sql @@ -13,6 +13,7 @@ CREATE TABLE prefix_forum ( assesstimestart integer NOT NULL default '0', assesstimefinish integer NOT NULL default '0', scale integer NOT NULL default '0', + maxbytes integer NOT NULL default '0', forcesubscribe integer NOT NULL default '0', timemodified integer NOT NULL default '0' ); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index b5053c6610..48c48649c1 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -39,6 +39,11 @@ if (!isset($CFG->forum_manydiscussions)) { set_config("forum_manydiscussions", 100); // Number of discussions on a page } +if (!isset($CFG->forum_maxbytes)) { + set_config("forum_maxbytes", 512000); // Default maximum size for all forums +} + + /// STANDARD FUNCTIONS /////////////////////////////////////////////////////////// @@ -1608,9 +1613,22 @@ function forum_add_attachment($post, $newfile) { return ""; } + if (!$forum = get_record("forum", "id", $post->forum)) { + return ""; + } + + if (!$course = get_record("course", "id", $forum->course)) { + return ""; + } + + $maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes, $forum->maxbytes); + $newfile_name = clean_filename($newfile['name']); if (valid_uploaded_file($newfile)) { + if ($maxbytes and $newfile['size'] > $maxbytes) { + return ""; + } if (! $newfile_name) { notify("This file had a wierd filename and couldn't be uploaded"); diff --git a/mod/forum/mod.html b/mod/forum/mod.html index 0dc9539179..51586ce959 100644 --- a/mod/forum/mod.html +++ b/mod/forum/mod.html @@ -17,6 +17,9 @@ if (!isset($form->forcesubscribe)) { $form->forcesubscribe = ""; } + if (!isset($form->maxbytes)) { + $form->maxbytes = $CFG->forum_maxbytes; + } ?>

action="mod.php"> @@ -91,6 +94,17 @@ ?> + + + + diff --git a/mod/forum/post.html b/mod/forum/post.html index ece418eb29..581752aab4 100644 --- a/mod/forum/post.html +++ b/mod/forum/post.html @@ -86,11 +86,12 @@ diff --git a/mod/forum/version.php b/mod/forum/version.php index b01b2974ae..8fab844995 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -5,7 +5,7 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2003082502; +$module->version = 2003100600; $module->cron = 60; ?> diff --git a/user/edit.html b/user/edit.html index 9fb983489a..e1855dd947 100644 --- a/user/edit.html +++ b/user/edit.html @@ -175,14 +175,17 @@ if (isadmin()) { -gdversion)) { ?> +maxbytes, $course->maxbytes); + if (!empty($CFG->gdversion) and $maxbytes) { +?> diff --git a/version.php b/version.php index 739af4957c..06e86fba94 100644 --- a/version.php +++ b/version.php @@ -5,7 +5,7 @@ // database to determine whether upgrades should // be performed (see lib/db/*.php) -$version = 2003091800; // The current version is a date (YYYYMMDDXX) +$version = 2003092900; // The current version is a date (YYYYMMDDXX) $release = "1.2 development"; // User-friendly version number

:

+ maxbytes, $course->maxbytes); + $choices[0] = get_string("courseuploadlimit") . " (".display_size($course->maxbytes).")"; + choose_from_menu ($choices, "maxbytes", $form->maxbytes, ""); + helpbutton("maxattachmentsize", get_string("maxattachmentsize", "forum"), "forum"); + ?> +

:

:
(

- + maxbytes, $course->maxbytes, $forum->maxbytes); ?> +

:

- +