]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/finishaddopenid.php
af96635d624a63a455fc342d0ecb27adc26edfe2
[quix0rs-gnu-social.git] / actions / finishaddopenid.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/openid.php');
23
24 class FinishaddopenidAction extends Action {
25
26         function handle($args) {
27                 parent::handle($args);
28                 if (!common_logged_in()) {
29                         common_user_error(_('Not logged in.'));
30                 } else {
31                         $this->try_login();
32                 }
33         }
34
35         function is_readonly() {
36                 return false;
37         }
38         
39         function try_login() {
40
41                 $consumer =& oid_consumer();
42
43                 $response = $consumer->complete(common_local_url('finishaddopenid'));
44
45                 if ($response->status == Auth_OpenID_CANCEL) {
46                         $this->message(_('OpenID authentication cancelled.'));
47                         return;
48                 } else if ($response->status == Auth_OpenID_FAILURE) {
49                         // Authentication failed; display the error message.
50                         $this->message(sprintf(_('OpenID authentication failed: %s'), $response->message));
51                 } else if ($response->status == Auth_OpenID_SUCCESS) {
52
53                         $display = $response->getDisplayIdentifier();
54                         $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
55                           $response->endpoint->canonicalID : $display;
56
57                         $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
58
59                         if ($sreg_resp) {
60                                 $sreg = $sreg_resp->contents();
61                         }
62
63                         $cur =& common_current_user();
64                         $other = oid_get_user($canonical);
65
66                         if ($other) {
67                                 if ($other->id == $cur->id) {
68                                         $this->message(_('You already have this OpenID!'));
69                                 } else {
70                                         $this->message(_('Someone else already has this OpenID.'));
71                                 }
72                                 return;
73                         }
74
75                         # start a transaction
76
77                         $cur->query('BEGIN');
78
79                         $result = oid_link_user($cur->id, $canonical, $display);
80
81                         if (!$result) {
82                                 $this->message(_('Error connecting user.'));
83                                 return;
84                         }
85                         if ($sreg) {
86                                 if (!oid_update_user($cur, $sreg)) {
87                                         $this->message(_('Error updating profile'));
88                                         return;
89                                 }
90                         }
91
92                         # success!
93
94                         $cur->query('COMMIT');
95
96                         oid_set_last($display);
97
98                         common_redirect(common_local_url('openidsettings'));
99                 }
100         }
101
102         function message($msg) {
103                 common_show_header(_('OpenID Login'));
104                 common_element('p', NULL, $msg);
105                 common_show_footer();
106         }
107 }