[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Bulk upload of user pictures 19 * 20 * Based on .../admin/uploaduser.php and .../lib/gdlib.php 21 * 22 * @package tool 23 * @subpackage uploaduser 24 * @copyright (C) 2007 Inaki Arenaza 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 */ 27 28 require('../../../config.php'); 29 require_once($CFG->libdir.'/adminlib.php'); 30 require_once($CFG->libdir.'/gdlib.php'); 31 require_once ('picture_form.php'); 32 33 define ('PIX_FILE_UPDATED', 0); 34 define ('PIX_FILE_ERROR', 1); 35 define ('PIX_FILE_SKIPPED', 2); 36 37 admin_externalpage_setup('tooluploaduserpictures'); 38 39 require_login(); 40 41 require_capability('tool/uploaduser:uploaduserpictures', context_system::instance()); 42 43 $site = get_site(); 44 45 if (!$adminuser = get_admin()) { 46 print_error('noadmins', 'error'); 47 } 48 49 $strfile = get_string('file'); 50 $struser = get_string('user'); 51 $strusersupdated = get_string('usersupdated', 'tool_uploaduser'); 52 $struploadpictures = get_string('uploadpictures','tool_uploaduser'); 53 54 $userfields = array ( 55 0 => 'username', 56 1 => 'idnumber', 57 2 => 'id' ); 58 59 $userfield = optional_param('userfield', 0, PARAM_INT); 60 $overwritepicture = optional_param('overwritepicture', 0, PARAM_BOOL); 61 62 /// Print the header 63 echo $OUTPUT->header(); 64 65 echo $OUTPUT->heading_with_help($struploadpictures, 'uploadpictures', 'tool_uploaduser'); 66 67 $mform = new admin_uploadpicture_form(null, $userfields); 68 if ($formdata = $mform->get_data()) { 69 if (!array_key_exists($userfield, $userfields)) { 70 echo $OUTPUT->notification(get_string('uploadpicture_baduserfield', 'tool_uploaduser')); 71 } else { 72 // Large files are likely to take their time and memory. Let PHP know 73 // that we'll take longer, and that the process should be recycled soon 74 // to free up memory. 75 core_php_time_limit::raise(); 76 raise_memory_limit(MEMORY_EXTRA); 77 78 // Create a unique temporary directory, to process the zip file 79 // contents. 80 $zipdir = my_mktempdir($CFG->tempdir.'/', 'usrpic'); 81 $dstfile = $zipdir.'/images.zip'; 82 83 if (!$mform->save_file('userpicturesfile', $dstfile, true)) { 84 echo $OUTPUT->notification(get_string('uploadpicture_cannotmovezip', 'tool_uploaduser')); 85 @remove_dir($zipdir); 86 } else { 87 $fp = get_file_packer('application/zip'); 88 $unzipresult = $fp->extract_to_pathname($dstfile, $zipdir); 89 if (!$unzipresult) { 90 echo $OUTPUT->notification(get_string('uploadpicture_cannotunzip', 'tool_uploaduser')); 91 @remove_dir($zipdir); 92 } else { 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. 95 @unlink($dstfile); 96 97 $results = array ('errors' => 0,'updated' => 0); 98 99 process_directory($zipdir, $userfields[$userfield], $overwritepicture, $results); 100 101 102 // Finally remove the temporary directory with all the user images and print some stats. 103 remove_dir($zipdir); 104 echo $OUTPUT->notification(get_string('usersupdated', 'tool_uploaduser') . ": " . $results['updated'], 'notifysuccess'); 105 echo $OUTPUT->notification(get_string('errors', 'tool_uploaduser') . ": " . $results['errors'], ($results['errors'] ? 'notifyproblem' : 'notifysuccess')); 106 echo '<hr />'; 107 } 108 } 109 } 110 } 111 $mform->display(); 112 echo $OUTPUT->footer(); 113 exit; 114 115 // ----------- Internal functions ---------------- 116 117 /** 118 * Create a unique temporary directory with a given prefix name, 119 * inside a given directory, with given permissions. Return the 120 * full path to the newly created temp directory. 121 * 122 * @param string $dir where to create the temp directory. 123 * @param string $prefix prefix for the temp directory name (default '') 124 * 125 * @return string The full path to the temp directory. 126 */ 127 function my_mktempdir($dir, $prefix='') { 128 global $CFG; 129 130 if (substr($dir, -1) != '/') { 131 $dir .= '/'; 132 } 133 134 do { 135 $path = $dir.$prefix.mt_rand(0, 9999999); 136 } while (file_exists($path)); 137 138 check_dir_exists($path); 139 140 return $path; 141 } 142 143 /** 144 * Recursively process a directory, picking regular files and feeding 145 * them to process_file(). 146 * 147 * @param string $dir the full path of the directory to process 148 * @param string $userfield the prefix_user table field to use to 149 * match picture files to users. 150 * @param bool $overwrite overwrite existing picture or not. 151 * @param array $results (by reference) accumulated statistics of 152 * users updated and errors. 153 * 154 * @return nothing 155 */ 156 function process_directory ($dir, $userfield, $overwrite, &$results) { 157 global $OUTPUT; 158 if(!($handle = opendir($dir))) { 159 echo $OUTPUT->notification(get_string('uploadpicture_cannotprocessdir', 'tool_uploaduser')); 160 return; 161 } 162 163 while (false !== ($item = readdir($handle))) { 164 if ($item != '.' && $item != '..') { 165 if (is_dir($dir.'/'.$item)) { 166 process_directory($dir.'/'.$item, $userfield, $overwrite, $results); 167 } else if (is_file($dir.'/'.$item)) { 168 $result = process_file($dir.'/'.$item, $userfield, $overwrite); 169 switch ($result) { 170 case PIX_FILE_ERROR: 171 $results['errors']++; 172 break; 173 case PIX_FILE_UPDATED: 174 $results['updated']++; 175 break; 176 } 177 } 178 // Ignore anything else that is not a directory or a file (e.g., 179 // symbolic links, sockets, pipes, etc.) 180 } 181 } 182 closedir($handle); 183 } 184 185 /** 186 * Given the full path of a file, try to find the user the file 187 * corresponds to and assign him/her this file as his/her picture. 188 * Make extensive checks to make sure we don't open any security holes 189 * and report back any success/error. 190 * 191 * @param string $file the full path of the file to process 192 * @param string $userfield the prefix_user table field to use to 193 * match picture files to users. 194 * @param bool $overwrite overwrite existing picture or not. 195 * 196 * @return integer either PIX_FILE_UPDATED, PIX_FILE_ERROR or 197 * PIX_FILE_SKIPPED 198 */ 199 function process_file ($file, $userfield, $overwrite) { 200 global $DB, $OUTPUT; 201 202 // Add additional checks on the filenames, as they are user 203 // controlled and we don't want to open any security holes. 204 $path_parts = pathinfo(cleardoubleslashes($file)); 205 $basename = $path_parts['basename']; 206 $extension = $path_parts['extension']; 207 208 // The picture file name (without extension) must match the 209 // userfield attribute. 210 $uservalue = substr($basename, 0, 211 strlen($basename) - 212 strlen($extension) - 1); 213 214 // userfield names are safe, so don't quote them. 215 if (!($user = $DB->get_record('user', array ($userfield => $uservalue, 'deleted' => 0)))) { 216 $a = new stdClass(); 217 $a->userfield = clean_param($userfield, PARAM_CLEANHTML); 218 $a->uservalue = clean_param($uservalue, PARAM_CLEANHTML); 219 echo $OUTPUT->notification(get_string('uploadpicture_usernotfound', 'tool_uploaduser', $a)); 220 return PIX_FILE_ERROR; 221 } 222 223 $haspicture = $DB->get_field('user', 'picture', array('id'=>$user->id)); 224 if ($haspicture && !$overwrite) { 225 echo $OUTPUT->notification(get_string('uploadpicture_userskipped', 'tool_uploaduser', $user->username)); 226 return PIX_FILE_SKIPPED; 227 } 228 229 if ($newrev = my_save_profile_image($user->id, $file)) { 230 $DB->set_field('user', 'picture', $newrev, array('id'=>$user->id)); 231 echo $OUTPUT->notification(get_string('uploadpicture_userupdated', 'tool_uploaduser', $user->username), 'notifysuccess'); 232 return PIX_FILE_UPDATED; 233 } else { 234 echo $OUTPUT->notification(get_string('uploadpicture_cannotsave', 'tool_uploaduser', $user->username)); 235 return PIX_FILE_ERROR; 236 } 237 } 238 239 /** 240 * Try to save the given file (specified by its full path) as the 241 * picture for the user with the given id. 242 * 243 * @param integer $id the internal id of the user to assign the 244 * picture file to. 245 * @param string $originalfile the full path of the picture file. 246 * 247 * @return mixed new unique revision number or false if not saved 248 */ 249 function my_save_profile_image($id, $originalfile) { 250 $context = context_user::instance($id); 251 return process_new_icon($context, 'user', 'icon', 0, $originalfile); 252 } 253 254
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |