editgroup.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Edit an existing group
  18. *
  19. * @category Group
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Sarven Capadisli <csarven@status.net>
  23. * @author Zach Copley <zach@status.net>
  24. * @copyright 2008-2011 StatusNet, Inc.
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. defined('GNUSOCIAL') || die();
  28. /**
  29. * Add a new group
  30. *
  31. * This is the form for adding a new group
  32. *
  33. * @category Group
  34. * @package GNUsocial
  35. * @author Evan Prodromou <evan@status.net>
  36. * @author Zach Copley <zach@status.net>
  37. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  38. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  39. */
  40. class EditgroupAction extends GroupAction
  41. {
  42. public $message = null;
  43. public $success = null;
  44. protected $canPost = true;
  45. public function title()
  46. {
  47. // TRANS: Title for form to edit a group. %s is a group nickname.
  48. return sprintf(_('Edit %s group'), $this->group->nickname);
  49. }
  50. public function showContent()
  51. {
  52. $form = new GroupEditForm($this, $this->group);
  53. $form->show();
  54. }
  55. public function showPageNoticeBlock()
  56. {
  57. parent::showPageNoticeBlock();
  58. if ($this->message) {
  59. $this->element(
  60. 'p',
  61. ($this->success) ? 'success' : 'error',
  62. $this->message
  63. );
  64. } else {
  65. $this->element(
  66. 'p',
  67. 'instructions',
  68. // TRANS: Form instructions for group edit form.
  69. _('Use this form to edit the group.')
  70. );
  71. }
  72. }
  73. public function showScripts()
  74. {
  75. parent::showScripts();
  76. $this->autofocus('fullname');
  77. }
  78. /**
  79. * Prepare to run
  80. * @param array $args
  81. * @return bool
  82. * @throws ClientException
  83. * @throws NicknameException
  84. */
  85. protected function prepare(array $args = [])
  86. {
  87. parent::prepare($args);
  88. if (!common_logged_in()) {
  89. // TRANS: Client error displayed trying to edit a group while not logged in.
  90. $this->clientError(_('You must be logged in to create a group.'));
  91. }
  92. $nickname_arg = $this->trimmed('nickname');
  93. $nickname = common_canonical_nickname($nickname_arg);
  94. // Permanent redirect on non-canonical nickname
  95. if ($nickname_arg != $nickname) {
  96. $args = ['nickname' => $nickname];
  97. common_redirect(common_local_url('editgroup', $args), 301);
  98. }
  99. if (!$nickname) {
  100. // TRANS: Client error displayed trying to edit a group while not proving a nickname for the group to edit.
  101. $this->clientError(_('No nickname.'), 404);
  102. }
  103. $groupid = $this->trimmed('groupid');
  104. if ($groupid) {
  105. $this->group = User_group::getKV('id', $groupid);
  106. } else {
  107. $local = Local_group::getKV('nickname', $nickname);
  108. if ($local) {
  109. $this->group = User_group::getKV('id', $local->group_id);
  110. }
  111. }
  112. if (!$this->group) {
  113. // TRANS: Client error displayed trying to edit a non-existing group.
  114. $this->clientError(_('No such group.'), 404);
  115. }
  116. $cur = common_current_user();
  117. if (!$cur->isAdmin($this->group)) {
  118. // TRANS: Client error displayed trying to edit a group while not being a group admin.
  119. $this->clientError(_('You must be an admin to edit the group.'), 403);
  120. }
  121. return true;
  122. }
  123. protected function handlePost()
  124. {
  125. parent::handlePost();
  126. $cur = common_current_user();
  127. if (!$cur->isAdmin($this->group)) {
  128. // TRANS: Client error displayed trying to edit a group while not being a group admin.
  129. $this->clientError(_('You must be an admin to edit the group.'), 403);
  130. }
  131. if (Event::handle('StartGroupSaveForm', [$this])) {
  132. // $nickname will only be set if this changenick value is true.
  133. $nickname = null;
  134. if (common_config('profile', 'changenick') == true) {
  135. try {
  136. $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
  137. } catch (NicknameTakenException $e) {
  138. // Abort only if the nickname is occupied by _another_ group
  139. if ($e->profile->id != $this->group->profile_id) {
  140. $this->setMessage($e->getMessage(), true);
  141. return;
  142. }
  143. $nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time
  144. } catch (NicknameException $e) {
  145. $this->setMessage($e->getMessage(), true);
  146. return;
  147. }
  148. }
  149. $fullname = $this->trimmed('fullname');
  150. $homepage = $this->trimmed('homepage');
  151. $description = $this->trimmed('description');
  152. $location = $this->trimmed('location');
  153. $aliasstring = $this->trimmed('aliases');
  154. $private = $this->boolean('private');
  155. if ($private) {
  156. $force_scope = 1;
  157. $join_policy = User_group::JOIN_POLICY_MODERATE;
  158. } else {
  159. $force_scope = 0;
  160. $join_policy = User_group::JOIN_POLICY_OPEN;
  161. }
  162. if (!is_null($homepage) && (strlen($homepage) > 0) &&
  163. !common_valid_http_url($homepage)) {
  164. // TRANS: Group edit form validation error.
  165. $this->setMessage(_('Homepage is not a valid URL.'), true);
  166. return;
  167. } elseif (!is_null($fullname) && mb_strlen($fullname) > 255) {
  168. // TRANS: Group edit form validation error.
  169. $this->setMessage(_('Full name is too long (maximum 255 characters).'), true);
  170. return;
  171. } elseif (User_group::descriptionTooLong($description)) {
  172. $this->setMessage(sprintf(
  173. // TRANS: Group edit form validation error.
  174. _m(
  175. 'Description is too long (maximum %d character).',
  176. 'Description is too long (maximum %d characters).',
  177. User_group::maxDescription()
  178. ),
  179. User_group::maxDescription()
  180. ), true);
  181. return;
  182. } elseif (!is_null($location) && mb_strlen($location) > 255) {
  183. // TRANS: Group edit form validation error.
  184. $this->setMessage(_('Location is too long (maximum 255 characters).'), true);
  185. return;
  186. }
  187. if (!empty($aliasstring)) {
  188. $aliases = array_map(
  189. ['Nickname', 'normalize'],
  190. array_unique(preg_split('/[\s,]+/', $aliasstring))
  191. );
  192. } else {
  193. $aliases = [];
  194. }
  195. if (count($aliases) > common_config('group', 'maxaliases')) {
  196. // TRANS: Group edit form validation error.
  197. // TRANS: %d is the maximum number of allowed aliases.
  198. $this->setMessage(sprintf(
  199. _m(
  200. 'Too many aliases! Maximum %d allowed.',
  201. 'Too many aliases! Maximum %d allowed.',
  202. common_config('group', 'maxaliases')
  203. ),
  204. common_config('group', 'maxaliases')
  205. ), true);
  206. return;
  207. }
  208. $this->group->query('START TRANSACTION');
  209. $orig = clone($this->group);
  210. if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) {
  211. assert(Nickname::normalize($nickname) === $nickname);
  212. common_debug("Changing group nickname from '{$this->group->nickname}' to '{$nickname}'.");
  213. $this->group->nickname = $nickname;
  214. $this->group->mainpage = common_local_url('showgroup', ['nickname' => $this->group->nickname]);
  215. }
  216. $this->group->fullname = $fullname;
  217. $this->group->homepage = $homepage;
  218. $this->group->description = $description;
  219. $this->group->location = $location;
  220. $this->group->join_policy = $join_policy;
  221. $this->group->force_scope = $force_scope;
  222. $result = $this->group->update($orig);
  223. if ($result === false) {
  224. common_log_db_error($this->group, 'UPDATE', __FILE__);
  225. // TRANS: Server error displayed when editing a group fails.
  226. $this->serverError(_('Could not update group.'));
  227. }
  228. $result = $this->group->setAliases($aliases);
  229. if (!$result) {
  230. // TRANS: Server error displayed when group aliases could not be added.
  231. $this->serverError(_('Could not create aliases.'));
  232. }
  233. $this->group->query('COMMIT');
  234. Event::handle('EndGroupSaveForm', [$this]);
  235. if ($this->group->nickname != $orig->nickname) {
  236. common_redirect(common_local_url('editgroup', ['nickname' => $this->group->nickname]), 303);
  237. }
  238. }
  239. // TRANS: Group edit form success message.
  240. $this->setMessage(_('Options saved.'));
  241. }
  242. public function setMessage($msg, $error = false)
  243. {
  244. $this->message = $msg;
  245. $this->success = !$error;
  246. }
  247. }