]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupprofileupdate.php
Initial work on API method for updating a group's profile info
[quix0rs-gnu-social.git] / actions / apigroupprofileupdate.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Update a group's profile
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    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/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/apiauth.php';
35
36 class ApiValidationException extends Exception { }
37
38 /**
39  * API analog to the group edit page
40  *
41  * @category API
42  * @package  StatusNet
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/
46  */
47
48 class ApiGroupProfileUpdateAction extends ApiAuthAction
49 {
50
51     /**
52      * Take arguments for running
53      *
54      * @param array $args $_REQUEST args
55      *
56      * @return boolean success flag
57      *
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
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');
70
71         $this->user  = $this->auth_user;
72         $this->group = $this->getTargetGroup($this->arg('id'));
73
74         return true;
75     }
76
77     /**
78      * Handle the request
79      *
80      * See which request params have been set, and update the profile
81      *
82      * @param array $args $_REQUEST data (unused)
83      *
84      * @return void
85      */
86
87     function handle($args)
88     {
89         parent::handle($args);
90
91         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
92             $this->clientError(
93                 _('This method requires a POST.'),
94                 400, $this->format
95             );
96             return;
97         }
98
99         if (!in_array($this->format, array('xml', 'json'))) {
100             $this->clientError(
101                 _('API method not found.'),
102                 404,
103                 $this->format
104             );
105             return;
106         }
107
108         if (empty($this->user)) {
109             $this->clientError(_('No such user.'), 404, $this->format);
110             return;
111         }
112
113         if (empty($this->group)) {
114             $this->clientError(_('Group not found.'), 404, $this->format);
115             return false;
116         }
117
118         if (!$this->user->isAdmin($this->group)) {
119             $this->clientError(_('You must be an admin to edit the group.'), 403);
120             return false;
121         }
122
123         $this->group->query('BEGIN');
124
125         $orig = clone($this->group);
126
127         try {
128
129             if (!empty($this->nickname)) {
130                 if ($this->validateNickname()) {
131                     $this->group->nickname = $this->nickname;
132                     $this->group->mainpage = common_local_url(
133                         'showgroup',
134                         array('nickname' => $this->nickname)
135                     );
136                 }
137             }
138
139             if (!empty($this->fullname)) {
140                 $this->validateFullname();
141                 $this->group->fullname = $this->fullname;
142             }
143
144             if (!empty($this->homepage)) {
145                 $this->validateHomepage();
146                 $this->group->homepage = $this->hompage;
147             }
148
149             if (!empty($this->description)) {
150                 $this->validateDescription();
151                 $this->group->description = $this->decription;
152             }
153
154             if (!empty($this->location)) {
155                 $this->validateLocation();
156                 $this->group->location = $this->location;
157             }
158
159         } catch (ApiValidationException $ave) {
160             $this->clientError(
161                 $ave->getMessage(),
162                 403,
163                 $this->format
164             );
165             return;
166         }
167
168         $result = $this->group->update($orig);
169
170         if (!$result) {
171             common_log_db_error($this->group, 'UPDATE', __FILE__);
172             $this->serverError(_('Could not update group.'));
173         }
174
175         $aliases = null;
176
177         try {
178
179             if (!empty($this->aliasstring)) {
180                 $aliases = $this->parseAliases();
181             }
182
183         } catch (ApiValidationException $ave) {
184             $this->clientError(
185                 $ave->getMessage(),
186                 403,
187                 $this->format
188             );
189             return;
190         }
191
192         $result = $this->group->setAliases($aliases);
193
194         if (!$result) {
195             $this->serverError(_('Could not create aliases.'));
196         }
197
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);
202         }
203
204         $this->group->query('COMMIT');
205
206         switch($this->format) {
207         case 'xml':
208             $this->showSingleXmlGroup($this->group);
209             break;
210         case 'json':
211             $this->showSingleJsonGroup($this->group);
212             break;
213         default:
214             $this->clientError(_('API method not found.'), 404, $this->format);
215             break;
216         }
217     }
218
219     function nicknameExists($nickname)
220     {
221         $group = Local_group::staticGet('nickname', $nickname);
222
223         if (!empty($group) &&
224             $group->group_id != $this->group->id) {
225             return true;
226         }
227
228         $alias = Group_alias::staticGet('alias', $nickname);
229
230         if (!empty($alias) &&
231             $alias->group_id != $this->group->id) {
232             return true;
233         }
234
235         return false;
236     }
237
238     function validateNickname()
239     {
240         if (!Validate::string(
241             $this->nickname, array(
242                 'min_length' => 1,
243                 'max_length' => 64,
244                 'format' => NICKNAME_FMT
245                 )
246             )
247         ) {
248             throw new ApiValidationException(
249                 _(
250                     'Nickname must have only lowercase letters ' .
251                     'and numbers and no spaces.'
252                 )
253             );
254         } else if ($this->nicknameExists($this->nickname)) {
255             throw new ApiValidationException(
256                 _('Nickname already in use. Try another one.')
257             );
258         } else if (!User_group::allowedNickname($this->nickname)) {
259             throw new ApiValidationException(
260                 _('Not a valid nickname.')
261             );
262         }
263     }
264
265     function validateHomepage()
266     {
267         if (!is_null($this->homepage)
268         && (strlen($this->homepage) > 0)
269         && !Validate::uri(
270                 $this->homepage,
271                 array('allowed_schemes' => array('http', 'https')
272                 )
273             )
274         ) {
275             throw new ApiValidationException(
276                 _('Homepage is not a valid URL.')
277             );
278         }
279     }
280
281     function validateFullname()
282     {
283         if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
284             throw new ApiValidationException(
285                 _('Full name is too long (max 255 chars).')
286             );
287         }
288     }
289
290     function validateDescription()
291     {
292         if (User_group::descriptionTooLong($this->description)) {
293             throw new ApiValidationException(
294                 sprintf(
295                     _('description is too long (max %d chars).'),
296                     User_group::maxDescription()
297                 )
298             );
299         }
300     }
301
302     function validateLocation()
303     {
304         if (!is_null($this->location) && mb_strlen($this->location) > 255) {
305             throw new ApiValidationException(
306                 _('Location is too long (max 255 chars).')
307             );
308         }
309     }
310
311     function validateAliases()
312     {
313         $aliases = array_map(
314             'common_canonical_nickname',
315             array_unique(
316                 preg_split('/[\s,]+/',
317                 $this->aliasstring
318                 )
319             )
320         );
321
322         if (empty($aliases)) {
323             $aliases = array();
324         }
325
326         if (count($aliases) > common_config('group', 'maxaliases')) {
327             throw new ApiValidationException(
328                 sprintf(
329                     _('Too many aliases! Maximum %d.'),
330                     common_config('group', 'maxaliases')
331                 )
332             );
333         }
334
335         foreach ($aliases as $alias) {
336             if (!Validate::string(
337                 $alias, array(
338                     'min_length' => 1,
339                     'max_length' => 64,
340                     'format' => NICKNAME_FMT)
341                 )
342             ) {
343                 throw new ApiValidationException(
344                     sprintf(
345                         _('Invalid alias: "%s"'),
346                         $alias
347                     )
348                 );
349             }
350
351             if ($this->nicknameExists($alias)) {
352                 throw new ApiValidationException(
353                     sprintf(
354                         _('Alias "%s" already in use. Try another one.'),
355                         $alias)
356                 );
357             }
358
359             // XXX assumes alphanum nicknames
360             if (strcmp($alias, $nickname) == 0) {
361                 throw new ApiValidationException(
362                     _('Alias can\'t be the same as nickname.')
363                 );
364             }
365         }
366
367         return $aliases;
368     }
369
370 }