3 ///////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2007 Inaki Arenaza //
7 // Based on .../admin/uploaduser.php and .../lib/gdlib.php //
9 // This program is free software; you can redistribute it and/or modify //
10 // it under the terms of the GNU General Public License as published by //
11 // the Free Software Foundation; either version 2 of the License, or //
12 // (at your option) any later version. //
14 // This program is distributed in the hope that it will be useful, //
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
17 // GNU General Public License for more details: //
19 // http://www.gnu.org/copyleft/gpl.html //
21 ///////////////////////////////////////////////////////////////////////////
23 require_once('../config.php');
24 require_once($CFG->libdir
.'/uploadlib.php');
25 require_once($CFG->libdir
.'/adminlib.php');
26 require_once($CFG->libdir
.'/gdlib.php');
27 require_once('uploadpicture_form.php');
29 define ('PIX_FILE_UPDATED', 0);
30 define ('PIX_FILE_ERROR', 1);
31 define ('PIX_FILE_SKIPPED', 2);
33 $adminroot = admin_get_root();
35 admin_externalpage_setup('uploadpictures', $adminroot);
39 require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM
));
41 if (!$site = get_site()) {
42 error("Could not find site-level course");
45 if (!$adminuser = get_admin()) {
46 error("Could not find site admin");
49 $strfile = get_string('file');
50 $struser = get_string('user');
51 $strusersupdated = get_string('usersupdated');
52 $struploadpictures = get_string('uploadpictures','admin');
59 $userfield = optional_param('userfield', 0, PARAM_INT
);
60 $overwritepicture = optional_param('overwritepicture', 0, PARAM_BOOL
);
63 admin_externalpage_print_header();
64 print_heading_with_help($struploadpictures, 'uploadpictures');
66 $mform = new admin_uploadpicture_form();
67 if ($formdata = $mform->get_data()) {
68 if (!array_key_exists($userfield, $userfields)) {
69 notify(get_string('uploadpicture_baduserfield','admin'));
71 // Large files are likely to take their time and memory. Let PHP know
72 // that we'll take longer, and that the process should be recycled soon
75 @raise_memory_limit
("192M");
76 if (function_exists('apache_child_terminate')) {
77 @apache_child_terminate
();
80 // Create a unique temporary directory, to process the zip file
82 $zipdir = my_mktempdir($CFG->dataroot
.'/temp/', 'usrpic');
84 if (!$mform->save_files($zipdir)) {
85 notify(get_string('uploadpicture_cannotmovezip','admin'));
88 $dstfile = $zipdir.'/'.$mform->get_new_filename();
89 if(!unzip_file($dstfile, $zipdir, false)) {
90 notify(get_string('uploadpicture_cannotunzip','admin'));
93 // We don't need the zip file any longer, so delete it to make
94 // it easier to process the rest of the files inside the directory.
97 $results = array ('errors' => 0,'updated' => 0);
99 process_directory($zipdir, $userfields[$userfield], $overwritepicture, $results);
101 // Finally remove the temporary directory with all the user images and print some stats.
103 notify(get_string('usersupdated', 'admin') . ": " . $results['updated']);
104 notify(get_string('errors', 'admin') . ": " . $results['errors']);
111 admin_externalpage_print_footer();
114 // ----------- Internal functions ----------------
117 * Create a unique temporary directory with a given prefix name,
118 * inside a given directory, with given permissions. Return the
119 * full path to the newly created temp directory.
121 * @param string $dir where to create the temp directory.
122 * @param string $prefix prefix for the temp directory name (default '')
123 * @param string $mode permissions for the temp directory (default 700)
125 * @return string The full path to the temp directory.
127 function my_mktempdir($dir, $prefix='', $mode=0700) {
128 if (substr($dir, -1) != '/') {
133 $path = $dir.$prefix.mt_rand(0, 9999999);
134 } while (!mkdir($path, $mode));
140 * Recursively process a directory, picking regular files and feeding
141 * them to process_file().
143 * @param string $dir the full path of the directory to process
144 * @param string $userfield the prefix_user table field to use to
145 * match picture files to users.
146 * @param bool $overwrite overwrite existing picture or not.
147 * @param array $results (by reference) accumulated statistics of
148 * users updated and errors.
152 function process_directory ($dir, $userfield, $overwrite, &$results) {
153 if(!($handle = opendir($dir))) {
154 notify(get_string('uploadpicture_cannotprocessdir','admin'));
158 while (false !== ($item = readdir($handle))) {
159 if ($item != '.' && $item != '..') {
160 if (is_dir($dir.'/'.$item)) {
161 process_directory($dir.'/'.$item, $userfield, $overwrite, $results);
162 } else if (is_file($dir.'/'.$item)) {
163 $result = process_file($dir.'/'.$item, $userfield, $overwrite);
166 $results['errors']++
;
168 case PIX_FILE_UPDATED
:
169 $results['updated']++
;
173 // Ignore anything else that is not a directory or a file (e.g.,
174 // symbolic links, sockets, pipes, etc.)
181 * Given the full path of a file, try to find the user the file
182 * corresponds to and assign him/her this file as his/her picture.
183 * Make extensive checks to make sure we don't open any security holes
184 * and report back any success/error.
186 * @param string $file the full path of the file to process
187 * @param string $userfield the prefix_user table field to use to
188 * match picture files to users.
189 * @param bool $overwrite overwrite existing picture or not.
191 * @return integer either PIX_FILE_UPDATED, PIX_FILE_ERROR or
194 function process_file ($file, $userfield, $overwrite) {
195 // Add additional checks on the filenames, as they are user
196 // controlled and we don't want to open any security holes.
197 $path_parts = pathinfo(cleardoubleslashes($file));
198 $basename = $path_parts['basename'];
199 $extension = $path_parts['extension'];
200 if ($basename != clean_param($basename, PARAM_CLEANFILE
)) {
201 // The original picture file name has invalid characters
202 notify(get_string('uploadpicture_invalidfilename', 'admin',
203 clean_param($basename, PARAM_CLEANHTML
)));
204 return PIX_FILE_ERROR
;
207 // The picture file name (without extension) must match the
208 // userfield attribute.
209 $uservalue = substr($basename, 0,
211 strlen($extension) - 1);
213 // userfield names are safe, so don't quote them.
214 if (!($user = get_record('user', $userfield, addslashes($uservalue)))) {
216 $a->userfield
= clean_param($userfield, PARAM_CLEANHTML
);
217 $a->uservalue
= clean_param($uservalue, PARAM_CLEANHTML
);
218 notify(get_string('uploadpicture_usernotfound', 'admin', $a));
219 return PIX_FILE_ERROR
;
222 $haspicture = get_field('user', 'picture', 'id', $user->id
);
223 if ($haspicture && !$overwrite) {
224 notify(get_string('uploadpicture_userskipped', 'admin', $user->username
));
225 return PIX_FILE_SKIPPED
;
228 if (my_save_profile_image($user->id
, $file)) {
229 set_field('user', 'picture', 1, 'id', $user->id
);
230 notify(get_string('uploadpicture_userupdated', 'admin', $user->username
));
231 return PIX_FILE_UPDATED
;
233 notify(get_string('uploadpicture_cannotsave', 'admin', $user->username
));
234 return PIX_FILE_ERROR
;
239 * Try to save the given file (specified by its full path) as the
240 * picture for the user with the given id.
242 * @param integer $id the internal id of the user to assign the
244 * @param string $originalfile the full path of the picture file.
248 function my_save_profile_image($id, $originalfile) {
249 $destination = create_profile_image_destination($id, 'user');
250 if ($destination === false) {
254 return process_profile_image($originalfile, $destination);