edit.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Elgg groups plugin edit action.
  4. *
  5. * @package ElggGroups
  6. */
  7. elgg_make_sticky_form('groups');
  8. /**
  9. * wrapper for recursive array walk decoding
  10. */
  11. function profile_array_decoder(&$v) {
  12. $v = _elgg_html_decode($v);
  13. }
  14. // Get group fields
  15. $input = array();
  16. foreach (elgg_get_config('group') as $shortname => $valuetype) {
  17. $input[$shortname] = get_input($shortname);
  18. // @todo treat profile fields as unescaped: don't filter, encode on output
  19. if (is_array($input[$shortname])) {
  20. array_walk_recursive($input[$shortname], 'profile_array_decoder');
  21. } else {
  22. $input[$shortname] = _elgg_html_decode($input[$shortname]);
  23. }
  24. if ($valuetype == 'tags') {
  25. $input[$shortname] = string_to_tag_array($input[$shortname]);
  26. }
  27. }
  28. $input['name'] = htmlspecialchars(get_input('name', '', false), ENT_QUOTES, 'UTF-8');
  29. $user = elgg_get_logged_in_user_entity();
  30. $group_guid = (int)get_input('group_guid');
  31. $is_new_group = $group_guid == 0;
  32. if ($is_new_group
  33. && (elgg_get_plugin_setting('limited_groups', 'groups') == 'yes')
  34. && !$user->isAdmin()) {
  35. register_error(elgg_echo("groups:cantcreate"));
  36. forward(REFERER);
  37. }
  38. $group = $group_guid ? get_entity($group_guid) : new ElggGroup();
  39. if (elgg_instanceof($group, "group") && !$group->canEdit()) {
  40. register_error(elgg_echo("groups:cantedit"));
  41. forward(REFERER);
  42. }
  43. // Assume we can edit or this is a new group
  44. if (sizeof($input) > 0) {
  45. foreach($input as $shortname => $value) {
  46. // update access collection name if group name changes
  47. if (!$is_new_group && $shortname == 'name' && $value != $group->name) {
  48. $group_name = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
  49. $ac_name = sanitize_string(elgg_echo('groups:group') . ": " . $group_name);
  50. $acl = get_access_collection($group->group_acl);
  51. if ($acl) {
  52. // @todo Elgg api does not support updating access collection name
  53. $db_prefix = elgg_get_config('dbprefix');
  54. $query = "UPDATE {$db_prefix}access_collections SET name = '$ac_name'
  55. WHERE id = $group->group_acl";
  56. update_data($query);
  57. }
  58. }
  59. if ($value === '') {
  60. // The group profile displays all profile fields that have a value.
  61. // We don't want to display fields with empty string value, so we
  62. // remove the metadata completely.
  63. $group->deleteMetadata($shortname);
  64. continue;
  65. }
  66. $group->$shortname = $value;
  67. }
  68. }
  69. // Validate create
  70. if (!$group->name) {
  71. register_error(elgg_echo("groups:notitle"));
  72. forward(REFERER);
  73. }
  74. // Set group tool options
  75. $tool_options = elgg_get_config('group_tool_options');
  76. if ($tool_options) {
  77. foreach ($tool_options as $group_option) {
  78. $option_toggle_name = $group_option->name . "_enable";
  79. $option_default = $group_option->default_on ? 'yes' : 'no';
  80. $group->$option_toggle_name = get_input($option_toggle_name, $option_default);
  81. }
  82. }
  83. // Group membership - should these be treated with same constants as access permissions?
  84. $is_public_membership = (get_input('membership') == ACCESS_PUBLIC);
  85. $group->membership = $is_public_membership ? ACCESS_PUBLIC : ACCESS_PRIVATE;
  86. $group->setContentAccessMode(get_input('content_access_mode'));
  87. if ($is_new_group) {
  88. $group->access_id = ACCESS_PUBLIC;
  89. }
  90. $old_owner_guid = $is_new_group ? 0 : $group->owner_guid;
  91. $new_owner_guid = (int) get_input('owner_guid');
  92. $owner_has_changed = false;
  93. $old_icontime = null;
  94. if (!$is_new_group && $new_owner_guid && $new_owner_guid != $old_owner_guid) {
  95. // verify new owner is member and old owner/admin is logged in
  96. if ($group->isMember(get_user($new_owner_guid)) && ($old_owner_guid == $user->guid || $user->isAdmin())) {
  97. $group->owner_guid = $new_owner_guid;
  98. if ($group->container_guid == $old_owner_guid) {
  99. // Even though this action defaults container_guid to the logged in user guid,
  100. // the group may have initially been created with a custom script that assigned
  101. // a different container entity. We want to make sure we preserve the original
  102. // container if it the group is not contained by the original owner.
  103. $group->container_guid = $new_owner_guid;
  104. }
  105. $metadata = elgg_get_metadata(array(
  106. 'guid' => $group_guid,
  107. 'limit' => false,
  108. ));
  109. if ($metadata) {
  110. foreach ($metadata as $md) {
  111. if ($md->owner_guid == $old_owner_guid) {
  112. $md->owner_guid = $new_owner_guid;
  113. $md->save();
  114. }
  115. }
  116. }
  117. // @todo Remove this when #4683 fixed
  118. $owner_has_changed = true;
  119. $old_icontime = $group->icontime;
  120. }
  121. }
  122. $must_move_icons = ($owner_has_changed && $old_icontime);
  123. if ($is_new_group) {
  124. // if new group, we need to save so group acl gets set in event handler
  125. if (!$group->save()) {
  126. register_error(elgg_echo("groups:save_error"));
  127. forward(REFERER);
  128. }
  129. }
  130. // Invisible group support
  131. // @todo this requires save to be called to create the acl for the group. This
  132. // is an odd requirement and should be removed. Either the acl creation happens
  133. // in the action or the visibility moves to a plugin hook
  134. if (elgg_get_plugin_setting('hidden_groups', 'groups') == 'yes') {
  135. $visibility = (int)get_input('vis');
  136. if ($visibility == ACCESS_PRIVATE) {
  137. // Make this group visible only to group members. We need to use
  138. // ACCESS_PRIVATE on the form and convert it to group_acl here
  139. // because new groups do not have acl until they have been saved once.
  140. $visibility = $group->group_acl;
  141. // Force all new group content to be available only to members
  142. $group->setContentAccessMode(ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY);
  143. }
  144. $group->access_id = $visibility;
  145. }
  146. if (!$group->save()) {
  147. register_error(elgg_echo("groups:save_error"));
  148. forward(REFERER);
  149. }
  150. // group saved so clear sticky form
  151. elgg_clear_sticky_form('groups');
  152. // group creator needs to be member of new group and river entry created
  153. if ($is_new_group) {
  154. // @todo this should not be necessary...
  155. elgg_set_page_owner_guid($group->guid);
  156. $group->join($user);
  157. elgg_create_river_item(array(
  158. 'view' => 'river/group/create',
  159. 'action_type' => 'create',
  160. 'subject_guid' => $user->guid,
  161. 'object_guid' => $group->guid,
  162. ));
  163. }
  164. $has_uploaded_icon = (!empty($_FILES['icon']['type']) && substr_count($_FILES['icon']['type'], 'image/'));
  165. if ($has_uploaded_icon) {
  166. $icon_sizes = elgg_get_config('icon_sizes');
  167. $prefix = "groups/" . $group->guid;
  168. $filehandler = new ElggFile();
  169. $filehandler->owner_guid = $group->owner_guid;
  170. $filehandler->setFilename($prefix . ".jpg");
  171. $filehandler->open("write");
  172. $filehandler->write(get_uploaded_file('icon'));
  173. $filehandler->close();
  174. $filename = $filehandler->getFilenameOnFilestore();
  175. $sizes = array('tiny', 'small', 'medium', 'large', 'master');
  176. $thumbs = array();
  177. foreach ($sizes as $size) {
  178. $thumbs[$size] = get_resized_image_from_existing_file(
  179. $filename,
  180. $icon_sizes[$size]['w'],
  181. $icon_sizes[$size]['h'],
  182. $icon_sizes[$size]['square']
  183. );
  184. }
  185. if ($thumbs['tiny']) { // just checking if resize successful
  186. $thumb = new ElggFile();
  187. $thumb->owner_guid = $group->owner_guid;
  188. $thumb->setMimeType('image/jpeg');
  189. foreach ($sizes as $size) {
  190. $thumb->setFilename("{$prefix}{$size}.jpg");
  191. $thumb->open("write");
  192. $thumb->write($thumbs[$size]);
  193. $thumb->close();
  194. }
  195. $group->icontime = time();
  196. }
  197. }
  198. // @todo Remove this when #4683 fixed
  199. if ($must_move_icons) {
  200. $filehandler = new ElggFile();
  201. $filehandler->setFilename('groups');
  202. $filehandler->owner_guid = $old_owner_guid;
  203. $old_path = $filehandler->getFilenameOnFilestore();
  204. $sizes = array('', 'tiny', 'small', 'medium', 'large');
  205. if ($has_uploaded_icon) {
  206. // delete those under old owner
  207. foreach ($sizes as $size) {
  208. unlink("$old_path/{$group_guid}{$size}.jpg");
  209. }
  210. } else {
  211. // move existing to new owner
  212. $filehandler->owner_guid = $group->owner_guid;
  213. $new_path = $filehandler->getFilenameOnFilestore();
  214. foreach ($sizes as $size) {
  215. rename("$old_path/{$group_guid}{$size}.jpg", "$new_path/{$group_guid}{$size}.jpg");
  216. }
  217. }
  218. if ($owner_changed_flag && $old_icontime) { // @todo Remove this when #4683 fixed
  219. $filehandler = new ElggFile();
  220. $filehandler->setFilename('groups');
  221. $filehandler->owner_guid = $old_owner_guid;
  222. $old_path = $filehandler->getFilenameOnFilestore();
  223. $sizes = array('', 'tiny', 'small', 'medium', 'large');
  224. foreach($sizes as $size) {
  225. unlink("$old_path/{$group_guid}{$size}.jpg");
  226. }
  227. }
  228. } elseif ($owner_changed_flag && $old_icontime) { // @todo Remove this when #4683 fixed
  229. $filehandler = new ElggFile();
  230. $filehandler->setFilename('groups');
  231. $filehandler->owner_guid = $old_owner_guid;
  232. $old_path = $filehandler->getFilenameOnFilestore();
  233. $filehandler->owner_guid = $group->owner_guid;
  234. $new_path = $filehandler->getFilenameOnFilestore();
  235. $sizes = array('', 'tiny', 'small', 'medium', 'large');
  236. foreach($sizes as $size) {
  237. rename("$old_path/{$group_guid}{$size}.jpg", "$new_path/{$group_guid}{$size}.jpg");
  238. }
  239. }
  240. system_message(elgg_echo("groups:saved"));
  241. forward($group->getUrl());