]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editgroup.php
Merge branch 'nofollowexternallink' into 0.9.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_logged_in()) {
68             $this->clientError(_('You must be logged in to create a group.'));
69             return false;
70         }
71
72         $nickname_arg = $this->trimmed('nickname');
73         $nickname = common_canonical_nickname($nickname_arg);
74
75         // Permanent redirect on non-canonical nickname
76
77         if ($nickname_arg != $nickname) {
78             $args = array('nickname' => $nickname);
79             common_redirect(common_local_url('editgroup', $args), 301);
80             return false;
81         }
82
83         if (!$nickname) {
84             $this->clientError(_('No nickname.'), 404);
85             return false;
86         }
87
88         $groupid = $this->trimmed('groupid');
89
90         if ($groupid) {
91             $this->group = User_group::staticGet('id', $groupid);
92         } else {
93             $local = Local_group::staticGet('nickname', $nickname);
94             if ($local) {
95                 $this->group = User_group::staticGet('id', $local->group_id);
96             }
97         }
98
99         if (!$this->group) {
100             $this->clientError(_('No such group.'), 404);
101             return false;
102         }
103
104         $cur = common_current_user();
105
106         if (!$cur->isAdmin($this->group)) {
107             $this->clientError(_('You must be an admin to edit the group.'), 403);
108             return false;
109         }
110
111         return true;
112     }
113
114     /**
115      * Handle the request
116      *
117      * On GET, show the form. On POST, try to save the group.
118      *
119      * @param array $args unused
120      *
121      * @return void
122      */
123
124     function handle($args)
125     {
126         parent::handle($args);
127         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
128             $this->trySave();
129         } else {
130             $this->showForm();
131         }
132     }
133
134     function showForm($msg=null)
135     {
136         $this->msg = $msg;
137         $this->showPage();
138     }
139
140     function showLocalNav()
141     {
142         $nav = new GroupNav($this, $this->group);
143         $nav->show();
144     }
145
146     function showContent()
147     {
148         $form = new GroupEditForm($this, $this->group);
149         $form->show();
150     }
151
152     function showPageNotice()
153     {
154         if ($this->msg) {
155             $this->element('p', 'error', $this->msg);
156         } else {
157             $this->element('p', 'instructions',
158                            _('Use this form to edit the group.'));
159         }
160     }
161
162     function showScripts()
163     {
164         parent::showScripts();
165         $this->autofocus('nickname');
166     }
167
168     function trySave()
169     {
170         $cur = common_current_user();
171         if (!$cur->isAdmin($this->group)) {
172             $this->clientError(_('You must be an admin to edit the group.'), 403);
173             return;
174         }
175
176         $nickname    = common_canonical_nickname($this->trimmed('nickname'));
177         $fullname    = $this->trimmed('fullname');
178         $homepage    = $this->trimmed('homepage');
179         $description = $this->trimmed('description');
180         $location    = $this->trimmed('location');
181         $aliasstring = $this->trimmed('aliases');
182
183         if (!Validate::string($nickname, array('min_length' => 1,
184                                                'max_length' => 64,
185                                                'format' => NICKNAME_FMT))) {
186             $this->showForm(_('Nickname must have only lowercase letters '.
187                               'and numbers and no spaces.'));
188             return;
189         } else if ($this->nicknameExists($nickname)) {
190             $this->showForm(_('Nickname already in use. Try another one.'));
191             return;
192         } else if (!User_group::allowedNickname($nickname)) {
193             $this->showForm(_('Not a valid nickname.'));
194             return;
195         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
196                    !Validate::uri($homepage,
197                                   array('allowed_schemes' =>
198                                         array('http', 'https')))) {
199             $this->showForm(_('Homepage is not a valid URL.'));
200             return;
201         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
202             $this->showForm(_('Full name is too long (maximum 255 characters).'));
203             return;
204         } else if (User_group::descriptionTooLong($description)) {
205             $this->showForm(sprintf(_('Description is too long (max %d chars).'), User_group::maxDescription()));
206             return;
207         } else if (!is_null($location) && mb_strlen($location) > 255) {
208             $this->showForm(_('Location is too long (maximum 255 characters).'));
209             return;
210         }
211
212         if (!empty($aliasstring)) {
213             $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
214         } else {
215             $aliases = array();
216         }
217
218         if (count($aliases) > common_config('group', 'maxaliases')) {
219             $this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
220                                     common_config('group', 'maxaliases')));
221             return;
222         }
223
224         foreach ($aliases as $alias) {
225             if (!Validate::string($alias, array('min_length' => 1,
226                                                 'max_length' => 64,
227                                                 'format' => NICKNAME_FMT))) {
228                 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
229                 return;
230             }
231             if ($this->nicknameExists($alias)) {
232                 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
233                                         $alias));
234                 return;
235             }
236             // XXX assumes alphanum nicknames
237             if (strcmp($alias, $nickname) == 0) {
238                 $this->showForm(_('Alias can\'t be the same as nickname.'));
239                 return;
240             }
241         }
242
243         $this->group->query('BEGIN');
244
245         $orig = clone($this->group);
246
247         $this->group->nickname    = $nickname;
248         $this->group->fullname    = $fullname;
249         $this->group->homepage    = $homepage;
250         $this->group->description = $description;
251         $this->group->location    = $location;
252         $this->group->mainpage    = common_local_url('showgroup', array('nickname' => $nickname));
253
254         $result = $this->group->update($orig);
255
256         if (!$result) {
257             common_log_db_error($this->group, 'UPDATE', __FILE__);
258             $this->serverError(_('Could not update group.'));
259         }
260
261         $result = $this->group->setAliases($aliases);
262
263         if (!$result) {
264             $this->serverError(_('Could not create aliases.'));
265         }
266
267         if ($nickname != $orig->nickname) {
268             common_log(LOG_INFO, "Saving local group info.");
269             $local = Local_group::staticGet('group_id', $this->group->id);
270             $local->setNickname($nickname);
271         }
272
273         $this->group->query('COMMIT');
274
275         if ($this->group->nickname != $orig->nickname) {
276             common_redirect(common_local_url('editgroup',
277                                              array('nickname' => $nickname)),
278                             303);
279         } else {
280             $this->showForm(_('Options saved.'));
281         }
282     }
283
284     function nicknameExists($nickname)
285     {
286         $group = Local_group::staticGet('nickname', $nickname);
287
288         if (!empty($group) &&
289             $group->group_id != $this->group->id) {
290             return true;
291         }
292
293         $alias = Group_alias::staticGet('alias', $nickname);
294
295         if (!empty($alias) &&
296             $alias->group_id != $this->group->id) {
297             return true;
298         }
299
300         return false;
301     }
302 }
303