upload.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Avatar upload action
  4. *
  5. * Modified to allow flash and html5 webcam uploads
  6. */
  7. $guid = get_input('guid');
  8. $owner = get_entity($guid);
  9. if (!$owner || !($owner instanceof ElggUser) || !$owner->canEdit()) {
  10. register_error(elgg_echo('avatar:upload:fail'));
  11. forward(REFERER);
  12. }
  13. // check for html5, and finally file upload
  14. $img_data = false;
  15. $html5 = get_input('webcam-image-base64');
  16. $url = get_input('avatar_url');
  17. $upload = isset($_FILES['avatar']['name']) && !empty($_FILES['avatar']['name']);
  18. if ($html5) {
  19. $img_data = base64_decode($html5);
  20. if (!$img_data){
  21. register_error(elgg_echo("avatar:upload:fail"));
  22. forward(REFERRER);
  23. }
  24. } elseif ($url) {
  25. $url = elgg_normalize_url($url);
  26. $img_data = file_get_contents($url);
  27. if (!$img_data) {
  28. // try curl
  29. if (function_exists('curl_init')) {
  30. $ch = curl_init();
  31. curl_setopt($ch, CURLOPT_URL, $url);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  33. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  35. $img_data = curl_exec($ch);
  36. curl_close($ch);
  37. }
  38. if (!$img_data) {
  39. register_error(elgg_echo('avatar:upload:fail'));
  40. forward(REFERRER);
  41. }
  42. }
  43. } elseif ($upload) {
  44. if ($_FILES['avatar']['error'] !== 0) {
  45. register_error(elgg_echo('avatar:upload:fail'));
  46. return $return;
  47. } elseif(!in_array(strtolower(substr($_FILES['avatar']['name'], -3)), array('jpg','png','gif'))) {
  48. register_error(elgg_echo('avatar:upload:fail'));
  49. return $return;
  50. }
  51. } else {
  52. // nothing was submitted
  53. register_error(elgg_echo('webcam:no_avatar_selected'));
  54. forward(REFERER);
  55. }
  56. // if we have img data, save it
  57. if ($img_data) {
  58. $filehandler = new ElggFile();
  59. $filehandler->owner_guid = $owner->getGUID();
  60. $filehandler->setFilename("profile/" . $owner->guid . "master.jpg");
  61. $filehandler->open("write");
  62. if (!$filehandler->write($img_data)) {
  63. register_error(elgg_echo("avatar:upload:fail"));
  64. forward(REFERRER);
  65. }
  66. $filename = $filehandler->getFilenameOnFilestore();
  67. $filehandler->close();
  68. }
  69. $icon_sizes = elgg_get_config('icon_sizes');
  70. // get the images and save their file handlers into an array
  71. // so we can do clean up if one fails.
  72. $files = array();
  73. foreach ($icon_sizes as $name => $size_info) {
  74. if ($upload) {
  75. $resized = get_resized_image_from_uploaded_file('avatar', $size_info['w'], $size_info['h'], $size_info['square'], $size_info['upscale']);
  76. } else {
  77. $resized = get_resized_image_from_existing_file(
  78. $filename,
  79. $size_info['w'],
  80. $size_info['h'],
  81. $size_info['square']
  82. );
  83. }
  84. if ($resized) {
  85. //@todo Make these actual entities. See exts #348.
  86. $file = new ElggFile();
  87. $file->owner_guid = $guid;
  88. $file->setFilename("profile/{$guid}{$name}.jpg");
  89. $file->open('write');
  90. $file->write($resized);
  91. $file->close();
  92. $files[] = $file;
  93. } else {
  94. // cleanup on fail
  95. foreach ($files as $file) {
  96. $file->delete();
  97. }
  98. register_error(elgg_echo('avatar:resize:fail'));
  99. forward(REFERER);
  100. }
  101. }
  102. // reset crop coordinates
  103. $owner->x1 = 0;
  104. $owner->x2 = 0;
  105. $owner->y1 = 0;
  106. $owner->y2 = 0;
  107. $owner->icontime = time();
  108. if (elgg_trigger_event('profileiconupdate', $owner->type, $owner)) {
  109. system_message(elgg_echo("avatar:upload:success"));
  110. $view = 'river/user/default/profileiconupdate';
  111. elgg_delete_river(array('subject_guid' => $owner->guid, 'view' => $view));
  112. add_to_river($view, 'update', $owner->guid, $owner->guid);
  113. }
  114. forward(REFERER);