The official, canonical postActiv repository. http://www.postactiv.com

apigroupcreate.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /* ============================================================================
  3. * Title: APIGroupCreate
  4. * Create a group via the API
  5. *
  6. * postActiv:
  7. * the micro-blogging software
  8. *
  9. * Copyright:
  10. * Copyright (C) 2016-2018, Maiyannah Bishop
  11. *
  12. * Derived from code copyright various sources:
  13. * o GNU Social (C) 2013-2016, Free Software Foundation, Inc
  14. * o StatusNet (C) 2008-2012, StatusNet, Inc
  15. * ----------------------------------------------------------------------------
  16. * License:
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * <https://www.gnu.org/licenses/agpl.html>
  31. * ----------------------------------------------------------------------------
  32. * About:
  33. * Create a group via the API
  34. *
  35. * PHP version:
  36. * Tested with PHP 7.0
  37. * ----------------------------------------------------------------------------
  38. * File Authors:
  39. * o Zach Copley
  40. * o Evan Prodromou
  41. * o Siebrand Mazeland <s.mazeland@xs4all.nl>
  42. * o Eric Helgeson <erichelgeson@gmail.com>
  43. * o Craig Andrews <candrews@integralblue.com>
  44. * o Brion Vibber <brion@pobox.com>
  45. * o Mikael Nordfeldth <mmn@hethane.se>
  46. * o Maiyannah Bishop <maiyannah.bishop@postactiv.com>
  47. *
  48. * Web:
  49. * o postActiv <http://www.postactiv.com>
  50. * o GNU social <https://www.gnu.org/s/social/>
  51. * ============================================================================
  52. */
  53. // This file is formatted so that it provides useful documentation output in
  54. // NaturalDocs. Please be considerate of this before changing formatting.
  55. if (!defined('POSTACTIV')) { exit(1); }
  56. /**
  57. * Make a new group. Sets the authenticated user as the administrator of the group.
  58. */
  59. class ApiGroupCreateAction extends ApiAuthAction
  60. {
  61. protected $needPost = true;
  62. var $group = null;
  63. var $nickname = null;
  64. var $fullname = null;
  65. var $homepage = null;
  66. var $description = null;
  67. var $location = null;
  68. var $aliasstring = null;
  69. var $aliases = null;
  70. /**
  71. * Take arguments for running
  72. *
  73. * @param array $args $_REQUEST args
  74. *
  75. * @return boolean success flag
  76. */
  77. protected function prepare(array $args=array())
  78. {
  79. parent::prepare($args);
  80. $this->nickname = Nickname::normalize($this->arg('nickname'), true);
  81. $this->fullname = $this->arg('full_name');
  82. $this->homepage = $this->arg('homepage');
  83. $this->description = $this->arg('description');
  84. $this->location = $this->arg('location');
  85. $this->aliasstring = $this->arg('aliases');
  86. return true;
  87. }
  88. /**
  89. * Handle the request
  90. *
  91. * Save the new group
  92. *
  93. * @return void
  94. */
  95. protected function handle()
  96. {
  97. parent::handle();
  98. if (empty($this->user)) {
  99. // TRANS: Client error given when a user was not found (404).
  100. $this->clientError(_('No such user.'), 404);
  101. }
  102. if ($this->validateParams() == false) {
  103. return;
  104. }
  105. $group = User_group::register(array('nickname' => $this->nickname,
  106. 'fullname' => $this->fullname,
  107. 'homepage' => $this->homepage,
  108. 'description' => $this->description,
  109. 'location' => $this->location,
  110. 'aliases' => $this->aliases,
  111. 'userid' => $this->user->id,
  112. 'local' => true));
  113. switch($this->format) {
  114. case 'xml':
  115. $this->showSingleXmlGroup($group);
  116. break;
  117. case 'json':
  118. $this->showSingleJsonGroup($group);
  119. break;
  120. default:
  121. // TRANS: Client error displayed when coming across a non-supported API method.
  122. $this->clientError(_('API method not found.'), 404);
  123. }
  124. }
  125. /**
  126. * Validate params for the new group
  127. *
  128. * @return void
  129. */
  130. function validateParams()
  131. {
  132. if (!is_null($this->homepage)
  133. && strlen($this->homepage) > 0
  134. && !common_valid_http_url($this->homepage)) {
  135. // TRANS: Client error in form for group creation.
  136. $this->clientError(_('Homepage is not a valid URL.'), 403);
  137. } elseif (!is_null($this->fullname)
  138. && mb_strlen($this->fullname) > 255) {
  139. // TRANS: Client error in form for group creation.
  140. $this->clientError(_('Full name is too long (maximum 255 characters).'), 403);
  141. } elseif (User_group::descriptionTooLong($this->description)) {
  142. // TRANS: Client error shown when providing too long a description during group creation.
  143. // TRANS: %d is the maximum number of allowed characters.
  144. $this->clientError(sprintf(_m('Description is too long (maximum %d character).',
  145. 'Description is too long (maximum %d characters).',
  146. User_group::maxDescription()), User_group::maxDescription()), 403);
  147. } elseif (!is_null($this->location)
  148. && mb_strlen($this->location) > 255) {
  149. // TRANS: Client error shown when providing too long a location during group creation.
  150. $this->clientError(_('Location is too long (maximum 255 characters).'), 403);
  151. }
  152. if (!empty($this->aliasstring)) {
  153. $this->aliases = array_map(
  154. array('Nickname', 'normalize'), // static call to Nickname::normalize
  155. array_unique(preg_split('/[\s,]+/', $this->aliasstring))
  156. );
  157. } else {
  158. $this->aliases = array();
  159. }
  160. if (count($this->aliases) > common_config('group', 'maxaliases')) {
  161. $this->clientError(sprintf(
  162. // TRANS: Client error shown when providing too many aliases during group creation.
  163. // TRANS: %d is the maximum number of allowed aliases.
  164. _m('Too many aliases! Maximum %d allowed.',
  165. 'Too many aliases! Maximum %d allowed.',
  166. common_config('group', 'maxaliases')),
  167. common_config('group', 'maxaliases')),
  168. 403);
  169. }
  170. // Everything looks OK
  171. return true;
  172. }
  173. }
  174. // END OF FILE
  175. // ============================================================================
  176. ?>