]> git.mjollnir.org Git - moodle.git/commitdiff
Integration of new centralised upload code with user profile, and group profile,...
authormjollnir_ <mjollnir_>
Thu, 16 Sep 2004 00:16:48 +0000 (00:16 +0000)
committermjollnir_ <mjollnir_>
Thu, 16 Sep 2004 00:16:48 +0000 (00:16 +0000)
These patches are maintained in an publicly accessible Arch repository, see: http://lists.eduforge.org/cgi-bin/archzoom.cgi/arch-eduforge@catalyst.net.nz--2004-MIRROR/moodle--eduforge--1.3.3

Index of arch patches in this commit:

arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-66
    2004-09-16 00:03:41 GMT
    Penny Leach <penny@catalyst.net.nz>
    changes to upload class to be silent if we want it to be, integration with editing user profile and group profile (which meant changes in lib/gdlib.php as well as course/group-edit and group.php and user/edit.html and edit.php

Full logs:

Revision: moodle--eduforge--1.3.3--patch-66
Archive: arch-eduforge@catalyst.net.nz--2004
Creator: Penny Leach <penny@catalyst.net.nz>
Date: Thu Sep 16 12:03:41 NZST 2004
Standard-date: 2004-09-16 00:03:41 GMT
Modified-files: course/group-edit.html course/group.php
    lib/gdlib.php lib/uploadlib.php user/edit.html
    user/edit.php
New-patches: arch-eduforge@catalyst.net.nz--2004/moodle--eduforge--1.3.3--patch-66
Summary: changes to upload class to be silent if we want it to be, integration with editing user profile and group profile (which meant changes in lib/gdlib.php as well as course/group-edit and group.php and user/edit.html and edit.php
Keywords:

course/group-edit.html
course/group.php
lib/gdlib.php
lib/uploadlib.php
user/edit.html
user/edit.php

index f022bc576b5c8bf76768b2735283ab8a6603996f..fe3e336da7061f50fced8b23f26891efe335fa88 100644 (file)
@@ -34,9 +34,9 @@
 <tr valign="top">
     <td align="right"><p><?php print_string("newpicture") ?>:</td>
     <td>
-    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $maxbytes ?>" />
-    <input type="file" name="imagefile" size="40" />
     <?php helpbutton("picture", get_string("helppicture"));
+       require_once($CFG->dirroot.'/lib/uploadlib.php');
+       upload_print_form_fragment(1,array('imagefile'),null,false,null,0,0,false);
        print_string("maxsize", "", display_size($maxbytes)); 
        if (isset($err["imagefile"])) formerr($err["imagefile"]);
     ?>
index 65c9141c7089bf944e7f1c8119039e1fb7fed3c6..cf5b611793b348cc61a26ad1a60e65a9ab9792c6 100644 (file)
             $err['name'] = get_string("missingname");
 
         } else {
-            if (!empty($_FILES['imagefile'])) {
+            require_once($CFG->dirroot.'/lib/uploadlib.php');
+            $um = new upload_manager('imagefile',false,false,null,false,0,false);
+            if ($um->preprocess_files()) {
                 require_once("$CFG->libdir/gdlib.php");
-                if ($filename = valid_uploaded_file($_FILES['imagefile'])) { 
-                    $group->picture = save_profile_image($group->id, $filename, 'groups');
-                }
+                $group->picture = save_profile_image($group->id, $um, 'groups');
             }
             $group->name        = $form->name;
             $group->description = $form->description;
@@ -77,7 +77,8 @@
             if (!update_record("groups", $group)) {
                 notify("A strange error occurred while trying to save ");
             } else {
-                redirect("group.php?id=$course->id&group=$group->id", get_string("changessaved"));
+                notify(get_string('changessaved'));
+                print_continue("group.php?id=$course->id&group=$group->id");
             }
         }
     }
index 5366322119380af8d96028018f22506168cdd4bd..47f2a99e165cfd01cb0e9e582142a09b88287150 100644 (file)
@@ -46,8 +46,8 @@ function ImageCopyBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $
     } 
 }
 
