3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
30 if (!defined('GNUSOCIAL')) { exit(1); }
32 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
37 * Lets users add, edit and delete OpenIDs from their account
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/
45 class OpenidsettingsAction extends SettingsAction
50 * @return string Page title
54 // TRANS: Title of OpenID settings page for a user.
55 return _m('TITLE','OpenID settings');
59 * Instructions for use
61 * @return string Instructions for use
63 function getInstructions()
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.');
72 function showScripts()
74 parent::showScripts();
75 $this->autofocus('openid_url');
79 * Show the form for OpenID management
81 * We have one form with a few different submit buttons to do different things.
85 function showContent()
87 if (!common_config('openid', 'trusted_provider')) {
88 $this->elementStart('form', array('method' => 'post',
89 'id' => 'form_settings_openid_add',
90 'class' => 'form_settings',
92 common_local_url('openidsettings')));
93 $this->elementStart('fieldset', array('id' => 'settings_openid_add'));
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');
112 $oid = new User_openid();
114 $oid->user_id = $this->scoped->getID();
119 // TRANS: Header on OpenID settings page.
120 $this->element('h2', null, _m('HEADER','Remove OpenID'));
122 if ($cnt == 1 && !$this->scoped->hasPassword()) {
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.'));
132 $this->elementStart('p');
133 $this->element('a', array('href' => $oid->canonical),
135 $this->elementEnd('p');
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".'));
146 while ($oid->fetch()) {
147 $this->elementStart('form',
148 array('method' => 'POST',
149 'id' => 'form_settings_openid_delete' . $idx,
150 'class' => 'form_settings',
152 common_local_url('openidsettings')));
153 $this->elementStart('fieldset');
154 $this->hidden('token', common_session_token());
155 $this->element('a', array('href' => $oid->canonical),
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');
167 $this->elementStart('form', array('method' => 'post',
168 'id' => 'form_settings_openid_trustroots',
169 'class' => 'form_settings',
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');
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');
202 $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
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');
213 $this->elementEnd('form');
217 * Handle a POST request
219 * Muxes to different sub-functions based on which button was pushed
223 protected function doPost()
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.'));
230 $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
231 if (is_string($result)) { // error message
232 throw new ServerException($result);
234 return _('Added new provider.');
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();
244 // TRANS: Unexpected form validation error.
245 throw new ServerException(_m('No known action for POST.'));
249 * Handles a request to remove OpenID trustroots from the user's account
251 * Validates input and, if everything is OK, deletes the trustroots.
252 * Reloads the form with a success or error notification.
256 function removeTrustroots()
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();
265 // TRANS: Form validation error when trying to remove a non-existing trustroot.
266 throw new ClientException(_m('No such OpenID trustroot.'));
270 // TRANS: Success message after removing trustroots.
271 return _m('Trustroots removed.');
275 * Handles a request to remove an OpenID from the user's account
277 * Validates input and, if everything is OK, deletes the OpenID.
278 * Reloads the form with a success or error notification.
282 function removeOpenid()
284 $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
286 if (!$oid instanceof User_openid) {
287 // TRANS: Form validation error for a non-existing OpenID.
288 throw new ClientException(_m('No such OpenID.'));
290 if ($this->scoped->getID() != $oid->user_id) {
291 // TRANS: Form validation error if OpenID is connected to another user.
292 throw new ClientException(_m('That OpenID does not belong to you.'));
295 // TRANS: Success message after removing an OpenID.
296 return _m('OpenID removed.');
300 * Handles a request to save preferences
302 * Validates input and, if everything is OK, deletes the OpenID.
303 * Reloads the form with a success or error notification.
310 $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
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();
317 $orig = clone($prefs);
320 $prefs->hide_profile_link = $this->booleanintstring('hide_profile_link');
322 if ($orig instanceof User_openid_prefs) {
323 $prefs->update($orig);
328 return _m('OpenID preferences saved.');