]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupcreate.php
Merge branch '0.9.x' into tinymce
[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
53 class ApiGroupCreateAction extends ApiAuthAction
54 {
55     var $group       = null;
56     var $nickname    = null;
57     var $fullname    = null;
58     var $homepage    = null;
59     var $description = null;
60     var $location    = null;
61     var $aliasstring = null;
62     var $aliases     = null;
63
64     /**
65      * Take arguments for running
66      *
67      * @param array $args $_REQUEST args
68      *
69      * @return boolean success flag
70      *
71      */
72
73     function prepare($args)
74     {
75         parent::prepare($args);
76
77         $this->user  = $this->auth_user;
78
79         $this->nickname    = $this->arg('nickname');
80         $this->fullname    = $this->arg('full_name');
81         $this->homepage    = $this->arg('homepage');
82         $this->description = $this->arg('description');
83         $this->location    = $this->arg('location');
84         $this->aliasstring = $this->arg('aliases');
85
86         return true;
87     }
88
89     /**
90      * Handle the request
91      *
92      * Save the new group
93      *
94      * @param array $args $_REQUEST data (unused)
95      *
96      * @return void
97      */
98
99     function handle($args)
100     {
101         parent::handle($args);
102
103         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
104              $this->clientError(
105                  _('This method requires a POST.'),
106                  400,
107                  $this->format
108              );
109              return;
110         }
111
112         if (empty($this->user)) {
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                 _('API method not found.'),
140                 404,
141                 $this->format
142             );
143             break;
144         }
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                 _(
167                     'Nickname must have only lowercase letters ' .
168                     'and numbers and no spaces.'
169                 ),
170                 403,
171                 $this->format
172             );
173             return false;
174         } elseif ($this->groupNicknameExists($this->nickname)) {
175             $this->clientError(
176                 _('Nickname already in use. Try another one.'),
177                 403,
178                 $this->format
179             );
180             return false;
181         } else if (!User_group::allowedNickname($this->nickname)) {
182             $this->clientError(
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                 _('Homepage is not a valid URL.'),
200                 403,
201                 $this->format
202             );
203             return false;
204         } elseif (
205             !is_null($this->fullname)
206             && mb_strlen($this->fullname) > 255) {
207                 $this->clientError(
208                     _('Full name is too long (max 255 chars).'),
209                     403,
210                     $this->format
211                 );
212             return false;
213         } elseif (User_group::descriptionTooLong($this->description)) {
214             $this->clientError(
215                 sprintf(
216                     _('Description is too long (max %d chars).'),
217                     User_group::maxDescription()
218                 ),
219                 403,
220                 $this->format
221             );
222             return false;
223         } elseif (
224             !is_null($this->location)
225             && mb_strlen($this->location) > 255) {
226                 $this->clientError(
227                     _('Location is too long (max 255 chars).'),
228                     403,
229                     $this->format
230                 );
231             return false;
232         }
233
234         if (!empty($this->aliasstring)) {
235             $this->aliases = array_map(
236                 'common_canonical_nickname',
237                 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
238             );
239         } else {
240             $this->aliases = array();
241         }
242
243         if (count($this->aliases) > common_config('group', 'maxaliases')) {
244             $this->clientError(
245                 sprintf(
246                     _('Too many aliases! Maximum %d.'),
247                     common_config('group', 'maxaliases')
248                 ),
249                 403,
250                 $this->format
251             );
252             return false;
253         }
254
255         foreach ($this->aliases as $alias) {
256
257             $valid = Validate::string(
258                 $alias, array(
259                     'min_length' => 1,
260                     'max_length' => 64,
261                     'format' => NICKNAME_FMT
262                 )
263             );
264
265             if (!$valid) {
266                 $this->clientError(
267                     sprintf(_('Invalid alias: "%s".'), $alias),
268                     403,
269                     $this->format
270                 );
271                 return false;
272             }
273             if ($this->groupNicknameExists($alias)) {
274                 $this->clientError(
275                     sprintf(
276                         _('Alias "%s" already in use. Try another one.'),
277                         $alias
278                     ),
279                     403,
280                     $this->format
281                 );
282                 return false;
283             }
284
285             // XXX assumes alphanum nicknames
286
287             if (strcmp($alias, $this->nickname) == 0) {
288                 $this->clientError(
289                     _('Alias can\'t be the same as nickname.'),
290                     403,
291                     $this->format
292                 );
293                 return false;
294             }
295         }
296
297         // Evarything looks OK
298
299         return true;
300     }
301
302     /**
303      * Check to see whether a nickname is already in use by a group
304      *
305      * @param String $nickname The nickname in question
306      *
307      * @return boolean true or false
308      */
309
310     function groupNicknameExists($nickname)
311     {
312         $local = Local_group::staticGet('nickname', $nickname);
313
314         if (!empty($local)) {
315             return true;
316         }
317
318         $alias = Group_alias::staticGet('alias', $nickname);
319
320         if (!empty($alias)) {
321             return true;
322         }
323
324         return false;
325     }
326
327 }