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 $this->clientError(_('Not logged in.'));
76 $user = common_current_user();
78 // ...because they're logged in
80 assert(!empty($user));
82 // It must be a "real" login, not saved cookie login
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);
93 // User must have the right to change admin settings
95 if (!$user->hasRight(Right::CONFIGURESITE)) {
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 $this->clientError(_('Changes to that panel are not allowed.'), 403);
117 * Check session token and try to save the settings if this is a
118 * POST. Otherwise, show the form.
120 * @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 $this->msg = _('Settings saved.');
138 } catch (Exception $e) {
139 $this->success = false;
140 $this->msg = $e->getMessage();
147 * Show tabset for this page
149 * 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.
169 function showContent()
175 * Show content block. Overrided just to add a special class
176 * to the content div to allow styling.
180 function showContentBlock()
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');
193 * There is no data for aside, so, we don't output
203 * show human-readable instructions for the page, or
204 * a success/failure on save.
209 function showPageNotice()
212 $this->element('div', ($this->success) ? 'success' : 'error',
215 $inst = $this->getInstructions();
216 $output = common_markup_to_html($inst);
218 $this->elementStart('div', 'instructions');
220 $this->elementEnd('div');
225 * Show the admin panel form
227 * Sub-classes should overload this.
234 $this->clientError(_('showForm() not implemented.'));
239 * Instructions for using this form.
241 * String with instructions for using the form.
243 * Subclasses should overload this.
248 function getInstructions()
254 * Save settings from the form
256 * Validate and save the settings from the user.
261 function saveSettings()
263 $this->clientError(_('saveSettings() not implemented.'));
268 * Delete a design setting
270 * // XXX: Maybe this should go in Design? --Z
272 * @return mixed $result false if something didn't work
275 function deleteSetting($section, $setting)
277 $config = new Config();
279 $config->section = $section;
280 $config->setting = $setting;
282 if ($config->find(true)) {
283 $result = $config->delete();
285 common_log_db_error($config, 'DELETE', __FILE__);
286 $this->clientError(_("Unable to delete design setting."));
294 function canAdmin($name)
298 if (Event::handle('AdminPanelCheck', array($name, &$isOK))) {
299 $isOK = in_array($name, common_config('admin', 'panels'));
307 * Menu for public group of actions
311 * @author Evan Prodromou <evan@status.net>
312 * @author Sarven Capadisli <csarven@status.net>
313 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
314 * @link http://status.net/
319 class AdminPanelNav extends Widget
326 * @param Action $action current action, used for output
329 function __construct($action=null)
331 parent::__construct($action);
332 $this->action = $action;
343 $action_name = $this->action->trimmed('action');
345 $this->action->elementStart('ul', array('class' => 'nav'));
347 if (Event::handle('StartAdminPanelNav', array($this))) {
349 if (AdminPanelAction::canAdmin('site')) {
350 $this->out->menuItem(common_local_url('siteadminpanel'), _('Site'),
351 _('Basic site configuration'), $action_name == 'siteadminpanel', 'nav_site_admin_panel');
354 if (AdminPanelAction::canAdmin('design')) {
355 $this->out->menuItem(common_local_url('designadminpanel'), _('Design'),
356 _('Design configuration'), $action_name == 'designadminpanel', 'nav_design_admin_panel');
359 if (AdminPanelAction::canAdmin('user')) {
360 $this->out->menuItem(common_local_url('useradminpanel'), _('User'),
361 _('User configuration'), $action_name == 'useradminpanel', 'nav_design_admin_panel');
364 if (AdminPanelAction::canAdmin('access')) {
365 $this->out->menuItem(common_local_url('accessadminpanel'), _('Access'),
366 _('Access configuration'), $action_name == 'accessadminpanel', 'nav_design_admin_panel');
369 if (AdminPanelAction::canAdmin('paths')) {
370 $this->out->menuItem(common_local_url('pathsadminpanel'), _('Paths'),
371 _('Paths configuration'), $action_name == 'pathsadminpanel', 'nav_design_admin_panel');
374 if (AdminPanelAction::canAdmin('sessions')) {
375 $this->out->menuItem(common_local_url('sessionsadminpanel'), _('Sessions'),
376 _('Sessions configuration'), $action_name == 'sessionsadminpanel', 'nav_design_admin_panel');
379 Event::handle('EndAdminPanelNav', array($this));
381 $this->action->elementEnd('ul');