]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/adminpanelaction.php
Merge branch 'master' into testing
[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 false;
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 false;
98         }
99
100         // This panel must be enabled
101
102         $name = $this->trimmed('action');
103
104         $name = mb_substr($name, 0, -10);
105
106         if (!in_array($name, common_config('admin', 'panels'))) {
107             $this->clientError(_('Changes to that panel are not allowed.'), 403);
108             return false;
109         }
110
111         return true;
112     }
113
114     /**
115      * handle the action
116      *
117      * Check session token and try to save the settings if this is a
118      * POST. Otherwise, show the form.
119      *
120      * @param array $args unused.
121      *
122      * @return void
123      */
124
125     function handle($args)
126     {
127         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
128             $this->checkSessionToken();
129             try {
130                 $this->saveSettings();
131
132                 // Reload settings
133
134                 Config::loadSettings();
135
136                 $this->success = true;
137                 $this->msg     = _('Settings saved.');
138             } catch (Exception $e) {
139                 $this->success = false;
140                 $this->msg     = $e->getMessage();
141             }
142         }
143         $this->showPage();
144     }
145
146     /**
147      * Show tabset for this page
148      *
149      * Uses the AdminPanelNav widget
150      *
151      * @return void
152      * @see AdminPanelNav
153      */
154
155     function showLocalNav()
156     {
157         $nav = new AdminPanelNav($this);
158         $nav->show();
159     }
160
161     /**
162      * Show the content section of the page
163      *
164      * Here, we show the admin panel's form.
165      *
166      * @return void.
167      */
168
169     function showContent()
170     {
171         $this->showForm();
172     }
173
174     /**
175      * show human-readable instructions for the page, or
176      * a success/failure on save.
177      *
178      * @return void
179      */
180
181     function showPageNotice()
182     {
183         if ($this->msg) {
184             $this->element('div', ($this->success) ? 'success' : 'error',
185                            $this->msg);
186         } else {
187             $inst   = $this->getInstructions();
188             $output = common_markup_to_html($inst);
189
190             $this->elementStart('div', 'instructions');
191             $this->raw($output);
192             $this->elementEnd('div');
193         }
194     }
195
196     /**
197      * Show the admin panel form
198      *
199      * Sub-classes should overload this.
200      *
201      * @return void
202      */
203
204     function showForm()
205     {
206         $this->clientError(_('showForm() not implemented.'));
207         return;
208     }
209
210     /**
211      * Instructions for using this form.
212      *
213      * String with instructions for using the form.
214      *
215      * Subclasses should overload this.
216      *
217      * @return void
218      */
219
220     function getInstructions()
221     {
222         return '';
223     }
224
225     /**
226      * Save settings from the form
227      *
228      * Validate and save the settings from the user.
229      *
230      * @return void
231      */
232
233     function saveSettings()
234     {
235         $this->clientError(_('saveSettings() not implemented.'));
236         return;
237     }
238
239     /**
240      * Delete a design setting
241      *
242      * // XXX: Maybe this should go in Design? --Z
243      *
244      * @return mixed $result false if something didn't work
245      */
246
247     function deleteSetting($section, $setting)
248     {
249         $config = new Config();
250
251         $config->section = $section;
252         $config->setting = $setting;
253
254         if ($config->find(true)) {
255             $result = $config->delete();
256             if (!$result) {
257                 common_log_db_error($config, 'DELETE', __FILE__);
258                 $this->clientError(_("Unable to delete design setting."));
259                 return null;
260             }
261         }
262
263         return $result;
264     }
265 }
266
267 /**
268  * Menu for public group of actions
269  *
270  * @category Output
271  * @package  StatusNet
272  * @author   Evan Prodromou <evan@status.net>
273  * @author   Sarven Capadisli <csarven@status.net>
274  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
275  * @link     http://status.net/
276  *
277  * @see      Widget
278  */
279
280 class AdminPanelNav extends Widget
281 {
282     var $action = null;
283
284     /**
285      * Construction
286      *
287      * @param Action $action current action, used for output
288      */
289
290     function __construct($action=null)
291     {
292         parent::__construct($action);
293         $this->action = $action;
294     }
295
296     /**
297      * Show the menu
298      *
299      * @return void
300      */
301
302     function show()
303     {
304         $action_name = $this->action->trimmed('action');
305
306         $this->action->elementStart('ul', array('class' => 'nav'));
307
308         if (Event::handle('StartAdminPanelNav', array($this))) {
309
310             if ($this->canAdmin('site')) {
311                 $this->out->menuItem(common_local_url('siteadminpanel'), _('Site'),
312                                      _('Basic site configuration'), $action_name == 'siteadminpanel', 'nav_site_admin_panel');
313             }
314
315             if ($this->canAdmin('design')) {
316                 $this->out->menuItem(common_local_url('designadminpanel'), _('Design'),
317                                      _('Design configuration'), $action_name == 'designadminpanel', 'nav_design_admin_panel');
318             }
319
320             if ($this->canAdmin('user')) {
321                 $this->out->menuItem(common_local_url('useradminpanel'), _('User'),
322                                      _('User configuration'), $action_name == 'useradminpanel', 'nav_design_admin_panel');
323             }
324
325             if ($this->canAdmin('access')) {
326                 $this->out->menuItem(common_local_url('accessadminpanel'), _('Access'),
327                                      _('Access configuration'), $action_name == 'accessadminpanel', 'nav_design_admin_panel');
328             }
329
330             if ($this->canAdmin('paths')) {
331                 $this->out->menuItem(common_local_url('pathsadminpanel'), _('Paths'),
332                                     _('Paths configuration'), $action_name == 'pathsadminpanel', 'nav_design_admin_panel');
333             }
334
335             if ($this->canAdmin('sessions')) {
336                 $this->out->menuItem(common_local_url('sessionsadminpanel'), _('Sessions'),
337                                      _('Sessions configuration'), $action_name == 'sessionsadminpanel', 'nav_design_admin_panel');
338             }
339
340             Event::handle('EndAdminPanelNav', array($this));
341         }
342         $this->action->elementEnd('ul');
343     }
344
345     function canAdmin($name)
346     {
347         return in_array($name, common_config('admin', 'panels'));
348     }
349 }