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')) {
34 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
39 * Lets users add, edit and delete OpenIDs from their account
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/
47 class OpenidsettingsAction extends SettingsAction
52 * @return string Page title
54 public function title()
56 // TRANS: Title of OpenID settings page for a user.
57 return _m('TITLE', 'OpenID settings');
61 * Instructions for use
63 * @return string Instructions for use
65 public function getInstructions()
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.');
74 public function showScripts()
76 parent::showScripts();
77 $this->autofocus('openid_url');
81 * Show the form for OpenID management
83 * We have one form with a few different submit buttons to do different things.
87 public function showContent()
89 if (!common_config('openid', 'trusted_provider')) {
90 $this->elementStart('form', ['method' => 'post',
91 'id' => 'form_settings_openid_add',
92 'class' => 'form_settings',
94 common_local_url('openidsettings')]);
95 $this->elementStart('fieldset', ['id' => 'settings_openid_add']);
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.'),
107 ['placeholder'=>'https://example.com/you']);
108 $this->elementEnd('li');
109 $this->elementStart('li');
110 // TRANS: Field label.
111 $this->checkbox('openid-synch', _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');
121 $oid = new User_openid();
123 $oid->user_id = $this->scoped->getID();
128 // TRANS: Header on OpenID settings page.
129 $this->element('h2', null, _m('HEADER', 'OpenID Actions'));
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".'));
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 synchronize OpenID with the GS profile.
150 $this->submit("synch", _m('BUTTON', 'Synchronize'), 'submit synch');
151 $this->elementEnd('fieldset');
152 $this->elementEnd('form');
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".'));
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("synch{$idx}", _m('BUTTON', 'Synchronize'), 'submit', 'synch');
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');
185 $this->elementStart('form', ['method' => 'post',
186 'id' => 'form_settings_openid_trustroots',
187 'class' => 'form_settings',
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');
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');
222 $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
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');
233 $this->elementEnd('form');
237 * Handle a POST request
239 * Muxes to different sub-functions based on which button was pushed
243 protected function doPost()
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.'));
250 common_ensure_session();
251 $_SESSION['openid_synch'] = $this->boolean('openid-synch');
253 $result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
254 if (is_string($result)) { // error message
255 unset($_SESSION['openid-synch']);
256 throw new ServerException($result);
258 return _('Added new provider.');
260 } elseif ($this->arg('remove')) {
261 return $this->removeOpenid();
262 } elseif ($this->arg('synch')) {
263 return $this->synchOpenid();
264 } elseif ($this->arg('remove_trustroots')) {
265 return $this->removeTrustroots();
266 } elseif ($this->arg('save_prefs')) {
267 return $this->savePrefs();
270 // TRANS: Unexpected form validation error.
271 throw new ServerException(_m('No known action for POST.'));
275 * Handles a request to remove OpenID trustroots from the user's account
277 * Validates input and, if everything is OK, deletes the trustroots.
278 * Reloads the form with a success or error notification.
282 public function removeTrustroots()
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]
289 if ($user_openid_trustroot) {
290 $user_openid_trustroot->delete();
292 // TRANS: Form validation error when trying to remove a non-existing trustroot.
293 throw new ClientException(_m('No such OpenID trustroot.'));
297 // TRANS: Success message after removing trustroots.
298 return _m('Trustroots removed.');
302 * Handles a request to remove an OpenID from the user's account
304 * Validates input and, if everything is OK, deletes the OpenID.
305 * Reloads the form with a success or error notification.
309 public function removeOpenid()
311 $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
313 if (!$oid instanceof User_openid) {
314 // TRANS: Form validation error for a non-existing OpenID.
315 throw new ClientException(_m('No such OpenID.'));
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.'));
322 // TRANS: Success message after removing an OpenID.
323 return _m('OpenID removed.');
327 * Handles a request to synch an OpenID to the user's profile
331 public function synchOpenid()
333 $oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
335 if (!$oid instanceof User_openid) {
336 throw new ClientException(_m('No such OpenID.'));
339 $result = oid_authenticate($this->trimmed('openid_url'), 'finishsynchopenid');
340 if (is_string($result)) { // error message
341 throw new ServerException($result);
343 return _m('Synchronized OpenID.');
347 * Handles a request to save preferences
349 * Validates input and, if everything is OK, deletes the OpenID.
350 * Reloads the form with a success or error notification.
354 public function savePrefs()
357 $prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());
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();
364 $orig = clone($prefs);
367 $prefs->hide_profile_link = $this->booleanintstring('hide_profile_link');
369 if ($orig instanceof User_openid_prefs) {
370 $prefs->update($orig);
375 return _m('OpenID preferences saved.');