]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/adminpanelaction.php
Merge commit 'refs/merge-requests/157' of git://gitorious.org/statusnet/mainline...
[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 class AdminPanelAction extends Action
48 {
49     var $success = true;
50     var $msg     = null;
51
52     /**
53      * Prepare for the action
54      *
55      * We check to see that the user is logged in, has
56      * authenticated in this session, and has the right
57      * to configure the site.
58      *
59      * @param array $args Array of arguments from Web driver
60      *
61      * @return boolean success flag
62      */
63     function prepare($args)
64     {
65         parent::prepare($args);
66
67         // User must be logged in.
68
69         if (!common_logged_in()) {
70             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
71             $this->clientError(_('Not logged in.'));
72             return false;
73         }
74
75         $user = common_current_user();
76
77         // ...because they're logged in
78
79         assert(!empty($user));
80
81         // It must be a "real" login, not saved cookie login
82
83         if (!common_is_real_login()) {
84             // Cookie theft is too easy; we require automatic
85             // logins to re-authenticate before admining the site
86             common_set_returnto($this->selfUrl());
87             if (Event::handle('RedirectToLogin', array($this, $user))) {
88                 common_redirect(common_local_url('login'), 303);
89             }
90         }
91
92         // User must have the right to change admin settings
93
94         if (!$user->hasRight(Right::CONFIGURESITE)) {
95             // TRANS: Client error message thrown when a user tries to change admin settings but has no access rights.
96             $this->clientError(_('You cannot make changes to this site.'));
97             return false;
98         }
99
100         // This panel must be enabled
101
102         $name = $this->trimmed('action');
103
104         $name = mb_substr($name, 0, -10);
105
106         if (!self::canAdmin($name)) {
107             // TRANS: Client error message throw when a certain panel's settings cannot be changed.
108             $this->clientError(_('Changes to that panel are not allowed.'), 403);
109             return false;
110         }
111
112         return true;
113     }
114
115     /**
116      * handle the action
117      *
118      * Check session token and try to save the settings if this is a
119      * POST. Otherwise, show the form.
120      *
121      * @param array $args unused.
122      *
123      * @return void
124      */
125     function handle($args)
126     {
127         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
128             $this->checkSessionToken();
129             try {
130                 $this->saveSettings();
131
132                 // Reload settings
133
134                 Config::loadSettings();
135
136                 $this->success = true;
137                 // TRANS: Message after successful saving of administrative settings.
138                 $this->msg     = _('Settings saved.');
139             } catch (Exception $e) {
140                 $this->success = false;
141                 $this->msg     = $e->getMessage();
142             }
143         }
144         $this->showPage();
145     }
146
147     /**
148      * Show tabset for this page
149      *
150      * Uses the AdminPanelNav widget
151      *
152      * @return void
153      * @see AdminPanelNav
154      */
155     function showLocalNav()
156     {
157         $nav = new AdminPanelNav($this);
158         $nav->show();
159     }
160
161     /**
162      * Show the content section of the page
163      *
164      * Here, we show the admin panel's form.
165      *
166      * @return void.
167      */
168     function showContent()
169     {
170         $this->showForm();
171     }
172
173     /**
174      * Show content block. Overrided just to add a special class
175      * to the content div to allow styling.
176      *
177      * @return nothing
178      */
179     function showContentBlock()
180     {
181         $this->elementStart('div', array('id' => 'content', 'class' => 'admin'));
182         $this->showPageTitle();
183         $this->showPageNoticeBlock();
184         $this->elementStart('div', array('id' => 'content_inner'));
185         // show the actual content (forms, lists, whatever)
186         $this->showContent();
187         $this->elementEnd('div');
188         $this->elementEnd('div');
189     }
190
191     /**
192      * show human-readable instructions for the page, or
193      * a success/failure on save.
194      *
195      * @return void
196      */
197     function showPageNotice()
198     {
199         if ($this->msg) {
200             $this->element('div', ($this->success) ? 'success' : 'error',
201                            $this->msg);
202         } else {
203             $inst   = $this->getInstructions();
204             $output = common_markup_to_html($inst);
205
206             $this->elementStart('div', 'instructions');
207             $this->raw($output);
208             $this->elementEnd('div');
209         }
210     }
211
212     /**
213      * Show the admin panel form
214      *
215      * Sub-classes should overload this.
216      *
217      * @return void
218      */
219     function showForm()
220     {
221         // TRANS: Client error message.
222         $this->clientError(_('showForm() not implemented.'));
223         return;
224     }
225
226     /**
227      * Instructions for using this form.
228      *
229      * String with instructions for using the form.
230      *
231      * Subclasses should overload this.
232      *
233      * @return void
234      */
235     function getInstructions()
236     {
237         return '';
238     }
239
240     /**
241      * Save settings from the form
242      *
243      * Validate and save the settings from the user.
244      *
245      * @return void
246      */
247     function saveSettings()
248     {
249         // TRANS: Client error message
250         $this->clientError(_('saveSettings() not implemented.'));
251         return;
252     }
253
254     function canAdmin($name)
255     {
256         $isOK = false;
257
258         if (Event::handle('AdminPanelCheck', array($name, &$isOK))) {
259             $isOK = in_array($name, common_config('admin', 'panels'));
260         }
261
262         return $isOK;
263     }
264
265     function showProfileBlock()
266     {
267     }
268 }