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';
37 * API analog to the group edit page
41 * @author Zach Copley <zach@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
46 class ApiGroupProfileUpdateAction extends ApiAuthAction
50 * Take arguments for running
52 * @param array $args $_REQUEST args
54 * @return boolean success flag
58 function prepare($args)
60 parent::prepare($args);
62 $this->nickname = common_canonical_nickname($this->trimmed('nickname'));
64 $this->fullname = $this->trimmed('fullname');
65 $this->homepage = $this->trimmed('homepage');
66 $this->description = $this->trimmed('description');
67 $this->location = $this->trimmed('location');
68 $this->aliasstring = $this->trimmed('aliases');
70 $this->user = $this->auth_user;
71 $this->group = $this->getTargetGroup($this->arg('id'));
79 * See which request params have been set, and update the profile
81 * @param array $args $_REQUEST data (unused)
86 function handle($args)
88 parent::handle($args);
90 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
92 _('This method requires a POST.'),
98 if (!in_array($this->format, array('xml', 'json'))) {
100 _('API method not found.'),
107 if (empty($this->user)) {
108 $this->clientError(_('No such user.'), 404, $this->format);
112 if (empty($this->group)) {
113 $this->clientError(_('Group not found.'), 404, $this->format);
117 if (!$this->user->isAdmin($this->group)) {
118 $this->clientError(_('You must be an admin to edit the group.'), 403);
122 $this->group->query('BEGIN');
124 $orig = clone($this->group);
128 if (!empty($this->nickname)) {
129 if ($this->validateNickname()) {
130 $this->group->nickname = $this->nickname;
131 $this->group->mainpage = common_local_url(
133 array('nickname' => $this->nickname)
138 if (!empty($this->fullname)) {
139 $this->validateFullname();
140 $this->group->fullname = $this->fullname;
143 if (!empty($this->homepage)) {
144 $this->validateHomepage();
145 $this->group->homepage = $this->hompage;
148 if (!empty($this->description)) {
149 $this->validateDescription();
150 $this->group->description = $this->decription;
153 if (!empty($this->location)) {
154 $this->validateLocation();
155 $this->group->location = $this->location;
158 } catch (ApiValidationException $ave) {
167 $result = $this->group->update($orig);
170 common_log_db_error($this->group, 'UPDATE', __FILE__);
171 $this->serverError(_('Could not update group.'));
178 if (!empty($this->aliasstring)) {
179 $aliases = $this->validateAliases();
182 } catch (ApiValidationException $ave) {
191 $result = $this->group->setAliases($aliases);
194 $this->serverError(_('Could not create aliases.'));
197 if (!empty($this->nickname) && ($this->nickname != $orig->nickname)) {
198 common_log(LOG_INFO, "Saving local group info.");
199 $local = Local_group::staticGet('group_id', $this->group->id);
200 $local->setNickname($this->nickname);
203 $this->group->query('COMMIT');
205 switch($this->format) {
207 $this->showSingleXmlGroup($this->group);
210 $this->showSingleJsonGroup($this->group);
213 $this->clientError(_('API method not found.'), 404, $this->format);
218 function nicknameExists($nickname)
220 $group = Local_group::staticGet('nickname', $nickname);
222 if (!empty($group) &&
223 $group->group_id != $this->group->id) {
227 $alias = Group_alias::staticGet('alias', $nickname);
229 if (!empty($alias) &&
230 $alias->group_id != $this->group->id) {
237 function validateNickname()
239 if (!Validate::string(
240 $this->nickname, array(
243 'format' => NICKNAME_FMT
247 throw new ApiValidationException(
249 'Nickname must have only lowercase letters ' .
250 'and numbers and no spaces.'
253 } else if ($this->nicknameExists($this->nickname)) {
254 throw new ApiValidationException(
255 _('Nickname already in use. Try another one.')
257 } else if (!User_group::allowedNickname($this->nickname)) {
258 throw new ApiValidationException(
259 _('Not a valid nickname.')
266 function validateHomepage()
268 if (!is_null($this->homepage)
269 && (strlen($this->homepage) > 0)
272 array('allowed_schemes' => array('http', 'https')
276 throw new ApiValidationException(
277 _('Homepage is not a valid URL.')
282 function validateFullname()
284 if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
285 throw new ApiValidationException(
286 _('Full name is too long (max 255 chars).')
291 function validateDescription()
293 if (User_group::descriptionTooLong($this->description)) {
294 throw new ApiValidationException(
296 _('description is too long (max %d chars).'),
297 User_group::maxDescription()
303 function validateLocation()
305 if (!is_null($this->location) && mb_strlen($this->location) > 255) {
306 throw new ApiValidationException(
307 _('Location is too long (max 255 chars).')
312 function validateAliases()
314 $aliases = array_map(
315 'common_canonical_nickname',
317 preg_split('/[\s,]+/',
323 if (count($aliases) > common_config('group', 'maxaliases')) {
324 throw new ApiValidationException(
326 _('Too many aliases! Maximum %d.'),
327 common_config('group', 'maxaliases')
332 foreach ($aliases as $alias) {
333 if (!Validate::string(
337 'format' => NICKNAME_FMT)
340 throw new ApiValidationException(
342 _('Invalid alias: "%s"'),
348 if ($this->nicknameExists($alias)) {
349 throw new ApiValidationException(
351 _('Alias "%s" already in use. Try another one.'),
356 // XXX assumes alphanum nicknames
357 if (strcmp($alias, $this->nickname) == 0) {
358 throw new ApiValidationException(
359 _('Alias can\'t be the same as nickname.')