]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/adminpanelaction.php
Ticket #2210: adjust locale setup fallback to try more locales on the system if en_US...
[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 (!self::canAdmin($name)) {
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 content block. Overrided just to add a special class
176      * to the content div to allow styling.
177      *
178      * @return nothing
179      */
180     function showContentBlock()
181     {
182         $this->elementStart('div', array('id' => 'content', 'class' => 'admin'));
183         $this->showPageTitle();
184         $this->showPageNoticeBlock();
185         $this->elementStart('div', array('id' => 'content_inner'));
186         // show the actual content (forms, lists, whatever)
187         $this->showContent();
188         $this->elementEnd('div');
189         $this->elementEnd('div');
190     }
191
192     /**
193      * show human-readable instructions for the page, or
194      * a success/failure on save.
195      *
196      * @return void
197      */
198
199     function showPageNotice()
200     {
201         if ($this->msg) {
202             $this->element('div', ($this->success) ? 'success' : 'error',
203                            $this->msg);
204         } else {
205             $inst   = $this->getInstructions();
206             $output = common_markup_to_html($inst);
207
208             $this->elementStart('div', 'instructions');
209             $this->raw($output);
210             $this->elementEnd('div');
211         }
212     }
213
214     /**
215      * Show the admin panel form
216      *
217      * Sub-classes should overload this.
218      *
219      * @return void
220      */
221
222     function showForm()
223     {
224         $this->clientError(_('showForm() not implemented.'));
225         return;
226     }
227
228     /**
229      * Instructions for using this form.
230      *
231      * String with instructions for using the form.
232      *
233      * Subclasses should overload this.
234      *
235      * @return void
236      */
237
238     function getInstructions()
239     {
240         return '';
241     }
242
243     /**
244      * Save settings from the form
245      *
246      * Validate and save the settings from the user.
247      *
248      * @return void
249      */
250
251     function saveSettings()
252     {
253         $this->clientError(_('saveSettings() not implemented.'));
254         return;
255     }
256
257     /**
258      * Delete a design setting
259      *
260      * // XXX: Maybe this should go in Design? --Z
261      *
262      * @return mixed $result false if something didn't work
263      */
264
265     function deleteSetting($section, $setting)
266     {
267         $config = new Config();
268
269         $config->section = $section;
270         $config->setting = $setting;
271
272         if ($config->find(true)) {
273             $result = $config->delete();
274             if (!$result) {
275                 common_log_db_error($config, 'DELETE', __FILE__);
276                 $this->clientError(_("Unable to delete design setting."));
277                 return null;
278             }
279         }
280
281         return $result;
282     }
283
284     function canAdmin($name)
285     {
286         $isOK = false;
287
288         if (Event::handle('AdminPanelCheck', array($name, &$isOK))) {
289             $isOK = in_array($name, common_config('admin', 'panels'));
290         }
291
292         return $isOK;
293     }
294 }
295
296 /**
297  * Menu for public group of actions
298  *
299  * @category Output
300  * @package  StatusNet
301  * @author   Evan Prodromou <evan@status.net>
302  * @author   Sarven Capadisli <csarven@status.net>
303  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
304  * @link     http://status.net/
305  *
306  * @see      Widget
307  */
308
309 class AdminPanelNav extends Widget
310 {
311     var $action = null;
312
313     /**
314      * Construction
315      *
316      * @param Action $action current action, used for output
317      */
318
319     function __construct($action=null)
320     {
321         parent::__construct($action);
322         $this->action = $action;
323     }
324
325     /**
326      * Show the menu
327      *
328      * @return void
329      */
330
331     function show()
332     {
333         $action_name = $this->action->trimmed('action');
334
335         $this->action->elementStart('ul', array('class' => 'nav'));
336
337         if (Event::handle('StartAdminPanelNav', array($this))) {
338
339             if (AdminPanelAction::canAdmin('site')) {
340                 $this->out->menuItem(common_local_url('siteadminpanel'), _('Site'),
341                                      _('Basic site configuration'), $action_name == 'siteadminpanel', 'nav_site_admin_panel');
342             }
343
344             if (AdminPanelAction::canAdmin('design')) {
345                 $this->out->menuItem(common_local_url('designadminpanel'), _('Design'),
346                                      _('Design configuration'), $action_name == 'designadminpanel', 'nav_design_admin_panel');
347             }
348
349             if (AdminPanelAction::canAdmin('user')) {
350                 $this->out->menuItem(common_local_url('useradminpanel'), _('User'),
351                                      _('User configuration'), $action_name == 'useradminpanel', 'nav_user_admin_panel');
352             }
353
354             if (AdminPanelAction::canAdmin('access')) {
355                 $this->out->menuItem(common_local_url('accessadminpanel'), _('Access'),
356                                      _('Access configuration'), $action_name == 'accessadminpanel', 'nav_access_admin_panel');
357             }
358
359             if (AdminPanelAction::canAdmin('paths')) {
360                 $this->out->menuItem(common_local_url('pathsadminpanel'), _('Paths'),
361                                     _('Paths configuration'), $action_name == 'pathsadminpanel', 'nav_paths_admin_panel');
362             }
363
364             if (AdminPanelAction::canAdmin('sessions')) {
365                 $this->out->menuItem(common_local_url('sessionsadminpanel'), _('Sessions'),
366                                      _('Sessions configuration'), $action_name == 'sessionsadminpanel', 'nav_sessions_admin_panel');
367             }
368
369             if (AdminPanelAction::canAdmin('sitenotice')) {
370                 $this->out->menuItem(common_local_url('sitenoticeadminpanel'), _('Site notice'),
371                                      _('Edit site notice'), $action_name == 'sitenoticeadminpanel', 'nav_sitenotice_admin_panel');
372             }
373
374             if (AdminPanelAction::canAdmin('snapshot')) {
375                 $this->out->menuItem(common_local_url('snapshotadminpanel'), _('Snapshots'),
376                                      _('Snapshots configuration'), $action_name == 'snapshotadminpanel', 'nav_snapshot_admin_panel');
377             }
378
379             Event::handle('EndAdminPanelNav', array($this));
380         }
381         $this->action->elementEnd('ul');
382     }
383
384 }