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