]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/openidsettings.php
manage existing OpenIDs
[quix0rs-gnu-social.git] / actions / openidsettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/lib/openid.php');
24
25 class OpenidsettingsAction extends SettingsAction {
26
27         function show_form($msg=NULL, $success=false) {
28                 
29                 $user = common_current_user();
30                 
31                 common_show_header(_t('OpenID settings'), NULL, NULL, array($this, 'settings_menu'));
32
33                 if ($msg) {
34                         $this->message($msg, $success);
35                 } else {
36                         common_element('div', 'instructions',
37                                                    _t('Manage your associated OpenIDs from here.'));
38                 }
39                 common_element_start('form', array('method' => 'POST',
40                                                                                    'id' => 'openidadd',
41                                                                                    'action' =>
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');
50
51                 $oid = new User_openid();
52                 $oid->user_id = $user->id;
53                 
54                 if ($oid->find()) {
55                         
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.'));
60                         $idx = 0;
61                         
62                         while ($oid->fetch()) {
63                                 common_element_start('p');
64                                 common_element_start('form', array('method' => 'POST',
65                                                                                                    'id' => 'openiddelete-' . $idx,
66                                                                                                    'action' =>
67                                                                                                    common_local_url('openidsettings')));
68                                 common_element('a', array('href' => $oid->canonical),
69                                                            $oid->display);
70                                 common_hidden('openid_url', $oid->canonical);
71                                 common_submit('remove', _t('Remove'));
72                                 common_element_end('form');
73                                 common_element_end('p');
74                                 $idx++;
75                         }
76                 }
77                 
78                 common_show_footer();
79         }
80
81         function handle_post() {
82                 if ($this->arg('add')) {
83                         $this->add_openid();
84                 } else if ($this->arg('remove')) {
85                         $this->remove_openid();
86                 } else {
87                         $this->show_form(_t('Something weird happened.'));
88                 }
89         }
90
91         function remove_openid() {
92                 
93                 $openid_url = $this->trimmed('openid_url');
94                 $oid = User_openid::staticGet('canonical', $openid_url);
95                 if (!$oid) {
96                         $this->show_form(_t('No such OpenID.'));
97                         return;
98                 }
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.'));
102                         return;
103                 }
104                 $oid->delete();
105                 $this->show_form(_t('OpenID removed.', true));
106                 return;
107         }
108         
109         function add_openid() {
110                 
111                 $openid_url = $this->trimmed('openid_url');
112
113                 $consumer = oid_consumer();
114
115                 if (!$consumer) {
116                         common_server_error(_t('Cannot instantiate OpenID consumer object.'));
117                         return;
118                 }
119
120                 common_ensure_session();
121
122                 $auth_request = $consumer->begin($openid_url);
123
124                 // Handle failure status return values.
125                 if (!$auth_request) {
126                         $this->show_form(_t('Not a valid OpenID.'));
127                         return;
128                 } else if (Auth_OpenID::isFailure($auth_request)) {
129                         $this->show_form(_t('OpenID failure: ') . $auth_request->message);
130                         return;
131                 }
132
133                 $sreg_request = Auth_OpenID_SRegRequest::build(// Required
134                                                                                                            array(),
135                                                                                                            // Optional
136                                                                                                            array('nickname',
137                                                                                                                          'email',
138                                                                                                                          'fullname',
139                                                                                                                          'language',
140                                                                                                                          'timezone',
141                                                                                                                          'postcode',
142                                                                                                                          'country'));
143
144                 if ($sreg_request) {
145                         $auth_request->addExtension($sreg_request);
146                 }
147
148                 $trust_root = common_root_url();
149                 $process_url = common_local_url('finishaddopenid');
150
151                 if ($auth_request->shouldSendRedirect()) {
152                         $redirect_url = $auth_request->redirectURL($trust_root,
153                                                                                                            $process_url);
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);
157                                 return;
158                         } else {
159                                 common_redirect($redirect_url);
160                         }
161                 } else {
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));
166                         
167                         # XXX: This is cheap, but things choke if we don't escape ampersands
168                         # in the HTML attributes
169                         
170                         $form_html = preg_replace('/&/', '&amp;', $form_html);
171                         
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);
176                         } else {
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(); '.
186                                                            '});');
187                                 common_show_footer();
188                         }
189                 }
190         }
191 }