upload.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Avatar upload action
  4. */
  5. $guid = get_input('guid');
  6. $owner = get_entity($guid);
  7. if (!$owner || !($owner instanceof ElggUser) || !$owner->canEdit()) {
  8. register_error(elgg_echo('avatar:upload:fail'));
  9. forward(REFERER);
  10. }
  11. $error = elgg_get_friendly_upload_error($_FILES['avatar']['error']);
  12. if ($error) {
  13. register_error($error);
  14. forward(REFERER);
  15. }
  16. $icon_sizes = elgg_get_config('icon_sizes');
  17. // get the images and save their file handlers into an array
  18. // so we can do clean up if one fails.
  19. $files = array();
  20. foreach ($icon_sizes as $name => $size_info) {
  21. $resized = get_resized_image_from_uploaded_file('avatar', $size_info['w'], $size_info['h'], $size_info['square'], $size_info['upscale']);
  22. if ($resized) {
  23. //@todo Make these actual entities. See exts #348.
  24. $file = new ElggFile();
  25. $file->owner_guid = $guid;
  26. $file->setFilename("profile/{$guid}{$name}.jpg");
  27. $file->open('write');
  28. $file->write($resized);
  29. $file->close();
  30. $files[] = $file;
  31. } else {
  32. // cleanup on fail
  33. foreach ($files as $file) {
  34. $file->delete();
  35. }
  36. register_error(elgg_echo('avatar:resize:fail'));
  37. forward(REFERER);
  38. }
  39. }
  40. // reset crop coordinates
  41. $owner->x1 = 0;
  42. $owner->x2 = 0;
  43. $owner->y1 = 0;
  44. $owner->y2 = 0;
  45. $owner->icontime = time();
  46. if (elgg_trigger_event('profileiconupdate', $owner->type, $owner)) {
  47. system_message(elgg_echo("avatar:upload:success"));
  48. $view = 'river/user/default/profileiconupdate';
  49. elgg_delete_river(array('subject_guid' => $owner->guid, 'view' => $view));
  50. elgg_create_river_item(array(
  51. 'view' => $view,
  52. 'action_type' => 'update',
  53. 'subject_guid' => $owner->guid,
  54. 'object_guid' => $owner->guid,
  55. ));
  56. }
  57. forward(REFERER);