apigroupprofileupdate.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Update a group's profile
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category API
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2010 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * API analog to the group edit page
  34. *
  35. * @category API
  36. * @package StatusNet
  37. * @author Zach Copley <zach@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. */
  41. class ApiGroupProfileUpdateAction extends ApiAuthAction
  42. {
  43. protected $needPost = true;
  44. /**
  45. * Take arguments for running
  46. *
  47. * @param array $args $_REQUEST args
  48. *
  49. * @return boolean success flag
  50. *
  51. */
  52. protected function prepare(array $args=array())
  53. {
  54. parent::prepare($args);
  55. $this->nickname = Nickname::normalize($this->trimmed('nickname'));
  56. $this->fullname = $this->trimmed('fullname');
  57. $this->homepage = $this->trimmed('homepage');
  58. $this->description = $this->trimmed('description');
  59. $this->location = $this->trimmed('location');
  60. $this->aliasstring = $this->trimmed('aliases');
  61. $this->user = $this->auth_user;
  62. $this->group = $this->getTargetGroup($this->arg('id'));
  63. return true;
  64. }
  65. /**
  66. * Handle the request
  67. *
  68. * See which request params have been set, and update the profile
  69. *
  70. * @return void
  71. */
  72. protected function handle()
  73. {
  74. parent::handle();
  75. if (!in_array($this->format, array('xml', 'json'))) {
  76. // TRANS: Client error displayed when coming across a non-supported API method.
  77. $this->clientError(_('API method not found.'), 404);
  78. }
  79. if (empty($this->user)) {
  80. // TRANS: Client error displayed when not providing a user or an invalid user.
  81. $this->clientError(_('No such user.'), 404);
  82. }
  83. if (empty($this->group)) {
  84. // TRANS: Client error displayed when not providing a group or an invalid group.
  85. $this->clientError(_('Group not found.'), 404);
  86. }
  87. if (!$this->user->isAdmin($this->group)) {
  88. // TRANS: Client error displayed when trying to edit a group without being an admin.
  89. $this->clientError(_('You must be an admin to edit the group.'), 403);
  90. }
  91. $this->group->query('BEGIN');
  92. $orig = clone($this->group);
  93. try {
  94. if (common_config('profile', 'changenick') == true && $this->group->nickname !== $this->nickname) {
  95. try {
  96. $this->group->nickname = Nickname::normalize($this->nickname, true);
  97. } catch (NicknameException $e) {
  98. throw new ApiValidationException($e->getMessage());
  99. }
  100. $this->group->mainpage = common_local_url('showgroup',
  101. array('nickname' => $this->group->nickname));
  102. }
  103. if (!empty($this->fullname)) {
  104. $this->validateFullname();
  105. $this->group->fullname = $this->fullname;
  106. }
  107. if (!empty($this->homepage)) {
  108. $this->validateHomepage();
  109. $this->group->homepage = $this->homepage;
  110. }
  111. if (!empty($this->description)) {
  112. $this->validateDescription();
  113. $this->group->description = $this->decription;
  114. }
  115. if (!empty($this->location)) {
  116. $this->validateLocation();
  117. $this->group->location = $this->location;
  118. }
  119. } catch (ApiValidationException $ave) {
  120. $this->clientError($ave->getMessage(), 400);
  121. }
  122. $result = $this->group->update($orig);
  123. if (!$result) {
  124. common_log_db_error($this->group, 'UPDATE', __FILE__);
  125. // TRANS: Server error displayed when group update fails.
  126. $this->serverError(_('Could not update group.'));
  127. }
  128. $aliases = array();
  129. try {
  130. if (!empty($this->aliasstring)) {
  131. $aliases = $this->validateAliases();
  132. }
  133. } catch (ApiValidationException $ave) {
  134. $this->clientError($ave->getMessage(), 403);
  135. }
  136. $result = $this->group->setAliases($aliases);
  137. if (!$result) {
  138. // TRANS: Server error displayed when adding group aliases fails.
  139. $this->serverError(_('Could not create aliases.'));
  140. }
  141. $this->group->query('COMMIT');
  142. switch($this->format) {
  143. case 'xml':
  144. $this->showSingleXmlGroup($this->group);
  145. break;
  146. case 'json':
  147. $this->showSingleJsonGroup($this->group);
  148. break;
  149. default:
  150. // TRANS: Client error displayed when coming across a non-supported API method.
  151. $this->clientError(_('API method not found.'), 404);
  152. }
  153. }
  154. function validateHomepage()
  155. {
  156. if (!is_null($this->homepage)
  157. && (strlen($this->homepage) > 0)
  158. && !common_valid_http_url($this->homepage)) {
  159. throw new ApiValidationException(
  160. // TRANS: API validation exception thrown when homepage URL does not validate.
  161. _('Homepage is not a valid URL.')
  162. );
  163. }
  164. }
  165. function validateFullname()
  166. {
  167. if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
  168. throw new ApiValidationException(
  169. // TRANS: API validation exception thrown when full name does not validate.
  170. _('Full name is too long (maximum 255 characters).')
  171. );
  172. }
  173. }
  174. function validateDescription()
  175. {
  176. if (User_group::descriptionTooLong($this->description)) {
  177. // TRANS: API validation exception thrown when description does not validate.
  178. // TRANS: %d is the maximum description length and used for plural.
  179. throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
  180. 'Description is too long (maximum %d characters).',
  181. User_group::maxDescription()),
  182. User_group::maxDescription()));
  183. }
  184. }
  185. function validateLocation()
  186. {
  187. if (!is_null($this->location) && mb_strlen($this->location) > 255) {
  188. throw new ApiValidationException(
  189. // TRANS: API validation exception thrown when location does not validate.
  190. _('Location is too long (maximum 255 characters).')
  191. );
  192. }
  193. }
  194. function validateAliases()
  195. {
  196. try {
  197. $aliases = array_map(array('Nickname', 'normalize'),
  198. array_unique(preg_split('/[\s,]+/', $this->aliasstring)));
  199. } catch (NicknameException $e) {
  200. throw new ApiValidationException(sprintf('Error processing aliases: %s', $e->getMessage()));
  201. }
  202. if (count($aliases) > common_config('group', 'maxaliases')) {
  203. // TRANS: API validation exception thrown when aliases do not validate.
  204. // TRANS: %d is the maximum number of aliases and used for plural.
  205. throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
  206. 'Too many aliases! Maximum %d allowed.',
  207. common_config('group', 'maxaliases')),
  208. common_config('group', 'maxaliases')));
  209. }
  210. return $aliases;
  211. }
  212. }