]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupprofileupdate.php
Merge branch '1.0.x' into testing
[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 class ApiGroupProfileUpdateAction extends ApiAuthAction
46 {
47     /**
48      * Take arguments for running
49      *
50      * @param array $args $_REQUEST args
51      *
52      * @return boolean success flag
53      *
54      */
55     function prepare($args)
56     {
57         parent::prepare($args);
58
59         $this->nickname    = common_canonical_nickname($this->trimmed('nickname'));
60
61         $this->fullname    = $this->trimmed('fullname');
62         $this->homepage    = $this->trimmed('homepage');
63         $this->description = $this->trimmed('description');
64         $this->location    = $this->trimmed('location');
65         $this->aliasstring = $this->trimmed('aliases');
66
67         $this->user  = $this->auth_user;
68         $this->group = $this->getTargetGroup($this->arg('id'));
69
70         return true;
71     }
72
73     /**
74      * Handle the request
75      *
76      * See which request params have been set, and update the profile
77      *
78      * @param array $args $_REQUEST data (unused)
79      *
80      * @return void
81      */
82     function handle($args)
83     {
84         parent::handle($args);
85
86         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
87             $this->clientError(
88                 // TRANS: Client error message. POST is a HTTP command. It should not be translated.
89                 _('This method requires a POST.'),
90                 400, $this->format
91             );
92             return;
93         }
94
95         if (!in_array($this->format, array('xml', 'json'))) {
96             $this->clientError(
97                 // TRANS: Client error displayed when coming across a non-supported API method.
98                 _('API method not found.'),
99                 404,
100                 $this->format
101             );
102             return;
103         }
104
105         if (empty($this->user)) {
106             // TRANS: Client error displayed when not providing a user or an invalid user.
107             $this->clientError(_('No such user.'), 404, $this->format);
108             return;
109         }
110
111         if (empty($this->group)) {
112             // TRANS: Client error displayed when not providing a group or an invalid group.
113             $this->clientError(_('Group not found.'), 404, $this->format);
114             return false;
115         }
116
117         if (!$this->user->isAdmin($this->group)) {
118             // TRANS: Client error displayed when trying to edit a group without being an admin.
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             // TRANS: Server error displayed when group update fails.
173             $this->serverError(_('Could not update group.'));
174         }
175
176         $aliases = array();
177
178         try {
179             if (!empty($this->aliasstring)) {
180                 $aliases = $this->validateAliases();
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             // TRANS: Server error displayed when adding group aliases fails.
196             $this->serverError(_('Could not create aliases.'));
197         }
198
199         if (!empty($this->nickname) && ($this->nickname != $orig->nickname)) {
200             common_log(LOG_INFO, "Saving local group info.");
201             $local = Local_group::staticGet('group_id', $this->group->id);
202             $local->setNickname($this->nickname);
203         }
204
205         $this->group->query('COMMIT');
206
207         switch($this->format) {
208         case 'xml':
209             $this->showSingleXmlGroup($this->group);
210             break;
211         case 'json':
212             $this->showSingleJsonGroup($this->group);
213             break;
214         default:
215             // TRANS: Client error displayed when coming across a non-supported API method.
216             $this->clientError(_('API method not found.'), 404, $this->format);
217             break;
218         }
219     }
220
221     function nicknameExists($nickname)
222     {
223         $group = Local_group::staticGet('nickname', $nickname);
224
225         if (!empty($group) &&
226             $group->group_id != $this->group->id) {
227             return true;
228         }
229
230         $alias = Group_alias::staticGet('alias', $nickname);
231
232         if (!empty($alias) &&
233             $alias->group_id != $this->group->id) {
234             return true;
235         }
236
237         return false;
238     }
239
240     function validateNickname()
241     {
242         if (!Validate::string(
243             $this->nickname, array(
244                 'min_length' => 1,
245                 'max_length' => 64,
246                 'format' => NICKNAME_FMT
247                 )
248             )
249         ) {
250             throw new ApiValidationException(
251                 // TRANS: API validation exception thrown when nickname does not validate.
252                 _('Nickname must have only lowercase letters and numbers and no spaces.')
253             );
254         } else if ($this->nicknameExists($this->nickname)) {
255             throw new ApiValidationException(
256                 // TRANS: API validation exception thrown when nickname is already used.
257                 _('Nickname already in use. Try another one.')
258             );
259         } else if (!User_group::allowedNickname($this->nickname)) {
260             throw new ApiValidationException(
261                 // TRANS: API validation exception thrown when nickname does not validate.
262                 _('Not a valid nickname.')
263             );
264         }
265
266                 return true;
267     }
268
269     function validateHomepage()
270     {
271         if (!is_null($this->homepage)
272         && (strlen($this->homepage) > 0)
273         && !Validate::uri(
274                 $this->homepage,
275                 array('allowed_schemes' => array('http', 'https')
276                 )
277             )
278         ) {
279             throw new ApiValidationException(
280                 // TRANS: API validation exception thrown when homepage URL does not validate.
281                 _('Homepage is not a valid URL.')
282             );
283         }
284     }
285
286     function validateFullname()
287     {
288         if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
289             throw new ApiValidationException(
290                 // TRANS: API validation exception thrown when full name does not validate.
291                 _('Full name is too long (maximum 255 characters).')
292             );
293         }
294     }
295
296     function validateDescription()
297     {
298         if (User_group::descriptionTooLong($this->description)) {
299             // TRANS: API validation exception thrown when description does not validate.
300             // TRANS: %d is the maximum description length and used for plural.
301             throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
302                                                         'Description is too long (maximum %d characters).',
303                                                         User_group::maxDescription()),
304                                                      User_group::maxDescription()));
305         }
306     }
307
308     function validateLocation()
309     {
310         if (!is_null($this->location) && mb_strlen($this->location) > 255) {
311             throw new ApiValidationException(
312                 // TRANS: API validation exception thrown when location does not validate.
313                 _('Location is too long (maximum 255 characters).')
314             );
315         }
316     }
317
318     function validateAliases()
319     {
320         $aliases = array_map(
321             'common_canonical_nickname',
322             array_unique(
323                 preg_split('/[\s,]+/',
324                 $this->aliasstring
325                 )
326             )
327         );
328
329         if (count($aliases) > common_config('group', 'maxaliases')) {
330             // TRANS: API validation exception thrown when aliases do not validate.
331             // TRANS: %d is the maximum number of aliases and used for plural.
332             throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
333                                                         'Too many aliases! Maximum %d allowed.',
334                                                         common_config('group', 'maxaliases')),
335                                                      common_config('group', 'maxaliases')));
336         }
337
338         foreach ($aliases as $alias) {
339             if (!Validate::string(
340                 $alias, array(
341                     'min_length' => 1,
342                     'max_length' => 64,
343                     'format' => NICKNAME_FMT)
344                 )
345             ) {
346                 throw new ApiValidationException(
347                     sprintf(
348                         // TRANS: API validation exception thrown when aliases does not validate.
349                         // TRANS: %s is the invalid alias.
350                         _('Invalid alias: "%s".'),
351                         $alias
352                     )
353                 );
354             }
355
356             if ($this->nicknameExists($alias)) {
357                 throw new ApiValidationException(
358                     sprintf(
359                         // TRANS: API validation exception thrown when aliases is already used.
360                         // TRANS: %s is the already used alias.
361                         _('Alias "%s" already in use. Try another one.'),
362                         $alias)
363                 );
364             }
365
366             // XXX assumes alphanum nicknames
367             if (strcmp($alias, $this->nickname) == 0) {
368                 throw new ApiValidationException(
369                     // TRANS: API validation exception thrown when alias is the same as nickname.
370                     _('Alias cannot be the same as nickname.')
371                 );
372             }
373         }
374
375         return $aliases;
376     }
377 }