]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/adminpanelaction.php
Moved most path and server settings to a new paths admin panel
[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             $this->clientError(_('Not logged in.'));
73             return;
74         }
75
76         $user = common_current_user();
77
78         // ...because they're logged in
79
80         assert(!empty($user));
81
82         // It must be a "real" login, not saved cookie login
83
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);
90             }
91         }
92
93         // User must have the right to change admin settings
94
95         if (!$user->hasRight(Right::CONFIGURESITE)) {
96             $this->clientError(_('You cannot make changes to this site.'));
97             return;
98         }
99
100         return true;
101     }
102
103     /**
104      * handle the action
105      *
106      * Check session token and try to save the settings if this is a
107      * POST. Otherwise, show the form.
108      *
109      * @param array $args unused.
110      *
111      * @return void
112      */
113
114     function handle($args)
115     {
116         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
117             $this->checkSessionToken();
118             try {
119                 $this->saveSettings();
120
121                 // Reload settings
122
123                 Config::loadSettings();
124
125                 $this->success = true;
126                 $this->msg     = _('Settings saved.');
127             } catch (Exception $e) {
128                 $this->success = false;
129                 $this->msg     = $e->getMessage();
130             }
131         }
132         $this->showPage();
133     }
134
135     /**
136      * Show tabset for this page
137      *
138      * Uses the AdminPanelNav widget
139      *
140      * @return void
141      * @see AdminPanelNav
142      */
143
144     function showLocalNav()
145     {
146         $nav = new AdminPanelNav($this);
147         $nav->show();
148     }
149
150     /**
151      * Show the content section of the page
152      *
153      * Here, we show the admin panel's form.
154      *
155      * @return void.
156      */
157
158     function showContent()
159     {
160         $this->showForm();
161     }
162
163     /**
164      * show human-readable instructions for the page, or
165      * a success/failure on save.
166      *
167      * @return void
168      */
169
170     function showPageNotice()
171     {
172         if ($this->msg) {
173             $this->element('div', ($this->success) ? 'success' : 'error',
174                            $this->msg);
175         } else {
176             $inst   = $this->getInstructions();
177             $output = common_markup_to_html($inst);
178
179             $this->elementStart('div', 'instructions');
180             $this->raw($output);
181             $this->elementEnd('div');
182         }
183     }
184
185     /**
186      * Show the admin panel form
187      *
188      * Sub-classes should overload this.
189      *
190      * @return void
191      */
192
193     function showForm()
194     {
195         $this->clientError(_('showForm() not implemented.'));
196         return;
197     }
198
199     /**
200      * Instructions for using this form.
201      *
202      * String with instructions for using the form.
203      *
204      * Subclasses should overload this.
205      *
206      * @return void
207      */
208
209     function getInstructions()
210     {
211         return '';
212     }
213
214     /**
215      * Save settings from the form
216      *
217      * Validate and save the settings from the user.
218      *
219      * @return void
220      */
221
222     function saveSettings()
223     {
224         $this->clientError(_('saveSettings() not implemented.'));
225         return;
226     }
227     
228     /**
229      * Delete a design setting
230      *
231      * // XXX: Maybe this should go in Design? --Z
232      *
233      * @return mixed $result false if something didn't work
234      */
235
236     function deleteSetting($section, $setting)
237     {
238         $config = new Config();
239
240         $config->section = $section;
241         $config->setting = $setting;
242
243         if ($config->find(true)) {
244             $result = $config->delete();
245             if (!$result) {
246                 common_log_db_error($config, 'DELETE', __FILE__);
247                 $this->clientError(_("Unable to delete design setting."));
248                 return null;
249             }
250         }
251
252         return $result;
253     }
254 }
255
256 /**
257  * Menu for public group of actions
258  *
259  * @category Output
260  * @package  StatusNet
261  * @author   Evan Prodromou <evan@status.net>
262  * @author   Sarven Capadisli <csarven@status.net>
263  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
264  * @link     http://status.net/
265  *
266  * @see      Widget
267  */
268
269 class AdminPanelNav extends Widget
270 {
271     var $action = null;
272
273     /**
274      * Construction
275      *
276      * @param Action $action current action, used for output
277      */
278
279     function __construct($action=null)
280     {
281         parent::__construct($action);
282         $this->action = $action;
283     }
284
285     /**
286      * Show the menu
287      *
288      * @return void
289      */
290
291     function show()
292     {
293         $action_name = $this->action->trimmed('action');
294
295         $this->action->elementStart('ul', array('class' => 'nav'));
296
297         if (Event::handle('StartAdminPanelNav', array($this))) {
298
299             $this->out->menuItem(common_local_url('siteadminpanel'), _('Site'),
300                 _('Basic site configuration'), $action_name == 'siteadminpanel', 'nav_site_admin_panel');
301
302             $this->out->menuItem(common_local_url('designadminpanel'), _('Design'),
303                 _('Design configuration'), $action_name == 'designadminpanel', 'nav_design_admin_panel');
304
305             $this->out->menuItem(common_local_url('pathsadminpanel'), _('Paths'),
306                 _('Paths configuration'), $action_name == 'pathsadminpanel', 'nav_design_admin_panel');
307
308             Event::handle('EndAdminPanelNav', array($this));
309         }
310         $this->action->elementEnd('ul');
311     }
312 }