]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidsettings.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / OpenID / openidsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Settings for OpenID
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    Evan Prodromou <evan@status.net>
25  * @copyright 2008-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') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
35 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
36
37 /**
38  * Settings for OpenID
39  *
40  * Lets users add, edit and delete OpenIDs from their account
41  *
42  * @category Settings
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48
49 class OpenidsettingsAction extends AccountSettingsAction
50 {
51     /**
52      * Title of the page
53      *
54      * @return string Page title
55      */
56
57     function title()
58     {
59         return _m('OpenID settings');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return string Instructions for use
66      */
67
68     function getInstructions()
69     {
70         return _m('[OpenID](%%doc.openid%%) lets you log into many sites' .
71                  ' with the same user account.'.
72                  ' Manage your associated OpenIDs from here.');
73     }
74
75     function showScripts()
76     {
77         parent::showScripts();
78         $this->autofocus('openid_url');
79     }
80
81     /**
82      * Show the form for OpenID management
83      *
84      * We have one form with a few different submit buttons to do different things.
85      *
86      * @return void
87      */
88
89     function showContent()
90     {
91         $user = common_current_user();
92
93         $this->elementStart('form', array('method' => 'post',
94                                           'id' => 'form_settings_openid_add',
95                                           'class' => 'form_settings',
96                                           'action' =>
97                                           common_local_url('openidsettings')));
98         $this->elementStart('fieldset', array('id' => 'settings_openid_add'));
99         $this->element('legend', null, _m('Add OpenID'));
100         $this->hidden('token', common_session_token());
101         $this->element('p', 'form_guide',
102                        _m('If you want to add an OpenID to your account, ' .
103                          'enter it in the box below and click "Add".'));
104         $this->elementStart('ul', 'form_data');
105         $this->elementStart('li');
106         $this->element('label', array('for' => 'openid_url'),
107                        _m('OpenID URL'));
108         $this->element('input', array('name' => 'openid_url',
109                                       'type' => 'text',
110                                       'id' => 'openid_url'));
111         $this->elementEnd('li');
112         $this->elementEnd('ul');
113         $this->element('input', array('type' => 'submit',
114                                       'id' => 'settings_openid_add_action-submit',
115                                       'name' => 'add',
116                                       'class' => 'submit',
117                                       'value' => _m('Add')));
118         $this->elementEnd('fieldset');
119         $this->elementEnd('form');
120
121         $oid = new User_openid();
122
123         $oid->user_id = $user->id;
124
125         $cnt = $oid->find();
126
127         if ($cnt > 0) {
128
129             $this->element('h2', null, _m('Remove OpenID'));
130
131             if ($cnt == 1 && !$user->password) {
132
133                 $this->element('p', 'form_guide',
134                                _m('Removing your only OpenID '.
135                                  'would make it impossible to log in! ' .
136                                  'If you need to remove it, '.
137                                  'add another OpenID first.'));
138
139                 if ($oid->fetch()) {
140                     $this->elementStart('p');
141                     $this->element('a', array('href' => $oid->canonical),
142                                    $oid->display);
143                     $this->elementEnd('p');
144                 }
145
146             } else {
147
148                 $this->element('p', 'form_guide',
149                                _m('You can remove an OpenID from your account '.
150                                  'by clicking the button marked "Remove".'));
151                 $idx = 0;
152
153                 while ($oid->fetch()) {
154                     $this->elementStart('form',
155                                         array('method' => 'POST',
156                                               'id' => 'form_settings_openid_delete' . $idx,
157                                               'class' => 'form_settings',
158                                               'action' =>
159                                               common_local_url('openidsettings')));
160                     $this->elementStart('fieldset');
161                     $this->hidden('token', common_session_token());
162                     $this->element('a', array('href' => $oid->canonical),
163                                    $oid->display);
164                     $this->element('input', array('type' => 'hidden',
165                                                   'id' => 'openid_url'.$idx,
166                                                   'name' => 'openid_url',
167                                                   'value' => $oid->canonical));
168                     $this->element('input', array('type' => 'submit',
169                                                   'id' => 'remove'.$idx,
170                                                   'name' => 'remove',
171                                                   'class' => 'submit remove',
172                                                   'value' => _m('Remove')));
173                     $this->elementEnd('fieldset');
174                     $this->elementEnd('form');
175                     $idx++;
176                 }
177             }
178         }
179
180         $this->elementStart('form', array('method' => 'post',
181                                           'id' => 'form_settings_openid_trustroots',
182                                           'class' => 'form_settings',
183                                           'action' =>
184                                           common_local_url('openidsettings')));
185         $this->elementStart('fieldset', array('id' => 'settings_openid_trustroots'));
186         $this->element('legend', null, _m('OpenID Trusted Sites'));
187         $this->hidden('token', common_session_token());
188         $this->element('p', 'form_guide',
189                        _m('The following sites are allowed to access your ' .
190                        'identity and log you in. You can remove a site from ' .
191                        'this list to deny it access to your OpenID.'));
192         $this->elementStart('ul', 'form_data');
193         $user_openid_trustroot = new User_openid_trustroot();
194         $user_openid_trustroot->user_id=$user->id;
195         if($user_openid_trustroot->find()) {
196             while($user_openid_trustroot->fetch()) {
197                 $this->elementStart('li');
198                 $this->element('input', array('name' => 'openid_trustroot[]',
199                                               'type' => 'checkbox',
200                                               'class' => 'checkbox',
201                                               'value' => $user_openid_trustroot->trustroot,
202                                               'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)));
203                 $this->element('label', array('class'=>'checkbox', 'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)),
204                                $user_openid_trustroot->trustroot);
205                 $this->elementEnd('li');
206             }
207         }
208         $this->elementEnd('ul');
209         $this->element('input', array('type' => 'submit',
210                                       'id' => 'settings_openid_trustroots_action-submit',
211                                       'name' => 'remove_trustroots',
212                                       'class' => 'submit',
213                                       'value' => _m('Remove')));
214         $this->elementEnd('fieldset');
215         $this->elementEnd('form');
216     }
217
218     /**
219      * Handle a POST request
220      *
221      * Muxes to different sub-functions based on which button was pushed
222      *
223      * @return void
224      */
225
226     function handlePost()
227     {
228         // CSRF protection
229         $token = $this->trimmed('token');
230         if (!$token || $token != common_session_token()) {
231             $this->showForm(_m('There was a problem with your session token. '.
232                               'Try again, please.'));
233             return;
234         }
235
236         if ($this->arg('add')) {
237             $result = oid_authenticate($this->trimmed('openid_url'),
238                                        'finishaddopenid');
239             if (is_string($result)) { // error message
240                 $this->showForm($result);
241             }
242         } else if ($this->arg('remove')) {
243             $this->removeOpenid();
244         } else if($this->arg('remove_trustroots')) {
245             $this->removeTrustroots();
246         } else {
247             $this->showForm(_m('Something weird happened.'));
248         }
249     }
250
251     /**
252      * Handles a request to remove OpenID trustroots from the user's account
253      *
254      * Validates input and, if everything is OK, deletes the trustroots.
255      * Reloads the form with a success or error notification.
256      *
257      * @return void
258      */
259
260     function removeTrustroots()
261     {
262         $user = common_current_user();
263         $trustroots = $this->arg('openid_trustroot');
264         if($trustroots) {
265             foreach($trustroots as $trustroot) {
266                 $user_openid_trustroot = User_openid_trustroot::pkeyGet(
267                                                 array('user_id'=>$user->id, 'trustroot'=>$trustroot));
268                 if($user_openid_trustroot) {
269                     $user_openid_trustroot->delete();
270                 } else {
271                     $this->showForm(_m('No such OpenID trustroot.'));
272                     return;
273                 }
274             }
275             $this->showForm(_m('Trustroots removed'), true);
276         } else {
277             $this->showForm();
278         }
279         return;
280     }
281
282     /**
283      * Handles a request to remove an OpenID from the user's account
284      *
285      * Validates input and, if everything is OK, deletes the OpenID.
286      * Reloads the form with a success or error notification.
287      *
288      * @return void
289      */
290
291     function removeOpenid()
292     {
293         $openid_url = $this->trimmed('openid_url');
294
295         $oid = User_openid::staticGet('canonical', $openid_url);
296
297         if (!$oid) {
298             $this->showForm(_m('No such OpenID.'));
299             return;
300         }
301         $cur = common_current_user();
302         if (!$cur || $oid->user_id != $cur->id) {
303             $this->showForm(_m('That OpenID does not belong to you.'));
304             return;
305         }
306         $oid->delete();
307         $this->showForm(_m('OpenID removed.'), true);
308         return;
309     }
310 }