]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidadminpanel.php
Localisation updates from http://translatewiki.net.
[quix0rs-gnu-social.git] / plugins / OpenID / openidadminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * OpenID bridge administration panel
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  Settings
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2010 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  * Administer global OpenID settings
36  *
37  * @category Admin
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43 class OpenidadminpanelAction extends AdminPanelAction
44 {
45     /**
46      * Returns the page title
47      *
48      * @return string page title
49      */
50     function title()
51     {
52         // TRANS: Title for OpenID bridge administration page.
53         return _m('TITLE','OpenID Settings');
54     }
55
56     /**
57      * Instructions for using this form.
58      *
59      * @return string instructions
60      */
61     function getInstructions()
62     {
63         // TRANS: Page instructions.
64         return _m('OpenID settings');
65     }
66
67     /**
68      * Show the OpenID admin panel form
69      *
70      * @return void
71      */
72     function showForm()
73     {
74         $form = new OpenIDAdminPanelForm($this);
75         $form->show();
76         return;
77     }
78
79     /**
80      * Save settings from the form
81      *
82      * @return void
83      */
84     function saveSettings()
85     {
86         static $settings = array(
87             'openid' => array('trusted_provider', 'required_team')
88         );
89
90         static $booleans = array(
91             'openid' => array('append_username'),
92             'site' => array('openidonly')
93         );
94
95         $values = array();
96
97         foreach ($settings as $section => $parts) {
98             foreach ($parts as $setting) {
99                 $values[$section][$setting]
100                     = $this->trimmed($setting);
101             }
102         }
103
104         foreach ($booleans as $section => $parts) {
105             foreach ($parts as $setting) {
106                 $values[$section][$setting]
107                     = ($this->boolean($setting)) ? 1 : 0;
108             }
109         }
110
111         // This throws an exception on validation errors
112
113         $this->validate($values);
114
115         // assert(all values are valid);
116
117         $config = new Config();
118
119         $config->query('BEGIN');
120
121         foreach ($settings as $section => $parts) {
122             foreach ($parts as $setting) {
123                 Config::save($section, $setting, $values[$section][$setting]);
124             }
125         }
126
127         foreach ($booleans as $section => $parts) {
128             foreach ($parts as $setting) {
129                 Config::save($section, $setting, $values[$section][$setting]);
130             }
131         }
132
133         $config->query('COMMIT');
134
135         return;
136     }
137
138     function validate(&$values)
139     {
140         // Validate consumer key and secret (can't be too long)
141
142         if (mb_strlen($values['openid']['trusted_provider']) > 255) {
143             $this->clientError(
144                 // TRANS: Client error displayed when OpenID provider URL is too long.
145                 _m('Invalid provider URL. Maximum length is 255 characters.')
146             );
147         }
148
149         if (mb_strlen($values['openid']['required_team']) > 255) {
150             $this->clientError(
151                 // TRANS: Client error displayed when Launchpad team name is too long.
152                 _m('Invalid team name. Maximum length is 255 characters.')
153             );
154         }
155     }
156 }
157
158 class OpenIDAdminPanelForm extends AdminForm
159 {
160     /**
161      * ID of the form
162      *
163      * @return int ID of the form
164      */
165     function id()
166     {
167         return 'openidadminpanel';
168     }
169
170     /**
171      * class of the form
172      *
173      * @return string class of the form
174      */
175     function formClass()
176     {
177         return 'form_settings';
178     }
179
180     /**
181      * Action of the form
182      *
183      * @return string URL of the action
184      */
185     function action()
186     {
187         return common_local_url('openidadminpanel');
188     }
189
190     /**
191      * Data elements of the form
192      *
193      * @return void
194      *
195      * @todo Some of the options could prevent users from logging in again.
196      *       Make sure that the acting administrator has a valid OpenID matching,
197      *       or more carefully warn folks.
198      */
199     function formData()
200     {
201         $this->out->elementStart(
202             'fieldset',
203             array('id' => 'settings_openid')
204         );
205         // TRANS: Fieldset legend.
206         $this->out->element('legend', null, _m('LEGEND','Trusted provider'));
207         $this->out->element('p', 'form_guide',
208             // TRANS: Form guide.
209             _m('By default, users are allowed to authenticate with any OpenID provider. ' .
210                'If you are using your own OpenID service for shared sign-in, ' .
211                'you can restrict access to only your own users here.'));
212         $this->out->elementStart('ul', 'form_data');
213
214         $this->li();
215         $this->input(
216             'trusted_provider',
217             // TRANS: Field label.
218             _m('Provider URL'),
219             // TRANS: Field title.
220             _m('All OpenID logins will be sent to this URL; other providers may not be used.'),
221             'openid'
222         );
223         $this->unli();
224
225         $this->li();
226         $this->out->checkbox(
227             // TRANS: Checkbox label.
228             'append_username', _m('Append a username to base URL'),
229             (bool) $this->value('append_username', 'openid'),
230             // TRANS: Checkbox title.
231             _m('Login form will show the base URL and prompt for a username to add at the end. Use when OpenID provider URL should be the profile page for individual users.'),
232             'true'
233         );
234         $this->unli();
235
236         $this->li();
237         $this->input(
238             'required_team',
239             // TRANS: Field label.
240             _m('Required team'),
241             // TRANS: Field title.
242             _m('Only allow logins from users in the given team (Launchpad extension).'),
243             'openid'
244         );
245         $this->unli();
246
247         $this->out->elementEnd('ul');
248         $this->out->elementEnd('fieldset');
249
250         $this->out->elementStart(
251             'fieldset',
252             array('id' => 'settings_openid-options')
253         );
254         // TRANS: Fieldset legend.
255         $this->out->element('legend', null, _m('LEGEND','Options'));
256
257         $this->out->elementStart('ul', 'form_data');
258
259         $this->li();
260
261         $this->out->checkbox(
262             // TRANS: Checkbox label.
263             'openidonly', _m('Enable OpenID-only mode'),
264             (bool) $this->value('openidonly', 'site'),
265             // TRANS: Checkbox title.
266             _m('Require all users to login via OpenID. Warning: disables password authentication for all users!'),
267             'true'
268         );
269         $this->unli();
270
271         $this->out->elementEnd('ul');
272
273         $this->out->elementEnd('fieldset');
274     }
275
276     /**
277      * Action elements
278      *
279      * @return void
280      */
281     function formActions()
282     {
283         // TRANS: Button text to save OpenID settings.
284         $this->out->submit('submit', _m('BUTTON','Save'), 'submit', null,
285                            // TRANS: Button title to save OpenID settings.
286                            _m('Save OpenID settings.'));
287     }
288 }