3 * StatusNet, the distributed open-source microblogging tool
5 * Superclass for admin panel actions
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
35 * superclass for admin panel actions
37 * Common code for all admin panel actions.
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/
45 * @todo Find some commonalities with SettingsAction and combine
47 class AdminPanelAction extends Action
53 * Prepare for the action
55 * We check to see that the user is logged in, has
56 * authenticated in this session, and has the right
57 * to configure the site.
59 * @param array $args Array of arguments from Web driver
61 * @return boolean success flag
63 function prepare($args)
65 parent::prepare($args);
67 // User must be logged in.
69 if (!common_logged_in()) {
70 // TRANS: Client error message thrown when trying to access the admin panel while not logged in.
71 $this->clientError(_('Not logged in.'));
75 $user = common_current_user();
77 // ...because they're logged in
79 assert(!empty($user));
81 // It must be a "real" login, not saved cookie login
83 if (!common_is_real_login()) {
84 // Cookie theft is too easy; we require automatic
85 // logins to re-authenticate before admining the site
86 common_set_returnto($this->selfUrl());
87 if (Event::handle('RedirectToLogin', array($this, $user))) {
88 common_redirect(common_local_url('login'), 303);
92 // User must have the right to change admin settings
94 if (!$user->hasRight(Right::CONFIGURESITE)) {
95 // TRANS: Client error message thrown when a user tries to change admin settings but has no access rights.
96 $this->clientError(_('You cannot make changes to this site.'));
100 // This panel must be enabled
102 $name = $this->trimmed('action');
104 $name = mb_substr($name, 0, -10);
106 if (!self::canAdmin($name)) {
107 // TRANS: Client error message throw when a certain panel's settings cannot be changed.
108 $this->clientError(_('Changes to that panel are not allowed.'), 403);
118 * Check session token and try to save the settings if this is a
119 * POST. Otherwise, show the form.
121 * @param array $args unused.
125 function handle($args)
127 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
128 $this->checkSessionToken();
130 $this->saveSettings();
134 Config::loadSettings();
136 $this->success = true;
137 // TRANS: Message after successful saving of administrative settings.
138 $this->msg = _('Settings saved.');
139 } catch (Exception $e) {
140 $this->success = false;
141 $this->msg = $e->getMessage();
148 * Show tabset for this page
150 * Uses the AdminPanelNav widget
155 function showLocalNav()
157 $nav = new AdminPanelNav($this);
162 * Show the content section of the page
164 * Here, we show the admin panel's form.
168 function showContent()
174 * Show content block. Overrided just to add a special class
175 * to the content div to allow styling.
179 function showContentBlock()
181 $this->elementStart('div', array('id' => 'content', 'class' => 'admin'));
182 $this->showPageTitle();
183 $this->showPageNoticeBlock();
184 $this->elementStart('div', array('id' => 'content_inner'));
185 // show the actual content (forms, lists, whatever)
186 $this->showContent();
187 $this->elementEnd('div');
188 $this->elementEnd('div');
192 * show human-readable instructions for the page, or
193 * a success/failure on save.
197 function showPageNotice()
200 $this->element('div', ($this->success) ? 'success' : 'error',
203 $inst = $this->getInstructions();
204 $output = common_markup_to_html($inst);
206 $this->elementStart('div', 'instructions');
208 $this->elementEnd('div');
213 * Show the admin panel form
215 * Sub-classes should overload this.
221 // TRANS: Client error message.
222 $this->clientError(_('showForm() not implemented.'));
227 * Instructions for using this form.
229 * String with instructions for using the form.
231 * Subclasses should overload this.
235 function getInstructions()
241 * Save settings from the form
243 * Validate and save the settings from the user.
247 function saveSettings()
249 // TRANS: Client error message
250 $this->clientError(_('saveSettings() not implemented.'));
255 * Delete a design setting
257 * // XXX: Maybe this should go in Design? --Z
259 * @return mixed $result false if something didn't work
261 function deleteSetting($section, $setting)
263 $config = new Config();
265 $config->section = $section;
266 $config->setting = $setting;
268 if ($config->find(true)) {
269 $result = $config->delete();
271 common_log_db_error($config, 'DELETE', __FILE__);
272 // TRANS: Client error message thrown if design settings could not be deleted in
273 // TRANS: the admin panel Design.
274 $this->clientError(_("Unable to delete design setting."));
283 function canAdmin($name)
287 if (Event::handle('AdminPanelCheck', array($name, &$isOK))) {
288 $isOK = in_array($name, common_config('admin', 'panels'));
296 * Menu for public group of actions
300 * @author Evan Prodromou <evan@status.net>
301 * @author Sarven Capadisli <csarven@status.net>
302 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
303 * @link http://status.net/
307 class AdminPanelNav extends Widget
314 * @param Action $action current action, used for output
316 function __construct($action=null)
318 parent::__construct($action);
319 $this->action = $action;
329 $action_name = $this->action->trimmed('action');
331 $this->action->elementStart('ul', array('class' => 'nav'));
333 if (Event::handle('StartAdminPanelNav', array($this))) {
335 if (AdminPanelAction::canAdmin('site')) {
336 // TRANS: Menu item title/tooltip
337 $menu_title = _('Basic site configuration');
338 // TRANS: Menu item for site administration
339 $this->out->menuItem(common_local_url('siteadminpanel'), _m('MENU', 'Site'),
340 $menu_title, $action_name == 'siteadminpanel', 'nav_site_admin_panel');
343 if (AdminPanelAction::canAdmin('design')) {
344 // TRANS: Menu item title/tooltip
345 $menu_title = _('Design configuration');
346 // TRANS: Menu item for site administration
347 $this->out->menuItem(common_local_url('designadminpanel'), _m('MENU', 'Design'),
348 $menu_title, $action_name == 'designadminpanel', 'nav_design_admin_panel');
351 if (AdminPanelAction::canAdmin('user')) {
352 // TRANS: Menu item title/tooltip
353 $menu_title = _('User configuration');
354 // TRANS: Menu item for site administration
355 $this->out->menuItem(common_local_url('useradminpanel'), _('User'),
356 $menu_title, $action_name == 'useradminpanel', 'nav_user_admin_panel');
359 if (AdminPanelAction::canAdmin('access')) {
360 // TRANS: Menu item title/tooltip
361 $menu_title = _('Access configuration');
362 // TRANS: Menu item for site administration
363 $this->out->menuItem(common_local_url('accessadminpanel'), _('Access'),
364 $menu_title, $action_name == 'accessadminpanel', 'nav_access_admin_panel');
367 if (AdminPanelAction::canAdmin('paths')) {
368 // TRANS: Menu item title/tooltip
369 $menu_title = _('Paths configuration');
370 // TRANS: Menu item for site administration
371 $this->out->menuItem(common_local_url('pathsadminpanel'), _('Paths'),
372 $menu_title, $action_name == 'pathsadminpanel', 'nav_paths_admin_panel');
375 if (AdminPanelAction::canAdmin('sessions')) {
376 // TRANS: Menu item title/tooltip
377 $menu_title = _('Sessions configuration');
378 // TRANS: Menu item for site administration
379 $this->out->menuItem(common_local_url('sessionsadminpanel'), _('Sessions'),
380 $menu_title, $action_name == 'sessionsadminpanel', 'nav_sessions_admin_panel');
383 if (AdminPanelAction::canAdmin('sitenotice')) {
384 // TRANS: Menu item title/tooltip
385 $menu_title = _('Edit site notice');
386 // TRANS: Menu item for site administration
387 $this->out->menuItem(common_local_url('sitenoticeadminpanel'), _('Site notice'),
388 $menu_title, $action_name == 'sitenoticeadminpanel', 'nav_sitenotice_admin_panel');
391 if (AdminPanelAction::canAdmin('snapshot')) {
392 // TRANS: Menu item title/tooltip
393 $menu_title = _('Snapshots configuration');
394 // TRANS: Menu item for site administration
395 $this->out->menuItem(common_local_url('snapshotadminpanel'), _('Snapshots'),
396 $menu_title, $action_name == 'snapshotadminpanel', 'nav_snapshot_admin_panel');
399 if (AdminPanelAction::canAdmin('license')) {
400 // TRANS: Menu item title/tooltip
401 $menu_title = _('Set site license');
402 // TRANS: Menu item for site administration
403 $this->out->menuItem(common_local_url('licenseadminpanel'), _('License'),
404 $menu_title, $action_name == 'licenseadminpanel', 'nav_license_admin_panel');
407 if (AdminPanelAction::canAdmin('plugins')) {
408 // TRANS: Menu item title/tooltip
409 $menu_title = _('Plugins configuration');
410 // TRANS: Menu item for site administration
411 $this->out->menuItem(common_local_url('pluginsadminpanel'), _('Plugins'),
412 $menu_title, $action_name == 'pluginsadminpanel', 'nav_design_admin_panel');
415 Event::handle('EndAdminPanelNav', array($this));
417 $this->action->elementEnd('ul');