]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editgroup.php
Merge branch '0.8.x' of git://gitorious.org/~brion/statusnet/brion-fixes into 0.8.x
[quix0rs-gnu-social.git] / actions / editgroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Edit an existing 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  * @author   Zach Copley <zach@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
33     exit(1);
34 }
35
36 /**
37  * Add a new group
38  *
39  * This is the form for adding a new group
40  *
41  * @category Group
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @author   Zach Copley <zach@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48
49 class EditgroupAction extends GroupDesignAction
50 {
51
52     var $msg;
53
54     function title()
55     {
56         return sprintf(_('Edit %s group'), $this->group->nickname);
57     }
58
59     /**
60      * Prepare to run
61      */
62
63     function prepare($args)
64     {
65         parent::prepare($args);
66
67         if (!common_config('inboxes','enabled')) {
68             $this->serverError(_('Inboxes must be enabled for groups to work'));
69             return false;
70         }
71
72         if (!common_logged_in()) {
73             $this->clientError(_('You must be logged in to create a group.'));
74             return false;
75         }
76
77         $nickname_arg = $this->trimmed('nickname');
78         $nickname = common_canonical_nickname($nickname_arg);
79
80         // Permanent redirect on non-canonical nickname
81
82         if ($nickname_arg != $nickname) {
83             $args = array('nickname' => $nickname);
84             common_redirect(common_local_url('editgroup', $args), 301);
85             return false;
86         }
87
88         if (!$nickname) {
89             $this->clientError(_('No nickname'), 404);
90             return false;
91         }
92
93         $groupid = $this->trimmed('groupid');
94         if ($groupid) {
95             $this->group = User_group::staticGet('id', $groupid);
96         } else {
97             $this->group = User_group::staticGet('nickname', $nickname);
98         }
99
100         if (!$this->group) {
101             $this->clientError(_('No such group'), 404);
102             return false;
103         }
104
105         $cur = common_current_user();
106
107         if (!$cur->isAdmin($this->group)) {
108             $this->clientError(_('You must be an admin to edit the group'), 403);
109             return false;
110         }
111
112         return true;
113     }
114
115     /**
116      * Handle the request
117      *
118      * On GET, show the form. On POST, try to save the group.
119      *
120      * @param array $args unused
121      *
122      * @return void
123      */
124
125     function handle($args)
126     {
127         parent::handle($args);
128         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
129             $this->trySave();
130         } else {
131             $this->showForm();
132         }
133     }
134
135     function showForm($msg=null)
136     {
137         $this->msg = $msg;
138         $this->showPage();
139     }
140
141     function showLocalNav()
142     {
143         $nav = new GroupNav($this, $this->group);
144         $nav->show();
145     }
146
147     function showContent()
148     {
149         $form = new GroupEditForm($this, $this->group);
150         $form->show();
151     }
152
153     function showPageNotice()
154     {
155         if ($this->msg) {
156             $this->element('p', 'error', $this->msg);
157         } else {
158             $this->element('p', 'instructions',
159                            _('Use this form to edit the group.'));
160         }
161     }
162
163     function showScripts()
164     {
165         parent::showScripts();
166         $this->autofocus('nickname');
167     }
168
169     function trySave()
170     {
171         $cur = common_current_user();
172         if (!$cur->isAdmin($this->group)) {
173             $this->clientError(_('You must be an admin to edit the group'), 403);
174             return;
175         }
176
177         $nickname    = common_canonical_nickname($this->trimmed('nickname'));
178         $fullname    = $this->trimmed('fullname');
179         $homepage    = $this->trimmed('homepage');
180         $description = $this->trimmed('description');
181         $location    = $this->trimmed('location');
182         $aliasstring = $this->trimmed('aliases');
183
184         if (!Validate::string($nickname, array('min_length' => 1,
185                                                'max_length' => 64,
186                                                'format' => NICKNAME_FMT))) {
187             $this->showForm(_('Nickname must have only lowercase letters '.
188                               'and numbers and no spaces.'));
189             return;
190         } else if ($this->nicknameExists($nickname)) {
191             $this->showForm(_('Nickname already in use. Try another one.'));
192             return;
193         } else if (!User_group::allowedNickname($nickname)) {
194             $this->showForm(_('Not a valid nickname.'));
195             return;
196         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
197                    !Validate::uri($homepage,
198                                   array('allowed_schemes' =>
199                                         array('http', 'https')))) {
200             $this->showForm(_('Homepage is not a valid URL.'));
201             return;
202         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
203             $this->showForm(_('Full name is too long (max 255 chars).'));
204             return;
205         } else if (!is_null($description) && mb_strlen($description) > 140) {
206             $this->showForm(_('description is too long (max 140 chars).'));
207             return;
208         } else if (!is_null($location) && mb_strlen($location) > 255) {
209             $this->showForm(_('Location is too long (max 255 chars).'));
210             return;
211         }
212
213         if (!empty($aliasstring)) {
214             $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
215         } else {
216             $aliases = array();
217         }
218
219         if (count($aliases) > common_config('group', 'maxaliases')) {
220             $this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
221                                     common_config('group', 'maxaliases')));
222             return;
223         }
224
225         foreach ($aliases as $alias) {
226             if (!Validate::string($alias, array('min_length' => 1,
227                                                 'max_length' => 64,
228                                                 'format' => NICKNAME_FMT))) {
229                 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
230                 return;
231             }
232             if ($this->nicknameExists($alias)) {
233                 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
234                                         $alias));
235                 return;
236             }
237             // XXX assumes alphanum nicknames
238             if (strcmp($alias, $nickname) == 0) {
239                 $this->showForm(_('Alias can\'t be the same as nickname.'));
240                 return;
241             }
242         }
243
244         $this->group->query('BEGIN');
245
246         $orig = clone($this->group);
247
248         $this->group->nickname    = $nickname;
249         $this->group->fullname    = $fullname;
250         $this->group->homepage    = $homepage;
251         $this->group->description = $description;
252         $this->group->location    = $location;
253         $this->group->created     = common_sql_now();
254
255         $result = $this->group->update($orig);
256
257         if (!$result) {
258             common_log_db_error($this->group, 'UPDATE', __FILE__);
259             $this->serverError(_('Could not update group.'));
260         }
261
262         $result = $this->group->setAliases($aliases);
263
264         if (!$result) {
265             $this->serverError(_('Could not create aliases.'));
266         }
267
268         $this->group->query('COMMIT');
269
270         if ($this->group->nickname != $orig->nickname) {
271             common_redirect(common_local_url('editgroup',
272                                              array('nickname' => $nickname)),
273                             303);
274         } else {
275             $this->showForm(_('Options saved.'));
276         }
277     }
278
279     function nicknameExists($nickname)
280     {
281         $group = User_group::staticGet('nickname', $nickname);
282
283         if (!empty($group) &&
284             $group->id != $this->group->id) {
285             return true;
286         }
287
288         $alias = Group_alias::staticGet('alias', $nickname);
289
290         if (!empty($alias) &&
291             $alias->group_id != $this->group->id) {
292             return true;
293         }
294
295         return false;
296     }
297 }
298