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