]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidadminpanel.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[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
44 class OpenidadminpanelAction extends AdminPanelAction
45 {
46     /**
47      * Returns the page title
48      *
49      * @return string page title
50      */
51
52     function title()
53     {
54         return _m('OpenID');
55     }
56
57     /**
58      * Instructions for using this form.
59      *
60      * @return string instructions
61      */
62
63     function getInstructions()
64     {
65         return _m('OpenID settings');
66     }
67
68     /**
69      * Show the OpenID admin panel form
70      *
71      * @return void
72      */
73
74     function showForm()
75     {
76         $form = new OpenIDAdminPanelForm($this);
77         $form->show();
78         return;
79     }
80
81     /**
82      * Save settings from the form
83      *
84      * @return void
85      */
86
87     function saveSettings()
88     {
89         static $settings = array(
90             'openid' => array('trusted_provider', 'required_team')
91         );
92
93         static $booleans = array(
94             'site' => array('openidonly')
95         );
96
97         $values = array();
98
99         foreach ($settings as $section => $parts) {
100             foreach ($parts as $setting) {
101                 $values[$section][$setting]
102                     = $this->trimmed($setting);
103             }
104         }
105
106         foreach ($booleans as $section => $parts) {
107             foreach ($parts as $setting) {
108                 $values[$section][$setting]
109                     = ($this->boolean($setting)) ? 1 : 0;
110             }
111         }
112
113         // This throws an exception on validation errors
114
115         $this->validate($values);
116
117         // assert(all values are valid);
118
119         $config = new Config();
120
121         $config->query('BEGIN');
122
123         foreach ($settings as $section => $parts) {
124             foreach ($parts as $setting) {
125                 Config::save($section, $setting, $values[$section][$setting]);
126             }
127         }
128
129         foreach ($booleans as $section => $parts) {
130             foreach ($parts as $setting) {
131                 Config::save($section, $setting, $values[$section][$setting]);
132             }
133         }
134
135         $config->query('COMMIT');
136
137         return;
138     }
139
140     function validate(&$values)
141     {
142         // Validate consumer key and secret (can't be too long)
143
144         if (mb_strlen($values['openid']['trusted_provider']) > 255) {
145             $this->clientError(
146                 _m("Invalid provider URL. Max length is 255 characters.")
147             );
148         }
149
150         if (mb_strlen($values['openid']['required_team']) > 255) {
151             $this->clientError(
152                 _m("Invalid team name. Max 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
166     function id()
167     {
168         return 'openidadminpanel';
169     }
170
171     /**
172      * class of the form
173      *
174      * @return string class of the form
175      */
176
177     function formClass()
178     {
179         return 'form_settings';
180     }
181
182     /**
183      * Action of the form
184      *
185      * @return string URL of the action
186      */
187
188     function action()
189     {
190         return common_local_url('openidadminpanel');
191     }
192
193     /**
194      * Data elements of the form
195      *
196      * @return void
197      *
198      * @todo Some of the options could prevent users from logging in again.
199      *       Make sure that the acting administrator has a valid OpenID matching,
200      *       or more carefully warn folks.
201      */
202
203     function formData()
204     {
205         $this->out->elementStart(
206             'fieldset',
207             array('id' => 'settings_openid')
208         );
209         $this->out->element('legend', null, _m('Trusted provider'));
210         $this->out->element('p', 'form_guide',
211             _m('By default, users are allowed to authenticate with any OpenID provider. ' .
212                'If you are using your own OpenID service for shared sign-in, ' .
213                'you can restrict access to only your own users here.'));
214         $this->out->elementStart('ul', 'form_data');
215
216         $this->li();
217         $this->input(
218             'trusted_provider',
219             _m('Provider URL'),
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->input(
227             'required_team',
228              _m('Required team'),
229             _m('Only allow logins from users in the given team (Launchpad extension).'),
230             'openid'
231         );
232         $this->unli();
233
234         $this->out->elementEnd('ul');
235         $this->out->elementEnd('fieldset');
236
237         $this->out->elementStart(
238             'fieldset',
239             array('id' => 'settings_openid-options')
240         );
241         $this->out->element('legend', null, _m('Options'));
242
243         $this->out->elementStart('ul', 'form_data');
244
245         $this->li();
246
247         $this->out->checkbox(
248             'openidonly', _m('Enable OpenID-only mode'),
249             (bool) $this->value('openidonly', 'site'),
250             _m('Require all users to login via OpenID. WARNING: disables password authentication for all users!'),
251             'true'
252         );
253         $this->unli();
254
255         $this->out->elementEnd('ul');
256
257         $this->out->elementEnd('fieldset');
258     }
259
260     /**
261      * Action elements
262      *
263      * @return void
264      */
265
266     function formActions()
267     {
268         $this->out->submit('submit', _('Save'), 'submit', null, _m('Save OpenID settings'));
269     }
270 }