]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupprofileupdate.php
Merge branch '1.0.x' into people_tags_rebase
[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                 _('This method requires a POST.'),
89                 400, $this->format
90             );
91             return;
92         }
93
94         if (!in_array($this->format, array('xml', 'json'))) {
95             $this->clientError(
96                 // TRANS: Client error displayed when using an unsupported API format.
97                 _('API method not found.'),
98                 404,
99                 $this->format
100             );
101             return;
102         }
103
104         if (empty($this->user)) {
105             // TRANS: Client error displayed when not providing a user or an invalid user.
106             $this->clientError(_('No such user.'), 404, $this->format);
107             return;
108         }
109
110         if (empty($this->group)) {
111             // TRANS: Client error displayed when not providing a group or an invalid group.
112             $this->clientError(_('Group not found.'), 404, $this->format);
113             return false;
114         }
115
116         if (!$this->user->isAdmin($this->group)) {
117             // TRANS: Client error displayed when trying to edit a group without being an admin.
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             // TRANS: Server error displayed when group update fails.
172             $this->serverError(_('Could not update group.'));
173         }
174
175         $aliases = array();
176
177         try {
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             // TRANS: Server error displayed when adding group aliases fails.
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             // TRANS: Client error displayed when using an unsupported API format.
215             $this->clientError(_('API method not found.'), 404, $this->format);
216             break;
217         }
218     }
219
220     function nicknameExists($nickname)
221     {
222         $group = Local_group::staticGet('nickname', $nickname);
223
224         if (!empty($group) &&
225             $group->group_id != $this->group->id) {
226             return true;
227         }
228
229         $alias = Group_alias::staticGet('alias', $nickname);
230
231         if (!empty($alias) &&
232             $alias->group_id != $this->group->id) {
233             return true;
234         }
235
236         return false;
237     }
238
239     function validateNickname()
240     {
241         if (!Validate::string(
242             $this->nickname, array(
243                 'min_length' => 1,
244                 'max_length' => 64,
245                 'format' => NICKNAME_FMT
246                 )
247             )
248         ) {
249             throw new ApiValidationException(
250                 // TRANS: API validation exception thrown when nickname does not validate.
251                 _('Nickname must have only lowercase letters and numbers and no spaces.')
252             );
253         } else if ($this->nicknameExists($this->nickname)) {
254             throw new ApiValidationException(
255                 // TRANS: API validation exception thrown when nickname is already used.
256                 _('Nickname already in use. Try another one.')
257             );
258         } else if (!User_group::allowedNickname($this->nickname)) {
259             throw new ApiValidationException(
260                 // TRANS: API validation exception thrown when nickname does not validate.
261                 _('Not a valid nickname.')
262             );
263         }
264
265                 return true;
266     }
267
268     function validateHomepage()
269     {
270         if (!is_null($this->homepage)
271         && (strlen($this->homepage) > 0)
272         && !Validate::uri(
273                 $this->homepage,
274                 array('allowed_schemes' => array('http', 'https')
275                 )
276             )
277         ) {
278             throw new ApiValidationException(
279                 // TRANS: API validation exception thrown when homepage URL does not validate.
280                 _('Homepage is not a valid URL.')
281             );
282         }
283     }
284
285     function validateFullname()
286     {
287         if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
288             throw new ApiValidationException(
289                 // TRANS: API validation exception thrown when full name does not validate.
290                 _('Full name is too long (maximum 255 characters).')
291             );
292         }
293     }
294
295     function validateDescription()
296     {
297         if (User_group::descriptionTooLong($this->description)) {
298             // TRANS: API validation exception thrown when description does not validate.
299             // TRANS: %d is the maximum description length and used for plural.
300             throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
301                                                         'Description is too long (maximum %d characters).',
302                                                         User_group::maxDescription()),
303                                                      User_group::maxDescription()));
304         }
305     }
306
307     function validateLocation()
308     {
309         if (!is_null($this->location) && mb_strlen($this->location) > 255) {
310             throw new ApiValidationException(
311                 // TRANS: API validation exception thrown when location does not validate.
312                 _('Location is too long (maximum 255 characters).')
313             );
314         }
315     }
316
317     function validateAliases()
318     {
319         $aliases = array_map(
320             'common_canonical_nickname',
321             array_unique(
322                 preg_split('/[\s,]+/',
323                 $this->aliasstring
324                 )
325             )
326         );
327
328         if (count($aliases) > common_config('group', 'maxaliases')) {
329             // TRANS: API validation exception thrown when aliases do not validate.
330             // TRANS: %d is the maximum number of aliases and used for plural.
331             throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
332                                                         'Too many aliases! Maximum %d allowed.',
333                                                         common_config('group', 'maxaliases')),
334                                                      common_config('group', 'maxaliases')));
335         }
336
337         foreach ($aliases as $alias) {
338             if (!Validate::string(
339                 $alias, array(
340                     'min_length' => 1,
341                     'max_length' => 64,
342                     'format' => NICKNAME_FMT)
343                 )
344             ) {
345                 throw new ApiValidationException(
346                     sprintf(
347                         // TRANS: API validation exception thrown when aliases does not validate.
348                         // TRANS: %s is the invalid alias.
349                         _('Invalid alias: "%s".'),
350                         $alias
351                     )
352                 );
353             }
354
355             if ($this->nicknameExists($alias)) {
356                 throw new ApiValidationException(
357                     sprintf(
358                         // TRANS: API validation exception thrown when aliases is already used.
359                         // TRANS: %s is the already used alias.
360                         _('Alias "%s" already in use. Try another one.'),
361                         $alias)
362                 );
363             }
364
365             // XXX assumes alphanum nicknames
366             if (strcmp($alias, $this->nickname) == 0) {
367                 throw new ApiValidationException(
368                     // TRANS: API validation exception thrown when alias is the same as nickname.
369                     _('Alias cannot be the same as nickname.')
370                 );
371             }
372         }
373
374         return $aliases;
375     }
376 }