]> git.mjollnir.org Git - moodle.git/commitdiff
These changes implement some better controls for controlling uploads.
authormoodler <moodler>
Mon, 6 Oct 2003 18:02:35 +0000 (18:02 +0000)
committermoodler <moodler>
Mon, 6 Oct 2003 18:02:35 +0000 (18:02 +0000)
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.

24 files changed:
admin/config.html
admin/module.php
course/edit.html
course/scales.php
lib/db/mysql.php
lib/db/mysql.sql
lib/db/postgres7.php
lib/db/postgres7.sql
lib/moodlelib.php
mod/assignment/config.html [new file with mode: 0644]
mod/assignment/lib.php
mod/assignment/mod.html
mod/forum/config.html
mod/forum/db/mysql.php
mod/forum/db/mysql.sql
mod/forum/db/oci8po.sql
mod/forum/db/postgres7.php
mod/forum/db/postgres7.sql
mod/forum/lib.php
mod/forum/mod.html
mod/forum/post.html
mod/forum/version.php
user/edit.html
version.php

index b8a79c71f8b3c57d370d3048ff5c0127d5a3edeb..2848bbd29908cb35ec4800b58ed60b036c5bd6cc 100644 (file)
     <?php print_string("configteacherassignteachers") ?>
     </td>
 </tr>
+<tr valign=top>
+       <td align=right><p>maxbytes:</td>
+       <td>
+    <?php 
+       $options = get_max_upload_sizes();
+       choose_from_menu ($options, "maxbytes", $config->maxbytes, "");
+    ?>
+    </td>
+    <td>
+    <?php print_string("configmaxbytes") ?>
+    </td>
+</tr>
 
 <tr>
     <td colspan=3 align=center>
index 5710e8f4f83fb7341d5ec5838995eaea6b93e794..5d4231ba6feb60c9983986489269de67ea2055dc 100644 (file)
@@ -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,
                   "<a href=\"index.php\">$stradmin</a> -> ".
                   "<a href=\"configure.php\">$strconfiguration</a> -> ".
                   "<a href=\"modules.php\">$strmanagemodules</a> -> $strmodulename", $focus);
index e8e316396566c3f0f25f28e3b3e6d9d58d77941e..0d9d7d2dc812d52e24f982c8f7768505502f54f8 100644 (file)
     helpbutton("coursegrades", get_string("grades")); ?>
        </td>
 </tr>
+<tr valign=top>
+       <td><P><?php  print_string("maximumupload") ?>:</td>
+       <td><?php
+    $choices = get_max_upload_sizes($CFG->maxbytes);
+    choose_from_menu ($choices, "maxbytes", $form->maxbytes, "");
+    helpbutton("courseuploadsize", get_string("maximumupload")); ?>
+       </td>
+</tr>
 <tr valign=top>
        <td><P><?php  print_string("wordforteacher") ?>:</td>
        <td><input type="text" name="teacher" maxlength="100" size=25 value="<?php  p($form->teacher) ?>">
index 8ce76a4c62a673a76bce9407e5e9b8deecd4d548..6f41f39d33065434d7dff33d176e940ba5e82e11 100644 (file)
 
         if ($scales = get_records("scale", "courseid", "$course->id", "name ASC")) {
             print_heading($strcustomscales);
+
+            if (isteacheredit($course->id)) {
+                echo "<p align=\"center\">(";
+                print_string("scalestip");
+                echo ")</p>";
+            }
+
             foreach ($scales as $scale) {
                 $scalemenu = make_menu_from_list($scale->scale);
 
                 print_simple_box_end();
                 echo "<hr />";
             }
+
+        } else {
+            if (isteacheredit($course->id)) {
+                echo "<p align=\"center\">(";
+                print_string("scalestip");
+                echo ")</p>";
+            }
         }
 
         if ($scales = get_records("scale", "courseid", "0", "name ASC")) {
index d5b6ef09180914b2e14403793ab76921f43a3381..3152c56f189ff75fc69a3037675286377bb4d440 100644 (file)
@@ -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;
 
 }
index 9b8b2a9b3b419db8c6f69c97e819c0a40caabe9e..d9da0b3d5806369e7c43c14e0ee4a5985e26bc63 100644 (file)
@@ -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',
index 89e6a89a030bcc234cef958ce4490042c82d467e..e7e09b7ce19d41d0799cb8c91e2861e722bd589a 100644 (file)
@@ -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;
 }
