--- auth/mnet/auth.php	2010-03-16 16:43:07.000000000 +0000
+++ auth/mnet/auth.php	2010-03-18 09:42:00.000000000 +0000
@@ -340,11 +340,13 @@
                         if (strlen($fetchrequest->response['f1']) > 0) {
                             $imagecontents = base64_decode($fetchrequest->response['f1']);
                             file_put_contents($filename, $imagecontents);
+                            @chmod($filename, $CFG->directorypermissions );
                             $localuser->picture = 1;
                         }
                         if (strlen($fetchrequest->response['f2']) > 0) {
                             $imagecontents = base64_decode($fetchrequest->response['f2']);
                             file_put_contents($dirname.'/f2.jpg', $imagecontents);
+                            @chmod( $dirname.'/f2.jpg', $CFG->directorypermissions );
                         }
                     }
                 }
--- backup/lib.php	2010-03-16 16:42:26.000000000 +0000
+++ backup/lib.php	2010-03-16 17:17:37.000000000 +0000
@@ -110,6 +110,7 @@
     //it supports a excluded dit too
     //Copied from the web !!
     function delete_dir_contents ($dir,$excludeddir="") {
+        global $CFG;
 
         if (!is_dir($dir)) {
             // if we've been given a directory that doesn't exist yet, return true.
@@ -124,7 +125,7 @@
         $dir_subdirs    = array();
 
         // Make sure we can delete it
-        chmod($dir, 0777);
+        chmod($dir, $CFG->directorypermissions );
 
         if ((($handle = opendir($dir))) == FALSE) {
             // The directory could not be opened
@@ -143,7 +144,7 @@
 
         // Delete all files in the curent directory return false and halt if a file cannot be removed
         for($i=0; $i<count($dir_files); $i++) {
-            chmod($dir_files[$i], 0777);
+            chmod($dir_files[$i], $CFG->directorypermissions );
             if (((unlink($dir_files[$i]))) == FALSE) {
                 return false;
             }
@@ -151,7 +152,7 @@
 
         // Empty sub directories and then remove the directory
         for($i=0; $i<count($dir_subdirs); $i++) {
-            chmod($dir_subdirs[$i], 0777);
+            chmod($dir_subdirs[$i], $CFG->directorypermissions );
             if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
                 return false;
             }
--- lib/gdlib.php	2010-03-16 16:41:41.000000000 +0000
+++ lib/gdlib.php	2010-03-18 10:15:25.000000000 +0000
@@ -243,8 +243,8 @@
         @touch($destination .'/f2.jpg');  // Helps in Safe mode
         if (ImageJpeg($im1, $destination .'/f1.jpg', 90) and
             ImageJpeg($im2, $destination .'/f2.jpg', 95) ) {
-            @chmod($destination .'/f1.jpg', 0666);
-            @chmod($destination .'/f2.jpg', 0666);
+            @chmod($destination .'/f1.jpg', $CFG->directorypermissions );
+            @chmod($destination .'/f2.jpg', $CFG->directorypermissions );
             return 1;
         }
     } else {
@@ -312,8 +312,8 @@
     if (function_exists('ImageJpeg')) {
         if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and
             ImageJpeg($im2, $CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
-            @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 0666);
-            @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 0666);
+            @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', $CFG->directorypermissions );
+            @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', $CFG->directorypermissions );
             return 1;
         }
     } else {
--- mod/data/field/picture/field.class.php	2010-03-16 16:42:57.000000000 +0000
+++ mod/data/field/picture/field.class.php	2010-03-18 10:12:19.000000000 +0000
@@ -246,14 +246,14 @@
                 @touch($thumbnaillocation);
                 // Helps in Safe mode
                 if (ImageJpeg($im1, $thumbnaillocation, 90)) {
-                    @chmod($thumbnaillocation, 0666);
+                    @chmod($thumbnaillocation, $CFG->directorypermissions );
                     // robertall: Why hardcode 0666??
                 }
             } elseif (function_exists('ImagePng') && $image->type == 3) {
                 @touch($thumbnaillocation);
                 // Helps in Safe mode
                 if (ImagePng($im1, $thumbnaillocation, 9)) {
-                    @chmod($thumbnaillocation, 0666);
+                    @chmod($thumbnaillocation, $CFG->directorypermissions );
                     // robertall: Why hardcode 0666??
                 }
             }
--- question/format/coursetestmanager/format.php	2010-03-16 16:43:08.000000000 +0000
+++ question/format/coursetestmanager/format.php	2010-03-18 09:56:16.000000000 +0000
@@ -51,7 +51,7 @@
                 if ($cleanfilename) {
                     $newfile = "$basedir/$cleanfilename";
                     if (move_uploaded_file($filename, $newfile)) {
-                        chmod($newfile, 0666);
+                        chmod($newfile, $CFG->directorypermissions );
                         clam_log_upload($newfile,$COURSE);
                     } else {
                         notify(get_string("uploadproblem", "", $filename));
@@ -108,7 +108,7 @@
                 if ($cleanfilename) {
                     $newfile = "$basedir/$cleanfilename";
                     if (move_uploaded_file($filename, $newfile)) {
-                        chmod($newfile, 0666);
+                        chmod($newfile, $CFG->directorypermissions );
                         clam_log_upload($newfile,$COURSE);
                     } else {
                         notify(get_string("uploadproblem", "", $filename));
