PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/uploadpicture.php

https://github.com/nadavkav/Moodle-RTL--Shenkar-Translation-Team-
PHP | 249 lines | 133 code | 32 blank | 84 comment | 29 complexity | c68ace383c3ade9b4abb7a26b63e1c66 MD5 | raw file
  1. <?php // $Id: uploadpicture.php,v 1.1.2.8 2010/01/22 02:01:03 rwijaya Exp $
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // Copyright (C) 2007 Inaki Arenaza //
  5. // //
  6. // Based on .../admin/uploaduser.php and .../lib/gdlib.php //
  7. // //
  8. // This program is free software; you can redistribute it and/or modify //
  9. // it under the terms of the GNU General Public License as published by //
  10. // the Free Software Foundation; either version 2 of the License, or //
  11. // (at your option) any later version. //
  12. // //
  13. // This program is distributed in the hope that it will be useful, //
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  16. // GNU General Public License for more details: //
  17. // //
  18. // http://www.gnu.org/copyleft/gpl.html //
  19. // //
  20. ///////////////////////////////////////////////////////////////////////////
  21. require_once('../config.php');
  22. require_once($CFG->libdir.'/uploadlib.php');
  23. require_once($CFG->libdir.'/adminlib.php');
  24. require_once($CFG->libdir.'/gdlib.php');
  25. require_once('uploadpicture_form.php');
  26. define ('PIX_FILE_UPDATED', 0);
  27. define ('PIX_FILE_ERROR', 1);
  28. define ('PIX_FILE_SKIPPED', 2);
  29. admin_externalpage_setup('uploadpictures');
  30. require_login();
  31. require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
  32. if (!$site = get_site()) {
  33. error("Could not find site-level course");
  34. }
  35. if (!$adminuser = get_admin()) {
  36. error("Could not find site admin");
  37. }
  38. $strfile = get_string('file');
  39. $struser = get_string('user');
  40. $strusersupdated = get_string('usersupdated');
  41. $struploadpictures = get_string('uploadpictures','admin');
  42. $userfields = array (
  43. 0 => 'username',
  44. 1 => 'idnumber',
  45. 2 => 'id' );
  46. $userfield = optional_param('userfield', 0, PARAM_INT);
  47. $overwritepicture = optional_param('overwritepicture', 0, PARAM_BOOL);
  48. /// Print the header
  49. admin_externalpage_print_header();
  50. print_heading_with_help($struploadpictures, 'uploadpictures');
  51. $mform = new admin_uploadpicture_form(null, $userfields);
  52. if ($formdata = $mform->get_data()) {
  53. if (!array_key_exists($userfield, $userfields)) {
  54. notify(get_string('uploadpicture_baduserfield','admin'));
  55. } else {
  56. // Large files are likely to take their time and memory. Let PHP know
  57. // that we'll take longer, and that the process should be recycled soon
  58. // to free up memory.
  59. @set_time_limit(0);
  60. @raise_memory_limit("192M");
  61. if (function_exists('apache_child_terminate')) {
  62. @apache_child_terminate();
  63. }
  64. // Create a unique temporary directory, to process the zip file
  65. // contents.
  66. $zipdir = my_mktempdir($CFG->dataroot.'/temp/', 'usrpic');
  67. if (!$mform->save_files($zipdir)) {
  68. notify(get_string('uploadpicture_cannotmovezip','admin'));
  69. @remove_dir($zipdir);
  70. } else {
  71. $dstfile = $zipdir.'/'.$mform->get_new_filename();
  72. if(!unzip_file($dstfile, $zipdir, false)) {
  73. notify(get_string('uploadpicture_cannotunzip','admin'));
  74. @remove_dir($zipdir);
  75. } else {
  76. // We don't need the zip file any longer, so delete it to make
  77. // it easier to process the rest of the files inside the directory.
  78. @unlink($dstfile);
  79. $results = array ('errors' => 0,'updated' => 0);
  80. process_directory($zipdir, $userfields[$userfield], $overwritepicture, $results);
  81. // Finally remove the temporary directory with all the user images and print some stats.
  82. remove_dir($zipdir);
  83. notify(get_string('usersupdated', 'admin') . ": " . $results['updated']);
  84. notify(get_string('errors', 'admin') . ": " . $results['errors']);
  85. echo '<hr />';
  86. }
  87. }
  88. }
  89. }
  90. $mform->display();
  91. admin_externalpage_print_footer();
  92. exit;
  93. // ----------- Internal functions ----------------
  94. /**
  95. * Create a unique temporary directory with a given prefix name,
  96. * inside a given directory, with given permissions. Return the
  97. * full path to the newly created temp directory.
  98. *
  99. * @param string $dir where to create the temp directory.
  100. * @param string $prefix prefix for the temp directory name (default '')
  101. * @param string $mode permissions for the temp directory (default 700)
  102. *
  103. * @return string The full path to the temp directory.
  104. */
  105. function my_mktempdir($dir, $prefix='', $mode=0700) {
  106. if (substr($dir, -1) != '/') {
  107. $dir .= '/';
  108. }
  109. do {
  110. $path = $dir.$prefix.mt_rand(0, 9999999);
  111. } while (!mkdir($path, $mode));
  112. return $path;
  113. }
  114. /**
  115. * Recursively process a directory, picking regular files and feeding
  116. * them to process_file().
  117. *
  118. * @param string $dir the full path of the directory to process
  119. * @param string $userfield the prefix_user table field to use to
  120. * match picture files to users.
  121. * @param bool $overwrite overwrite existing picture or not.
  122. * @param array $results (by reference) accumulated statistics of
  123. * users updated and errors.
  124. *
  125. * @return nothing
  126. */
  127. function process_directory ($dir, $userfield, $overwrite, &$results) {
  128. if(!($handle = opendir($dir))) {
  129. notify(get_string('uploadpicture_cannotprocessdir','admin'));
  130. return;
  131. }
  132. while (false !== ($item = readdir($handle))) {
  133. if ($item != '.' && $item != '..') {
  134. if (is_dir($dir.'/'.$item)) {
  135. process_directory($dir.'/'.$item, $userfield, $overwrite, $results);
  136. } else if (is_file($dir.'/'.$item)) {
  137. $result = process_file($dir.'/'.$item, $userfield, $overwrite);
  138. switch ($result) {
  139. case PIX_FILE_ERROR:
  140. $results['errors']++;
  141. break;
  142. case PIX_FILE_UPDATED:
  143. $results['updated']++;
  144. break;
  145. }
  146. }
  147. // Ignore anything else that is not a directory or a file (e.g.,
  148. // symbolic links, sockets, pipes, etc.)
  149. }
  150. }
  151. closedir($handle);
  152. }
  153. /**
  154. * Given the full path of a file, try to find the user the file
  155. * corresponds to and assign him/her this file as his/her picture.
  156. * Make extensive checks to make sure we don't open any security holes
  157. * and report back any success/error.
  158. *
  159. * @param string $file the full path of the file to process
  160. * @param string $userfield the prefix_user table field to use to
  161. * match picture files to users.
  162. * @param bool $overwrite overwrite existing picture or not.
  163. *
  164. * @return integer either PIX_FILE_UPDATED, PIX_FILE_ERROR or
  165. * PIX_FILE_SKIPPED
  166. */
  167. function process_file ($file, $userfield, $overwrite) {
  168. // Add additional checks on the filenames, as they are user
  169. // controlled and we don't want to open any security holes.
  170. $path_parts = pathinfo(cleardoubleslashes($file));
  171. $basename = $path_parts['basename'];
  172. $extension = $path_parts['extension'];
  173. // The picture file name (without extension) must match the
  174. // userfield attribute.
  175. $uservalue = substr($basename, 0,
  176. strlen($basename) -
  177. strlen($extension) - 1);
  178. // userfield names are safe, so don't quote them.
  179. if (!($user = get_record('user', $userfield, addslashes($uservalue),'deleted',0))) {
  180. $a = new Object();
  181. $a->userfield = clean_param($userfield, PARAM_CLEANHTML);
  182. $a->uservalue = clean_param($uservalue, PARAM_CLEANHTML);
  183. notify(get_string('uploadpicture_usernotfound', 'admin', $a));
  184. return PIX_FILE_ERROR;
  185. }
  186. $haspicture = get_field('user', 'picture', 'id', $user->id);
  187. if ($haspicture && !$overwrite) {
  188. notify(get_string('uploadpicture_userskipped', 'admin', $user->username));
  189. return PIX_FILE_SKIPPED;
  190. }
  191. if (my_save_profile_image($user->id, $file)) {
  192. set_field('user', 'picture', 1, 'id', $user->id);
  193. notify(get_string('uploadpicture_userupdated', 'admin', $user->username));
  194. return PIX_FILE_UPDATED;
  195. } else {
  196. notify(get_string('uploadpicture_cannotsave', 'admin', $user->username));
  197. return PIX_FILE_ERROR;
  198. }
  199. }
  200. /**
  201. * Try to save the given file (specified by its full path) as the
  202. * picture for the user with the given id.
  203. *
  204. * @param integer $id the internal id of the user to assign the
  205. * picture file to.
  206. * @param string $originalfile the full path of the picture file.
  207. *
  208. * @return bool
  209. */
  210. function my_save_profile_image($id, $originalfile) {
  211. $destination = create_profile_image_destination($id, 'user');
  212. if ($destination === false) {
  213. return false;
  214. }
  215. return process_profile_image($originalfile, $destination);
  216. }
  217. ?>