]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/adminpanelaction.php
2e9261711b8464e386f06a2a6f64e8091706349e
[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         // It must be a "real" login, not saved cookie login
79
80         if (!common_is_real_login()) {
81             // Cookie theft is too easy; we require automatic
82             // logins to re-authenticate before admining the site
83             common_set_returnto($this->selfUrl());
84             if (Event::handle('RedirectToLogin', array($this, $user))) {
85                 common_redirect(common_local_url('login'), 303);
86             }
87         }
88
89         // User must have the right to change admin settings
90
91         $user = common_current_user();
92
93         if (!$user->hasRight(Right::CONFIGURESITE)) {
94             $this->clientError(_('You cannot make changes to this site.'));
95             return;
96         }
97
98         return true;
99     }
100
101     /**
102      * handle the action
103      *
104      * Check session token and try to save the settings if this is a
105      * POST. Otherwise, show the form.
106      *
107      * @param array $args unused.
108      *
109      * @return void
110      */
111
112     function handle($args)
113     {
114         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
115             $this->checkSessionToken();
116             try {
117                 $this->saveSettings();
118
119                 $this->success = true;
120                 $this->msg     = _('Settings saved.');
121             } catch (Exception $e) {
122                 $this->success = false;
123                 $this->msg     = $e->getMessage();
124             }
125         }
126         $this->showPage();
127     }
128
129     /**
130      * Show the content section of the page
131      *
132      * Here, we show the admin panel's form.
133      *
134      * @return void.
135      */
136
137     function showContent()
138     {
139         $this->showForm();
140     }
141
142     /**
143      * show human-readable instructions for the page, or
144      * a success/failure on save.
145      *
146      * @return void
147      */
148
149     function showPageNotice()
150     {
151         if ($this->msg) {
152             $this->element('div', ($this->success) ? 'success' : 'error',
153                            $this->msg);
154         } else {
155             $inst   = $this->getInstructions();
156             $output = common_markup_to_html($inst);
157
158             $this->elementStart('div', 'instructions');
159             $this->raw($output);
160             $this->elementEnd('div');
161         }
162     }
163
164     /**
165      * Show the admin panel form
166      *
167      * Sub-classes should overload this.
168      *
169      * @return void
170      */
171
172     function showForm()
173     {
174         $this->clientError(_('showForm() not implemented.'));
175         return;
176     }
177
178     /**
179      * Instructions for using this form.
180      *
181      * String with instructions for using the form.
182      *
183      * Subclasses should overload this.
184      *
185      * @return void
186      */
187
188     function getInstructions()
189     {
190         return '';
191     }
192
193     /**
194      * Save settings from the form
195      *
196      * Validate and save the settings from the user.
197      *
198      * @return void
199      */
200
201     function saveSettings()
202     {
203         $this->clientError(_('saveSettings() not implemented.'));
204         return;
205     }
206 }