]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
Merge from 1.0.x
[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 class NewgroupAction extends Action
47 {
48     var $msg;
49
50     function title()
51     {
52         // TRANS: Title for form to create a group.
53         return _('New group');
54     }
55
56     /**
57      * Prepare to run
58      */
59     function prepare($args)
60     {
61         parent::prepare($args);
62
63         if (!common_logged_in()) {
64             // TRANS: Client error displayed trying to create a group while not 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     function handle($args)
89     {
90         parent::handle($args);
91         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
92             $this->trySave();
93         } else {
94             $this->showForm();
95         }
96     }
97
98     function showForm($msg=null)
99     {
100         $this->msg = $msg;
101         $this->showPage();
102     }
103
104     function showContent()
105     {
106         $form = new GroupEditForm($this);
107         $form->show();
108     }
109
110     function showPageNotice()
111     {
112         if ($this->msg) {
113             $this->element('p', 'error', $this->msg);
114         } else {
115             $this->element('p', 'instructions',
116                            // TRANS: Form instructions for group create form.
117                            _('Use this form to create a new group.'));
118         }
119     }
120
121     function trySave()
122     {
123         if (Event::handle('StartGroupSaveForm', array($this))) {
124             try {
125                 $nickname = Nickname::normalize($this->trimmed('newnickname'));
126             } catch (NicknameException $e) {
127                 $this->showForm($e->getMessage());
128             }
129             $fullname    = $this->trimmed('fullname');
130             $homepage    = $this->trimmed('homepage');
131             $description = $this->trimmed('description');
132             $location    = $this->trimmed('location');
133             $private     = $this->boolean('private');
134             $aliasstring = $this->trimmed('aliases');
135
136             if ($this->nicknameExists($nickname)) {
137                 // TRANS: Group create form validation error.
138                 $this->showForm(_('Nickname already in use. Try another one.'));
139                 return;
140             } else if (!User_group::allowedNickname($nickname)) {
141                 // TRANS: Group create form validation error.
142                 $this->showForm(_('Not a valid nickname.'));
143                 return;
144             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
145                        !Validate::uri($homepage,
146                                       array('allowed_schemes' =>
147                                             array('http', 'https')))) {
148                 // TRANS: Group create form validation error.
149                 $this->showForm(_('Homepage is not a valid URL.'));
150                 return;
151             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
152                 // TRANS: Group create form validation error.
153                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
154                 return;
155             } else if (User_group::descriptionTooLong($description)) {
156                 // TRANS: Group create form validation error.
157                 // TRANS: %d is the maximum number of allowed characters.
158                 $this->showForm(sprintf(_m('Description is too long (maximum %d character).',
159                                            'Description is too long (maximum %d characters).',
160                                            User_group::maxDescription()),
161                                         User_group::maxDescription()));
162                 return;
163             } else if (!is_null($location) && mb_strlen($location) > 255) {
164                 // TRANS: Group create form validation error.
165                 $this->showForm(_('Location is too long (maximum 255 characters).'));
166                 return;
167             }
168
169             if (!empty($aliasstring)) {
170                 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
171             } else {
172                 $aliases = array();
173             }
174
175             if (count($aliases) > common_config('group', 'maxaliases')) {
176                 // TRANS: Group create form validation error.
177                 // TRANS: %d is the maximum number of allowed aliases.
178                 $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
179                                            'Too many aliases! Maximum %d allowed.',
180                                            common_config('group', 'maxaliases')),
181                                         common_config('group', 'maxaliases')));
182                 return;
183             }
184
185             foreach ($aliases as $alias) {
186                 if (!Nickname::isValid($alias)) {
187                     // TRANS: Group create form validation error.
188                     // TRANS: %s is the invalid alias.
189                     $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
190                     return;
191                 }
192                 if ($this->nicknameExists($alias)) {
193                     // TRANS: Group create form validation error. %s is the already used alias.
194                     $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
195                                             $alias));
196                     return;
197                 }
198                 // XXX assumes alphanum nicknames
199                 if (strcmp($alias, $nickname) == 0) {
200                     // TRANS: Group create form validation error.
201                     $this->showForm(_('Alias cannot be the same as nickname.'));
202                     return;
203                 }
204             }
205
206             if ($private) {
207                 $force_scope = 1;
208                 $join_policy = User_group::JOIN_POLICY_MODERATE;
209             } else {
210                 $force_scope = 0;
211                 $join_policy = User_group::JOIN_POLICY_OPEN;
212             }
213
214             $cur = common_current_user();
215
216             // Checked in prepare() above
217
218             assert(!is_null($cur));
219
220             $group = User_group::register(array('nickname' => $nickname,
221                                                 'fullname' => $fullname,
222                                                 'homepage' => $homepage,
223                                                 'description' => $description,
224                                                 'location' => $location,
225                                                 'aliases'  => $aliases,
226                                                 'userid'   => $cur->id,
227                                                 'join_policy' => $join_policy,
228                                                 'force_scope' => $force_scope,
229                                                 'local'    => true));
230
231             $this->group = $group;
232
233             Event::handle('EndGroupSaveForm', array($this));
234
235             common_redirect($group->homeUrl(), 303);
236         }
237     }
238
239     function nicknameExists($nickname)
240     {
241         $local = Local_group::staticGet('nickname', $nickname);
242
243         if (!empty($local)) {
244             return true;
245         }
246
247         $alias = Group_alias::staticGet('alias', $nickname);
248
249         if (!empty($alias)) {
250             return true;
251         }
252
253         return false;
254     }
255 }