]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
Merge branch 'master' of gitorious.org:statusnet/mainline into 0.9.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         return true;
70     }
71
72     /**
73      * Handle the request
74      *
75      * On GET, show the form. On POST, try to save the group.
76      *
77      * @param array $args unused
78      *
79      * @return void
80      */
81     function handle($args)
82     {
83         parent::handle($args);
84         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
85             $this->trySave();
86         } else {
87             $this->showForm();
88         }
89     }
90
91     function showForm($msg=null)
92     {
93         $this->msg = $msg;
94         $this->showPage();
95     }
96
97     function showContent()
98     {
99         $form = new GroupEditForm($this);
100         $form->show();
101     }
102
103     function showPageNotice()
104     {
105         if ($this->msg) {
106             $this->element('p', 'error', $this->msg);
107         } else {
108             $this->element('p', 'instructions',
109                            // TRANS: Form instructions for group create form.
110                            _('Use this form to create a new group.'));
111         }
112     }
113
114     function trySave()
115     {
116         $nickname    = $this->trimmed('nickname');
117         $fullname    = $this->trimmed('fullname');
118         $homepage    = $this->trimmed('homepage');
119         $description = $this->trimmed('description');
120         $location    = $this->trimmed('location');
121         $aliasstring = $this->trimmed('aliases');
122
123         if (!Validate::string($nickname, array('min_length' => 1,
124                                                'max_length' => 64,
125                                                'format' => NICKNAME_FMT))) {
126             // TRANS: Group create form validation error.
127             $this->showForm(_('Nickname must have only lowercase letters '.
128                               'and numbers and no spaces.'));
129             return;
130         } else if ($this->nicknameExists($nickname)) {
131             // TRANS: Group create form validation error.
132             $this->showForm(_('Nickname already in use. Try another one.'));
133             return;
134         } else if (!User_group::allowedNickname($nickname)) {
135             // TRANS: Group create form validation error.
136             $this->showForm(_('Not a valid nickname.'));
137             return;
138         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
139                    !Validate::uri($homepage,
140                                   array('allowed_schemes' =>
141                                         array('http', 'https')))) {
142             // TRANS: Group create form validation error.
143             $this->showForm(_('Homepage is not a valid URL.'));
144             return;
145         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
146             // TRANS: Group create form validation error.
147             $this->showForm(_('Full name is too long (maximum 255 characters).'));
148             return;
149         } else if (User_group::descriptionTooLong($description)) {
150             // TRANS: Group create form validation error.
151             // TRANS: %d is the maximum number of allowed characters.
152             $this->showForm(sprintf(_m('Description is too long (maximum %d character).',
153                                        'Description is too long (maximum %d characters).',
154                                        User_group::maxDescription()),
155                                     User_group::maxDescription()));
156             return;
157         } else if (!is_null($location) && mb_strlen($location) > 255) {
158             // TRANS: Group create form validation error.
159             $this->showForm(_('Location is too long (maximum 255 characters).'));
160             return;
161         }
162
163         if (!empty($aliasstring)) {
164             $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
165         } else {
166             $aliases = array();
167         }
168
169         if (count($aliases) > common_config('group', 'maxaliases')) {
170             // TRANS: Group create form validation error.
171             // TRANS: %d is the maximum number of allowed aliases.
172             $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
173                                        'Too many aliases! Maximum %d allowed.',
174                                        common_config('group', 'maxaliases')),
175                                     common_config('group', 'maxaliases')));
176             return;
177         }
178
179         foreach ($aliases as $alias) {
180             if (!Validate::string($alias, array('min_length' => 1,
181                                                 'max_length' => 64,
182                                                 'format' => NICKNAME_FMT))) {
183                 // TRANS: Group create form validation error.
184                 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
185                 return;
186             }
187             if ($this->nicknameExists($alias)) {
188                 // TRANS: Group create form validation error.
189                 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
190                                         $alias));
191                 return;
192             }
193             // XXX assumes alphanum nicknames
194             if (strcmp($alias, $nickname) == 0) {
195                 // TRANS: Group create form validation error.
196                 $this->showForm(_('Alias can\'t be the same as nickname.'));
197                 return;
198             }
199         }
200
201         $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
202
203         $cur = common_current_user();
204
205         // Checked in prepare() above
206
207         assert(!is_null($cur));
208
209         $group = User_group::register(array('nickname' => $nickname,
210                                             'fullname' => $fullname,
211                                             'homepage' => $homepage,
212                                             'description' => $description,
213                                             'location' => $location,
214                                             'aliases'  => $aliases,
215                                             'userid'   => $cur->id,
216                                             'mainpage' => $mainpage,
217                                             'local'    => true));
218
219         common_redirect($group->homeUrl(), 303);
220     }
221
222     function nicknameExists($nickname)
223     {
224         $local = Local_group::staticGet('nickname', $nickname);
225
226         if (!empty($local)) {
227             return true;
228         }
229
230         $alias = Group_alias::staticGet('alias', $nickname);
231
232         if (!empty($alias)) {
233             return true;
234         }
235
236         return false;
237     }
238 }