]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/actions/openidsettings.php
[OpenID] Add sync confirmation in both OpenID settings and login connection
[quix0rs-gnu-social.git] / plugins / OpenID / 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('GNUSOCIAL')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
35
36 /**
37  * Settings for OpenID
38  *
39  * Lets users add, edit and delete OpenIDs from their account
40  *
41  * @category Settings
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47 class OpenidsettingsAction extends SettingsAction
48 {
49     /**
50      * Title of the page
51      *
52      * @return string Page title
53      */
54     public function title()
55     {
56         // TRANS: Title of OpenID settings page for a user.
57         return _m('TITLE', 'OpenID settings');
58     }
59
60     /**
61      * Instructions for use
62      *
63      * @return string Instructions for use
64      */
65     public function getInstructions()
66     {
67         // TRANS: Form instructions for OpenID settings.
68         // TRANS: This message contains Markdown links in the form [description](link).
69         return _m('[OpenID](%%doc.openid%%) lets you log into many sites ' .
70                   'with the same user account. '.
71                   'Manage your associated OpenIDs from here.');
72     }
73
74     public function showScripts()
75     {
76         parent::showScripts();
77         $this->autofocus('openid_url');
78     }
79
80     /**
81      * Show the form for OpenID management
82      *
83      * We have one form with a few different submit buttons to do different things.
84      *
85      * @return void
86      */
87     public function showContent()
88     {
89         if (!common_config('openid', 'trusted_provider')) {
90             $this->elementStart('form', ['method' => 'post',
91                                          'id' => 'form_settings_openid_add',
92                                          'class' => 'form_settings',
93                                          'action' =>
94                                          common_local_url('openidsettings')]);
95             $this->elementStart('fieldset', ['id' => 'settings_openid_add']);
96     
97             // TRANS: Fieldset legend.
98             $this->element('legend', null, _m('LEGEND', 'Add OpenID'));
99             $this->hidden('token', common_session_token());
100             $this->elementStart('ul', 'form_data');
101             $this->elementStart('li');
102             // TRANS: Field label.
103             $this->input('openid_url', _m('OpenID URL'), null,
104                          // TRANS: Form guide.
105                          _m('An OpenID URL which identifies you.'),
106                          null, true,
107                          ['placeholder'=>'https://example.com/you']);
108             $this->elementEnd('li');
109             $this->elementStart('li');
110             // TRANS: Field label.
111             $this->checkbox('openid-sync', _m('Sync Account'), false,
112                 _m('Syncronize GNU social profile with this OpenID identity.'));
113             $this->elementEnd('li');
114             $this->elementEnd('ul');
115             // TRANS: Button text for adding an OpenID URL.
116             $this->submit('settings_openid_add_action-submit', _m('BUTTON', 'Add'), 'submit', 'add');
117             $this->elementEnd('fieldset');
118             $this->elementEnd('form');
119         }
120         $oid = new User_openid();
121
122         $oid->user_id = $this->scoped->getID();
123
124         $cnt = $oid->find();
125
126         if ($cnt > 0) {
127             // TRANS: Header on OpenID settings page.
128             $this->element('h2', null, _m('HEADER', 'Remove OpenID'));
129
130             if ($cnt == 1 && !$this->scoped->hasPassword()) {
131                 $this->element('p', 'form_guide',
132                                // TRANS: Form guide.
133                                _m('Removing your only OpenID '.
134                                   'would make it impossible to log in! ' .
135                                   'If you need to remove it, '.
136                                   'add another OpenID first.'));
137
138                 if ($oid->fetch()) {
139                     $this->elementStart('p');
140                     $this->element('a', ['href' => $oid->canonical], $oid->display);
141                     $this->elementEnd('p');
142                 }
143             } else {
144                 $this->element('p', 'form_guide',
145                                // TRANS: Form guide.
146                                _m('You can remove an OpenID from your account '.
147                                   'by clicking the button marked "Remove".'));
148                 $idx = 0;
149
150                 while ($oid->fetch()) {
151                     $this->elementStart('form', ['method' => 'POST',
152                                                  'id' => 'form_settings_openid_delete' . $idx,
153                                                  'class' => 'form_settings',
154                                                  'action' =>
155                                                  common_local_url('openidsettings')]);
156                     $this->elementStart('fieldset');
157                     $this->hidden('token', common_session_token());
158                     $this->element('a', ['href' => $oid->canonical], $oid->display);
159                     $this->hidden("openid_url{$idx}", $oid->canonical, 'openid_url');
160                     // TRANS: Button text to remove an OpenID.
161                     $this->submit("remove{$idx}", _m('BUTTON', 'Remove'), 'submit remove', 'remove');
162                     $this->elementEnd('fieldset');
163                     $this->elementEnd('form');
164                     $idx++;
165                 }
166             }
167         }
168
169         $this->elementStart('form', ['method' => 'post',
170                                      'id' => 'form_settings_openid_trustroots',
171                                      'class' => 'form_settings',
172                                      'action' =>
173                                      common_local_url('openidsettings')]);
174         $this->elementStart('fieldset', ['id' => 'settings_openid_trustroots']);
175         // TRANS: Fieldset legend.
176         $this->element('legend', null, _m('OpenID Trusted Sites'));
177         $this->hidden('token', common_session_token());
178         $this->element('p', 'form_guide',
179                        // TRANS: Form guide.
180                        _m('The following sites are allowed to access your ' .
181                           'identity and log you in. You can remove a site from ' .
182                           'this list to deny it access to your OpenID.'));
183         $this->elementStart('ul', 'form_data');
184         $user_openid_trustroot = new User_openid_trustroot();
185         $user_openid_trustroot->user_id = $this->scoped->getID();
186         if ($user_openid_trustroot->find()) {
187             while ($user_openid_trustroot->fetch()) {
188                 $this->elementStart('li');
189                 $this->element('input', ['name' => 'openid_trustroot[]',
190                                          'type' => 'checkbox',
191                                          'class' => 'checkbox',
192                                          'value' => $user_openid_trustroot->trustroot,
193                                          'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)]);
194                 $this->element('label',
195                                ['class'=>'checkbox',
196                                 'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)],
197                                $user_openid_trustroot->trustroot);
198                 $this->elementEnd('li');
199             }
200         }
201         $this->elementEnd('ul');
202         // TRANS: Button text to remove an OpenID trustroot.
203         $this->submit('settings_openid_trustroots_action-submit', _m('BUTTON', 'Remove'), 'submit', 'remove_trustroots');
204         $this->elementEnd('fieldset');
205         
206         $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
207
208         $this->elementStart('fieldset');
209         $this->element('legend', null, _m('LEGEND', 'Preferences'));
210         $this->elementStart('ul', 'form_data');
211         $this->checkbox('hide_profile_link', "Hide OpenID links from my profile", !empty($prefs) && $prefs->hide_profile_link);
212         // TRANS: Button text to save OpenID prefs
213         $this->submit('settings_openid_prefs_save', _m('BUTTON', 'Save'), 'submit', 'save_prefs');
214         $this->elementEnd('ul');
215         $this->elementEnd('fieldset');
216
217         $this->elementEnd('form');
218     }
219
220     /**
221      * Handle a POST request
222      *
223      * Muxes to different sub-functions based on which button was pushed
224      *
225      * @return void
226      */
227     protected function doPost()
228     {
229         if ($this->arg('add')) {
230             if (common_config('openid', 'trusted_provider')) {
231                 // TRANS: Form validation error if no OpenID providers can be added.
232                 throw new ServerException(_m('Cannot add new providers.'));
233             } else {
234                 common_ensure_session();
235                 $_SESSION['openid_sync'] = $this->boolean('openid-sync');
236                 
237                 $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
238                 if (is_string($result)) { // error message
239                     unset($_SESSION['openid-sync']);
240                     throw new ServerException($result);
241                 }
242                 return _('Added new provider.');
243             }
244         } elseif ($this->arg('remove')) {
245             return $this->removeOpenid();
246         } elseif ($this->arg('remove_trustroots')) {
247             return $this->removeTrustroots();
248         } elseif ($this->arg('save_prefs')) {
249             return $this->savePrefs();
250         }
251
252         // TRANS: Unexpected form validation error.
253         throw new ServerException(_m('No known action for POST.'));
254     }
255
256     /**
257      * Handles a request to remove OpenID trustroots from the user's account
258      *
259      * Validates input and, if everything is OK, deletes the trustroots.
260      * Reloads the form with a success or error notification.
261      *
262      * @return void
263      */
264     public function removeTrustroots()
265     {
266         $trustroots = $this->arg('openid_trustroot', []);
267         foreach ($trustroots as $trustroot) {
268             $user_openid_trustroot = User_openid_trustroot::pkeyGet(
269                 ['user_id'=>$this->scoped->getID(), 'trustroot'=>$trustroot]
270             );
271             if ($user_openid_trustroot) {
272                 $user_openid_trustroot->delete();
273             } else {
274                 // TRANS: Form validation error when trying to remove a non-existing trustroot.
275                 throw new ClientException(_m('No such OpenID trustroot.'));
276             }
277         }
278
279         // TRANS: Success message after removing trustroots.
280         return _m('Trustroots removed.');
281     }
282
283     /**
284      * Handles a request to remove an OpenID from the user's account
285      *
286      * Validates input and, if everything is OK, deletes the OpenID.
287      * Reloads the form with a success or error notification.
288      *
289      * @return void
290      */
291     public function removeOpenid()
292     {
293         $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
294
295         if (!$oid instanceof User_openid) {
296             // TRANS: Form validation error for a non-existing OpenID.
297             throw new ClientException(_m('No such OpenID.'));
298         }
299         if ($this->scoped->getID() != $oid->user_id) {
300             // TRANS: Form validation error if OpenID is connected to another user.
301             throw new ClientException(_m('That OpenID does not belong to you.'));
302         }
303         $oid->delete();
304         // TRANS: Success message after removing an OpenID.
305         return _m('OpenID removed.');
306     }
307
308     /**
309      * Handles a request to save preferences
310      *
311      * Validates input and, if everything is OK, deletes the OpenID.
312      * Reloads the form with a success or error notification.
313      *
314      * @return void
315      */
316     public function savePrefs()
317     {
318         $orig  = null;
319         $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
320
321         if (!$prefs instanceof User_openid_prefs) {
322             $prefs          = new User_openid_prefs();
323             $prefs->user_id = $this->scoped->getID();
324             $prefs->created = common_sql_now();
325         } else {
326             $orig = clone($prefs);
327         }
328
329         $prefs->hide_profile_link = $this->booleanintstring('hide_profile_link');
330
331         if ($orig instanceof User_openid_prefs) {
332             $prefs->update($orig);
333         } else {
334             $prefs->insert();
335         }
336
337         return _m('OpenID preferences saved.');
338     }
339 }