index 5e4fe2d3217745c1543bc74e4f046816bf849288..30a5f4141e9d1c7f67a42856484088725d6c4d3e 100644 (file)
@@ -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'
index 8a57b0350e639ea5c082c2f1c4896270cfbaaff1..ec9d6bfcccb05530adccdeafb7a45699ee4284cb 100644 (file)
@@ -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 (file)
index 0000000..2624896
--- /dev/null
@@ -0,0 +1,22 @@
+<form method="post" action="module.php" name="form">
+
+<table cellpadding=9 cellspacing=0 >
+<tr valign=top>
+       <td align=right><p>assignment_maxbytes:</td>
+    <td><?php
+    $choices = get_max_upload_sizes($CFG->maxbytes);
+    choose_from_menu ($choices, "assignment_maxbytes", $CFG->assignment_maxbytes, "");
+    ?>
+    </td>
+    <td>
+    <?php print_string("configmaxbytes", "assignment") ?>
+    </td>
+</tr>
+
+<tr>
+    <td colspan=3 align=center>
+       <input type="submit" value="<?php print_string("savechanges") ?>"></td>
+</tr>
+</table>
+
+</form>
index 8dfacf800cd24afa5911d069855ee7a5bc0b8373..37dab00ec6ddd6209608987ea46a17f93e22e032 100644 (file)
@@ -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, 
index 224c612f2de7f8e54996db62303331d5eac53393..031bcf2b37b4081101ce7c5b2d18529b3ea74799 100644 (file)
@@ -25,7 +25,7 @@
         $form->grade = 100;
     }
     if (empty($form->maxbytes)) {
-        $form->maxbytes = "";
+        $form->maxbytes = $CFG->assignment_maxbytes;
     }
     if (empty($form->timedue)) {
         $form->timedue = "";
 </tr>
 <tr valign=top>
     <td align=right><p><b><?php print_string("maximumsize", "assignment") ?>:</b></p></td>
-    <td>
-        <?php
-        $sizelist = array("10Kb", "50Kb", "100Kb", "500Kb", "1Mb", "2Mb", "5Mb", "10Mb", "20Mb", "50Mb");
-        $maxsize = get_max_upload_file_size();
-        $sizeinlist = false;
-        foreach ($sizelist as $size) {
-            $sizebytes = get_real_size($size);
-            if ($sizebytes < $maxsize) {
-                $filesize[$sizebytes] = $size;
-            }
-            if ($form->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", "");
-        ?>
-    </td>
+       <td><?php
+    $choices = get_max_upload_sizes($CFG->maxbytes, $course->maxbytes);
+    choose_from_menu ($choices, "maxbytes", $form->maxbytes, "");
+    ?>
+       </td>
 </tr>
 <tr valign=top>
     <td align=right><p><b><?php print_string("duedate", "assignment") ?>:</b></td>
index 6686c4be6f6077c13d6f757bb998644dea0e5dcf..a374d2eeb616a6ce226cdbf0b329ff6ecec6321f 100644 (file)
     <?php print_string("configmanydiscussions", "forum") ?>
     </td>
 </tr>
+<tr valign=top>
+       <td align=right><p>forum_maxbytes:</td>
+    <td><?php
+    $choices = get_max_upload_sizes($CFG->maxbytes);
+    choose_from_menu ($choices, "forum_maxbytes", $CFG->forum_maxbytes, "");
+    ?>
+    </td>
+    <td>
+    <?php print_string("configmaxbytes", "forum") ?>
+    </td>
+</tr>
+
 <tr>
     <td colspan=3 align=center>
        <input type="submit" value="<?php print_string("savechanges") ?>"></td>
index e0f2d5c1016428a7b2cbe56f48630479e2acc12e..69f7f1090b015e78abbbeb5700e5b6bdacbfc88e 100644 (file)
@@ -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;
 
index ece465b13a4ff3b5f865da13a5378fefd12b9992..ac037783606cfb5ef1ff6b98adc763aeeaf534c1 100644 (file)
@@ -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),
index 6bad0b8acefade3af386fe0b8c22cd647b78501a..d12718d50d3bd72567f81542c714ab34e34db0c6 100755 (executable)
@@ -14,6 +14,7 @@ CREATE TABLE prefix_forum (
   open number(2)  default '2' not null,\r
   assessed number(10)  default '0' NOT NULL,\r
   scale number(10)  default '0' NOT NULL,\r
+  maxbytes number(10)  default '0' NOT NULL,\r
   forcesubscribe number(1)  default '0' NOT NULL,\r
   timemodified number(10)  default '0' NOT NULL\r
 );\r
index bc985d952f4f281e0ece0e3fbb50b718d7ce5f04..149b8af39185e70fe2cad4dc817026c10ec67834 100644 (file)
@@ -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;
 
 }
index 7ac576c5284c3c0011d376de890e75b32fab75b9..c77bffccef3403cc045af62c52b95fbaad55bc74 100644 (file)
@@ -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'
 );
