diff --git a/course/group-edit.html b/course/group-edit.html index f022bc576b5..fe3e336da70 100644 --- a/course/group-edit.html +++ b/course/group-edit.html @@ -34,9 +34,9 @@

: - - 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"]); ?> diff --git a/course/group.php b/course/group.php index 65c9141c708..cf5b611793b 100644 --- a/course/group.php +++ b/course/group.php @@ -65,11 +65,11 @@ $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"); } } } diff --git a/lib/gdlib.php b/lib/gdlib.php index 53663221193..47f2a99e165 100644 --- a/lib/gdlib.php +++ b/lib/gdlib.php @@ -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); diff --git a/lib/uploadlib.php b/lib/uploadlib.php index 05fab58a9d4..7a0a589e03e 100644 --- a/lib/uploadlib.php +++ b/lib/uploadlib.php @@ -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 .= "
".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 .= "
".$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 .= "
".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. */ diff --git a/user/edit.html b/user/edit.html index d3ce5422607..4a64b1a340b 100644 --- a/user/edit.html +++ b/user/edit.html @@ -219,9 +219,10 @@ if (isadmin()) { : - - - 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"]); ?> diff --git a/user/edit.php b/user/edit.php index b6cd2b0f054..4b5ef8d962c 100644 --- a/user/edit.php +++ b/user/edit.php @@ -64,13 +64,12 @@ $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)) { @@ -83,10 +82,8 @@ } 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); @@ -203,7 +200,7 @@ /// 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;