]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupprofileupdate.php
Making many of the API actions more consistent with coding style
[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     protected $needPost = true;
46     /**
47      * Take arguments for running
48      *
49      * @param array $args $_REQUEST args
50      *
51      * @return boolean success flag
52      *
53      */
54     protected function prepare($args)
55     {
56         parent::prepare($args);
57
58         $this->nickname    = common_canonical_nickname($this->trimmed('nickname'));
59
60         $this->fullname    = $this->trimmed('fullname');
61         $this->homepage    = $this->trimmed('homepage');
62         $this->description = $this->trimmed('description');
63         $this->location    = $this->trimmed('location');
64         $this->aliasstring = $this->trimmed('aliases');
65
66         $this->user  = $this->auth_user;
67         $this->group = $this->getTargetGroup($this->arg('id'));
68
69         return true;
70     }
71
72     /**
73      * Handle the request
74      *
75      * See which request params have been set, and update the profile
76      *
77      * @return void
78      */
79     protected function handle()
80     {
81         parent::handle();
82
83         if (!in_array($this->format, array('xml', 'json'))) {
84             // TRANS: Client error displayed when coming across a non-supported API method.
85             $this->clientError(_('API method not found.'), 404);
86         }
87
88         if (empty($this->user)) {
89             // TRANS: Client error displayed when not providing a user or an invalid user.
90             $this->clientError(_('No such user.'), 404);
91         }
92
93         if (empty($this->group)) {
94             // TRANS: Client error displayed when not providing a group or an invalid group.
95             $this->clientError(_('Group not found.'), 404);
96         }
97
98         if (!$this->user->isAdmin($this->group)) {
99             // TRANS: Client error displayed when trying to edit a group without being an admin.
100             $this->clientError(_('You must be an admin to edit the group.'), 403);
101         }
102
103         $this->group->query('BEGIN');
104
105         $orig = clone($this->group);
106
107         try {
108
109             if (!empty($this->nickname)) {
110                 if ($this->validateNickname()) {
111                     $this->group->nickname = $this->nickname;
112                     $this->group->mainpage = common_local_url(
113                         'showgroup',
114                         array('nickname' => $this->nickname)
115                     );
116                 }
117             }
118
119             if (!empty($this->fullname)) {
120                 $this->validateFullname();
121                 $this->group->fullname = $this->fullname;
122             }
123
124             if (!empty($this->homepage)) {
125                 $this->validateHomepage();
126                 $this->group->homepage = $this->hompage;
127             }
128
129             if (!empty($this->description)) {
130                 $this->validateDescription();
131                 $this->group->description = $this->decription;
132             }
133
134             if (!empty($this->location)) {
135                 $this->validateLocation();
136                 $this->group->location = $this->location;
137             }
138
139         } catch (ApiValidationException $ave) {
140             $this->clientError($ave->getMessage(), 403);
141         }
142
143         $result = $this->group->update($orig);
144
145         if (!$result) {
146             common_log_db_error($this->group, 'UPDATE', __FILE__);
147             // TRANS: Server error displayed when group update fails.
148             $this->serverError(_('Could not update group.'));
149         }
150
151         $aliases = array();
152
153         try {
154             if (!empty($this->aliasstring)) {
155                 $aliases = $this->validateAliases();
156             }
157
158         } catch (ApiValidationException $ave) {
159             $this->clientError($ave->getMessage(), 403);
160         }
161
162         $result = $this->group->setAliases($aliases);
163
164         if (!$result) {
165             // TRANS: Server error displayed when adding group aliases fails.
166             $this->serverError(_('Could not create aliases.'));
167         }
168
169         if (!empty($this->nickname) && ($this->nickname != $orig->nickname)) {
170             common_log(LOG_INFO, "Saving local group info.");
171             $local = Local_group::getKV('group_id', $this->group->id);
172             $local->setNickname($this->nickname);
173         }
174
175         $this->group->query('COMMIT');
176
177         switch($this->format) {
178         case 'xml':
179             $this->showSingleXmlGroup($this->group);
180             break;
181         case 'json':
182             $this->showSingleJsonGroup($this->group);
183             break;
184         default:
185             // TRANS: Client error displayed when coming across a non-supported API method.
186             $this->clientError(_('API method not found.'), 404);
187         }
188     }
189
190     function nicknameExists($nickname)
191     {
192         $group = Local_group::getKV('nickname', $nickname);
193
194         if (!empty($group) &&
195             $group->group_id != $this->group->id) {
196             return true;
197         }
198
199         $alias = Group_alias::getKV('alias', $nickname);
200
201         if (!empty($alias) &&
202             $alias->group_id != $this->group->id) {
203             return true;
204         }
205
206         return false;
207     }
208
209     function validateNickname()
210     {
211         if (!Validate::string(
212             $this->nickname, array(
213                 'min_length' => 1,
214                 'max_length' => 64,
215                 'format' => NICKNAME_FMT
216                 )
217             )
218         ) {
219             throw new ApiValidationException(
220                 // TRANS: API validation exception thrown when nickname does not validate.
221                 _('Nickname must have only lowercase letters and numbers and no spaces.')
222             );
223         } else if ($this->nicknameExists($this->nickname)) {
224             throw new ApiValidationException(
225                 // TRANS: API validation exception thrown when nickname is already used.
226                 _('Nickname already in use. Try another one.')
227             );
228         } else if (!User_group::allowedNickname($this->nickname)) {
229             throw new ApiValidationException(
230                 // TRANS: API validation exception thrown when nickname does not validate.
231                 _('Not a valid nickname.')
232             );
233         }
234
235                 return true;
236     }
237
238     function validateHomepage()
239     {
240         if (!is_null($this->homepage)
241                 && (strlen($this->homepage) > 0)
242                 && !common_valid_http_url($this->homepage)) {
243             throw new ApiValidationException(
244                 // TRANS: API validation exception thrown when homepage URL does not validate.
245                 _('Homepage is not a valid URL.')
246             );
247         }
248     }
249
250     function validateFullname()
251     {
252         if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
253             throw new ApiValidationException(
254                 // TRANS: API validation exception thrown when full name does not validate.
255                 _('Full name is too long (maximum 255 characters).')
256             );
257         }
258     }
259
260     function validateDescription()
261     {
262         if (User_group::descriptionTooLong($this->description)) {
263             // TRANS: API validation exception thrown when description does not validate.
264             // TRANS: %d is the maximum description length and used for plural.
265             throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
266                                                         'Description is too long (maximum %d characters).',
267                                                         User_group::maxDescription()),
268                                                      User_group::maxDescription()));
269         }
270     }
271
272     function validateLocation()
273     {
274         if (!is_null($this->location) && mb_strlen($this->location) > 255) {
275             throw new ApiValidationException(
276                 // TRANS: API validation exception thrown when location does not validate.
277                 _('Location is too long (maximum 255 characters).')
278             );
279         }
280     }
281
282     function validateAliases()
283     {
284         $aliases = array_map(
285             'common_canonical_nickname',
286             array_unique(
287                 preg_split('/[\s,]+/',
288                 $this->aliasstring
289                 )
290             )
291         );
292
293         if (count($aliases) > common_config('group', 'maxaliases')) {
294             // TRANS: API validation exception thrown when aliases do not validate.
295             // TRANS: %d is the maximum number of aliases and used for plural.
296             throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
297                                                         'Too many aliases! Maximum %d allowed.',
298                                                         common_config('group', 'maxaliases')),
299                                                      common_config('group', 'maxaliases')));
300         }
301
302         foreach ($aliases as $alias) {
303             if (!Validate::string(
304                 $alias, array(
305                     'min_length' => 1,
306                     'max_length' => 64,
307                     'format' => NICKNAME_FMT)
308                 )
309             ) {
310                 throw new ApiValidationException(
311                     sprintf(
312                         // TRANS: API validation exception thrown when aliases does not validate.
313                         // TRANS: %s is the invalid alias.
314                         _('Invalid alias: "%s".'),
315                         $alias
316                     )
317                 );
318             }
319
320             if ($this->nicknameExists($alias)) {
321                 throw new ApiValidationException(
322                     sprintf(
323                         // TRANS: API validation exception thrown when aliases is already used.
324                         // TRANS: %s is the already used alias.
325                         _('Alias "%s" already in use. Try another one.'),
326                         $alias)
327                 );
328             }
329
330             // XXX assumes alphanum nicknames
331             if (strcmp($alias, $this->nickname) == 0) {
332                 throw new ApiValidationException(
333                     // TRANS: API validation exception thrown when alias is the same as nickname.
334                     _('Alias cannot be the same as nickname.')
335                 );
336             }
337         }
338
339         return $aliases;
340     }
341 }