Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/uploaduser/picture.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00028 require('../../../config.php');
00029 require_once($CFG->libdir.'/adminlib.php');
00030 require_once($CFG->libdir.'/gdlib.php');
00031 require_once('picture_form.php');
00032 
00033 define ('PIX_FILE_UPDATED', 0);
00034 define ('PIX_FILE_ERROR',   1);
00035 define ('PIX_FILE_SKIPPED', 2);
00036 
00037 admin_externalpage_setup('tooluploaduserpictures');
00038 
00039 require_login();
00040 
00041 require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
00042 
00043 $site = get_site();
00044 
00045 if (!$adminuser = get_admin()) {
00046     print_error('noadmins', 'error');
00047 }
00048 
00049 $strfile = get_string('file');
00050 $struser = get_string('user');
00051 $strusersupdated = get_string('usersupdated', 'tool_uploaduser');
00052 $struploadpictures = get_string('uploadpictures','tool_uploaduser');
00053 
00054 $userfields = array (
00055     0 => 'username',
00056     1 => 'idnumber',
00057     2 => 'id' );
00058 
00059 $userfield = optional_param('userfield', 0, PARAM_INT);
00060 $overwritepicture = optional_param('overwritepicture', 0, PARAM_BOOL);
00061 
00063 echo $OUTPUT->header();
00064 
00065 echo $OUTPUT->heading_with_help($struploadpictures, 'uploadpictures', 'tool_uploaduser');
00066 
00067 $mform = new admin_uploadpicture_form(null, $userfields);
00068 if ($formdata = $mform->get_data()) {
00069     if (!array_key_exists($userfield, $userfields)) {
00070         echo $OUTPUT->notification(get_string('uploadpicture_baduserfield', 'tool_uploaduser'));
00071     } else {
00072         // Large files are likely to take their time and memory. Let PHP know
00073         // that we'll take longer, and that the process should be recycled soon
00074         // to free up memory.
00075         @set_time_limit(0);
00076         raise_memory_limit(MEMORY_EXTRA);
00077 
00078         // Create a unique temporary directory, to process the zip file
00079         // contents.
00080         $zipdir = my_mktempdir($CFG->tempdir.'/', 'usrpic');
00081         $dstfile = $zipdir.'/images.zip';
00082 
00083         if (!$mform->save_file('userpicturesfile', $dstfile, true)) {
00084             echo $OUTPUT->notification(get_string('uploadpicture_cannotmovezip', 'tool_uploaduser'));
00085             @remove_dir($zipdir);
00086         } else {
00087             $fp = get_file_packer('application/zip');
00088             $unzipresult = $fp->extract_to_pathname($dstfile, $zipdir);
00089             if (!$unzipresult) {
00090                 echo $OUTPUT->notification(get_string('uploadpicture_cannotunzip', 'tool_uploaduser'));
00091                 @remove_dir($zipdir);
00092             } else {
00093                 // We don't need the zip file any longer, so delete it to make
00094                 // it easier to process the rest of the files inside the directory.
00095                 @unlink($dstfile);
00096 
00097                 $results = array ('errors' => 0,'updated' => 0);
00098 
00099                 process_directory($zipdir, $userfields[$userfield], $overwritepicture, $results);
00100 
00101 
00102                 // Finally remove the temporary directory with all the user images and print some stats.
00103                 remove_dir($zipdir);
00104                 echo $OUTPUT->notification(get_string('usersupdated', 'tool_uploaduser') . ": " . $results['updated'], 'notifysuccess');
00105                 echo $OUTPUT->notification(get_string('errors', 'tool_uploaduser') . ": " . $results['errors'], ($results['errors'] ? 'notifyproblem' : 'notifysuccess'));
00106                 echo '<hr />';
00107             }
00108         }
00109     }
00110 }
00111 $mform->display();
00112 echo $OUTPUT->footer();
00113 exit;
00114 
00115 // ----------- Internal functions ----------------
00116 
00127 function my_mktempdir($dir, $prefix='') {
00128     global $CFG;
00129 
00130     if (substr($dir, -1) != '/') {
00131         $dir .= '/';
00132     }
00133 
00134     do {
00135         $path = $dir.$prefix.mt_rand(0, 9999999);
00136     } while (file_exists($path));
00137 
00138     check_dir_exists($path);
00139 
00140     return $path;
00141 }
00142 
00156 function process_directory ($dir, $userfield, $overwrite, &$results) {
00157     global $OUTPUT;
00158     if(!($handle = opendir($dir))) {
00159         echo $OUTPUT->notification(get_string('uploadpicture_cannotprocessdir', 'tool_uploaduser'));
00160         return;
00161     }
00162 
00163     while (false !== ($item = readdir($handle))) {
00164         if ($item != '.' && $item != '..') {
00165             if (is_dir($dir.'/'.$item)) {
00166                 process_directory($dir.'/'.$item, $userfield, $overwrite, $results);
00167             } else if (is_file($dir.'/'.$item))  {
00168                 $result = process_file($dir.'/'.$item, $userfield, $overwrite);
00169                 switch ($result) {
00170                     case PIX_FILE_ERROR:
00171                         $results['errors']++;
00172                         break;
00173                     case PIX_FILE_UPDATED:
00174                         $results['updated']++;
00175                         break;
00176                 }
00177             }
00178             // Ignore anything else that is not a directory or a file (e.g.,
00179             // symbolic links, sockets, pipes, etc.)
00180         }
00181     }
00182     closedir($handle);
00183 }
00184 
00199 function process_file ($file, $userfield, $overwrite) {
00200     global $DB, $OUTPUT;
00201 
00202     // Add additional checks on the filenames, as they are user
00203     // controlled and we don't want to open any security holes.
00204     $path_parts = pathinfo(cleardoubleslashes($file));
00205     $basename  = $path_parts['basename'];
00206     $extension = $path_parts['extension'];
00207 
00208     // The picture file name (without extension) must match the
00209     // userfield attribute.
00210     $uservalue = substr($basename, 0,
00211                         strlen($basename) -
00212                         strlen($extension) - 1);
00213 
00214     // userfield names are safe, so don't quote them.
00215     if (!($user = $DB->get_record('user', array ($userfield => $uservalue, 'deleted' => 0)))) {
00216         $a = new stdClass();
00217         $a->userfield = clean_param($userfield, PARAM_CLEANHTML);
00218         $a->uservalue = clean_param($uservalue, PARAM_CLEANHTML);
00219         echo $OUTPUT->notification(get_string('uploadpicture_usernotfound', 'tool_uploaduser', $a));
00220         return PIX_FILE_ERROR;
00221     }
00222 
00223     $haspicture = $DB->get_field('user', 'picture', array('id'=>$user->id));
00224     if ($haspicture && !$overwrite) {
00225         echo $OUTPUT->notification(get_string('uploadpicture_userskipped', 'tool_uploaduser', $user->username));
00226         return PIX_FILE_SKIPPED;
00227     }
00228 
00229     if (my_save_profile_image($user->id, $file)) {
00230         $DB->set_field('user', 'picture', 1, array('id'=>$user->id));
00231         echo $OUTPUT->notification(get_string('uploadpicture_userupdated', 'tool_uploaduser', $user->username), 'notifysuccess');
00232         return PIX_FILE_UPDATED;
00233     } else {
00234         echo $OUTPUT->notification(get_string('uploadpicture_cannotsave', 'tool_uploaduser', $user->username));
00235         return PIX_FILE_ERROR;
00236     }
00237 }
00238 
00249 function my_save_profile_image($id, $originalfile) {
00250     $context = get_context_instance(CONTEXT_USER, $id);
00251     return process_new_icon($context, 'user', 'icon', 0, $originalfile);
00252 }
00253 
00254 
 All Data Structures Namespaces Files Functions Variables Enumerations