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