]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
Proper definition of $args array in NewgroupAction->prepare
[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')) {
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 class NewgroupAction extends FormAction
47 {
48     function title()
49     {
50         // TRANS: Title for form to create a group.
51         return _('New group');
52     }
53
54     /**
55      * Prepare to run
56      */
57     protected function prepare(array $args=array())
58     {
59         parent::prepare($args);
60
61         if (!common_logged_in()) {
62             // TRANS: Client error displayed trying to create a group while not logged in.
63             $this->clientError(_('You must be logged in to create a group.'), 403);
64         }
65
66         // $this->scoped is the current user profile
67         if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
68             // TRANS: Client exception thrown when a user tries to create a group while banned.
69             $this->clientError(_('You are not allowed to create groups on this site.'), 403);
70         }
71
72         return true;
73     }
74
75     public function showContent()
76     {
77         $form = new GroupEditForm($this);
78         $form->show();
79     }
80
81     public function showInstructions()
82     {
83         $this->element('p', 'instructions',
84                        // TRANS: Form instructions for group create form.
85                        _('Use this form to create a new group.'));
86     }
87
88     protected function handlePost()
89     {
90         parent::handlePost();
91
92         if (Event::handle('StartGroupSaveForm', array($this))) {
93             $nickname = Nickname::normalize($this->trimmed('newnickname'));
94
95             $fullname    = $this->trimmed('fullname');
96             $homepage    = $this->trimmed('homepage');
97             $description = $this->trimmed('description');
98             $location    = $this->trimmed('location');
99             $private     = $this->boolean('private');
100             $aliasstring = $this->trimmed('aliases');
101
102             if ($this->nicknameExists($nickname)) {
103                 // TRANS: Group create form validation error.
104                 throw new ClientException(_('Nickname already in use. Try another one.'));
105             } else if (!User_group::allowedNickname($nickname)) {
106                 // TRANS: Group create form validation error.
107                 throw new ClientException(_('Not a valid nickname.'));
108             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
109                        !Validate::uri($homepage,
110                                       array('allowed_schemes' =>
111                                             array('http', 'https')))) {
112                 // TRANS: Group create form validation error.
113                 throw new ClientException(_('Homepage is not a valid URL.'));
114             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
115                 // TRANS: Group create form validation error.
116                 throw new ClientException(_('Full name is too long (maximum 255 characters).'));
117             } else if (User_group::descriptionTooLong($description)) {
118                 // TRANS: Group create form validation error.
119                 // TRANS: %d is the maximum number of allowed characters.
120                 throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
121                                            'Description is too long (maximum %d characters).',
122                                            User_group::maxDescription()),
123                                         User_group::maxDescription()));
124             } else if (!is_null($location) && mb_strlen($location) > 255) {
125                 // TRANS: Group create form validation error.
126                 throw new ClientException(_('Location is too long (maximum 255 characters).'));
127             }
128
129             if (!empty($aliasstring)) {
130                 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
131             } else {
132                 $aliases = array();
133             }
134
135             if (count($aliases) > common_config('group', 'maxaliases')) {
136                 // TRANS: Group create form validation error.
137                 // TRANS: %d is the maximum number of allowed aliases.
138                 throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
139                                            'Too many aliases! Maximum %d allowed.',
140                                            common_config('group', 'maxaliases')),
141                                         common_config('group', 'maxaliases')));
142                 return;
143             }
144
145             foreach ($aliases as $alias) {
146                 if (!Nickname::isValid($alias)) {
147                     // TRANS: Group create form validation error.
148                     // TRANS: %s is the invalid alias.
149                     throw new ClientException(sprintf(_('Invalid alias: "%s"'), $alias));
150                 }
151                 if ($this->nicknameExists($alias)) {
152                     // TRANS: Group create form validation error. %s is the already used alias.
153                     throw new ClientException(sprintf(_('Alias "%s" already in use. Try another one.'),
154                                             $alias));
155                 }
156                 // XXX assumes alphanum nicknames
157                 if (strcmp($alias, $nickname) == 0) {
158                     // TRANS: Group create form validation error.
159                     throw new ClientException(_('Alias cannot be the same as nickname.'));
160                 }
161             }
162
163             if ($private) {
164                 $force_scope = 1;
165                 $join_policy = User_group::JOIN_POLICY_MODERATE;
166             } else {
167                 $force_scope = 0;
168                 $join_policy = User_group::JOIN_POLICY_OPEN;
169             }
170
171             // This is set up in parent->prepare and checked in self->prepare
172             assert(!is_null($this->scoped));
173
174             $group = User_group::register(array('nickname' => $nickname,
175                                                 'fullname' => $fullname,
176                                                 'homepage' => $homepage,
177                                                 'description' => $description,
178                                                 'location' => $location,
179                                                 'aliases'  => $aliases,
180                                                 'userid'   => $this->scoped->id,
181                                                 'join_policy' => $join_policy,
182                                                 'force_scope' => $force_scope,
183                                                 'local'    => true));
184
185             $this->group = $group;
186
187             Event::handle('EndGroupSaveForm', array($this));
188
189             common_redirect($group->homeUrl(), 303);
190         }
191     }
192
193     function nicknameExists($nickname)
194     {
195         $local = Local_group::getKV('nickname', $nickname);
196
197         if (!empty($local)) {
198             return true;
199         }
200
201         $alias = Group_alias::getKV('alias', $nickname);
202
203         if (!empty($alias)) {
204             return true;
205         }
206
207         return false;
208     }
209 }