index b5053c66107f7a181209e7d0cead1a1f331a7378..48c48649c1c2ee7cbed7acdd5895b822cb06f013 100644 (file)
@@ -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");
 
index 0dc953917911830877c7f91556723ebef57803a5..51586ce9597c25a8fe4b2321c52c1dca1f499e7b 100644 (file)
@@ -17,6 +17,9 @@
     if (!isset($form->forcesubscribe)) {
         $form->forcesubscribe = "";
     }
+    if (!isset($form->maxbytes)) {
+        $form->maxbytes = $CFG->forum_maxbytes;
+    }
 ?>
 <form name="form" method="post" <?php echo $onsubmit ?> action="mod.php">
 <table cellpadding=5>
         ?>
     </td>
 </tr>
+<tr valign=top>
+       <td align=right><p><b><?php  print_string("maxattachmentsize", "forum") ?>:</b></p></td>
+       <td>
+        <?php
+            $choices = get_max_upload_sizes($CFG->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");
+        ?>
+       </td>
+</tr>
 
 <tr>
     <td align=right valign=top><p><b><?php print_string("allowratings", "forum") ?>:</b></p></td>
index ece418eb2908d241db185bc5b7c8494832b7b306..581752aab44e5e40ac48a18e5e50dfd71966b8f7 100644 (file)
 <tr valign=top>
     <td align=right><p><b><?php print_string("attachment", "forum") ?>:<br \>(<?php print_string("optional") ?>)&nbsp;</b></p></td>
     <td>
-    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo get_max_upload_file_size() ?>">
+    <?php $maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes, $forum->maxbytes); ?>
+    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $maxbytes ?>">
     <input type="file" name="attachment" size=40> 
     <?php 
        helpbutton("attachment", get_string("attachment", "forum"), "forum"); 
-       print_string("maxsize", "", display_size(get_max_upload_file_size())); 
+       print_string("maxsize", "", display_size($maxbytes)); 
     ?>
     </td>
 
index b01b2974ae85d81c19bfe29945eb9700af15d7f9..8fab844995d5ab5942280eb7fb9ed7153f67211d 100644 (file)
@@ -5,7 +5,7 @@
 //  This fragment is called by /admin/index.php
 ////////////////////////////////////////////////////////////////////////////////
 
-$module->version  = 2003082502;
+$module->version  = 2003100600;
 $module->cron     = 60;
 
 ?>
index 9fb983489a52d1136c2aa9996046f69d8a1c9a16..e1855dd94759eeb145e75e3d94197fe575c6e3d6 100644 (file)
@@ -175,14 +175,17 @@ if (isadmin()) {
 </tr>
 
 
-<?php if (!empty($CFG->gdversion)) {  ?>
+<?php 
+    $maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes);
+    if (!empty($CFG->gdversion) and $maxbytes) {  
+?>
 <tr valign=top>
     <td><p><?php print_string("newpicture") ?>:</td>
     <td>
-    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo get_max_upload_file_size() ?>">
+    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $maxbytes ?>">
     <input type="file" name="imagefile" size=40>
     <?php helpbutton("picture", get_string("helppicture"));
-       print_string("maxsize", "", display_size(get_max_upload_file_size())); 
+       print_string("maxsize", "", display_size($maxbytes)); 
        if (isset($err["imagefile"])) formerr($err["imagefile"]);
     ?>
     </td>
index 739af4957c05427142dfe12431273f48262e280a..06e86fba94f3ba86d0123fc98ce0df5e9f573da0 100644 (file)
@@ -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