mirror of
https://github.com/moodle/moodle.git
synced 2025-02-21 01:48:45 +01:00
AMOS BEGIN MOV [uploadpictures,core_admin],[uploadpictures,tool_uploaduser] MOV [uploadpictures_help,core_admin],[uploadpictures_help,tool_uploaduser] MOV [uploadusers,core_admin],[uploadusers,tool_uploaduser] MOV [uploadusers_help,core_admin],[uploadusers_help,tool_uploaduser] MOV [uploaduserspreview,core_admin],[uploaduserspreview,tool_uploaduser] MOV [uploadusersresult,core_admin],[uploadusersresult,tool_uploaduser] MOV [uucsvline,core_admin],[uucsvline,tool_uploaduser] MOV [userrenamed,core_admin],[userrenamed,tool_uploaduser] MOV [useraccountupdated,core_admin],[useraccountupdated,tool_uploaduser] MOV [useraccountuptodate,core_admin],[useraccountuptodate,tool_uploaduser] MOV [userdeleted,core_admin],[userdeleted,tool_uploaduser] MOV [uupasswordcron,core_admin],[uupasswordcron,tool_uploaduser] MOV [userscreated,core_admin],[userscreated,tool_uploaduser] MOV [usersupdated,core_admin],[usersupdated,tool_uploaduser] MOV [usersdeleted,core_admin],[usersdeleted,tool_uploaduser] MOV [deleteerrors,core_admin],[deleteerrors,tool_uploaduser] MOV [usersrenamed,core_admin],[usersrenamed,tool_uploaduser] MOV [renameerrors,core_admin],[renameerrors,tool_uploaduser] MOV [usersskipped,core_admin],[usersskipped,tool_uploaduser] MOV [usersweakpassword,core_admin],[usersweakpassword,tool_uploaduser] MOV [errors,core_admin],[errors,tool_uploaduser] MOV [csvdelimiter,core_admin],[csvdelimiter,tool_uploaduser] MOV [encoding,core_admin],[encoding,tool_uploaduser] MOV [rowpreviewnum,core_admin],[rowpreviewnum,tool_uploaduser] MOV [uuoptype_addinc,core_admin],[uuoptype_addinc,tool_uploaduser] MOV [uuoptype_addnew,core_admin],[uuoptype_addnew,tool_uploaduser] MOV [uuoptype_addupdate,core_admin],[uuoptype_addupdate,tool_uploaduser] MOV [uuoptype_update,core_admin],[uuoptype_update,tool_uploaduser] MOV [uuoptype,core_admin],[uuoptype,tool_uploaduser] MOV [uupasswordnew,core_admin],[uupasswordnew,tool_uploaduser] MOV [uupasswordold,core_admin],[uupasswordold,tool_uploaduser] MOV [nochanges,core_admin],[nochanges,tool_uploaduser] MOV [uuupdateall,core_admin],[uuupdateall,tool_uploaduser] MOV [uuupdatefromfile,core_admin],[uuupdatefromfile,tool_uploaduser] MOV [uuupdatemissing,core_admin],[uuupdatemissing,tool_uploaduser] MOV [uuupdatetype,core_admin],[uuupdatetype,tool_uploaduser] MOV [allowrenames,core_admin],[allowrenames,tool_uploaduser] MOV [allowdeletes,core_admin],[allowdeletes,tool_uploaduser] MOV [uunoemailduplicates,core_admin],[uunoemailduplicates,tool_uploaduser] MOV [uustandardusernames,core_admin],[uustandardusernames,tool_uploaduser] MOV [uubulk,core_admin],[uubulk,tool_uploaduser] MOV [uubulkall,core_admin],[uubulkall,tool_uploaduser] MOV [uubulknew,core_admin],[uubulknew,tool_uploaduser] MOV [uubulkupdated,core_admin],[uubulkupdated,tool_uploaduser] MOV [uulegacy1role,core_admin],[uulegacy1role,tool_uploaduser] MOV [uulegacy2role,core_admin],[uulegacy2role,tool_uploaduser] MOV [uulegacy3role,core_admin],[uulegacy3role,tool_uploaduser] MOV [defaultvalues,core_admin],[defaultvalues,tool_uploaduser] MOV [uuusernametemplate,core_admin],[uuusernametemplate,tool_uploaduser] MOV [requiredtemplate,core_admin],[requiredtemplate,tool_uploaduser] MOV [uploadpicture_baduserfield,core_admin],[uploadpicture_baduserfield,tool_uploaduser] MOV [uploadpicture_cannotmovezip,core_admin],[uploadpicture_cannotmovezip,tool_uploaduser] MOV [uploadpicture_cannotprocessdir,core_admin],[uploadpicture_cannotprocessdir,tool_uploaduser] MOV [uploadpicture_cannotsave,core_admin],[uploadpicture_cannotsave,tool_uploaduser] MOV [uploadpicture_cannotunzip,core_admin],[uploadpicture_cannotunzip,tool_uploaduser] MOV [uploadpicture_invalidfilename,core_admin],[uploadpicture_invalidfilename,tool_uploaduser] MOV [uploadpicture_overwrite,core_admin],[uploadpicture_overwrite,tool_uploaduser] MOV [uploadpicture_userfield,core_admin],[uploadpicture_userfield,tool_uploaduser] MOV [uploadpicture_usernotfound,core_admin],[uploadpicture_usernotfound,tool_uploaduser] MOV [uploadpicture_userskipped,core_admin],[uploadpicture_userskipped,tool_uploaduser] MOV [uploadpicture_userupdated,core_admin],[uploadpicture_userupdated,tool_uploaduser] AMOS END
255 lines
9.0 KiB
PHP
255 lines
9.0 KiB
PHP
<?php
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
* Bulk upload of user pictures
|
|
*
|
|
* Based on .../admin/uploaduser.php and .../lib/gdlib.php
|
|
*
|
|
* @package tool
|
|
* @subpackage uploaduser
|
|
* @copyright (C) 2007 Inaki Arenaza
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
require('../../../config.php');
|
|
require_once($CFG->libdir.'/adminlib.php');
|
|
require_once($CFG->libdir.'/gdlib.php');
|
|
require_once('picture_form.php');
|
|
|
|
define ('PIX_FILE_UPDATED', 0);
|
|
define ('PIX_FILE_ERROR', 1);
|
|
define ('PIX_FILE_SKIPPED', 2);
|
|
|
|
admin_externalpage_setup('tooluploaduserpictures');
|
|
|
|
require_login();
|
|
|
|
require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
|
|
|
|
$site = get_site();
|
|
|
|
if (!$adminuser = get_admin()) {
|
|
print_error('noadmins', 'error');
|
|
}
|
|
|
|
$strfile = get_string('file');
|
|
$struser = get_string('user');
|
|
$strusersupdated = get_string('usersupdated', 'tool_uploaduser');
|
|
$struploadpictures = get_string('uploadpictures','tool_uploaduser');
|
|
|
|
$userfields = array (
|
|
0 => 'username',
|
|
1 => 'idnumber',
|
|
2 => 'id' );
|
|
|
|
$userfield = optional_param('userfield', 0, PARAM_INT);
|
|
$overwritepicture = optional_param('overwritepicture', 0, PARAM_BOOL);
|
|
|
|
/// Print the header
|
|
echo $OUTPUT->header();
|
|
|
|
echo $OUTPUT->heading_with_help($struploadpictures, 'uploadpictures', 'tool_uploaduser');
|
|
|
|
$mform = new admin_uploadpicture_form(null, $userfields);
|
|
if ($formdata = $mform->get_data()) {
|
|
if (!array_key_exists($userfield, $userfields)) {
|
|
echo $OUTPUT->notification(get_string('uploadpicture_baduserfield', 'tool_uploaduser'));
|
|
} else {
|
|
// Large files are likely to take their time and memory. Let PHP know
|
|
// that we'll take longer, and that the process should be recycled soon
|
|
// to free up memory.
|
|
@set_time_limit(0);
|
|
raise_memory_limit(MEMORY_EXTRA);
|
|
|
|
// Create a unique temporary directory, to process the zip file
|
|
// contents.
|
|
$zipdir = my_mktempdir($CFG->tempdir.'/', 'usrpic');
|
|
$dstfile = $zipdir.'/images.zip';
|
|
|
|
if (!$mform->save_file('userpicturesfile', $dstfile, true)) {
|
|
echo $OUTPUT->notification(get_string('uploadpicture_cannotmovezip', 'tool_uploaduser'));
|
|
@remove_dir($zipdir);
|
|
} else {
|
|
$fp = get_file_packer('application/zip');
|
|
$unzipresult = $fp->extract_to_pathname($dstfile, $zipdir);
|
|
if (!$unzipresult) {
|
|
echo $OUTPUT->notification(get_string('uploadpicture_cannotunzip', 'tool_uploaduser'));
|
|
@remove_dir($zipdir);
|
|
} else {
|
|
// We don't need the zip file any longer, so delete it to make
|
|
// it easier to process the rest of the files inside the directory.
|
|
@unlink($dstfile);
|
|
|
|
$results = array ('errors' => 0,'updated' => 0);
|
|
|
|
process_directory($zipdir, $userfields[$userfield], $overwritepicture, $results);
|
|
|
|
|
|
// Finally remove the temporary directory with all the user images and print some stats.
|
|
remove_dir($zipdir);
|
|
echo $OUTPUT->notification(get_string('usersupdated', 'tool_uploaduser') . ": " . $results['updated'], 'notifysuccess');
|
|
echo $OUTPUT->notification(get_string('errors', 'tool_uploaduser') . ": " . $results['errors'], ($results['errors'] ? 'notifyproblem' : 'notifysuccess'));
|
|
echo '<hr />';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$mform->display();
|
|
echo $OUTPUT->footer();
|
|
exit;
|
|
|
|
// ----------- Internal functions ----------------
|
|
|
|
/**
|
|
* Create a unique temporary directory with a given prefix name,
|
|
* inside a given directory, with given permissions. Return the
|
|
* full path to the newly created temp directory.
|
|
*
|
|
* @param string $dir where to create the temp directory.
|
|
* @param string $prefix prefix for the temp directory name (default '')
|
|
*
|
|
* @return string The full path to the temp directory.
|
|
*/
|
|
function my_mktempdir($dir, $prefix='') {
|
|
global $CFG;
|
|
|
|
if (substr($dir, -1) != '/') {
|
|
$dir .= '/';
|
|
}
|
|
|
|
do {
|
|
$path = $dir.$prefix.mt_rand(0, 9999999);
|
|
} while (file_exists($path));
|
|
|
|
check_dir_exists($path);
|
|
|
|
return $path;
|
|
}
|
|
|
|
/**
|
|
* Recursively process a directory, picking regular files and feeding
|
|
* them to process_file().
|
|
*
|
|
* @param string $dir the full path of the directory to process
|
|
* @param string $userfield the prefix_user table field to use to
|
|
* match picture files to users.
|
|
* @param bool $overwrite overwrite existing picture or not.
|
|
* @param array $results (by reference) accumulated statistics of
|
|
* users updated and errors.
|
|
*
|
|
* @return nothing
|
|
*/
|
|
function process_directory ($dir, $userfield, $overwrite, &$results) {
|
|
global $OUTPUT;
|
|
if(!($handle = opendir($dir))) {
|
|
echo $OUTPUT->notification(get_string('uploadpicture_cannotprocessdir', 'tool_uploaduser'));
|
|
return;
|
|
}
|
|
|
|
while (false !== ($item = readdir($handle))) {
|
|
if ($item != '.' && $item != '..') {
|
|
if (is_dir($dir.'/'.$item)) {
|
|
process_directory($dir.'/'.$item, $userfield, $overwrite, $results);
|
|
} else if (is_file($dir.'/'.$item)) {
|
|
$result = process_file($dir.'/'.$item, $userfield, $overwrite);
|
|
switch ($result) {
|
|
case PIX_FILE_ERROR:
|
|
$results['errors']++;
|
|
break;
|
|
case PIX_FILE_UPDATED:
|
|
$results['updated']++;
|
|
break;
|
|
}
|
|
}
|
|
// Ignore anything else that is not a directory or a file (e.g.,
|
|
// symbolic links, sockets, pipes, etc.)
|
|
}
|
|
}
|
|
closedir($handle);
|
|
}
|
|
|
|
/**
|
|
* Given the full path of a file, try to find the user the file
|
|
* corresponds to and assign him/her this file as his/her picture.
|
|
* Make extensive checks to make sure we don't open any security holes
|
|
* and report back any success/error.
|
|
*
|
|
* @param string $file the full path of the file to process
|
|
* @param string $userfield the prefix_user table field to use to
|
|
* match picture files to users.
|
|
* @param bool $overwrite overwrite existing picture or not.
|
|
*
|
|
* @return integer either PIX_FILE_UPDATED, PIX_FILE_ERROR or
|
|
* PIX_FILE_SKIPPED
|
|
*/
|
|
function process_file ($file, $userfield, $overwrite) {
|
|
global $DB, $OUTPUT;
|
|
|
|
// Add additional checks on the filenames, as they are user
|
|
// controlled and we don't want to open any security holes.
|
|
$path_parts = pathinfo(cleardoubleslashes($file));
|
|
$basename = $path_parts['basename'];
|
|
$extension = $path_parts['extension'];
|
|
|
|
// The picture file name (without extension) must match the
|
|
// userfield attribute.
|
|
$uservalue = substr($basename, 0,
|
|
strlen($basename) -
|
|
strlen($extension) - 1);
|
|
|
|
// userfield names are safe, so don't quote them.
|
|
if (!($user = $DB->get_record('user', array ($userfield => $uservalue, 'deleted' => 0)))) {
|
|
$a = new stdClass();
|
|
$a->userfield = clean_param($userfield, PARAM_CLEANHTML);
|
|
$a->uservalue = clean_param($uservalue, PARAM_CLEANHTML);
|
|
echo $OUTPUT->notification(get_string('uploadpicture_usernotfound', 'tool_uploaduser', $a));
|
|
return PIX_FILE_ERROR;
|
|
}
|
|
|
|
$haspicture = $DB->get_field('user', 'picture', array('id'=>$user->id));
|
|
if ($haspicture && !$overwrite) {
|
|
echo $OUTPUT->notification(get_string('uploadpicture_userskipped', 'tool_uploaduser', $user->username));
|
|
return PIX_FILE_SKIPPED;
|
|
}
|
|
|
|
if (my_save_profile_image($user->id, $file)) {
|
|
$DB->set_field('user', 'picture', 1, array('id'=>$user->id));
|
|
echo $OUTPUT->notification(get_string('uploadpicture_userupdated', 'tool_uploaduser', $user->username), 'notifysuccess');
|
|
return PIX_FILE_UPDATED;
|
|
} else {
|
|
echo $OUTPUT->notification(get_string('uploadpicture_cannotsave', 'tool_uploaduser', $user->username));
|
|
return PIX_FILE_ERROR;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Try to save the given file (specified by its full path) as the
|
|
* picture for the user with the given id.
|
|
*
|
|
* @param integer $id the internal id of the user to assign the
|
|
* picture file to.
|
|
* @param string $originalfile the full path of the picture file.
|
|
*
|
|
* @return bool
|
|
*/
|
|
function my_save_profile_image($id, $originalfile) {
|
|
$context = get_context_instance(CONTEXT_USER, $id);
|
|
return process_new_icon($context, 'user', 'icon', 0, $originalfile);
|
|
}
|
|
|
|
|