]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/actions/openidsettings.php
Merge branch 'openid-settings-sync' of tenma/gnu-social into nightly
[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('Synchronize Account'), false,
112                             // TRANS: Form guide.
113                             _m('Synchronize GNU social profile with this OpenID identity.'));
114             $this->elementEnd('li');
115             $this->elementEnd('ul');
116             // TRANS: Button text for adding an OpenID URL.
117             $this->submit('settings_openid_add_action-submit', _m('BUTTON', 'Add'), 'submit', 'add');
118             $this->elementEnd('fieldset');
119             $this->elementEnd('form');
120         }
121         $oid = new User_openid();
122
123         $oid->user_id = $this->scoped->getID();
124
125         $cnt = $oid->find();
126
127         if ($cnt > 0) {
128             // TRANS: Header on OpenID settings page.
129             $this->element('h2', null, _m('HEADER', 'OpenID Actions'));
130             
131             if ($cnt == 1 && !$this->scoped->hasPassword()) {
132                 $this->element('p', 'form_guide',
133                                // TRANS: Form guide.
134                                _m('You can\'t remove your main OpenID account ' .
135                                   'without either adding a password to your ' .
136                                   'GNU social account or another OpenID account. ' .
137                                   'You can synchronize your profile with your ' .
138                                   'OpenID by clicking the button labeled "Synchronize".'));
139
140                 if ($oid->fetch()) {
141                     $this->elementStart('form', ['method' => 'POST',
142                                                  'id' => 'form_settings_openid_actions' . $idx,
143                                                  'class' => 'form_settings',
144                                                  'action' => common_local_url('openidsettings')]);
145                     $this->elementStart('fieldset');
146                     $this->hidden('token', common_session_token());
147                     $this->element('a', ['href' => $oid->canonical], $oid->display);
148                     $this->hidden("openid_url", $oid->canonical);
149                     // TRANS: Button text to sync OpenID with the GS profile.
150                     $this->submit("sync", _m('BUTTON', 'Synchronize'), 'submit sync');
151                     $this->elementEnd('fieldset');
152                     $this->elementEnd('form');
153                 }
154             } else {
155                 $this->element('p', 'form_guide',
156                                // TRANS: Form guide.
157                                _m('You can remove an OpenID from your account ' .
158                                   'by clicking the button labeled "Remove". ' .
159                                   'You can synchronize your profile with an OpenID ' .
160                                   'by clicking the button labeled "Synchronize".'));
161                 $idx = 0;
162
163                 while ($oid->fetch()) {
164                     $this->elementStart('form', ['method' => 'POST',
165                                                  'id' => 'form_settings_openid_actions' . $idx,
166                                                  'class' => 'form_settings',
167                                                  'action' => common_local_url('openidsettings')]);
168                     $this->elementStart('fieldset');
169                     $this->hidden('token', common_session_token());
170                     $this->element('a', ['href' => $oid->canonical], $oid->display);
171                     $this->hidden("openid_url{$idx}", $oid->canonical, 'openid_url');
172                     $this->elementStart('span', ['class' => 'element_actions']);
173                     // TRANS: Button text to sync an OpenID with the GS profile.
174                     $this->submit("sync{$idx}", _m('BUTTON', 'Synchronize'), 'submit', 'sync');
175                     // TRANS: Button text to remove an OpenID.
176                     $this->submit("remove{$idx}", _m('BUTTON', 'Remove'), 'submit', 'remove');
177                     $this->elementEnd('span');
178                     $this->elementEnd('fieldset');
179                     $this->elementEnd('form');
180                     $idx++;
181                 }
182             }
183         }
184
185         $this->elementStart('form', ['method' => 'post',
186                                      'id' => 'form_settings_openid_trustroots',
187                                      'class' => 'form_settings',
188                                      'action' =>
189                                      common_local_url('openidsettings')]);
190         $this->elementStart('fieldset', ['id' => 'settings_openid_trustroots']);
191         // TRANS: Fieldset legend.
192         $this->element('legend', null, _m('OpenID Trusted Sites'));
193         $this->hidden('token', common_session_token());
194         $this->element('p', 'form_guide',
195                        // TRANS: Form guide.
196                        _m('The following sites are allowed to access your ' .
197                           'identity and log you in. You can remove a site from ' .
198                           'this list to deny it access to your OpenID.'));
199         $this->elementStart('ul', 'form_data');
200         $user_openid_trustroot = new User_openid_trustroot();
201         $user_openid_trustroot->user_id = $this->scoped->getID();
202         if ($user_openid_trustroot->find()) {
203             while ($user_openid_trustroot->fetch()) {
204                 $this->elementStart('li');
205                 $this->element('input', ['name' => 'openid_trustroot[]',
206                                          'type' => 'checkbox',
207                                          'class' => 'checkbox',
208                                          'value' => $user_openid_trustroot->trustroot,
209                                          'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)]);
210                 $this->element('label',
211                                ['class'=>'checkbox',
212                                 'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)],
213                                $user_openid_trustroot->trustroot);
214                 $this->elementEnd('li');
215             }
216         }
217         $this->elementEnd('ul');
218         // TRANS: Button text to remove an OpenID trustroot.
219         $this->submit('settings_openid_trustroots_action-submit', _m('BUTTON', 'Remove'), 'submit', 'remove_trustroots');
220         $this->elementEnd('fieldset');
221         
222         $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
223
224         $this->elementStart('fieldset');
225         $this->element('legend', null, _m('LEGEND', 'Preferences'));
226         $this->elementStart('ul', 'form_data');
227         $this->checkbox('hide_profile_link', "Hide OpenID links from my profile", !empty($prefs) && $prefs->hide_profile_link);
228         // TRANS: Button text to save OpenID prefs
229         $this->submit('settings_openid_prefs_save', _m('BUTTON', 'Save'), 'submit', 'save_prefs');
230         $this->elementEnd('ul');
231         $this->elementEnd('fieldset');
232
233         $this->elementEnd('form');
234     }
235
236     /**
237      * Handle a POST request
238      *
239      * Muxes to different sub-functions based on which button was pushed
240      *
241      * @return void
242      */
243     protected function doPost()
244     {
245         if ($this->arg('add')) {
246             if (common_config('openid', 'trusted_provider')) {
247                 // TRANS: Form validation error if no OpenID providers can be added.
248                 throw new ServerException(_m('Cannot add new providers.'));
249             } else {
250                 common_ensure_session();
251                 $_SESSION['openid_sync'] = $this->boolean('openid-sync');
252                 
253                 $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
254                 if (is_string($result)) { // error message
255                     unset($_SESSION['openid-sync']);
256                     throw new ServerException($result);
257                 }
258                 return _('Added new provider.');
259             }
260         } elseif ($this->arg('remove')) {
261             return $this->removeOpenid();
262         } elseif ($this->arg('sync')) {
263             return $this->syncOpenid();
264         } elseif ($this->arg('remove_trustroots')) {
265             return $this->removeTrustroots();
266         } elseif ($this->arg('save_prefs')) {
267             return $this->savePrefs();
268         }
269
270         // TRANS: Unexpected form validation error.
271         throw new ServerException(_m('No known action for POST.'));
272     }
273
274     /**
275      * Handles a request to remove OpenID trustroots from the user's account
276      *
277      * Validates input and, if everything is OK, deletes the trustroots.
278      * Reloads the form with a success or error notification.
279      *
280      * @return void
281      */
282     public function removeTrustroots()
283     {
284         $trustroots = $this->arg('openid_trustroot', []);
285         foreach ($trustroots as $trustroot) {
286             $user_openid_trustroot = User_openid_trustroot::pkeyGet(
287                 ['user_id'=>$this->scoped->getID(), 'trustroot'=>$trustroot]
288             );
289             if ($user_openid_trustroot) {
290                 $user_openid_trustroot->delete();
291             } else {
292                 // TRANS: Form validation error when trying to remove a non-existing trustroot.
293                 throw new ClientException(_m('No such OpenID trustroot.'));
294             }
295         }
296
297         // TRANS: Success message after removing trustroots.
298         return _m('Trustroots removed.');
299     }
300
301     /**
302      * Handles a request to remove an OpenID from the user's account
303      *
304      * Validates input and, if everything is OK, deletes the OpenID.
305      * Reloads the form with a success or error notification.
306      *
307      * @return void
308      */
309     public function removeOpenid()
310     {
311         $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
312
313         if (!$oid instanceof User_openid) {
314             // TRANS: Form validation error for a non-existing OpenID.
315             throw new ClientException(_m('No such OpenID.'));
316         }
317         if ($this->scoped->getID() != $oid->user_id) {
318             // TRANS: Form validation error if OpenID is connected to another user.
319             throw new ClientException(_m('That OpenID does not belong to you.'));
320         }
321         $oid->delete();
322         // TRANS: Success message after removing an OpenID.
323         return _m('OpenID removed.');
324     }
325
326     /**
327      * Handles a request to sync an OpenID to the user's profile
328      *
329      * @return void
330      */
331     public function syncOpenid()
332     {
333         $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
334
335         if (!$oid instanceof User_openid) {
336             throw new ClientException(_m('No such OpenID.'));
337         }
338         
339         $result = oid_authenticate($this->trimmed('openid_url'), 'finishsyncopenid');
340         if (is_string($result)) { // error message
341             throw new ServerException($result);
342         }
343         return _m('Synchronized OpenID.');
344     }
345
346     /**
347      * Handles a request to save preferences
348      *
349      * Validates input and, if everything is OK, deletes the OpenID.
350      * Reloads the form with a success or error notification.
351      *
352      * @return void
353      */
354     public function savePrefs()
355     {
356         $orig  = null;
357         $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
358
359         if (!$prefs instanceof User_openid_prefs) {
360             $prefs          = new User_openid_prefs();
361             $prefs->user_id = $this->scoped->getID();
362             $prefs->created = common_sql_now();
363         } else {
364             $orig = clone($prefs);
365         }
366
367         $prefs->hide_profile_link = $this->booleanintstring('hide_profile_link');
368
369         if ($orig instanceof User_openid_prefs) {
370             $prefs->update($orig);
371         } else {
372             $prefs->insert();
373         }
374
375         return _m('OpenID preferences saved.');
376     }
377 }