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