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