]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupcreate.php
Merge branch 'master' into 0.9.x
[quix0rs-gnu-social.git] / actions / apigroupcreate.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Create a group via the API
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    Craig Andrews <candrews@integralblue.com>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Jeffery To <jeffery.to@gmail.com>
27  * @author    Zach Copley <zach@status.net>
28  * @copyright 2009 StatusNet, Inc.
29  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
31  * @link      http://status.net/
32  */
33
34 if (!defined('STATUSNET')) {
35     exit(1);
36 }
37
38 require_once INSTALLDIR . '/lib/apiauth.php';
39
40 /**
41  * Make a new group. Sets the authenticated user as the administrator of the group.
42  *
43  * @category API
44  * @package  StatusNet
45  * @author   Craig Andrews <candrews@integralblue.com>
46  * @author   Evan Prodromou <evan@status.net>
47  * @author   Jeffery To <jeffery.to@gmail.com>
48  * @author   Zach Copley <zach@status.net>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://status.net/
51  */
52 class ApiGroupCreateAction extends ApiAuthAction
53 {
54     var $group       = null;
55     var $nickname    = null;
56     var $fullname    = null;
57     var $homepage    = null;
58     var $description = null;
59     var $location    = null;
60     var $aliasstring = null;
61     var $aliases     = null;
62
63     /**
64      * Take arguments for running
65      *
66      * @param array $args $_REQUEST args
67      *
68      * @return boolean success flag
69      */
70     function prepare($args)
71     {
72         parent::prepare($args);
73
74         $this->user  = $this->auth_user;
75
76         $this->nickname    = $this->arg('nickname');
77         $this->fullname    = $this->arg('full_name');
78         $this->homepage    = $this->arg('homepage');
79         $this->description = $this->arg('description');
80         $this->location    = $this->arg('location');
81         $this->aliasstring = $this->arg('aliases');
82
83         return true;
84     }
85
86     /**
87      * Handle the request
88      *
89      * Save the new group
90      *
91      * @param array $args $_REQUEST data (unused)
92      *
93      * @return void
94      */
95     function handle($args)
96     {
97         parent::handle($args);
98
99         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
100              $this->clientError(
101                  // TRANS: Client error. POST is a HTTP command. It should not be translated.
102                  _('This method requires a POST.'),
103                  400,
104                  $this->format
105              );
106              return;
107         }
108
109         if (empty($this->user)) {
110             // TRANS: Client error given when a user was not found (404).
111             $this->clientError(_('No such user.'), 404, $this->format);
112             return;
113         }
114
115         if ($this->validateParams() == false) {
116             return;
117         }
118
119         $group = User_group::register(array('nickname' => $this->nickname,
120                                             'fullname' => $this->fullname,
121                                             'homepage' => $this->homepage,
122                                             'description' => $this->description,
123                                             'location' => $this->location,
124                                             'aliases'  => $this->aliases,
125                                             'userid'   => $this->user->id,
126                                             'local'    => true));
127
128         switch($this->format) {
129         case 'xml':
130             $this->showSingleXmlGroup($group);
131             break;
132         case 'json':
133             $this->showSingleJsonGroup($group);
134             break;
135         default:
136             $this->clientError(
137                 // TRANS: Client error given when an API method was not found (404).
138                 _('API method not found.'),
139                 404,
140                 $this->format
141             );
142             break;
143         }
144     }
145
146     /**
147      * Validate params for the new group
148      *
149      * @return void
150      */
151     function validateParams()
152     {
153         $valid = Validate::string(
154             $this->nickname, array(
155                 'min_length' => 1,
156                 'max_length' => 64,
157                 'format' => NICKNAME_FMT
158             )
159         );
160
161         if (!$valid) {
162             $this->clientError(
163                 // TRANS: Validation error in form for group creation.
164                 _(
165                     'Nickname must have only lowercase letters ' .
166                     'and numbers and no spaces.'
167                 ),
168                 403,
169                 $this->format
170             );
171             return false;
172         } elseif ($this->groupNicknameExists($this->nickname)) {
173             $this->clientError(
174                 // TRANS: Client error trying to create a group with a nickname this is already in use.
175                 _('Nickname already in use. Try another one.'),
176                 403,
177                 $this->format
178             );
179             return false;
180         } else if (!User_group::allowedNickname($this->nickname)) {
181             $this->clientError(
182                 // TRANS: Client error in form for group creation.
183                 _('Not a valid nickname.'),
184                 403,
185                 $this->format
186             );
187             return false;
188
189         } elseif (
190             !is_null($this->homepage)
191             && strlen($this->homepage) > 0
192             && !Validate::uri(
193                 $this->homepage, array(
194                     'allowed_schemes' =>
195                     array('http', 'https')
196                 )
197             )) {
198             $this->clientError(
199                 // TRANS: Client error in form for group creation.
200                 _('Homepage is not a valid URL.'),
201                 403,
202                 $this->format
203             );
204             return false;
205         } elseif (
206             !is_null($this->fullname)
207             && mb_strlen($this->fullname) > 255) {
208                 $this->clientError(
209                     // TRANS: Client error in form for group creation.
210                     _('Full name is too long (maximum 255 characters).'),
211                     403,
212                     $this->format
213                 );
214             return false;
215         } elseif (User_group::descriptionTooLong($this->description)) {
216             $this->clientError(
217                 sprintf(
218                     // TRANS: Client error shown when providing too long a description during group creation.
219                     // TRANS: %d is the maximum number of allowed characters.
220                     _m('Description is too long (maximum %d character).',
221                       'Description is too long (maximum %d characters).',
222                       User_group::maxDescription()),
223                     User_group::maxDescription()
224                 ),
225                 403,
226                 $this->format
227             );
228             return false;
229         } elseif (
230             !is_null($this->location)
231             && mb_strlen($this->location) > 255) {
232                 $this->clientError(
233                     // TRANS: Client error shown when providing too long a location during group creation.
234                     _('Location is too long (maximum 255 characters).'),
235                     403,
236                     $this->format
237                 );
238             return false;
239         }
240
241         if (!empty($this->aliasstring)) {
242             $this->aliases = array_map(
243                 'common_canonical_nickname',
244                 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
245             );
246         } else {
247             $this->aliases = array();
248         }
249
250         if (count($this->aliases) > common_config('group', 'maxaliases')) {
251             $this->clientError(
252                 sprintf(
253                     // TRANS: Client error shown when providing too many aliases during group creation.
254                     // TRANS: %d is the maximum number of allowed aliases.
255                     _m('Too many aliases! Maximum %d allowed.',
256                        'Too many aliases! Maximum %d allowed.',
257                        common_config('group', 'maxaliases')),
258                     common_config('group', 'maxaliases')
259                 ),
260                 403,
261                 $this->format
262             );
263             return false;
264         }
265
266         foreach ($this->aliases as $alias) {
267
268             $valid = Validate::string(
269                 $alias, array(
270                     'min_length' => 1,
271                     'max_length' => 64,
272                     'format' => NICKNAME_FMT
273                 )
274             );
275
276             if (!$valid) {
277                 $this->clientError(
278                     // TRANS: Client error shown when providing an invalid alias during group creation.
279                     // TRANS: %s is the invalid alias.
280                     sprintf(_('Invalid alias: "%s".'), $alias),
281                     403,
282                     $this->format
283                 );
284                 return false;
285             }
286             if ($this->groupNicknameExists($alias)) {
287                 $this->clientError(
288                     sprintf(
289                         // TRANS: Client error displayed when trying to use an alias during group creation that is already in use.
290                         // TRANS: %s is the alias that is already in use.
291                         _('Alias "%s" already in use. Try another one.'),
292                         $alias
293                     ),
294                     403,
295                     $this->format
296                 );
297                 return false;
298             }
299
300             // XXX assumes alphanum nicknames
301
302             if (strcmp($alias, $this->nickname) == 0) {
303                 $this->clientError(
304                     // TRANS: Client error displayed when trying to use an alias during group creation that is the same as the group's nickname.
305                     _('Alias can\'t be the same as nickname.'),
306                     403,
307                     $this->format
308                 );
309                 return false;
310             }
311         }
312
313         // Everything looks OK
314
315         return true;
316     }
317
318     /**
319      * Check to see whether a nickname is already in use by a group
320      *
321      * @param String $nickname The nickname in question
322      *
323      * @return boolean true or false
324      */
325     function groupNicknameExists($nickname)
326     {
327         $local = Local_group::staticGet('nickname', $nickname);
328
329         if (!empty($local)) {
330             return true;
331         }
332
333         $alias = Group_alias::staticGet('alias', $nickname);
334
335         if (!empty($alias)) {
336             return true;
337         }
338
339         return false;
340     }
341 }