edit.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Elgg profile edit action
  4. *
  5. */
  6. elgg_make_sticky_form('profile:edit');
  7. $guid = get_input('guid');
  8. $owner = get_entity($guid);
  9. if (!$owner || !($owner instanceof ElggUser) || !$owner->canEdit()) {
  10. register_error(elgg_echo('profile:noaccess'));
  11. forward(REFERER);
  12. }
  13. // grab the defined profile field names and their load the values from POST.
  14. // each field can have its own access, so sort that too.
  15. $input = array();
  16. $accesslevel = get_input('accesslevel');
  17. if (!is_array($accesslevel)) {
  18. $accesslevel = array();
  19. }
  20. /**
  21. * wrapper for recursive array walk decoding
  22. */
  23. function profile_array_decoder(&$v) {
  24. $v = _elgg_html_decode($v);
  25. }
  26. $profile_fields = elgg_get_config('profile_fields');
  27. foreach ($profile_fields as $shortname => $valuetype) {
  28. // the decoding is a stop gap to prevent &amp;&amp; showing up in profile fields
  29. // because it is escaped on both input (get_input()) and output (view:output/text). see #561 and #1405.
  30. // must decode in utf8 or string corruption occurs. see #1567.
  31. $value = get_input($shortname);
  32. if (is_array($value)) {
  33. array_walk_recursive($value, 'profile_array_decoder');
  34. } else {
  35. $value = _elgg_html_decode($value);
  36. }
  37. // limit to reasonable sizes
  38. // @todo - throwing away changes due to this is dumb!
  39. // ^^ This is a sticky form so changes aren't lost...?
  40. if (!is_array($value) && $valuetype != 'longtext' && elgg_strlen($value) > 250) {
  41. $error = elgg_echo('profile:field_too_long', array(elgg_echo("profile:{$shortname}")));
  42. register_error($error);
  43. forward(REFERER);
  44. }
  45. if ($value && $valuetype == 'url' && !preg_match('~^https?\://~i', $value)) {
  46. $value = "http://$value";
  47. }
  48. if ($valuetype == 'tags') {
  49. $value = string_to_tag_array($value);
  50. }
  51. if ($valuetype == 'email' && !empty($value) && !is_email_address($value)) {
  52. register_error(elgg_echo('profile:invalid_email', array(
  53. elgg_echo("profile:{$shortname}")
  54. )));
  55. forward(REFERER);
  56. }
  57. $input[$shortname] = $value;
  58. }
  59. // display name is handled separately
  60. $name = strip_tags(get_input('name'));
  61. if ($name) {
  62. if (elgg_strlen($name) > 50) {
  63. register_error(elgg_echo('user:name:fail'));
  64. } elseif ($owner->name != $name) {
  65. $owner->name = $name;
  66. $owner->save();
  67. }
  68. }
  69. // go through custom fields
  70. if (sizeof($input) > 0) {
  71. foreach ($input as $shortname => $value) {
  72. $options = array(
  73. 'guid' => $owner->guid,
  74. 'metadata_name' => $shortname,
  75. 'limit' => false
  76. );
  77. elgg_delete_metadata($options);
  78. if (!is_null($value) && ($value !== '')) {
  79. // only create metadata for non empty values (0 is allowed) to prevent metadata records
  80. // with empty string values #4858
  81. if (isset($accesslevel[$shortname])) {
  82. $access_id = (int) $accesslevel[$shortname];
  83. } else {
  84. // this should never be executed since the access level should always be set
  85. $access_id = ACCESS_DEFAULT;
  86. }
  87. if (is_array($value)) {
  88. $i = 0;
  89. foreach ($value as $interval) {
  90. $i++;
  91. $multiple = ($i > 1) ? TRUE : FALSE;
  92. create_metadata($owner->guid, $shortname, $interval, 'text', $owner->guid, $access_id, $multiple);
  93. }
  94. } else {
  95. create_metadata($owner->getGUID(), $shortname, $value, 'text', $owner->getGUID(), $access_id);
  96. }
  97. }
  98. }
  99. $owner->save();
  100. // Notify of profile update
  101. elgg_trigger_event('profileupdate', $owner->type, $owner);
  102. elgg_clear_sticky_form('profile:edit');
  103. system_message(elgg_echo("profile:saved"));
  104. }
  105. forward($owner->getUrl());