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