]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/openidsettings.php
5f59ebc014d57bfe9ff4a1279dc3a4714e98fb72
[quix0rs-gnu-social.git] / actions / openidsettings.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!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  Laconica
44  * @author   Evan Prodromou <evan@controlyourself.ca>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://laconi.ca/
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     /**
76      * Show the form for OpenID management
77      *
78      * We have one form with a few different submit buttons to do different things.
79      *
80      * @return void
81      */
82
83     function showContent()
84     {
85         $user = common_current_user();
86
87         $this->elementStart('form', array('method' => 'post',
88                                           'id' => 'form_settings_openid_add',
89                                           'class' => 'form_settings',
90                                           'action' =>
91                                           common_local_url('openidsettings')));
92         $this->elementStart('fieldset', array('id' => 'settings_openid_add'));
93         $this->element('legend', null, _('Add OpenID'));
94         $this->hidden('token', common_session_token());
95         $this->element('p', 'form_guide',
96                        _('If you want to add an OpenID to your account, ' .
97                          'enter it in the box below and click "Add".'));
98         $this->elementStart('ul', 'form_data');
99         $this->elementStart('li');
100         $this->element('label', array('for' => 'openid_url'),
101                        _('OpenID URL'));
102         $this->element('input', array('name' => 'openid_url',
103                                       'type' => 'text',
104                                       'id' => 'openid_url'));
105         $this->elementEnd('li');
106         $this->elementEnd('ul');
107         $this->element('input', array('type' => 'submit',
108                                       'id' => 'settings_openid_add_action-submit',
109                                       'name' => 'add',
110                                       'class' => 'submit',
111                                       'value' => _('Add')));
112         $this->elementEnd('fieldset');
113         $this->elementEnd('form');
114
115         $oid = new User_openid();
116
117         $oid->user_id = $user->id;
118
119         $cnt = $oid->find();
120
121         if ($cnt > 0) {
122
123             $this->element('h2', null, _('Remove OpenID'));
124
125             if ($cnt == 1 && !$user->password) {
126
127                 $this->element('p', 'form_guide',
128                                _('Removing your only OpenID '.
129                                  'would make it impossible to log in! ' .
130                                  'If you need to remove it, '.
131                                  'add another OpenID first.'));
132
133                 if ($oid->fetch()) {
134                     $this->elementStart('p');
135                     $this->element('a', array('href' => $oid->canonical),
136                                    $oid->display);
137                     $this->elementEnd('p');
138                 }
139
140             } else {
141
142                 $this->element('p', 'form_guide',
143                                _('You can remove an OpenID from your account '.
144                                  'by clicking the button marked "Remove".'));
145                 $idx = 0;
146
147                 while ($oid->fetch()) {
148                     $this->elementStart('form',
149                                         array('method' => 'POST',
150                                               'id' => 'form_settings_openid_delete' . $idx,
151                                               'class' => 'form_settings',
152                                               'action' =>
153                                               common_local_url('openidsettings')));
154                     $this->elementStart('fieldset');
155                     $this->hidden('token', common_session_token());
156                     $this->element('a', array('href' => $oid->canonical),
157                                    $oid->display);
158                     $this->element('input', array('type' => 'hidden',
159                                                   'id' => 'openid_url'.$idx,
160                                                   'name' => 'openid_url',
161                                                   'value' => $oid->canonical));
162                     $this->element('input', array('type' => 'submit',
163                                                   'id' => 'remove'.$idx,
164                                                   'name' => 'remove',
165                                                   'class' => 'submit remove',
166                                                   'value' => _('Remove')));
167                     $this->elementEnd('fieldset');
168                     $this->elementEnd('form');
169                     $idx++;
170                 }
171             }
172         }
173     }
174
175     /**
176      * Handle a POST request
177      *
178      * Muxes to different sub-functions based on which button was pushed
179      *
180      * @return void
181      */
182
183     function handlePost()
184     {
185         // CSRF protection
186         $token = $this->trimmed('token');
187         if (!$token || $token != common_session_token()) {
188             $this->showForm(_('There was a problem with your session token. '.
189                               'Try again, please.'));
190             return;
191         }
192
193         if ($this->arg('add')) {
194             $result = oid_authenticate($this->trimmed('openid_url'),
195                                        'finishaddopenid');
196             if (is_string($result)) { // error message
197                 $this->showForm($result);
198             }
199         } else if ($this->arg('remove')) {
200             $this->removeOpenid();
201         } else {
202             $this->showForm(_('Something weird happened.'));
203         }
204     }
205
206     /**
207      * Handles a request to remove an OpenID from the user's account
208      *
209      * Validates input and, if everything is OK, deletes the OpenID.
210      * Reloads the form with a success or error notification.
211      *
212      * @return void
213      */
214
215     function removeOpenid()
216     {
217         $openid_url = $this->trimmed('openid_url');
218
219         $oid = User_openid::staticGet('canonical', $openid_url);
220
221         if (!$oid) {
222             $this->showForm(_('No such OpenID.'));
223             return;
224         }
225         $cur = common_current_user();
226         if (!$cur || $oid->user_id != $cur->id) {
227             $this->showForm(_('That OpenID does not belong to you.'));
228             return;
229         }
230         $oid->delete();
231         $this->showForm(_('OpenID removed.'), true);
232         return;
233     }
234 }