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