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