]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
Prevent group creation by silenced users.
[quix0rs-gnu-social.git] / actions / newgroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Add a new group
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  Group
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Add a new group
37  *
38  * This is the form for adding a new group
39  *
40  * @category Group
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class NewgroupAction extends Action
48 {
49     var $msg;
50
51     function title()
52     {
53         return _('New group');
54     }
55
56     /**
57      * Prepare to run
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         if (!common_logged_in()) {
65             $this->clientError(_('You must be logged in to create a group.'));
66             return false;
67         }
68
69         $user = common_current_user();
70         $profile = $user->getProfile();
71         if (!$profile->hasRight(Right::CREATEGROUP)) {
72             // TRANS: Client exception thrown when a user tries to create a group while banned.
73             throw new ClientException(_('You are not allowed to create groups on this site.'), 403);
74         }
75
76         return true;
77     }
78
79     /**
80      * Handle the request
81      *
82      * On GET, show the form. On POST, try to save the group.
83      *
84      * @param array $args unused
85      *
86      * @return void
87      */
88
89     function handle($args)
90     {
91         parent::handle($args);
92         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
93             $this->trySave();
94         } else {
95             $this->showForm();
96         }
97     }
98
99     function showForm($msg=null)
100     {
101         $this->msg = $msg;
102         $this->showPage();
103     }
104
105     function showContent()
106     {
107         $form = new GroupEditForm($this);
108         $form->show();
109     }
110
111     function showPageNotice()
112     {
113         if ($this->msg) {
114             $this->element('p', 'error', $this->msg);
115         } else {
116             $this->element('p', 'instructions',
117                            _('Use this form to create a new group.'));
118         }
119     }
120
121     function trySave()
122     {
123         $nickname    = $this->trimmed('nickname');
124         $fullname    = $this->trimmed('fullname');
125         $homepage    = $this->trimmed('homepage');
126         $description = $this->trimmed('description');
127         $location    = $this->trimmed('location');
128         $aliasstring = $this->trimmed('aliases');
129
130         if (!Validate::string($nickname, array('min_length' => 1,
131                                                'max_length' => 64,
132                                                'format' => NICKNAME_FMT))) {
133             $this->showForm(_('Nickname must have only lowercase letters '.
134                               'and numbers and no spaces.'));
135             return;
136         } else if ($this->nicknameExists($nickname)) {
137             $this->showForm(_('Nickname already in use. Try another one.'));
138             return;
139         } else if (!User_group::allowedNickname($nickname)) {
140             $this->showForm(_('Not a valid nickname.'));
141             return;
142         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
143                    !Validate::uri($homepage,
144                                   array('allowed_schemes' =>
145                                         array('http', 'https')))) {
146             $this->showForm(_('Homepage is not a valid URL.'));
147             return;
148         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
149             $this->showForm(_('Full name is too long (maximum 255 characters).'));
150             return;
151         } else if (User_group::descriptionTooLong($description)) {
152             // TRANS: Form validation error creating a new group because the description is too long.
153             // TRANS: %d is the maximum number of allowed characters.
154             $this->showForm(sprintf(_m('Description is too long (maximum %d character).',
155                                        'Description is too long (maximum %d characters).',
156                                        User_group::maxDescription()),
157                                     User_group::maxDescription()));
158             return;
159         } else if (!is_null($location) && mb_strlen($location) > 255) {
160             $this->showForm(_('Location is too long (maximum 255 characters).'));
161             return;
162         }
163
164         if (!empty($aliasstring)) {
165             $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
166         } else {
167             $aliases = array();
168         }
169
170         if (count($aliases) > common_config('group', 'maxaliases')) {
171             // TRANS: Client error shown when providing too many aliases during group creation.
172             // TRANS: %d is the maximum number of allowed aliases.
173             $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
174                                        'Too many aliases! Maximum %d allowed.',
175                                        common_config('group', 'maxaliases')),
176                                     common_config('group', 'maxaliases')));
177             return;
178         }
179
180         foreach ($aliases as $alias) {
181             if (!Validate::string($alias, array('min_length' => 1,
182                                                 'max_length' => 64,
183                                                 'format' => NICKNAME_FMT))) {
184                 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
185                 return;
186             }
187             if ($this->nicknameExists($alias)) {
188                 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
189                                         $alias));
190                 return;
191             }
192             // XXX assumes alphanum nicknames
193             if (strcmp($alias, $nickname) == 0) {
194                 $this->showForm(_('Alias can\'t be the same as nickname.'));
195                 return;
196             }
197         }
198
199         $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
200
201         $cur = common_current_user();
202
203         // Checked in prepare() above
204
205         assert(!is_null($cur));
206
207         $group = User_group::register(array('nickname' => $nickname,
208                                             'fullname' => $fullname,
209                                             'homepage' => $homepage,
210                                             'description' => $description,
211                                             'location' => $location,
212                                             'aliases'  => $aliases,
213                                             'userid'   => $cur->id,
214                                             'mainpage' => $mainpage,
215                                             'local'    => true));
216
217         common_redirect($group->homeUrl(), 303);
218     }
219
220     function nicknameExists($nickname)
221     {
222         $local = Local_group::staticGet('nickname', $nickname);
223
224         if (!empty($local)) {
225             return true;
226         }
227
228         $alias = Group_alias::staticGet('alias', $nickname);
229
230         if (!empty($alias)) {
231             return true;
232         }
233
234         return false;
235     }
236 }
237