3 * StatusNet, the distributed open-source microblogging tool
5 * Update a group's profile
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 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/
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR . '/lib/apiauth.php';
36 class ApiValidationException extends Exception { }
39 * API analog to the group edit page
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class ApiGroupProfileUpdateAction extends ApiAuthAction
52 * Take arguments for running
54 * @param array $args $_REQUEST args
56 * @return boolean success flag
60 function prepare($args)
62 parent::prepare($args);
64 $this->nickname = common_canonical_nickname($this->trimmed('nickname'));
65 $this->fullname = $this->trimmed('fullname');
66 $this->homepage = $this->trimmed('homepage');
67 $this->description = $this->trimmed('description');
68 $this->location = $this->trimmed('location');
69 $this->aliasstring = $this->trimmed('aliases');
71 $this->user = $this->auth_user;
72 $this->group = $this->getTargetGroup($this->arg('id'));
80 * See which request params have been set, and update the profile
82 * @param array $args $_REQUEST data (unused)
87 function handle($args)
89 parent::handle($args);
91 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
93 _('This method requires a POST.'),
99 if (!in_array($this->format, array('xml', 'json'))) {
101 _('API method not found.'),
108 if (empty($this->user)) {
109 $this->clientError(_('No such user.'), 404, $this->format);
113 if (empty($this->group)) {
114 $this->clientError(_('Group not found.'), 404, $this->format);
118 if (!$this->user->isAdmin($this->group)) {
119 $this->clientError(_('You must be an admin to edit the group.'), 403);
123 $this->group->query('BEGIN');
125 $orig = clone($this->group);
129 if (!empty($this->nickname)) {
130 if ($this->validateNickname()) {
131 $this->group->nickname = $this->nickname;
132 $this->group->mainpage = common_local_url(
134 array('nickname' => $this->nickname)
139 if (!empty($this->fullname)) {
140 $this->validateFullname();
141 $this->group->fullname = $this->fullname;
144 if (!empty($this->homepage)) {
145 $this->validateHomepage();
146 $this->group->homepage = $this->hompage;
149 if (!empty($this->description)) {
150 $this->validateDescription();
151 $this->group->description = $this->decription;
154 if (!empty($this->location)) {
155 $this->validateLocation();
156 $this->group->location = $this->location;
159 } catch (ApiValidationException $ave) {
168 $result = $this->group->update($orig);
171 common_log_db_error($this->group, 'UPDATE', __FILE__);
172 $this->serverError(_('Could not update group.'));
179 if (!empty($this->aliasstring)) {
180 $aliases = $this->parseAliases();
183 } catch (ApiValidationException $ave) {
192 $result = $this->group->setAliases($aliases);
195 $this->serverError(_('Could not create aliases.'));
198 if (!empty($this->nickname) && $this->nickname != $orig->nickname) {
199 common_log(LOG_INFO, "Saving local group info.");
200 $local = Local_group::staticGet('group_id', $this->group->id);
201 $local->setNickname($this->nickname);
204 $this->group->query('COMMIT');
206 switch($this->format) {
208 $this->showSingleXmlGroup($this->group);
211 $this->showSingleJsonGroup($this->group);
214 $this->clientError(_('API method not found.'), 404, $this->format);
219 function nicknameExists($nickname)
221 $group = Local_group::staticGet('nickname', $nickname);
223 if (!empty($group) &&
224 $group->group_id != $this->group->id) {
228 $alias = Group_alias::staticGet('alias', $nickname);
230 if (!empty($alias) &&
231 $alias->group_id != $this->group->id) {
238 function validateNickname()
240 if (!Validate::string(
241 $this->nickname, array(
244 'format' => NICKNAME_FMT
248 throw new ApiValidationException(
250 'Nickname must have only lowercase letters ' .
251 'and numbers and no spaces.'
254 } else if ($this->nicknameExists($this->nickname)) {
255 throw new ApiValidationException(
256 _('Nickname already in use. Try another one.')
258 } else if (!User_group::allowedNickname($this->nickname)) {
259 throw new ApiValidationException(
260 _('Not a valid nickname.')
265 function validateHomepage()
267 if (!is_null($this->homepage)
268 && (strlen($this->homepage) > 0)
271 array('allowed_schemes' => array('http', 'https')
275 throw new ApiValidationException(
276 _('Homepage is not a valid URL.')
281 function validateFullname()
283 if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
284 throw new ApiValidationException(
285 _('Full name is too long (max 255 chars).')
290 function validateDescription()
292 if (User_group::descriptionTooLong($this->description)) {
293 throw new ApiValidationException(
295 _('description is too long (max %d chars).'),
296 User_group::maxDescription()
302 function validateLocation()
304 if (!is_null($this->location) && mb_strlen($this->location) > 255) {
305 throw new ApiValidationException(
306 _('Location is too long (max 255 chars).')
311 function validateAliases()
313 $aliases = array_map(
314 'common_canonical_nickname',
316 preg_split('/[\s,]+/',
322 if (empty($aliases)) {
326 if (count($aliases) > common_config('group', 'maxaliases')) {
327 throw new ApiValidationException(
329 _('Too many aliases! Maximum %d.'),
330 common_config('group', 'maxaliases')
335 foreach ($aliases as $alias) {
336 if (!Validate::string(
340 'format' => NICKNAME_FMT)
343 throw new ApiValidationException(
345 _('Invalid alias: "%s"'),
351 if ($this->nicknameExists($alias)) {
352 throw new ApiValidationException(
354 _('Alias "%s" already in use. Try another one.'),
359 // XXX assumes alphanum nicknames
360 if (strcmp($alias, $nickname) == 0) {
361 throw new ApiValidationException(
362 _('Alias can\'t be the same as nickname.')