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