3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/lib/openid.php');
25 class OpenidsettingsAction extends SettingsAction {
27 function show_form($msg=NULL, $success=false) {
29 $user = common_current_user();
31 common_show_header(_t('OpenID settings'), NULL, NULL, array($this, 'settings_menu'));
34 $this->message($msg, $success);
36 common_element('div', 'instructions',
37 _t('Manage your associated OpenIDs from here.'));
39 common_element_start('form', array('method' => 'POST',
42 common_local_url('openidsettings')));
43 common_element('h2', NULL, _t('Add OpenID'));
44 common_element('p', NULL,
45 _t('If you want to add an OpenID to your account, ',
46 'enter it in the box below and click "Add".'));
47 common_input('openid_url', _t('OpenID URL'));
48 common_submit('add', _t('Add'));
49 common_element_end('form');
51 $oid = new User_openid();
52 $oid->user_id = $user->id;
56 common_element('h2', NULL, _t('OpenID'));
57 common_element('p', NULL,
58 _t('You can remove an OpenID from your account ',
59 'by clicking the button marked "Delete" next to it.'));
62 while ($oid->fetch()) {
63 common_element_start('p');
64 common_element_start('form', array('method' => 'POST',
65 'id' => 'openiddelete-' . $idx,
67 common_local_url('openidsettings')));
68 common_element('a', array('href' => $oid->canonical),
70 common_hidden('openid_url', $oid->canonical);
71 common_submit('remove', _t('Remove'));
72 common_element_end('form');
73 common_element_end('p');
81 function handle_post() {
82 if ($this->arg('add')) {
84 } else if ($this->arg('remove')) {
85 $this->remove_openid();
87 $this->show_form(_t('Something weird happened.'));
91 function remove_openid() {
93 $openid_url = $this->trimmed('openid_url');
94 $oid = User_openid::staticGet('canonical', $openid_url);
96 $this->show_form(_t('No such OpenID.'));
99 $cur = common_current_user();
100 if (!$cur || $oid->user_id != $cur->id) {
101 $this->show_form(_t('That OpenID does not belong to you.'));
105 $this->show_form(_t('OpenID removed.', true));
109 function add_openid() {
111 $openid_url = $this->trimmed('openid_url');
113 $consumer = oid_consumer();
116 common_server_error(_t('Cannot instantiate OpenID consumer object.'));
120 common_ensure_session();
122 $auth_request = $consumer->begin($openid_url);
124 // Handle failure status return values.
125 if (!$auth_request) {
126 $this->show_form(_t('Not a valid OpenID.'));
128 } else if (Auth_OpenID::isFailure($auth_request)) {
129 $this->show_form(_t('OpenID failure: ') . $auth_request->message);
133 $sreg_request = Auth_OpenID_SRegRequest::build(// Required
145 $auth_request->addExtension($sreg_request);
148 $trust_root = common_root_url();
149 $process_url = common_local_url('finishaddopenid');
151 if ($auth_request->shouldSendRedirect()) {
152 $redirect_url = $auth_request->redirectURL($trust_root,
154 if (!$redirect_url) {
155 } else if (Auth_OpenID::isFailure($redirect_url)) {
156 $this->show_form(_t('Could not redirect to server: ') . $redirect_url->message);
159 common_redirect($redirect_url);
162 // Generate form markup and render it.
163 $form_id = 'openid_message';
164 $form_html = $auth_request->formMarkup($trust_root, $process_url,
165 false, array('id' => $form_id));
167 # XXX: This is cheap, but things choke if we don't escape ampersands
168 # in the HTML attributes
170 $form_html = preg_replace('/&/', '&', $form_html);
172 // Display an error if the form markup couldn't be generated;
173 // otherwise, render the HTML.
174 if (Auth_OpenID::isFailure($form_html)) {
175 $this->show_form(_t('Could not create OpenID form: ') . $form_html->message);
177 common_show_header(_t('OpenID Auto-Submit'));
178 common_element('p', 'instructions',
179 _t('This form should automatically submit itself. '.
180 'If not, click the submit button to go to your '.
181 'OpenID provider.'));
182 common_raw($form_html);
183 common_element('script', NULL,
184 '$(document).ready(function() { ' .
185 ' $("#'. $form_id .'").submit(); '.
187 common_show_footer();