]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/openidsettings.php
Merge branch '0.8.x' of git://gitorious.org/~brion/statusnet/brion-fixes into 0.8.x
[quix0rs-gnu-social.git] / actions / 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.'/lib/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 _('OpenID settings');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return string Instructions for use
66      */
67
68     function getInstructions()
69     {
70         return _('[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         if (!common_config('openid', 'enabled')) {
92             $this->element('div', array('class' => 'error'),
93                            _('OpenID is not available.'));
94             return;
95         }
96
97         $user = common_current_user();
98
99         $this->elementStart('form', array('method' => 'post',
100                                           'id' => 'form_settings_openid_add',
101                                           'class' => 'form_settings',
102                                           'action' =>
103                                           common_local_url('openidsettings')));
104         $this->elementStart('fieldset', array('id' => 'settings_openid_add'));
105         $this->element('legend', null, _('Add OpenID'));
106         $this->hidden('token', common_session_token());
107         $this->element('p', 'form_guide',
108                        _('If you want to add an OpenID to your account, ' .
109                          'enter it in the box below and click "Add".'));
110         $this->elementStart('ul', 'form_data');
111         $this->elementStart('li');
112         $this->element('label', array('for' => 'openid_url'),
113                        _('OpenID URL'));
114         $this->element('input', array('name' => 'openid_url',
115                                       'type' => 'text',
116                                       'id' => 'openid_url'));
117         $this->elementEnd('li');
118         $this->elementEnd('ul');
119         $this->element('input', array('type' => 'submit',
120                                       'id' => 'settings_openid_add_action-submit',
121                                       'name' => 'add',
122                                       'class' => 'submit',
123                                       'value' => _('Add')));
124         $this->elementEnd('fieldset');
125         $this->elementEnd('form');
126
127         $oid = new User_openid();
128
129         $oid->user_id = $user->id;
130
131         $cnt = $oid->find();
132
133         if ($cnt > 0) {
134
135             $this->element('h2', null, _('Remove OpenID'));
136
137             if ($cnt == 1 && !$user->password) {
138
139                 $this->element('p', 'form_guide',
140                                _('Removing your only OpenID '.
141                                  'would make it impossible to log in! ' .
142                                  'If you need to remove it, '.
143                                  'add another OpenID first.'));
144
145                 if ($oid->fetch()) {
146                     $this->elementStart('p');
147                     $this->element('a', array('href' => $oid->canonical),
148                                    $oid->display);
149                     $this->elementEnd('p');
150                 }
151
152             } else {
153
154                 $this->element('p', 'form_guide',
155                                _('You can remove an OpenID from your account '.
156                                  'by clicking the button marked "Remove".'));
157                 $idx = 0;
158
159                 while ($oid->fetch()) {
160                     $this->elementStart('form',
161                                         array('method' => 'POST',
162                                               'id' => 'form_settings_openid_delete' . $idx,
163                                               'class' => 'form_settings',
164                                               'action' =>
165                                               common_local_url('openidsettings')));
166                     $this->elementStart('fieldset');
167                     $this->hidden('token', common_session_token());
168                     $this->element('a', array('href' => $oid->canonical),
169                                    $oid->display);
170                     $this->element('input', array('type' => 'hidden',
171                                                   'id' => 'openid_url'.$idx,
172                                                   'name' => 'openid_url',
173                                                   'value' => $oid->canonical));
174                     $this->element('input', array('type' => 'submit',
175                                                   'id' => 'remove'.$idx,
176                                                   'name' => 'remove',
177                                                   'class' => 'submit remove',
178                                                   'value' => _('Remove')));
179                     $this->elementEnd('fieldset');
180                     $this->elementEnd('form');
181                     $idx++;
182                 }
183             }
184         }
185     }
186
187     /**
188      * Handle a POST request
189      *
190      * Muxes to different sub-functions based on which button was pushed
191      *
192      * @return void
193      */
194
195     function handlePost()
196     {
197         // CSRF protection
198         $token = $this->trimmed('token');
199         if (!$token || $token != common_session_token()) {
200             $this->showForm(_('There was a problem with your session token. '.
201                               'Try again, please.'));
202             return;
203         }
204
205         if ($this->arg('add')) {
206             $result = oid_authenticate($this->trimmed('openid_url'),
207                                        'finishaddopenid');
208             if (is_string($result)) { // error message
209                 $this->showForm($result);
210             }
211         } else if ($this->arg('remove')) {
212             $this->removeOpenid();
213         } else {
214             $this->showForm(_('Something weird happened.'));
215         }
216     }
217
218     /**
219      * Handles a request to remove an OpenID from the user's account
220      *
221      * Validates input and, if everything is OK, deletes the OpenID.
222      * Reloads the form with a success or error notification.
223      *
224      * @return void
225      */
226
227     function removeOpenid()
228     {
229         $openid_url = $this->trimmed('openid_url');
230
231         $oid = User_openid::staticGet('canonical', $openid_url);
232
233         if (!$oid) {
234             $this->showForm(_('No such OpenID.'));
235             return;
236         }
237         $cur = common_current_user();
238         if (!$cur || $oid->user_id != $cur->id) {
239             $this->showForm(_('That OpenID does not belong to you.'));
240             return;
241         }
242         $oid->delete();
243         $this->showForm(_('OpenID removed.'), true);
244         return;
245     }
246 }