]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/adminpanelaction.php
6d4b974c3773c03513f9bf23ecbce46469b3c25a
[quix0rs-gnu-social.git] / lib / adminpanelaction.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Superclass for admin panel actions
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  UI
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * superclass for admin panel actions
36  *
37  * Common code for all admin panel actions.
38  *
39  * @category UI
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  *
45  * @todo Find some commonalities with SettingsAction and combine
46  */
47
48 class AdminPanelAction extends Action
49 {
50     var $success = true;
51     var $msg     = null;
52
53     /**
54      * Prepare for the action
55      *
56      * We check to see that the user is logged in, has
57      * authenticated in this session, and has the right
58      * to configure the site.
59      *
60      * @param array $args Array of arguments from Web driver
61      *
62      * @return boolean success flag
63      */
64
65     function prepare($args)
66     {
67         parent::prepare($args);
68
69         // User must be logged in.
70
71         if (!common_logged_in()) {
72             $this->clientError(_('Not logged in.'));
73             return;
74         }
75
76         $user = common_current_user();
77
78         // ...because they're logged in
79
80         assert(!empty($user));
81
82         // It must be a "real" login, not saved cookie login
83
84         if (!common_is_real_login()) {
85             // Cookie theft is too easy; we require automatic
86             // logins to re-authenticate before admining the site
87             common_set_returnto($this->selfUrl());
88             if (Event::handle('RedirectToLogin', array($this, $user))) {
89                 common_redirect(common_local_url('login'), 303);
90             }
91         }
92
93         // User must have the right to change admin settings
94
95         if (!$user->hasRight(Right::CONFIGURESITE)) {
96             $this->clientError(_('You cannot make changes to this site.'));
97             return;
98         }
99
100         return true;
101     }
102
103     /**
104      * handle the action
105      *
106      * Check session token and try to save the settings if this is a
107      * POST. Otherwise, show the form.
108      *
109      * @param array $args unused.
110      *
111      * @return void
112      */
113
114     function handle($args)
115     {
116         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
117             $this->checkSessionToken();
118             try {
119                 $this->saveSettings();
120
121                 // Reload settings
122
123                 Config::loadSettings();
124
125                 $this->success = true;
126                 $this->msg     = _('Settings saved.');
127             } catch (Exception $e) {
128                 $this->success = false;
129                 $this->msg     = $e->getMessage();
130             }
131         }
132         $this->showPage();
133     }
134
135     /**
136      * Show tabset for this page
137      *
138      * Uses the AdminPanelNav widget
139      *
140      * @return void
141      * @see AdminPanelNav
142      */
143
144     function showLocalNav()
145     {
146         $nav = new AdminPanelNav($this);
147         $nav->show();
148     }
149
150     /**
151      * Show the content section of the page
152      *
153      * Here, we show the admin panel's form.
154      *
155      * @return void.
156      */
157
158     function showContent()
159     {
160         $this->showForm();
161     }
162
163     /**
164      * show human-readable instructions for the page, or
165      * a success/failure on save.
166      *
167      * @return void
168      */
169
170     function showPageNotice()
171     {
172         if ($this->msg) {
173             $this->element('div', ($this->success) ? 'success' : 'error',
174                            $this->msg);
175         } else {
176             $inst   = $this->getInstructions();
177             $output = common_markup_to_html($inst);
178
179             $this->elementStart('div', 'instructions');
180             $this->raw($output);
181             $this->elementEnd('div');
182         }
183     }
184
185     /**
186      * Show the admin panel form
187      *
188      * Sub-classes should overload this.
189      *
190      * @return void
191      */
192
193     function showForm()
194     {
195         $this->clientError(_('showForm() not implemented.'));
196         return;
197     }
198
199     /**
200      * Instructions for using this form.
201      *
202      * String with instructions for using the form.
203      *
204      * Subclasses should overload this.
205      *
206      * @return void
207      */
208
209     function getInstructions()
210     {
211         return '';
212     }
213
214     /**
215      * Save settings from the form
216      *
217      * Validate and save the settings from the user.
218      *
219      * @return void
220      */
221
222     function saveSettings()
223     {
224         $this->clientError(_('saveSettings() not implemented.'));
225         return;
226     }
227 }
228
229 /**
230  * Menu for public group of actions
231  *
232  * @category Output
233  * @package  StatusNet
234  * @author   Evan Prodromou <evan@status.net>
235  * @author   Sarven Capadisli <csarven@status.net>
236  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
237  * @link     http://status.net/
238  *
239  * @see      Widget
240  */
241
242 class AdminPanelNav extends Widget
243 {
244     var $action = null;
245
246     /**
247      * Construction
248      *
249      * @param Action $action current action, used for output
250      */
251
252     function __construct($action=null)
253     {
254         parent::__construct($action);
255         $this->action = $action;
256     }
257
258     /**
259      * Show the menu
260      *
261      * @return void
262      */
263
264     function show()
265     {
266         $action_name = $this->action->trimmed('action');
267
268         $this->action->elementStart('ul', array('class' => 'nav'));
269
270         if (Event::handle('StartAdminPanelNav', array($this))) {
271
272             $this->out->menuItem(common_local_url('siteadminpanel'), _('Site'),
273                 _('Basic site configuration'), $action_name == 'siteadminpanel', 'nav_site_admin_panel');
274
275             Event::handle('EndAdminPanelNav', array($this));
276         }
277         $this->action->elementEnd('ul');
278     }
279 }
280
281 /**
282  * Menu for admin group of actions
283  *
284  * @category Output
285  * @package  StatusNet
286  * @author   Evan Prodromou <evan@status.net>
287  * @author   Sarven Capadisli <csarven@status.net>
288  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
289  * @link     http://status.net/
290  *
291  * @see      Widget
292  */
293
294 class PublicGroupNav extends Widget
295 {
296     var $action = null;
297
298     /**
299      * Construction
300      *
301      * @param Action $action current action, used for output
302      */
303
304     function __construct($action=null)
305     {
306         parent::__construct($action);
307         $this->action = $action;
308     }
309
310     /**
311      * Show the menu
312      *
313      * @return void
314      */
315
316     function show()
317     {
318         $action_name = $this->action->trimmed('action');
319
320         $this->action->elementStart('ul', array('class' => 'nav'));
321
322         if (Event::handle('StartPublicGroupNav', array($this))) {
323             $this->out->menuItem(common_local_url('public'), _('Public'),
324                 _('Public timeline'), $action_name == 'public', 'nav_timeline_public');
325
326             $this->out->menuItem(common_local_url('groups'), _('Groups'),
327                 _('User groups'), $action_name == 'groups', 'nav_groups');
328
329             $this->out->menuItem(common_local_url('publictagcloud'), _('Recent tags'),
330                 _('Recent tags'), $action_name == 'publictagcloud', 'nav_recent-tags');
331
332             if (count(common_config('nickname', 'featured')) > 0) {
333                 $this->out->menuItem(common_local_url('featured'), _('Featured'),
334                     _('Featured users'), $action_name == 'featured', 'nav_featured');
335             }
336
337             $this->out->menuItem(common_local_url('favorited'), _('Popular'),
338                 _("Popular notices"), $action_name == 'favorited', 'nav_timeline_favorited');
339
340             Event::handle('EndPublicGroupNav', array($this));
341         }
342         $this->action->elementEnd('ul');
343     }
344 }