]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupcreate.php
Merge branch 'nofollowexternallink' 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      */
71
72     function prepare($args)
73     {
74         parent::prepare($args);
75
76         $this->user  = $this->auth_user;
77
78         $this->nickname    = $this->arg('nickname');
79         $this->fullname    = $this->arg('full_name');
80         $this->homepage    = $this->arg('homepage');
81         $this->description = $this->arg('description');
82         $this->location    = $this->arg('location');
83         $this->aliasstring = $this->arg('aliases');
84
85         return true;
86     }
87
88     /**
89      * Handle the request
90      *
91      * Save the new group
92      *
93      * @param array $args $_REQUEST data (unused)
94      *
95      * @return void
96      */
97     function handle($args)
98     {
99         parent::handle($args);
100
101         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
102              $this->clientError(
103                  // TRANS: Client error. POST is a HTTP command. It should not be translated.
104                  _('This method requires a POST.'),
105                  400,
106                  $this->format
107              );
108              return;
109         }
110
111         if (empty($this->user)) {
112             // TRANS: Client error given when a user was not found (404).
113             $this->clientError(_('No such user.'), 404, $this->format);
114             return;
115         }
116
117         if ($this->validateParams() == false) {
118             return;
119         }
120
121         $group = User_group::register(array('nickname' => $this->nickname,
122                                             'fullname' => $this->fullname,
123                                             'homepage' => $this->homepage,
124                                             'description' => $this->description,
125                                             'location' => $this->location,
126                                             'aliases'  => $this->aliases,
127                                             'userid'   => $this->user->id,
128                                             'local'    => true));
129
130         switch($this->format) {
131         case 'xml':
132             $this->showSingleXmlGroup($group);
133             break;
134         case 'json':
135             $this->showSingleJsonGroup($group);
136             break;
137         default:
138             $this->clientError(
139                 // TRANS: Client error given when an API method was not found (404).
140                 _('API method not found.'),
141                 404,
142                 $this->format
143             );
144             break;
145         }
146     }
147
148     /**
149      * Validate params for the new group
150      *
151      * @return void
152      */
153
154     function validateParams()
155     {
156         $valid = Validate::string(
157             $this->nickname, array(
158                 'min_length' => 1,
159                 'max_length' => 64,
160                 'format' => NICKNAME_FMT
161             )
162         );
163
164         if (!$valid) {
165             $this->clientError(
166                 // TRANS: Validation error in form for group creation.
167                 _(
168                     'Nickname must have only lowercase letters ' .
169                     'and numbers and no spaces.'
170                 ),
171                 403,
172                 $this->format
173             );
174             return false;
175         } elseif ($this->groupNicknameExists($this->nickname)) {
176             $this->clientError(
177                 // TRANS: Client error trying to create a group with a nickname this is already in use.
178                 _('Nickname already in use. Try another one.'),
179                 403,
180                 $this->format
181             );
182             return false;
183         } else if (!User_group::allowedNickname($this->nickname)) {
184             $this->clientError(
185                 // TRANS: Client error in form for group creation.
186                 _('Not a valid nickname.'),
187                 403,
188                 $this->format
189             );
190             return false;
191
192         } elseif (
193             !is_null($this->homepage)
194             && strlen($this->homepage) > 0
195             && !Validate::uri(
196                 $this->homepage, array(
197                     'allowed_schemes' =>
198                     array('http', 'https')
199                 )
200             )) {
201             $this->clientError(
202                 // TRANS: Client error in form for group creation.
203                 _('Homepage is not a valid URL.'),
204                 403,
205                 $this->format
206             );
207             return false;
208         } elseif (
209             !is_null($this->fullname)
210             && mb_strlen($this->fullname) > 255) {
211                 $this->clientError(
212                     // TRANS: Client error in form for group creation.
213                     _('Full name is too long (maximum 255 characters).'),
214                     403,
215                     $this->format
216                 );
217             return false;
218         } elseif (User_group::descriptionTooLong($this->description)) {
219             $this->clientError(
220                 sprintf(
221                     _('Description is too long (max %d chars).'),
222                     User_group::maxDescription()
223                 ),
224                 403,
225                 $this->format
226             );
227             return false;
228         } elseif (
229             !is_null($this->location)
230             && mb_strlen($this->location) > 255) {
231                 $this->clientError(
232                     _('Location is too long (maximum 255 characters).'),
233                     403,
234                     $this->format
235                 );
236             return false;
237         }
238
239         if (!empty($this->aliasstring)) {
240             $this->aliases = array_map(
241                 'common_canonical_nickname',
242                 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
243             );
244         } else {
245             $this->aliases = array();
246         }
247
248         if (count($this->aliases) > common_config('group', 'maxaliases')) {
249             $this->clientError(
250                 sprintf(
251                     _('Too many aliases! Maximum %d.'),
252                     common_config('group', 'maxaliases')
253                 ),
254                 403,
255                 $this->format
256             );
257             return false;
258         }
259
260         foreach ($this->aliases as $alias) {
261
262             $valid = Validate::string(
263                 $alias, array(
264                     'min_length' => 1,
265                     'max_length' => 64,
266                     'format' => NICKNAME_FMT
267                 )
268             );
269
270             if (!$valid) {
271                 $this->clientError(
272                     sprintf(_('Invalid alias: "%s".'), $alias),
273                     403,
274                     $this->format
275                 );
276                 return false;
277             }
278             if ($this->groupNicknameExists($alias)) {
279                 $this->clientError(
280                     sprintf(
281                         _('Alias "%s" already in use. Try another one.'),
282                         $alias
283                     ),
284                     403,
285                     $this->format
286                 );
287                 return false;
288             }
289
290             // XXX assumes alphanum nicknames
291
292             if (strcmp($alias, $this->nickname) == 0) {
293                 $this->clientError(
294                     _('Alias can\'t be the same as nickname.'),
295                     403,
296                     $this->format
297                 );
298                 return false;
299             }
300         }
301
302         // Evarything looks OK
303
304         return true;
305     }
306
307     /**
308      * Check to see whether a nickname is already in use by a group
309      *
310      * @param String $nickname The nickname in question
311      *
312      * @return boolean true or false
313      */
314
315     function groupNicknameExists($nickname)
316     {
317         $local = Local_group::staticGet('nickname', $nickname);
318
319         if (!empty($local)) {
320             return true;
321         }
322
323         $alias = Group_alias::staticGet('alias', $nickname);
324
325         if (!empty($alias)) {
326             return true;
327         }
328
329         return false;
330     }
331
332 }