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
48 class AdminPanelAction extends Action
54 * Prepare for the action
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.
60 * @param array $args Array of arguments from Web driver
62 * @return boolean success flag
65 function prepare($args)
67 parent::prepare($args);
69 // User must be logged in.
71 if (!common_logged_in()) {
72 // TRANS: Client error message thrown when trying to access the admin panel while not logged in.
73 $this->clientError(_('Not logged in.'));
77 $user = common_current_user();
79 // ...because they're logged in
81 assert(!empty($user));
83 // It must be a "real" login, not saved cookie login
85 if (!common_is_real_login()) {
86 // Cookie theft is too easy; we require automatic
87 // logins to re-authenticate before admining the site
88 common_set_returnto($this->selfUrl());
89 if (Event::handle('RedirectToLogin', array($this, $user))) {
90 common_redirect(common_local_url('login'), 303);
94 // User must have the right to change admin settings
96 if (!$user->hasRight(Right::CONFIGURESITE)) {
97 // TRANS: Client error message thrown when a user tries to change admin settings but has no access rights.
98 $this->clientError(_('You cannot make changes to this site.'));
102 // This panel must be enabled
104 $name = $this->trimmed('action');
106 $name = mb_substr($name, 0, -10);
108 if (!self::canAdmin($name)) {
109 // TRANS: Client error message throw when a certain panel's settings cannot be changed.
110 $this->clientError(_('Changes to that panel are not allowed.'), 403);
120 * Check session token and try to save the settings if this is a
121 * POST. Otherwise, show the form.
123 * @param array $args unused.
128 function handle($args)
130 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
131 $this->checkSessionToken();
133 $this->saveSettings();
137 Config::loadSettings();
139 $this->success = true;
140 // TRANS: Message after successful saving of administrative settings.
141 $this->msg = _('Settings saved.');
142 } catch (Exception $e) {
143 $this->success = false;
144 $this->msg = $e->getMessage();
151 * Show tabset for this page
153 * Uses the AdminPanelNav widget
159 function showLocalNav()
161 $nav = new AdminPanelNav($this);
166 * Show the content section of the page
168 * Here, we show the admin panel's form.
173 function showContent()
179 * Show content block. Overrided just to add a special class
180 * to the content div to allow styling.
184 function showContentBlock()
186 $this->elementStart('div', array('id' => 'content', 'class' => 'admin'));
187 $this->showPageTitle();
188 $this->showPageNoticeBlock();
189 $this->elementStart('div', array('id' => 'content_inner'));
190 // show the actual content (forms, lists, whatever)
191 $this->showContent();
192 $this->elementEnd('div');
193 $this->elementEnd('div');
197 * show human-readable instructions for the page, or
198 * a success/failure on save.
203 function showPageNotice()
206 $this->element('div', ($this->success) ? 'success' : 'error',
209 $inst = $this->getInstructions();
210 $output = common_markup_to_html($inst);
212 $this->elementStart('div', 'instructions');
214 $this->elementEnd('div');
219 * Show the admin panel form
221 * Sub-classes should overload this.
228 // TRANS: Client error message.
229 $this->clientError(_('showForm() not implemented.'));
234 * Instructions for using this form.
236 * String with instructions for using the form.
238 * Subclasses should overload this.
243 function getInstructions()
249 * Save settings from the form
251 * Validate and save the settings from the user.
256 function saveSettings()
258 // TRANS: Client error message
259 $this->clientError(_('saveSettings() not implemented.'));
264 * Delete a design setting
266 * // XXX: Maybe this should go in Design? --Z
268 * @return mixed $result false if something didn't work
271 function deleteSetting($section, $setting)
273 $config = new Config();
275 $config->section = $section;
276 $config->setting = $setting;
278 if ($config->find(true)) {
279 $result = $config->delete();
281 common_log_db_error($config, 'DELETE', __FILE__);
282 // TRANS: Client error message thrown if design settings could not be deleted in
283 // TRANS: the admin panel Design.
284 $this->clientError(_("Unable to delete design setting."));
292 function canAdmin($name)
296 if (Event::handle('AdminPanelCheck', array($name, &$isOK))) {
297 $isOK = in_array($name, common_config('admin', 'panels'));
305 * Menu for public group of actions
309 * @author Evan Prodromou <evan@status.net>
310 * @author Sarven Capadisli <csarven@status.net>
311 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
312 * @link http://status.net/
317 class AdminPanelNav extends Widget
324 * @param Action $action current action, used for output
327 function __construct($action=null)
329 parent::__construct($action);
330 $this->action = $action;
341 $action_name = $this->action->trimmed('action');
343 $this->action->elementStart('ul', array('class' => 'nav'));
345 if (Event::handle('StartAdminPanelNav', array($this))) {
347 if (AdminPanelAction::canAdmin('site')) {
348 // TRANS: Menu item title/tooltip
349 $menu_title = _('Basic site configuration');
350 // TRANS: Menu item for site administration
351 $this->out->menuItem(common_local_url('siteadminpanel'), _m('MENU', 'Site'),
352 $menu_title, $action_name == 'siteadminpanel', 'nav_site_admin_panel');
355 if (AdminPanelAction::canAdmin('design')) {
356 // TRANS: Menu item title/tooltip
357 $menu_title = _('Design configuration');
358 // TRANS: Menu item for site administration
359 $this->out->menuItem(common_local_url('designadminpanel'), _m('MENU', 'Design'),
360 $menu_title, $action_name == 'designadminpanel', 'nav_design_admin_panel');
363 if (AdminPanelAction::canAdmin('user')) {
364 // TRANS: Menu item title/tooltip
365 $menu_title = _('User configuration');
366 // TRANS: Menu item for site administration
367 $this->out->menuItem(common_local_url('useradminpanel'), _('User'),
368 $menu_title, $action_name == 'useradminpanel', 'nav_user_admin_panel');
371 if (AdminPanelAction::canAdmin('access')) {
372 // TRANS: Menu item title/tooltip
373 $menu_title = _('Access configuration');
374 // TRANS: Menu item for site administration
375 $this->out->menuItem(common_local_url('accessadminpanel'), _('Access'),
376 $menu_title, $action_name == 'accessadminpanel', 'nav_access_admin_panel');
379 if (AdminPanelAction::canAdmin('paths')) {
380 // TRANS: Menu item title/tooltip
381 $menu_title = _('Paths configuration');
382 // TRANS: Menu item for site administration
383 $this->out->menuItem(common_local_url('pathsadminpanel'), _('Paths'),
384 $menu_title, $action_name == 'pathsadminpanel', 'nav_paths_admin_panel');
387 if (AdminPanelAction::canAdmin('sessions')) {
388 // TRANS: Menu item title/tooltip
389 $menu_title = _('Sessions configuration');
390 // TRANS: Menu item for site administration
391 $this->out->menuItem(common_local_url('sessionsadminpanel'), _('Sessions'),
392 $menu_title, $action_name == 'sessionsadminpanel', 'nav_sessions_admin_panel');
395 if (AdminPanelAction::canAdmin('sitenotice')) {
396 // TRANS: Menu item title/tooltip
397 $menu_title = _('Edit site notice');
398 // TRANS: Menu item for site administration
399 $this->out->menuItem(common_local_url('sitenoticeadminpanel'), _('Site notice'),
400 $menu_title, $action_name == 'sitenoticeadminpanel', 'nav_sitenotice_admin_panel');
403 if (AdminPanelAction::canAdmin('snapshot')) {
404 // TRANS: Menu item title/tooltip
405 $menu_title = _('Snapshots configuration');
406 // TRANS: Menu item for site administration
407 $this->out->menuItem(common_local_url('snapshotadminpanel'), _('Snapshots'),
408 $menu_title, $action_name == 'snapshotadminpanel', 'nav_snapshot_admin_panel');
411 Event::handle('EndAdminPanelNav', array($this));
413 $this->action->elementEnd('ul');