]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupcreate.php
Removed plugin Google-Analytics as this is free/libre and decentralized
[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     protected $needPost = true;
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     protected function prepare(array $args=array())
71     {
72         parent::prepare($args);
73
74         $this->nickname    = Nickname::normalize($this->arg('nickname'), true);
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      * @return void
90      */
91     protected function handle()
92     {
93         parent::handle();
94
95         if (empty($this->user)) {
96             // TRANS: Client error given when a user was not found (404).
97             $this->clientError(_('No such user.'), 404);
98         }
99
100         if ($this->validateParams() == false) {
101             return;
102         }
103
104         $group = User_group::register(array('nickname' => $this->nickname,
105                                             'fullname' => $this->fullname,
106                                             'homepage' => $this->homepage,
107                                             'description' => $this->description,
108                                             'location' => $this->location,
109                                             'aliases'  => $this->aliases,
110                                             'userid'   => $this->user->id,
111                                             'local'    => true));
112
113         switch($this->format) {
114         case 'xml':
115             $this->showSingleXmlGroup($group);
116             break;
117         case 'json':
118             $this->showSingleJsonGroup($group);
119             break;
120         default:
121             // TRANS: Client error displayed when coming across a non-supported API method.
122             $this->clientError(_('API method not found.'), 404);
123         }
124     }
125
126     /**
127      * Validate params for the new group
128      *
129      * @return void
130      */
131     function validateParams()
132     {
133         if (!is_null($this->homepage)
134                 && strlen($this->homepage) > 0
135                 && !common_valid_http_url($this->homepage)) {
136             // TRANS: Client error in form for group creation.
137             $this->clientError(_('Homepage is not a valid URL.'), 403);
138
139         } elseif (!is_null($this->fullname)
140                 && mb_strlen($this->fullname) > 255) {
141             // TRANS: Client error in form for group creation.
142             $this->clientError(_('Full name is too long (maximum 255 characters).'), 403);
143
144         } elseif (User_group::descriptionTooLong($this->description)) {
145             // TRANS: Client error shown when providing too long a description during group creation.
146             // TRANS: %d is the maximum number of allowed characters.
147             $this->clientError(sprintf(_m('Description is too long (maximum %d character).',
148                                 'Description is too long (maximum %d characters).',
149                                 User_group::maxDescription()), User_group::maxDescription()), 403);
150
151         } elseif (!is_null($this->location)
152                 && mb_strlen($this->location) > 255) {
153             // TRANS: Client error shown when providing too long a location during group creation.
154             $this->clientError(_('Location is too long (maximum 255 characters).'), 403);
155         }
156
157         if (!empty($this->aliasstring)) {
158             $this->aliases = array_map(
159                 array('Nickname', 'normalize'), // static call to Nickname::normalize
160                 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
161             );
162         } else {
163             $this->aliases = array();
164         }
165
166         if (count($this->aliases) > common_config('group', 'maxaliases')) {
167             $this->clientError(sprintf(
168                     // TRANS: Client error shown when providing too many aliases during group creation.
169                     // TRANS: %d is the maximum number of allowed aliases.
170                     _m('Too many aliases! Maximum %d allowed.',
171                        'Too many aliases! Maximum %d allowed.',
172                        common_config('group', 'maxaliases')),
173                     common_config('group', 'maxaliases')),
174                 403);
175         }
176
177         // Everything looks OK
178
179         return true;
180     }
181 }