diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php index ea718ae4b03..e9410bd40a4 100644 --- a/lib/filestorage/stored_file.php +++ b/lib/filestorage/stored_file.php @@ -390,6 +390,29 @@ class stored_file { return copy($path, $pathname); } + /** + * Copy content of file to temporary folder and returns file path + * + * @param string $dir name of the temporary directory + * @param string $fileprefix prefix of temporary file. + * @return string|bool path of temporary file or false. + */ + public function copy_content_to_temp($dir = 'files', $fileprefix = 'tempup_') { + $tempfile = false; + if (!$dir = make_temp_directory($dir)) { + return false; + } + if (!$tempfile = tempnam($dir, $fileprefix)) { + return false; + } + if (!$this->copy_content_to($tempfile)) { + // something went wrong + @unlink($tempfile); + return false; + } + return $tempfile; + } + /** * List contents of archive. * diff --git a/user/edit.php b/user/edit.php index 6fa160819e9..ca52ed30746 100644 --- a/user/edit.php +++ b/user/edit.php @@ -153,7 +153,19 @@ $editoroptions = array( ); $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0); -$userform = new user_edit_form(null, array('editoroptions'=>$editoroptions)); +// Prepare filemanager draft area. +$draftitemid = 0; +$filemanagercontext = $editoroptions['context']; +$filemanageroptions = array('maxbytes' => $CFG->maxbytes, + 'subdirs' => 0, + 'maxfiles' => 1, + 'accepted_types' => 'web_image'); +file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions); +$user->imagefile = $draftitemid; +//create form +$userform = new user_edit_form(null, array( + 'editoroptions' => $editoroptions, + 'filemanageroptions' => $filemanageroptions)); if (empty($user->country)) { // MDL-16308 - we must unset the value here so $CFG->country can be used as default one unset($user->country); @@ -211,7 +223,7 @@ if ($usernew = $userform->get_data()) { //update user picture if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) { - useredit_update_picture($usernew, $userform); + useredit_update_picture($usernew, $userform, $filemanageroptions); } // update mail bounces diff --git a/user/edit_form.php b/user/edit_form.php index 2fdfe1e596e..e53672fe7fa 100644 --- a/user/edit_form.php +++ b/user/edit_form.php @@ -18,6 +18,11 @@ class user_edit_form extends moodleform { } else { $editoroptions = null; } + if (is_array($this->_customdata) && array_key_exists('filemanageroptions', $this->_customdata)) { + $filemanageroptions = $this->_customdata['filemanageroptions']; + } else { + $filemanageroptions = null; + } //Accessibility: "Required" is bad legend text. $strgeneral = get_string('general'); $strrequired = get_string('required'); @@ -32,7 +37,7 @@ class user_edit_form extends moodleform { $mform->addElement('header', 'moodle', $strgeneral); /// shared fields - useredit_shared_definition($mform, $editoroptions); + useredit_shared_definition($mform, $editoroptions, $filemanageroptions); /// extra settigs if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) { diff --git a/user/editadvanced.php b/user/editadvanced.php index 3c660d7f546..5226c0a54e9 100644 --- a/user/editadvanced.php +++ b/user/editadvanced.php @@ -134,8 +134,19 @@ if ($user->id !== -1) { ); } +// Prepare filemanager draft area. +$draftitemid = 0; +$filemanagercontext = $editoroptions['context']; +$filemanageroptions = array('maxbytes' => $CFG->maxbytes, + 'subdirs' => 0, + 'maxfiles' => 1, + 'accepted_types' => 'web_image'); +file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions); +$user->imagefile = $draftitemid; //create form -$userform = new user_editadvanced_form(null, array('editoroptions'=>$editoroptions)); +$userform = new user_editadvanced_form(null, array( + 'editoroptions' => $editoroptions, + 'filemanageroptions' => $filemanageroptions)); $userform->set_data($user); if ($usernew = $userform->get_data()) { @@ -203,7 +214,7 @@ if ($usernew = $userform->get_data()) { //update user picture if (!empty($CFG->gdversion) and empty($USER->newadminuser)) { - useredit_update_picture($usernew, $userform); + useredit_update_picture($usernew, $userform, $filemanageroptions); } // update mail bounces diff --git a/user/editadvanced_form.php b/user/editadvanced_form.php index 58ab2a0d0a0..7092f021a87 100644 --- a/user/editadvanced_form.php +++ b/user/editadvanced_form.php @@ -19,6 +19,11 @@ class user_editadvanced_form extends moodleform { } else { $editoroptions = null; } + if (is_array($this->_customdata) && array_key_exists('filemanageroptions', $this->_customdata)) { + $filemanageroptions = $this->_customdata['filemanageroptions']; + } else { + $filemanageroptions = null; + } //Accessibility: "Required" is bad legend text. $strgeneral = get_string('general'); @@ -58,7 +63,7 @@ class user_editadvanced_form extends moodleform { $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange')); $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange'); /// shared fields - useredit_shared_definition($mform, $editoroptions); + useredit_shared_definition($mform, $editoroptions, $filemanageroptions); /// Next the customisable profile fields profile_definition($mform); diff --git a/user/editlib.php b/user/editlib.php index 0d542998770..f229a04509a 100644 --- a/user/editlib.php +++ b/user/editlib.php @@ -42,7 +42,7 @@ function useredit_update_user_preference($usernew) { * @param moodleform $userform The form that was submitted to edit the form * @return bool True if the user was updated, false if it stayed the same. */ -function useredit_update_picture(stdClass $usernew, moodleform $userform) { +function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) { global $CFG, $DB; require_once("$CFG->libdir/gdlib.php"); @@ -50,20 +50,40 @@ function useredit_update_picture(stdClass $usernew, moodleform $userform) { $user = $DB->get_record('user', array('id'=>$usernew->id), 'id, picture', MUST_EXIST); $newpicture = $user->picture; - + // Get file_storage to process files. + $fs = get_file_storage(); if (!empty($usernew->deletepicture)) { // The user has chosen to delete the selected users picture - $fs = get_file_storage(); $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area $newpicture = 0; - } else if ($iconfile = $userform->save_temp_file('imagefile')) { - // There is a new image that has been uploaded - // Process the new image and set the user to make use of it. - // NOTE: Uploaded images always take over Gravatar - $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile); - // Delete the file that has now been processed - @unlink($iconfile); + } else { + // Save newly uploaded file, this will avoid context mismatch for newly created users. + file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions); + if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) { + // Get file which was uploaded in draft area + foreach ($iconfiles as $file) { + if (!$file->is_directory()) { + break; + } + } + // Copy file to temporary location and the send it for processing icon + if ($iconfile = $file->copy_content_to_temp()) { + // There is a new image that has been uploaded + // Process the new image and set the user to make use of it. + // NOTE: Uploaded images always take over Gravatar + $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile); + // Delete temporary file + @unlink($iconfile); + // Remove uploaded file. + $fs->delete_area_files($context->id, 'user', 'newicon'); + } else { + // Something went wrong while creating temp file. + // Remove uploaded file. + $fs->delete_area_files($context->id, 'user', 'newicon'); + return false; + } + } } if ($newpicture != $user->picture) { @@ -101,7 +121,7 @@ function useredit_update_interests($user, $interests) { tag_set('user', $user->id, $interests); } -function useredit_shared_definition(&$mform, $editoroptions = null) { +function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) { global $CFG, $USER, $DB; $user = $DB->get_record('user', array('id' => $USER->id)); @@ -258,7 +278,7 @@ function useredit_shared_definition(&$mform, $editoroptions = null) { $mform->addElement('checkbox', 'deletepicture', get_string('delete')); $mform->setDefault('deletepicture', 0); - $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes))); + $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions); $mform->addHelpButton('imagefile', 'newpicture'); $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');