apigroupcreate.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Create a group via the API
  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 Craig Andrews <candrews@integralblue.com>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Jeffery To <jeffery.to@gmail.com>
  27. * @author Zach Copley <zach@status.net>
  28. * @copyright 2009 StatusNet, Inc.
  29. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  30. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  31. * @link http://status.net/
  32. */
  33. if (!defined('STATUSNET')) {
  34. exit(1);
  35. }
  36. /**
  37. * Make a new group. Sets the authenticated user as the administrator of the group.
  38. *
  39. * @category API
  40. * @package StatusNet
  41. * @author Craig Andrews <candrews@integralblue.com>
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Jeffery To <jeffery.to@gmail.com>
  44. * @author Zach Copley <zach@status.net>
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. */
  48. class ApiGroupCreateAction extends ApiAuthAction
  49. {
  50. protected $needPost = true;
  51. var $group = null;
  52. var $nickname = null;
  53. var $fullname = null;
  54. var $homepage = null;
  55. var $description = null;
  56. var $location = null;
  57. var $aliasstring = null;
  58. var $aliases = null;
  59. /**
  60. * Take arguments for running
  61. *
  62. * @param array $args $_REQUEST args
  63. *
  64. * @return boolean success flag
  65. */
  66. protected function prepare(array $args=array())
  67. {
  68. parent::prepare($args);
  69. $this->nickname = Nickname::normalize($this->arg('nickname'), true);
  70. $this->fullname = $this->arg('full_name');
  71. $this->homepage = $this->arg('homepage');
  72. $this->description = $this->arg('description');
  73. $this->location = $this->arg('location');
  74. $this->aliasstring = $this->arg('aliases');
  75. return true;
  76. }
  77. /**
  78. * Handle the request
  79. *
  80. * Save the new group
  81. *
  82. * @return void
  83. */
  84. protected function handle()
  85. {
  86. parent::handle();
  87. if (empty($this->user)) {
  88. // TRANS: Client error given when a user was not found (404).
  89. $this->clientError(_('No such user.'), 404);
  90. }
  91. if ($this->validateParams() == false) {
  92. return;
  93. }
  94. $group = User_group::register(array('nickname' => $this->nickname,
  95. 'fullname' => $this->fullname,
  96. 'homepage' => $this->homepage,
  97. 'description' => $this->description,
  98. 'location' => $this->location,
  99. 'aliases' => $this->aliases,
  100. 'userid' => $this->user->id,
  101. 'local' => true));
  102. switch($this->format) {
  103. case 'xml':
  104. $this->showSingleXmlGroup($group);
  105. break;
  106. case 'json':
  107. $this->showSingleJsonGroup($group);
  108. break;
  109. default:
  110. // TRANS: Client error displayed when coming across a non-supported API method.
  111. $this->clientError(_('API method not found.'), 404);
  112. }
  113. }
  114. /**
  115. * Validate params for the new group
  116. *
  117. * @return void
  118. */
  119. function validateParams()
  120. {
  121. if (!is_null($this->homepage)
  122. && strlen($this->homepage) > 0
  123. && !common_valid_http_url($this->homepage)) {
  124. // TRANS: Client error in form for group creation.
  125. $this->clientError(_('Homepage is not a valid URL.'), 403);
  126. } elseif (!is_null($this->fullname)
  127. && mb_strlen($this->fullname) > 255) {
  128. // TRANS: Client error in form for group creation.
  129. $this->clientError(_('Full name is too long (maximum 255 characters).'), 403);
  130. } elseif (User_group::descriptionTooLong($this->description)) {
  131. // TRANS: Client error shown when providing too long a description during group creation.
  132. // TRANS: %d is the maximum number of allowed characters.
  133. $this->clientError(sprintf(_m('Description is too long (maximum %d character).',
  134. 'Description is too long (maximum %d characters).',
  135. User_group::maxDescription()), User_group::maxDescription()), 403);
  136. } elseif (!is_null($this->location)
  137. && mb_strlen($this->location) > 255) {
  138. // TRANS: Client error shown when providing too long a location during group creation.
  139. $this->clientError(_('Location is too long (maximum 255 characters).'), 403);
  140. }
  141. if (!empty($this->aliasstring)) {
  142. $this->aliases = array_map(
  143. array('Nickname', 'normalize'), // static call to Nickname::normalize
  144. array_unique(preg_split('/[\s,]+/', $this->aliasstring))
  145. );
  146. } else {
  147. $this->aliases = array();
  148. }
  149. if (count($this->aliases) > common_config('group', 'maxaliases')) {
  150. $this->clientError(sprintf(
  151. // TRANS: Client error shown when providing too many aliases during group creation.
  152. // TRANS: %d is the maximum number of allowed aliases.
  153. _m('Too many aliases! Maximum %d allowed.',
  154. 'Too many aliases! Maximum %d allowed.',
  155. common_config('group', 'maxaliases')),
  156. common_config('group', 'maxaliases')),
  157. 403);
  158. }
  159. // Everything looks OK
  160. return true;
  161. }
  162. }