]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidadminpanel.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[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             'openid' => array('append_username'),
95             'site' => array('openidonly')
96         );
97
98         $values = array();
99
100         foreach ($settings as $section => $parts) {
101             foreach ($parts as $setting) {
102                 $values[$section][$setting]
103                     = $this->trimmed($setting);
104             }
105         }
106
107         foreach ($booleans as $section => $parts) {
108             foreach ($parts as $setting) {
109                 $values[$section][$setting]
110                     = ($this->boolean($setting)) ? 1 : 0;
111             }
112         }
113
114         // This throws an exception on validation errors
115
116         $this->validate($values);
117
118         // assert(all values are valid);
119
120         $config = new Config();
121
122         $config->query('BEGIN');
123
124         foreach ($settings as $section => $parts) {
125             foreach ($parts as $setting) {
126                 Config::save($section, $setting, $values[$section][$setting]);
127             }
128         }
129
130         foreach ($booleans as $section => $parts) {
131             foreach ($parts as $setting) {
132                 Config::save($section, $setting, $values[$section][$setting]);
133             }
134         }
135
136         $config->query('COMMIT');
137
138         return;
139     }
140
141     function validate(&$values)
142     {
143         // Validate consumer key and secret (can't be too long)
144
145         if (mb_strlen($values['openid']['trusted_provider']) > 255) {
146             $this->clientError(
147                 _m("Invalid provider URL. Max length is 255 characters.")
148             );
149         }
150
151         if (mb_strlen($values['openid']['required_team']) > 255) {
152             $this->clientError(
153                 _m("Invalid team name. Max length is 255 characters.")
154             );
155         }
156     }
157 }
158
159 class OpenIDAdminPanelForm extends AdminForm
160 {
161     /**
162      * ID of the form
163      *
164      * @return int ID of the form
165      */
166
167     function id()
168     {
169         return 'openidadminpanel';
170     }
171
172     /**
173      * class of the form
174      *
175      * @return string class of the form
176      */
177
178     function formClass()
179     {
180         return 'form_settings';
181     }
182
183     /**
184      * Action of the form
185      *
186      * @return string URL of the action
187      */
188
189     function action()
190     {
191         return common_local_url('openidadminpanel');
192     }
193
194     /**
195      * Data elements of the form
196      *
197      * @return void
198      *
199      * @todo Some of the options could prevent users from logging in again.
200      *       Make sure that the acting administrator has a valid OpenID matching,
201      *       or more carefully warn folks.
202      */
203
204     function formData()
205     {
206         $this->out->elementStart(
207             'fieldset',
208             array('id' => 'settings_openid')
209         );
210         $this->out->element('legend', null, _m('Trusted provider'));
211         $this->out->element('p', 'form_guide',
212             _m('By default, users are allowed to authenticate with any OpenID provider. ' .
213                'If you are using your own OpenID service for shared sign-in, ' .
214                'you can restrict access to only your own users here.'));
215         $this->out->elementStart('ul', 'form_data');
216
217         $this->li();
218         $this->input(
219             'trusted_provider',
220             _m('Provider URL'),
221             _m('All OpenID logins will be sent to this URL; other providers may not be used.'),
222             'openid'
223         );
224         $this->unli();
225
226         $this->li();
227         $this->out->checkbox(
228             'append_username', _m('Append a username to base URL'),
229             (bool) $this->value('append_username', 'openid'),
230             _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.'),
231             'true'
232         );
233         $this->unli();
234
235         $this->li();
236         $this->input(
237             'required_team',
238              _m('Required team'),
239             _m('Only allow logins from users in the given team (Launchpad extension).'),
240             'openid'
241         );
242         $this->unli();
243
244         $this->out->elementEnd('ul');
245         $this->out->elementEnd('fieldset');
246
247         $this->out->elementStart(
248             'fieldset',
249             array('id' => 'settings_openid-options')
250         );
251         $this->out->element('legend', null, _m('Options'));
252
253         $this->out->elementStart('ul', 'form_data');
254
255         $this->li();
256
257         $this->out->checkbox(
258             'openidonly', _m('Enable OpenID-only mode'),
259             (bool) $this->value('openidonly', 'site'),
260             _m('Require all users to login via OpenID. WARNING: disables password authentication for all users!'),
261             'true'
262         );
263         $this->unli();
264
265         $this->out->elementEnd('ul');
266
267         $this->out->elementEnd('fieldset');
268     }
269
270     /**
271      * Action elements
272      *
273      * @return void
274      */
275
276     function formActions()
277     {
278         $this->out->submit('submit', _('Save'), 'submit', null, _m('Save OpenID settings'));
279     }
280 }