3 * StatusNet, the distributed open-source microblogging tool
5 * Create a group via the API
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.
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.
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/>.
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/
34 if (!defined('STATUSNET')) {
38 require_once INSTALLDIR . '/lib/apiauth.php';
41 * Make a new group. Sets the authenticated user as the administrator of the group.
45 * @author Craig Andrews <candrews@integralblue.com>
46 * @author Evan Prodromou <evan@status.net>
47 * @author Jeffery To <jeffery.to@gmail.com>
48 * @author Zach Copley <zach@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
52 class ApiGroupCreateAction extends ApiAuthAction
58 var $description = null;
60 var $aliasstring = null;
64 * Take arguments for running
66 * @param array $args $_REQUEST args
68 * @return boolean success flag
72 function prepare($args)
74 parent::prepare($args);
76 $this->user = $this->auth_user;
78 $this->nickname = $this->arg('nickname');
79 $this->fullname = $this->arg('full_name');
80 $this->homepage = $this->arg('homepage');
81 $this->description = $this->arg('description');
82 $this->location = $this->arg('location');
83 $this->aliasstring = $this->arg('aliases');
93 * @param array $args $_REQUEST data (unused)
97 function handle($args)
99 parent::handle($args);
101 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
103 // TRANS: Client error. POST is a HTTP command. It should not be translated.
104 _('This method requires a POST.'),
111 if (empty($this->user)) {
112 // TRANS: Client error given when a user was not found (404).
113 $this->clientError(_('No such user.'), 404, $this->format);
117 if ($this->validateParams() == false) {
121 $group = User_group::register(array('nickname' => $this->nickname,
122 'fullname' => $this->fullname,
123 'homepage' => $this->homepage,
124 'description' => $this->description,
125 'location' => $this->location,
126 'aliases' => $this->aliases,
127 'userid' => $this->user->id,
130 switch($this->format) {
132 $this->showSingleXmlGroup($group);
135 $this->showSingleJsonGroup($group);
139 // TRANS: Client error given when an API method was not found (404).
140 _('API method not found.'),
149 * Validate params for the new group
154 function validateParams()
156 $valid = Validate::string(
157 $this->nickname, array(
160 'format' => NICKNAME_FMT
166 // TRANS: Validation error in form for group creation.
168 'Nickname must have only lowercase letters ' .
169 'and numbers and no spaces.'
175 } elseif ($this->groupNicknameExists($this->nickname)) {
177 // TRANS: Client error trying to create a group with a nickname this is already in use.
178 _('Nickname already in use. Try another one.'),
183 } else if (!User_group::allowedNickname($this->nickname)) {
185 // TRANS: Client error in form for group creation.
186 _('Not a valid nickname.'),
193 !is_null($this->homepage)
194 && strlen($this->homepage) > 0
196 $this->homepage, array(
198 array('http', 'https')
202 // TRANS: Client error in form for group creation.
203 _('Homepage is not a valid URL.'),
209 !is_null($this->fullname)
210 && mb_strlen($this->fullname) > 255) {
212 // TRANS: Client error in form for group creation.
213 _('Full name is too long (maximum 255 characters).'),
218 } elseif (User_group::descriptionTooLong($this->description)) {
221 _('Description is too long (max %d chars).'),
222 User_group::maxDescription()
229 !is_null($this->location)
230 && mb_strlen($this->location) > 255) {
232 _('Location is too long (maximum 255 characters).'),
239 if (!empty($this->aliasstring)) {
240 $this->aliases = array_map(
241 'common_canonical_nickname',
242 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
245 $this->aliases = array();
248 if (count($this->aliases) > common_config('group', 'maxaliases')) {
251 _('Too many aliases! Maximum %d.'),
252 common_config('group', 'maxaliases')
260 foreach ($this->aliases as $alias) {
262 $valid = Validate::string(
266 'format' => NICKNAME_FMT
272 sprintf(_('Invalid alias: "%s".'), $alias),
278 if ($this->groupNicknameExists($alias)) {
281 _('Alias "%s" already in use. Try another one.'),
290 // XXX assumes alphanum nicknames
292 if (strcmp($alias, $this->nickname) == 0) {
294 _('Alias can\'t be the same as nickname.'),
302 // Evarything looks OK
308 * Check to see whether a nickname is already in use by a group
310 * @param String $nickname The nickname in question
312 * @return boolean true or false
315 function groupNicknameExists($nickname)
317 $local = Local_group::staticGet('nickname', $nickname);
319 if (!empty($local)) {
323 $alias = Group_alias::staticGet('alias', $nickname);
325 if (!empty($alias)) {