2010071001.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Change profile image names to use guid rather than username
  4. */
  5. /**
  6. * Need the same function to generate a user matrix, but can't call it
  7. * the same thing as the previous update.
  8. *
  9. * @param int $guid User guid.
  10. *
  11. * @return string File matrix
  12. */
  13. function user_file_matrix_2010071001($guid) {
  14. // lookup the entity
  15. $user = get_entity($guid);
  16. if ($user->type != 'user') {
  17. // only to be used for user directories
  18. return FALSE;
  19. }
  20. if (!$user->time_created) {
  21. // no idea where this user has its files
  22. return FALSE;
  23. }
  24. $time_created = date('Y/m/d', $user->time_created);
  25. return "$time_created/$user->guid/";
  26. }
  27. $sizes = array('large', 'medium', 'small', 'tiny', 'master', 'topbar');
  28. global $ENTITY_CACHE, $CONFIG;
  29. $users = mysql_query("SELECT guid, username FROM {$CONFIG->dbprefix}users_entity
  30. WHERE username != ''");
  31. while ($user = mysql_fetch_object($users)) {
  32. $ENTITY_CACHE = array();
  33. $user_directory = user_file_matrix_2010071001($user->guid);
  34. if (!$user_directory) {
  35. continue;
  36. }
  37. $profile_directory = $CONFIG->dataroot . $user_directory . "profile/";
  38. if (!file_exists($profile_directory)) {
  39. continue;
  40. }
  41. foreach ($sizes as $size) {
  42. $old_filename = "$profile_directory{$user->username}{$size}.jpg";
  43. $new_filename = "$profile_directory{$user->guid}{$size}.jpg";
  44. if (file_exists($old_filename)) {
  45. if (!rename($old_filename, $new_filename)) {
  46. error_log("Failed to rename profile photo for $user->username");
  47. }
  48. }
  49. }
  50. }