-function save_profile_image($id, $filename, $dir="users") {
-// Given a filename to a known image, this function scales and crops
+function save_profile_image($id, $uploadmanager, $dir="users") {
+// Given an upload manager with the right settings, this function performs a virus scan, and then scales and crops
 // it and saves it in the right place to be a "user" or "group" image.
 
     global $CFG;
@@ -56,6 +56,10 @@ function save_profile_image($id, $filename, $dir="users") {
         return false;
     }
 
+    if (!$uploadmanager) {
+        return false;
+    }
+
     umask(0000);
 
     if (!file_exists("$CFG->dataroot/$dir")) {
@@ -69,14 +73,13 @@ function save_profile_image($id, $filename, $dir="users") {
             return false;
         }
     }
-    
-    $originalfile = "$CFG->dataroot/$dir/$id/original";
 
-    if (!move_uploaded_file($filename, $originalfile)) {
+    $destination = "$CFG->dataroot/$dir/$id";
+    if (!$uploadmanager->save_files($destination)) {
         return false;
     }
 
-    @chmod($originalfile, 0666);
+    $originalfile = $uploadmanager->get_new_filepath();
 
     $imageinfo = GetImageSize($originalfile);
     
index 05fab58a9d40605095210d512ad8d3270f43bb49..7a0a589e03e311b7f699f97ea0bfc4e85f18b93c 100644 (file)
@@ -10,6 +10,7 @@ class upload_manager {
     var $status; // keep track of if we're ok (errors for each file are kept in $files['whatever']['uploadlog']
     var $course; // the course this file has been uploaded for (for logging and virus notifications)
     var $inputname; // if we're only getting one file.
+    var $notify; // if we're given silent=true in the constructor, this gets built up to hold info about the process.
 
     /**
      * Constructor, sets up configuration stuff so we know how to act.
@@ -19,9 +20,10 @@ class upload_manager {
      * @param $handlecollisions - whether to use handle_filename_collision() or not. (optional, defaults to false)
      * @param $course - the course the files are being uploaded for (for logging and virus notifications)
      * @param $recoverifmultiple - if we come across a virus, or if a file doesn't validate or whatever, do we continue? optional, defaults to true.
-     * @param $modbytes - max bytes for this module - this and $course->maxbytes are used to get the maxbytes to use (lowest) from get_max_upload_file_size().
+     * @param $modbytes - max bytes for this module - this and $course->maxbytes are used to get the maxbytes from get_max_upload_file_size().
+     * @param $silent - whether to notify errors or not.
      */
-    function upload_manager($inputname='',$deleteothers=false,$handlecollisions=false,$course=null,$recoverifmultiple=false,$modbytes=0) {
+    function upload_manager($inputname='',$deleteothers=false,$handlecollisions=false,$course=null,$recoverifmultiple=false,$modbytes=0,$silent=false) {
         
         global $CFG;
         
@@ -29,6 +31,7 @@ class upload_manager {
         $this->config->handlecollisions = $handlecollisions;
         $this->config->recoverifmultiple = $recoverifmultiple;
         $this->config->maxbytes = get_max_upload_file_size($CFG->maxbytes,$course->maxbytes,$modbytes);
+        $this->config->silent = $silent;
         $this->files = array();
         $this->status = false; 
         $this->course = $course;
@@ -58,12 +61,22 @@ class upload_manager {
                     if (!$this->config->recoverifmultiple && count($this->files) > 1) {
                         $a->name = $this->files[$name]['originalname'];
                         $a->problem = $this->files[$name]['uploadlog'];
-                        notify(get_string('uploadfailednotrecovering','moodle',$a));
+                        if (!$this->config->silent) {
+                            notify(get_string('uploadfailednotrecovering','moodle',$a));
+                        }
+                        else {
+                            $this->notify .= "<br />".get_string('uploadfailednotrecovering','moodle',$a);
+                        }
                         $this->status = false;
                         return false;
                     }
                     else if (count($this->files) == 1) {
-                        notify($this->files[$name]['uploadlog']);
+                        if (!$this->config->silent) {
+                            notify($this->files[$name]['uploadlog']);
+                        }
+                        else {
+                            $this->notify .= "<br />".$this->files[$name]['uploadlog'];
+                        }
                         $this->status = false;
                         return false;
                     }
@@ -200,7 +213,12 @@ class upload_manager {
             }
         }
         if ($deletedsomething) {
-            notify(get_string('uploadoldfilesdeleted'));
+            if (!$this->config->silent) {
+                notify(get_string('uploadoldfilesdeleted'));
+            }
+            else {
+                $this->notify .= "<br />".get_string('uploadoldfilesdeleted');
+            }
         }
     }
     
@@ -309,6 +327,16 @@ class upload_manager {
         return false;
     }
 
+    /** 
+     * If we're only handling one file (if input name was given in the constructor) this will return the full path to the saved file.
+     */
+    function get_new_filepath() {
+        if (!empty($this->inputname) && count($this->files) == 1) {
+            return $this->files[$this->inputname]['fullpath'];
+        }
+        return false;
+    }
+
     /** 
      * If we're only handling one file (if inputname was given in the constructor) this will return the ORIGINAL filename of the file.
      */
index d3ce5422607f13b0f9770e6005042b381393684b..4a64b1a340b61089526f742709bd92a6c44c3c1f 100644 (file)
@@ -219,9 +219,10 @@ if (isadmin()) {
 <tr valign="top">
     <td align="right"> <?php print_string("newpicture") ?>: </td>
     <td> 
-    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $maxbytes ?>" />
-    <input type="file" name="imagefile" size="40" />
-    <?php helpbutton("picture", get_string("helppicture"));
+    <?php 
+       require_once($CFG->dirroot.'/lib/uploadlib.php');
+       upload_print_form_fragment(1,array('imagefile'),null,false,null,0,0,false);
+       helpbutton("picture", get_string("helppicture"));
        print_string("maxsize", "", display_size($maxbytes));
        if (isset($err["imagefile"])) formerr($err["imagefile"]);
     ?>
index b6cd2b0f054484ed44e6cfb1c83dc7ce88f79753..4b5ef8d962c83fa752ed146382e9413c96393005 100644 (file)
             $usernew->username = trim(moodle_strtolower($usernew->username));
         }
 
-        if (empty($_FILES['imagefile'])) {
-            $_FILES['imagefile'] = NULL;    // To avoid using uninitialised variable later
-        }
 
-        if (find_form_errors($user, $usernew, $err)) {
-            if ($filename = valid_uploaded_file($_FILES['imagefile'])) { 
-                $usernew->picture = save_profile_image($user->id, $filename);
+        require_once($CFG->dirroot.'/lib/uploadlib.php');
+        $um = new upload_manager('imagefile',false,false,null,false,0,true);
+
+        if (find_form_errors($user, $usernew, $err,$um)) {
+            if (empty($err['imagefile']) && $usernew->picture = save_profile_image($user->id, $um,'users')) {
                 set_field('user', 'picture', $usernew->picture, 'id', $user->id);  /// Note picture in DB
             } else {
                 if (!empty($usernew->deletepicture)) {
 
         } else {
             $timenow = time();
-
-            if ($filename = valid_uploaded_file($_FILES['imagefile'])) { 
-                $usernew->picture = save_profile_image($user->id, $filename);
-            } else {
+            
+            if (!$usernew->picture = save_profile_image($user->id,$um,'users')) {
                 if (!empty($usernew->deletepicture)) {
                     set_field('user', 'picture', 0, 'id', $user->id);  /// Delete picture
                     $usernew->picture = 0;
@@ -94,9 +91,9 @@
                     $usernew->picture = $user->picture;
                 }
             }
-    
+            
             $usernew->timemodified = time();
-
+            
             if (isadmin()) {
                 if (!empty($usernew->newpassword)) {
                     $usernew->password = md5($usernew->newpassword);
 
 /// FUNCTIONS ////////////////////
 
-function find_form_errors(&$user, &$usernew, &$err) {
+function find_form_errors(&$user, &$usernew, &$err, &$um) {
     global $CFG;
 
     if (isadmin()) {
@@ -262,6 +259,9 @@ function find_form_errors(&$user, &$usernew, &$err) {
             $err["email"] = $error;
         }
     }
+    if (!$um->preprocess_files()) {
+        $err['imagefile'] = $um->notify;
+    }
 
     $user->email = $usernew->email;