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/
53 class ApiGroupCreateAction extends ApiAuthAction
59 var $description = null;
61 var $aliasstring = null;
65 * Take arguments for running
67 * @param array $args $_REQUEST args
69 * @return boolean success flag
73 function prepare($args)
75 parent::prepare($args);
77 $this->user = $this->auth_user;
79 $this->nickname = $this->arg('nickname');
80 $this->fullname = $this->arg('full_name');
81 $this->homepage = $this->arg('homepage');
82 $this->description = $this->arg('description');
83 $this->location = $this->arg('location');
84 $this->aliasstring = $this->arg('aliases');
94 * @param array $args $_REQUEST data (unused)
99 function handle($args)
101 parent::handle($args);
103 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
105 _('This method requires a POST.'),
112 if (empty($this->user)) {
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 _('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
167 'Nickname must have only lowercase letters ' .
168 'and numbers and no spaces.'
174 } elseif ($this->groupNicknameExists($this->nickname)) {
176 _('Nickname already in use. Try another one.'),
181 } else if (!User_group::allowedNickname($this->nickname)) {
183 _('Not a valid nickname.'),
190 !is_null($this->homepage)
191 && strlen($this->homepage) > 0
193 $this->homepage, array(
195 array('http', 'https')
199 _('Homepage is not a valid URL.'),
205 !is_null($this->fullname)
206 && mb_strlen($this->fullname) > 255) {
208 _('Full name is too long (max 255 chars).'),
213 } elseif (User_group::descriptionTooLong($this->description)) {
216 _('Description is too long (max %d chars).'),
217 User_group::maxDescription()
224 !is_null($this->location)
225 && mb_strlen($this->location) > 255) {
227 _('Location is too long (max 255 chars).'),
234 if (!empty($this->aliasstring)) {
235 $this->aliases = array_map(
236 'common_canonical_nickname',
237 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
240 $this->aliases = array();
243 if (count($this->aliases) > common_config('group', 'maxaliases')) {
246 _('Too many aliases! Maximum %d.'),
247 common_config('group', 'maxaliases')
255 foreach ($this->aliases as $alias) {
257 $valid = Validate::string(
261 'format' => NICKNAME_FMT
267 sprintf(_('Invalid alias: "%s".'), $alias),
273 if ($this->groupNicknameExists($alias)) {
276 _('Alias "%s" already in use. Try another one.'),
285 // XXX assumes alphanum nicknames
287 if (strcmp($alias, $this->nickname) == 0) {
289 _('Alias can\'t be the same as nickname.'),
297 // Evarything looks OK
303 * Check to see whether a nickname is already in use by a group
305 * @param String $nickname The nickname in question
307 * @return boolean true or false
310 function groupNicknameExists($nickname)
312 $local = Local_group::staticGet('nickname', $nickname);
314 if (!empty($local)) {
318 $alias = Group_alias::staticGet('alias', $nickname);
320 if (!empty($alias)) {