]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/adminpanelaction.php
Merge branch 'apinamespace' into 0.9.x
[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 thrown when trying to access the admin panel while not logged in.
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 thrown when a user tries to change admin settings but has no access rights.
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 throw when a certain panel's settings cannot be changed.
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 content block. Overrided just to add a special class
180      * to the content div to allow styling.
181      *
182      * @return nothing
183      */
184     function showContentBlock()
185     {
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');
194     }
195
196     /**
197      * show human-readable instructions for the page, or
198      * a success/failure on save.
199      *
200      * @return void
201      */
202
203     function showPageNotice()
204     {
205         if ($this->msg) {
206             $this->element('div', ($this->success) ? 'success' : 'error',
207                            $this->msg);
208         } else {
209             $inst   = $this->getInstructions();
210             $output = common_markup_to_html($inst);
211
212             $this->elementStart('div', 'instructions');
213             $this->raw($output);
214             $this->elementEnd('div');
215         }
216     }
217
218     /**
219      * Show the admin panel form
220      *
221      * Sub-classes should overload this.
222      *
223      * @return void
224      */
225
226     function showForm()
227     {
228         // TRANS: Client error message.
229         $this->clientError(_('showForm() not implemented.'));
230         return;
231     }
232
233     /**
234      * Instructions for using this form.
235      *
236      * String with instructions for using the form.
237      *
238      * Subclasses should overload this.
239      *
240      * @return void
241      */
242
243     function getInstructions()
244     {
245         return '';
246     }
247
248     /**
249      * Save settings from the form
250      *
251      * Validate and save the settings from the user.
252      *
253      * @return void
254      */
255
256     function saveSettings()
257     {
258         // TRANS: Client error message
259         $this->clientError(_('saveSettings() not implemented.'));
260         return;
261     }
262
263     /**
264      * Delete a design setting
265      *
266      * // XXX: Maybe this should go in Design? --Z
267      *
268      * @return mixed $result false if something didn't work
269      */
270
271     function deleteSetting($section, $setting)
272     {
273         $config = new Config();
274
275         $config->section = $section;
276         $config->setting = $setting;
277
278         if ($config->find(true)) {
279             $result = $config->delete();
280             if (!$result) {
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."));
285                 return null;
286             }
287             return $result;
288         }
289
290         return null;
291     }
292
293     function canAdmin($name)
294     {
295         $isOK = false;
296
297         if (Event::handle('AdminPanelCheck', array($name, &$isOK))) {
298             $isOK = in_array($name, common_config('admin', 'panels'));
299         }
300
301         return $isOK;
302     }
303 }
304
305 /**
306  * Menu for public group of actions
307  *
308  * @category Output
309  * @package  StatusNet
310  * @author   Evan Prodromou <evan@status.net>
311  * @author   Sarven Capadisli <csarven@status.net>
312  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
313  * @link     http://status.net/
314  *
315  * @see      Widget
316  */
317
318 class AdminPanelNav extends Widget
319 {
320     var $action = null;
321
322     /**
323      * Construction
324      *
325      * @param Action $action current action, used for output
326      */
327
328     function __construct($action=null)
329     {
330         parent::__construct($action);
331         $this->action = $action;
332     }
333
334     /**
335      * Show the menu
336      *
337      * @return void
338      */
339
340     function show()
341     {
342         $action_name = $this->action->trimmed('action');
343
344         $this->action->elementStart('ul', array('class' => 'nav'));
345
346         if (Event::handle('StartAdminPanelNav', array($this))) {
347
348             if (AdminPanelAction::canAdmin('site')) {
349                 // TRANS: Menu item title/tooltip
350                 $menu_title = _('Basic site configuration');
351                 // TRANS: Menu item for site administration
352                 $this->out->menuItem(common_local_url('siteadminpanel'), _m('MENU', 'Site'),
353                                      $menu_title, $action_name == 'siteadminpanel', 'nav_site_admin_panel');
354             }
355
356             if (AdminPanelAction::canAdmin('design')) {
357                 // TRANS: Menu item title/tooltip
358                 $menu_title = _('Design configuration');
359                 // TRANS: Menu item for site administration
360                 $this->out->menuItem(common_local_url('designadminpanel'), _m('MENU', 'Design'),
361                                      $menu_title, $action_name == 'designadminpanel', 'nav_design_admin_panel');
362             }
363
364             if (AdminPanelAction::canAdmin('user')) {
365                 // TRANS: Menu item title/tooltip
366                 $menu_title = _('User configuration');
367                 // TRANS: Menu item for site administration
368                 $this->out->menuItem(common_local_url('useradminpanel'), _('User'),
369                                      $menu_title, $action_name == 'useradminpanel', 'nav_user_admin_panel');
370             }
371
372             if (AdminPanelAction::canAdmin('access')) {
373                 // TRANS: Menu item title/tooltip
374                 $menu_title = _('Access configuration');
375                 // TRANS: Menu item for site administration
376                 $this->out->menuItem(common_local_url('accessadminpanel'), _('Access'),
377                                      $menu_title, $action_name == 'accessadminpanel', 'nav_access_admin_panel');
378             }
379
380             if (AdminPanelAction::canAdmin('paths')) {
381                 // TRANS: Menu item title/tooltip
382                 $menu_title = _('Paths configuration');
383                 // TRANS: Menu item for site administration
384                 $this->out->menuItem(common_local_url('pathsadminpanel'), _('Paths'),
385                                     $menu_title, $action_name == 'pathsadminpanel', 'nav_paths_admin_panel');
386             }
387
388             if (AdminPanelAction::canAdmin('sessions')) {
389                 // TRANS: Menu item title/tooltip
390                 $menu_title = _('Sessions configuration');
391                 // TRANS: Menu item for site administration
392                 $this->out->menuItem(common_local_url('sessionsadminpanel'), _('Sessions'),
393                                      $menu_title, $action_name == 'sessionsadminpanel', 'nav_sessions_admin_panel');
394             }
395
396             if (AdminPanelAction::canAdmin('sitenotice')) {
397                 // TRANS: Menu item title/tooltip
398                 $menu_title = _('Edit site notice');
399                 // TRANS: Menu item for site administration
400                 $this->out->menuItem(common_local_url('sitenoticeadminpanel'), _('Site notice'),
401                                      $menu_title, $action_name == 'sitenoticeadminpanel', 'nav_sitenotice_admin_panel');
402             }
403
404             if (AdminPanelAction::canAdmin('snapshot')) {
405                 // TRANS: Menu item title/tooltip
406                 $menu_title = _('Snapshots configuration');
407                 // TRANS: Menu item for site administration
408                 $this->out->menuItem(common_local_url('snapshotadminpanel'), _('Snapshots'),
409                                      $menu_title, $action_name == 'snapshotadminpanel', 'nav_snapshot_admin_panel');
410             }
411
412             Event::handle('EndAdminPanelNav', array($this));
413         }
414         $this->action->elementEnd('ul');
415     }
416
417 }