]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupcreate.php
6992da9db985c0a5b71f130e39b9aa2a62d4296f
[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 (
169             !is_null($this->homepage)
170             && strlen($this->homepage) > 0
171             && !Validate::uri(
172                 $this->homepage, array(
173                     'allowed_schemes' =>
174                     array('http', 'https')
175                 )
176             )) {
177             $this->clientError(
178                 // TRANS: Client error in form for group creation.
179                 _('Homepage is not a valid URL.'),
180                 403,
181                 $this->format
182             );
183             return false;
184         } elseif (
185             !is_null($this->fullname)
186             && mb_strlen($this->fullname) > 255) {
187                 $this->clientError(
188                     // TRANS: Client error in form for group creation.
189                     _('Full name is too long (maximum 255 characters).'),
190                     403,
191                     $this->format
192                 );
193             return false;
194         } elseif (User_group::descriptionTooLong($this->description)) {
195             $this->clientError(
196                 sprintf(
197                     // TRANS: Client error shown when providing too long a description during group creation.
198                     // TRANS: %d is the maximum number of allowed characters.
199                     _m('Description is too long (maximum %d character).',
200                       'Description is too long (maximum %d characters).',
201                       User_group::maxDescription()),
202                     User_group::maxDescription()
203                 ),
204                 403,
205                 $this->format
206             );
207             return false;
208         } elseif (
209             !is_null($this->location)
210             && mb_strlen($this->location) > 255) {
211                 $this->clientError(
212                     // TRANS: Client error shown when providing too long a location during group creation.
213                     _('Location is too long (maximum 255 characters).'),
214                     403,
215                     $this->format
216                 );
217             return false;
218         }
219
220         if (!empty($this->aliasstring)) {
221             $this->aliases = array_map(
222                 'common_canonical_nickname',
223                 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
224             );
225         } else {
226             $this->aliases = array();
227         }
228
229         if (count($this->aliases) > common_config('group', 'maxaliases')) {
230             $this->clientError(
231                 sprintf(
232                     // TRANS: Client error shown when providing too many aliases during group creation.
233                     // TRANS: %d is the maximum number of allowed aliases.
234                     _m('Too many aliases! Maximum %d allowed.',
235                        'Too many aliases! Maximum %d allowed.',
236                        common_config('group', 'maxaliases')),
237                     common_config('group', 'maxaliases')
238                 ),
239                 403,
240                 $this->format
241             );
242             return false;
243         }
244
245         foreach ($this->aliases as $alias) {
246
247             if (!Nickname::isValid($alias)) {
248                 $this->clientError(
249                     // TRANS: Client error shown when providing an invalid alias during group creation.
250                     // TRANS: %s is the invalid alias.
251                     sprintf(_('Invalid alias: "%s".'), $alias),
252                     403,
253                     $this->format
254                 );
255                 return false;
256             }
257             if ($this->groupNicknameExists($alias)) {
258                 $this->clientError(
259                     sprintf(
260                         // TRANS: Client error displayed when trying to use an alias during group creation that is already in use.
261                         // TRANS: %s is the alias that is already in use.
262                         _('Alias "%s" already in use. Try another one.'),
263                         $alias
264                     ),
265                     403,
266                     $this->format
267                 );
268                 return false;
269             }
270
271             // XXX assumes alphanum nicknames
272
273             if (strcmp($alias, $this->nickname) == 0) {
274                 $this->clientError(
275                     // TRANS: Client error displayed when trying to use an alias during group creation that is the same as the group's nickname.
276                     _('Alias can\'t be the same as nickname.'),
277                     403,
278                     $this->format
279                 );
280                 return false;
281             }
282         }
283
284         // Everything looks OK
285
286         return true;
287     }
288
289     /**
290      * Check to see whether a nickname is already in use by a group
291      *
292      * @param String $nickname The nickname in question
293      *
294      * @return boolean true or false
295      */
296     function groupNicknameExists($nickname)
297     {
298         $local = Local_group::getKV('nickname', $nickname);
299
300         if (!empty($local)) {
301             return true;
302         }
303
304         $alias = Group_alias::getKV('alias', $nickname);
305
306         if (!empty($alias)) {
307             return true;
308         }
309
310         return false;
311     }
312 }