register.php 3.4 KB

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