MDL-24751 fixed default memory limit when deciding to use apache_child_terminate...
[moodle.git] / admin / uploadpicture.php
CommitLineData
9e492db0 1<?php
b352b2e9 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// Copyright (C) 2007 Inaki Arenaza //
6// //
7// Based on .../admin/uploaduser.php and .../lib/gdlib.php //
8// //
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. //
13// //
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: //
18// //
19// http://www.gnu.org/copyleft/gpl.html //
20// //
21///////////////////////////////////////////////////////////////////////////
22
23require_once('../config.php');
b352b2e9 24require_once($CFG->libdir.'/adminlib.php');
25require_once($CFG->libdir.'/gdlib.php');
26require_once('uploadpicture_form.php');
27
c74ca0e9 28define ('PIX_FILE_UPDATED', 0);
29define ('PIX_FILE_ERROR', 1);
20207b82
PS
30define ('PIX_FILE_SKIPPED', 2);
31
86540fcc 32admin_externalpage_setup('uploadpictures');
b352b2e9 33
34require_login();
35
957f6fc9 36require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
b352b2e9 37
2a250a0b 38$site = get_site();
b352b2e9 39
40if (!$adminuser = get_admin()) {
8e9d88f2 41 print_error('noadmins', 'error');
b352b2e9 42}
43
44$strfile = get_string('file');
45$struser = get_string('user');
6c7a5df7 46$strusersupdated = get_string('usersupdated', 'admin');
b352b2e9 47$struploadpictures = get_string('uploadpictures','admin');
b352b2e9 48
49$userfields = array (
50 0 => 'username',
51 1 => 'idnumber',
52 2 => 'id' );
53
54$userfield = optional_param('userfield', 0, PARAM_INT);
55$overwritepicture = optional_param('overwritepicture', 0, PARAM_BOOL);
56
57/// Print the header
61ef8f9f 58echo $OUTPUT->header();
9e492db0 59
6c7a5df7 60echo $OUTPUT->heading_with_help($struploadpictures, 'uploadpictures', 'admin');
b352b2e9 61
70f01544 62$mform = new admin_uploadpicture_form(null, $userfields);
294ce987 63if ($formdata = $mform->get_data()) {
b352b2e9 64 if (!array_key_exists($userfield, $userfields)) {
8fbce1c8 65 echo $OUTPUT->notification(get_string('uploadpicture_baduserfield','admin'));
b352b2e9 66 } else {
67 // Large files are likely to take their time and memory. Let PHP know
68 // that we'll take longer, and that the process should be recycled soon
69 // to free up memory.
70 @set_time_limit(0);
c22473a2 71 raise_memory_limit(MEMORY_EXTRA);
20207b82 72
b352b2e9 73 // Create a unique temporary directory, to process the zip file
74 // contents.
941c3027
RW
75 $zipdir = my_mktempdir($CFG->dataroot.'/temp/', 'usrpic');
76 $dstfile = $zipdir.'/images.zip';
669a9e56 77
941c3027 78 if (!$mform->save_file('userpicturesfile', $dstfile, true)) {
8fbce1c8 79 echo $OUTPUT->notification(get_string('uploadpicture_cannotmovezip','admin'));
b352b2e9 80 @remove_dir($zipdir);
81 } else {
8d3950ea
SH
82 $fp = get_file_packer('application/zip');
83 $unzipresult = $fp->extract_to_pathname($dstfile, $zipdir);
84 if (!$unzipresult) {
8fbce1c8 85 echo $OUTPUT->notification(get_string('uploadpicture_cannotunzip','admin'));
b352b2e9 86 @remove_dir($zipdir);
87 } else {
88 // We don't need the zip file any longer, so delete it to make
89 // it easier to process the rest of the files inside the directory.
90 @unlink($dstfile);
20207b82 91
c74ca0e9 92 $results = array ('errors' => 0,'updated' => 0);
93
94 process_directory($zipdir, $userfields[$userfield], $overwritepicture, $results);
95
20207b82 96
b352b2e9 97 // Finally remove the temporary directory with all the user images and print some stats.
98 remove_dir($zipdir);
8fbce1c8 99 echo $OUTPUT->notification(get_string('usersupdated', 'admin') . ": " . $results['updated']);
100 echo $OUTPUT->notification(get_string('errors', 'admin') . ": " . $results['errors']);
b352b2e9 101 echo '<hr />';
102 }
103 }
104 }
105}
106$mform->display();
73d6f52f 107echo $OUTPUT->footer();
b352b2e9 108exit;
109
110// ----------- Internal functions ----------------
111
c74ca0e9 112/**
113 * Create a unique temporary directory with a given prefix name,
114 * inside a given directory, with given permissions. Return the
115 * full path to the newly created temp directory.
116 *
117 * @param string $dir where to create the temp directory.
118 * @param string $prefix prefix for the temp directory name (default '')
c74ca0e9 119 *
120 * @return string The full path to the temp directory.
121 */
48183b41
PS
122function my_mktempdir($dir, $prefix='') {
123 global $CFG;
124
b352b2e9 125 if (substr($dir, -1) != '/') {
126 $dir .= '/';
127 }
128
129 do {
130 $path = $dir.$prefix.mt_rand(0, 9999999);
48183b41
PS
131 } while (file_exists($path));
132
133 check_dir_exists($path);
b352b2e9 134
135 return $path;
136}
137
c74ca0e9 138/**
139 * Recursively process a directory, picking regular files and feeding
140 * them to process_file().
141 *
142 * @param string $dir the full path of the directory to process
143 * @param string $userfield the prefix_user table field to use to
144 * match picture files to users.
145 * @param bool $overwrite overwrite existing picture or not.
146 * @param array $results (by reference) accumulated statistics of
147 * users updated and errors.
148 *
149 * @return nothing
150 */
151function process_directory ($dir, $userfield, $overwrite, &$results) {
8fbce1c8 152 global $OUTPUT;
c74ca0e9 153 if(!($handle = opendir($dir))) {
8fbce1c8 154 echo $OUTPUT->notification(get_string('uploadpicture_cannotprocessdir','admin'));
c74ca0e9 155 return;
156 }
157
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);
164 switch ($result) {
165 case PIX_FILE_ERROR:
166 $results['errors']++;
167 break;
168 case PIX_FILE_UPDATED:
169 $results['updated']++;
170 break;
171 }
172 }
173 // Ignore anything else that is not a directory or a file (e.g.,
174 // symbolic links, sockets, pipes, etc.)
175 }
176 }
177 closedir($handle);
178}
179
180/**
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.
185 *
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.
190 *
191 * @return integer either PIX_FILE_UPDATED, PIX_FILE_ERROR or
192 * PIX_FILE_SKIPPED
193 */
194function process_file ($file, $userfield, $overwrite) {
8fbce1c8 195 global $DB, $OUTPUT;
20207b82 196
c74ca0e9 197 // Add additional checks on the filenames, as they are user
198 // controlled and we don't want to open any security holes.
199 $path_parts = pathinfo(cleardoubleslashes($file));
200 $basename = $path_parts['basename'];
201 $extension = $path_parts['extension'];
669a9e56 202
c74ca0e9 203 // The picture file name (without extension) must match the
204 // userfield attribute.
205 $uservalue = substr($basename, 0,
206 strlen($basename) -
207 strlen($extension) - 1);
208
209 // userfield names are safe, so don't quote them.
086f0e6f 210 if (!($user = $DB->get_record('user', array ($userfield => $uservalue, 'deleted' => 0)))) {
8c6521bb 211 $a = new stdClass();
c74ca0e9 212 $a->userfield = clean_param($userfield, PARAM_CLEANHTML);
213 $a->uservalue = clean_param($uservalue, PARAM_CLEANHTML);
8fbce1c8 214 echo $OUTPUT->notification(get_string('uploadpicture_usernotfound', 'admin', $a));
c74ca0e9 215 return PIX_FILE_ERROR;
216 }
217
218 $haspicture = $DB->get_field('user', 'picture', array('id'=>$user->id));
219 if ($haspicture && !$overwrite) {
8fbce1c8 220 echo $OUTPUT->notification(get_string('uploadpicture_userskipped', 'admin', $user->username));
c74ca0e9 221 return PIX_FILE_SKIPPED;
222 }
223
224 if (my_save_profile_image($user->id, $file)) {
225 $DB->set_field('user', 'picture', 1, array('id'=>$user->id));
8fbce1c8 226 echo $OUTPUT->notification(get_string('uploadpicture_userupdated', 'admin', $user->username));
c74ca0e9 227 return PIX_FILE_UPDATED;
228 } else {
8fbce1c8 229 echo $OUTPUT->notification(get_string('uploadpicture_cannotsave', 'admin', $user->username));
c74ca0e9 230 return PIX_FILE_ERROR;
231 }
232}
233
234/**
235 * Try to save the given file (specified by its full path) as the
236 * picture for the user with the given id.
237 *
238 * @param integer $id the internal id of the user to assign the
239 * picture file to.
240 * @param string $originalfile the full path of the picture file.
241 *
20207b82 242 * @return bool
c74ca0e9 243 */
b352b2e9 244function my_save_profile_image($id, $originalfile) {
edfd6a5e
PS
245 $context = get_context_instance(CONTEXT_USER, $id);
246 return process_new_icon($context, 'user', 'icon', 0, $originalfile);
b352b2e9 247}
248
0df0df23 249