]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupcreate.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline 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  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 require_once INSTALLDIR . '/lib/apiauth.php';
38
39 /**
40  * Make a new group. Sets the authenticated user as the administrator of the group.
41  *
42  * @category API
43  * @package  StatusNet
44  * @author   Craig Andrews <candrews@integralblue.com>
45  * @author   Evan Prodromou <evan@status.net>
46  * @author   Jeffery To <jeffery.to@gmail.com>
47  * @author   Zach Copley <zach@status.net>
48  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49  * @link     http://status.net/
50  */
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
98     function handle($args)
99     {
100         parent::handle($args);
101
102         if (!common_config('inboxes', 'enabled')) {
103             $this->serverError(
104                 _('Inboxes must be enabled for groups to work'),
105                 400,
106                 $this->format
107             );
108             return false;
109         }
110
111         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
112              $this->clientError(
113                  _('This method requires a POST.'),
114                  400,
115                  $this->format
116              );
117              return;
118         }
119
120         if (empty($this->user)) {
121             $this->clientError(_('No such user!'), 404, $this->format);
122             return;
123         }
124
125         if ($this->validateParams() == false) {
126             return;
127         }
128
129         $group = new User_group();
130
131         $group->query('BEGIN');
132
133         $group->nickname    = $this->nickname;
134         $group->fullname    = $this->fullname;
135         $group->homepage    = $this->homepage;
136         $group->description = $this->description;
137         $group->location    = $this->location;
138         $group->created     = common_sql_now();
139
140         $result = $group->insert();
141
142         if (!$result) {
143             common_log_db_error($group, 'INSERT', __FILE__);
144             $this->serverError(
145                 _('Could not create group.'),
146                 500,
147                 $this->format
148             );
149             return;
150         }
151
152         $result = $group->setAliases($this->aliases);
153
154         if (!$result) {
155             $this->serverError(
156                 _('Could not create aliases.'),
157                 500,
158                 $this->format
159             );
160             return;
161         }
162
163         $member = new Group_member();
164
165         $member->group_id   = $group->id;
166         $member->profile_id = $this->user->id;
167         $member->is_admin   = 1;
168         $member->created    = $group->created;
169
170         $result = $member->insert();
171
172         if (!$result) {
173             common_log_db_error($member, 'INSERT', __FILE__);
174             $this->serverError(
175                 _('Could not set group membership.'),
176                 500,
177                 $this->format
178             );
179             return;
180         }
181
182         $group->query('COMMIT');
183
184         switch($this->format) {
185         case 'xml':
186             $this->showSingleXmlGroup($group);
187             break;
188         case 'json':
189             $this->showSingleJsonGroup($group);
190             break;
191         default:
192             $this->clientError(
193                 _('API method not found!'),
194                 404,
195                 $this->format
196             );
197             break;
198         }
199
200     }
201
202     /**
203      * Validate params for the new group
204      *
205      * @return void
206      */
207
208     function validateParams()
209     {
210         $valid = Validate::string(
211             $this->nickname, array(
212                 'min_length' => 1,
213                 'max_length' => 64,
214                 'format' => NICKNAME_FMT
215             )
216         );
217
218         if (!$valid) {
219             $this->clientError(
220                 _(
221                     'Nickname must have only lowercase letters ' .
222                     'and numbers and no spaces.'
223                 ),
224                 403,
225                 $this->format
226             );
227             return false;
228         } elseif ($this->groupNicknameExists($this->nickname)) {
229             $this->clientError(
230                 _('Nickname already in use. Try another one.'),
231                 403,
232                 $this->format
233             );
234             return false;
235         } else if (!User_group::allowedNickname($this->nickname)) {
236             $this->clientError(
237                 _('Not a valid nickname.'),
238                 403,
239                 $this->format
240             );
241             return false;
242
243         } elseif (
244             !is_null($this->homepage)
245             && strlen($this->homepage) > 0
246             && !Validate::uri(
247                 $this->homepage, array(
248                     'allowed_schemes' =>
249                     array('http', 'https')
250                 )
251             )) {
252             $this->clientError(
253                 _('Homepage is not a valid URL.'),
254                 403,
255                 $this->format
256             );
257             return false;
258         } elseif (
259             !is_null($this->fullname)
260             && mb_strlen($this->fullname) > 255) {
261                 $this->clientError(
262                     _('Full name is too long (max 255 chars).'),
263                     403,
264                     $this->format
265                 );
266             return false;
267         } elseif (User_group::descriptionTooLong($this->description)) {
268             $this->clientError(
269                 sprintf(
270                     _('Description is too long (max %d chars).'),
271                     User_group::maxDescription()
272                 ),
273                 403,
274                 $this->format
275             );
276             return false;
277         } elseif (
278             !is_null($this->location)
279             && mb_strlen($this->location) > 255) {
280                 $this->clientError(
281                     _('Location is too long (max 255 chars).'),
282                     403,
283                     $this->format
284                 );
285             return false;
286         }
287
288         if (!empty($this->aliasstring)) {
289             $this->aliases = array_map(
290                 'common_canonical_nickname',
291                 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
292             );
293         } else {
294             $this->aliases = array();
295         }
296
297         if (count($this->aliases) > common_config('group', 'maxaliases')) {
298             $this->clientError(
299                 sprintf(
300                     _('Too many aliases! Maximum %d.'),
301                     common_config('group', 'maxaliases')
302                 ),
303                 403,
304                 $this->format
305             );
306             return false;
307         }
308
309         foreach ($this->aliases as $alias) {
310
311             $valid = Validate::string(
312                 $alias, array(
313                     'min_length' => 1,
314                     'max_length' => 64,
315                     'format' => NICKNAME_FMT
316                 )
317             );
318
319             if (!$valid) {
320                 $this->clientError(
321                     sprintf(_('Invalid alias: "%s"'), $alias),
322                     403,
323                     $this->format
324                 );
325                 return false;
326             }
327             if ($this->groupNicknameExists($alias)) {
328                 $this->clientError(
329                     sprintf(
330                         _('Alias "%s" already in use. Try another one.'),
331                         $alias
332                     ),
333                     403,
334                     $this->format
335                 );
336                 return false;
337             }
338
339             // XXX assumes alphanum nicknames
340
341             if (strcmp($alias, $this->nickname) == 0) {
342                 $this->clientError(
343                     _('Alias can\'t be the same as nickname.'),
344                     403,
345                     $this->format
346                 );
347                 return false;
348             }
349         }
350
351         // Evarything looks OK
352
353         return true;
354     }
355
356     /**
357      * Check to see whether a nickname is already in use by a group
358      *
359      * @param String $nickname The nickname in question
360      *
361      * @return boolean true or false
362      */
363
364     function groupNicknameExists($nickname)
365     {
366         $group = User_group::staticGet('nickname', $nickname);
367
368         if (!empty($group)) {
369             return true;
370         }
371
372         $alias = Group_alias::staticGet('alias', $nickname);
373
374         if (!empty($alias)) {
375             return true;
376         }
377
378         return false;
379     }
380
381 }