]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupprofileupdate.php
Validate::uri replaced with filter_var for HTTP[S] URL checks
[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 /**
35  * API analog to the group edit page
36  *
37  * @category API
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43 class ApiGroupProfileUpdateAction extends ApiAuthAction
44 {
45     /**
46      * Take arguments for running
47      *
48      * @param array $args $_REQUEST args
49      *
50      * @return boolean success flag
51      *
52      */
53     function prepare($args)
54     {
55         parent::prepare($args);
56
57         $this->nickname    = common_canonical_nickname($this->trimmed('nickname'));
58
59         $this->fullname    = $this->trimmed('fullname');
60         $this->homepage    = $this->trimmed('homepage');
61         $this->description = $this->trimmed('description');
62         $this->location    = $this->trimmed('location');
63         $this->aliasstring = $this->trimmed('aliases');
64
65         $this->user  = $this->auth_user;
66         $this->group = $this->getTargetGroup($this->arg('id'));
67
68         return true;
69     }
70
71     /**
72      * Handle the request
73      *
74      * See which request params have been set, and update the profile
75      *
76      * @param array $args $_REQUEST data (unused)
77      *
78      * @return void
79      */
80     function handle($args)
81     {
82         parent::handle($args);
83
84         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
85             $this->clientError(
86                 // TRANS: Client error message. POST is a HTTP command. It should not be translated.
87                 _('This method requires a POST.'),
88                 400, $this->format
89             );
90             return;
91         }
92
93         if (!in_array($this->format, array('xml', 'json'))) {
94             $this->clientError(
95                 // TRANS: Client error displayed when coming across a non-supported API method.
96                 _('API method not found.'),
97                 404,
98                 $this->format
99             );
100             return;
101         }
102
103         if (empty($this->user)) {
104             // TRANS: Client error displayed when not providing a user or an invalid user.
105             $this->clientError(_('No such user.'), 404, $this->format);
106             return;
107         }
108
109         if (empty($this->group)) {
110             // TRANS: Client error displayed when not providing a group or an invalid group.
111             $this->clientError(_('Group not found.'), 404, $this->format);
112             return false;
113         }
114
115         if (!$this->user->isAdmin($this->group)) {
116             // TRANS: Client error displayed when trying to edit a group without being an admin.
117             $this->clientError(_('You must be an admin to edit the group.'), 403);
118             return false;
119         }
120
121         $this->group->query('BEGIN');
122
123         $orig = clone($this->group);
124
125         try {
126
127             if (!empty($this->nickname)) {
128                 if ($this->validateNickname()) {
129                     $this->group->nickname = $this->nickname;
130                     $this->group->mainpage = common_local_url(
131                         'showgroup',
132                         array('nickname' => $this->nickname)
133                     );
134                 }
135             }
136
137             if (!empty($this->fullname)) {
138                 $this->validateFullname();
139                 $this->group->fullname = $this->fullname;
140             }
141
142             if (!empty($this->homepage)) {
143                 $this->validateHomepage();
144                 $this->group->homepage = $this->hompage;
145             }
146
147             if (!empty($this->description)) {
148                 $this->validateDescription();
149                 $this->group->description = $this->decription;
150             }
151
152             if (!empty($this->location)) {
153                 $this->validateLocation();
154                 $this->group->location = $this->location;
155             }
156
157         } catch (ApiValidationException $ave) {
158             $this->clientError(
159                 $ave->getMessage(),
160                 403,
161                 $this->format
162             );
163             return;
164         }
165
166         $result = $this->group->update($orig);
167
168         if (!$result) {
169             common_log_db_error($this->group, 'UPDATE', __FILE__);
170             // TRANS: Server error displayed when group update fails.
171             $this->serverError(_('Could not update group.'));
172         }
173
174         $aliases = array();
175
176         try {
177             if (!empty($this->aliasstring)) {
178                 $aliases = $this->validateAliases();
179             }
180
181         } catch (ApiValidationException $ave) {
182             $this->clientError(
183                 $ave->getMessage(),
184                 403,
185                 $this->format
186             );
187             return;
188         }
189
190         $result = $this->group->setAliases($aliases);
191
192         if (!$result) {
193             // TRANS: Server error displayed when adding group aliases fails.
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::getKV('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             // TRANS: Client error displayed when coming across a non-supported API method.
214             $this->clientError(_('API method not found.'), 404, $this->format);
215             break;
216         }
217     }
218
219     function nicknameExists($nickname)
220     {
221         $group = Local_group::getKV('nickname', $nickname);
222
223         if (!empty($group) &&
224             $group->group_id != $this->group->id) {
225             return true;
226         }
227
228         $alias = Group_alias::getKV('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                 // TRANS: API validation exception thrown when nickname does not validate.
250                 _('Nickname must have only lowercase letters and numbers and no spaces.')
251             );
252         } else if ($this->nicknameExists($this->nickname)) {
253             throw new ApiValidationException(
254                 // TRANS: API validation exception thrown when nickname is already used.
255                 _('Nickname already in use. Try another one.')
256             );
257         } else if (!User_group::allowedNickname($this->nickname)) {
258             throw new ApiValidationException(
259                 // TRANS: API validation exception thrown when nickname does not validate.
260                 _('Not a valid nickname.')
261             );
262         }
263
264                 return true;
265     }
266
267     function validateHomepage()
268     {
269         if (!is_null($this->homepage)
270                 && (strlen($this->homepage) > 0)
271                 && !common_valid_http_url($this->homepage)) {
272             throw new ApiValidationException(
273                 // TRANS: API validation exception thrown when homepage URL does not validate.
274                 _('Homepage is not a valid URL.')
275             );
276         }
277     }
278
279     function validateFullname()
280     {
281         if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
282             throw new ApiValidationException(
283                 // TRANS: API validation exception thrown when full name does not validate.
284                 _('Full name is too long (maximum 255 characters).')
285             );
286         }
287     }
288
289     function validateDescription()
290     {
291         if (User_group::descriptionTooLong($this->description)) {
292             // TRANS: API validation exception thrown when description does not validate.
293             // TRANS: %d is the maximum description length and used for plural.
294             throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
295                                                         'Description is too long (maximum %d characters).',
296                                                         User_group::maxDescription()),
297                                                      User_group::maxDescription()));
298         }
299     }
300
301     function validateLocation()
302     {
303         if (!is_null($this->location) && mb_strlen($this->location) > 255) {
304             throw new ApiValidationException(
305                 // TRANS: API validation exception thrown when location does not validate.
306                 _('Location is too long (maximum 255 characters).')
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 (count($aliases) > common_config('group', 'maxaliases')) {
323             // TRANS: API validation exception thrown when aliases do not validate.
324             // TRANS: %d is the maximum number of aliases and used for plural.
325             throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
326                                                         'Too many aliases! Maximum %d allowed.',
327                                                         common_config('group', 'maxaliases')),
328                                                      common_config('group', 'maxaliases')));
329         }
330
331         foreach ($aliases as $alias) {
332             if (!Validate::string(
333                 $alias, array(
334                     'min_length' => 1,
335                     'max_length' => 64,
336                     'format' => NICKNAME_FMT)
337                 )
338             ) {
339                 throw new ApiValidationException(
340                     sprintf(
341                         // TRANS: API validation exception thrown when aliases does not validate.
342                         // TRANS: %s is the invalid alias.
343                         _('Invalid alias: "%s".'),
344                         $alias
345                     )
346                 );
347             }
348
349             if ($this->nicknameExists($alias)) {
350                 throw new ApiValidationException(
351                     sprintf(
352                         // TRANS: API validation exception thrown when aliases is already used.
353                         // TRANS: %s is the already used alias.
354                         _('Alias "%s" already in use. Try another one.'),
355                         $alias)
356                 );
357             }
358
359             // XXX assumes alphanum nicknames
360             if (strcmp($alias, $this->nickname) == 0) {
361                 throw new ApiValidationException(
362                     // TRANS: API validation exception thrown when alias is the same as nickname.
363                     _('Alias cannot be the same as nickname.')
364                 );
365             }
366         }
367
368         return $aliases;
369     }
370 }