Advertisement
jonwitts

Moodle updatepics.php

Feb 10th, 2015
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.65 KB | None | 0 0
  1. <?php
  2.  
  3. define('CLI_SCRIPT', true);
  4.  
  5. require(dirname(dirname(dirname(__FILE__))).'/config.php');
  6. require_once($CFG->libdir.'/adminlib.php');
  7. require_once($CFG->libdir.'/gdlib.php');
  8. require_once($CFG->libdir.'/clilib.php');      // cli only functions
  9.  
  10.  // now get cli options
  11. list($options) = cli_get_params(array('dir'=>''));
  12.  
  13. define ('PIX_FILE_UPDATED', 0);
  14. define ('PIX_FILE_ERROR',   1);
  15. define ('PIX_FILE_SKIPPED', 2);
  16.  
  17. @set_time_limit(0);
  18. raise_memory_limit(MEMORY_EXTRA);
  19.  
  20. process_directory($options['dir'], 'username', 'true', $results);
  21.  
  22. exit;
  23.  
  24. // ----------- Internal functions ----------------
  25.  
  26.  
  27.  /**
  28.  * Recursively process a directory, picking regular files and feeding
  29.  * them to process_file().
  30.  *
  31.  * @param string $dir the full path of the directory to process
  32.  * @param string $userfield the prefix_user table field to use to
  33.  *               match picture files to users.
  34.  * @param bool $overwrite overwrite existing picture or not.
  35.  * @param array $results (by reference) accumulated statistics of
  36.  *              users updated and errors.
  37.  *
  38.  * @return nothing
  39.  */
  40. function process_directory ($dir, $userfield, $overwrite, &$results) {
  41.     if(!($handle = opendir($dir))) {
  42.         echo 'cannotprocessdir';
  43.         return;
  44.     }
  45.  
  46.     while (false !== ($item = readdir($handle))) {
  47.         if ($item != '.' && $item != '..') {
  48.             if (is_dir($dir.'/'.$item)) {
  49.                 process_directory($dir.'/'.$item, $userfield, $overwrite, $results);
  50.             } else if (is_file($dir.'/'.$item))  {
  51.                 $result = process_file($dir.'/'.$item, $userfield, $overwrite);
  52.                 switch ($result) {
  53.                     case PIX_FILE_ERROR:
  54.                         $results['errors']++;
  55.                         break;
  56.                     case PIX_FILE_UPDATED:
  57.                         $results['updated']++;
  58.                         break;
  59.                 }
  60.             }
  61.             // Ignore anything else that is not a directory or a file (e.g.,
  62.             // symbolic links, sockets, pipes, etc.)
  63.         }
  64.     }
  65.     closedir($handle);
  66. }
  67.  
  68. /**
  69.  * Given the full path of a file, try to find the user the file
  70.  * corresponds to and assign him/her this file as his/her picture.
  71.  * Make extensive checks to make sure we don't open any security holes
  72.  * and report back any success/error.
  73.  *
  74.  * @param string $file the full path of the file to process
  75.  * @param string $userfield the prefix_user table field to use to
  76.  *               match picture files to users.
  77.  * @param bool $overwrite overwrite existing picture or not.
  78.  *
  79.  * @return integer either PIX_FILE_UPDATED, PIX_FILE_ERROR or
  80.  *                  PIX_FILE_SKIPPED
  81.  */
  82. function process_file ($file, $userfield, $overwrite) {
  83.     global $DB;
  84.  
  85.     // Add additional checks on the filenames, as they are user
  86.     // controlled and we don't want to open any security holes.
  87.     $path_parts = pathinfo(cleardoubleslashes($file));
  88.     $basename  = $path_parts['basename'];
  89.     $extension = $path_parts['extension'];
  90.  
  91.     // The picture file name (without extension) must match the
  92.     // userfield attribute.
  93.     $uservalue = substr($basename, 0,
  94.                         strlen($basename) -
  95.                         strlen($extension) - 1);
  96.  
  97.     // userfield names are safe, so don't quote them.
  98.     if (!($user = $DB->get_record('user', array ($userfield => $uservalue, 'deleted' => 0)))) {
  99.         $a = new stdClass();
  100.         $a->userfield = clean_param($userfield, PARAM_CLEANHTML);
  101.         $a->uservalue = clean_param($uservalue, PARAM_CLEANHTML);
  102.         echo 'usernotfound - '.$a."\n";
  103.         return PIX_FILE_ERROR;
  104.     }
  105.  
  106.     $haspicture = $DB->get_field('user', 'picture', array('id'=>$user->id));
  107.     if ($haspicture && !$overwrite) {
  108.         echo 'userskipped - '.$user->username."\n";
  109.         return PIX_FILE_SKIPPED;
  110.     }
  111.  
  112.     if (my_save_profile_image($user->id, $file)) {
  113.         $DB->set_field('user', 'picture', 1, array('id'=>$user->id));
  114.         echo 'userupdated - '.$user->username."\n";
  115.         return PIX_FILE_UPDATED;
  116.     } else {
  117.         echo 'cannotsave - '.$user->username."\n";
  118.         return PIX_FILE_ERROR;
  119.     }
  120. }
  121.  
  122. /**
  123.  * Try to save the given file (specified by its full path) as the
  124.  * picture for the user with the given id.
  125.  *
  126.  * @param integer $id the internal id of the user to assign the
  127.  *                picture file to.
  128.  * @param string $originalfile the full path of the picture file.
  129.  *
  130.  * @return bool
  131.  */
  132. function my_save_profile_image($id, $originalfile) {
  133.     $context = get_context_instance(CONTEXT_USER, $id);
  134.     return process_new_icon($context, 'user', 'icon', 0, $originalfile);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement