]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/finishaddopenid.php
237f0454f7ded23a67486bf89b3142e72d6f6670
[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(_t('Not logged in.'));
30                 } else {
31                         $this->try_login();
32                 }
33         }
34
35         function try_login() {
36
37                 $consumer =& oid_consumer();
38
39                 $response = $consumer->complete(common_local_url('finishaddopenid'));
40
41                 if ($response->status == Auth_OpenID_CANCEL) {
42                         $this->message(_t('OpenID authentication cancelled.'));
43                         return;
44                 } else if ($response->status == Auth_OpenID_FAILURE) {
45                         // Authentication failed; display the error message.
46                         $this->message(_t('OpenID authentication failed: ') . $response->message);
47                 } else if ($response->status == Auth_OpenID_SUCCESS) {
48                         
49                         $display = $response->getDisplayIdentifier();
50                         $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
51                           $response->endpoint->canonicalID : $display;
52
53                         $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
54
55                         if ($sreg_resp) {
56                                 $sreg = $sreg_resp->contents();
57                         }
58
59                         $cur =& common_current_user();
60                         $other = $this->get_user($canonical);
61                         
62                         if ($other) {
63                                 if ($other->id == $cur->id) {
64                                         $this->message(_t('You already have this OpenID!'));
65                                 } else {
66                                         $this->message(_t('Someone else already has this OpenID.'));
67                                 }
68                                 return;
69                         }
70
71                         # start a transaction
72                         
73                         $cur->query('BEGIN');
74                         
75                         $result = oid_link_user($cur->id, $display, $canonical);
76                         
77                         if (!$result) {
78                                 $this->message(_t('Error connecting user.'));
79                                 return;
80                         }
81                         if ($sreg) {
82                                 if (!$this->update_user($cur, $sreg)) {
83                                         $this->message(_t('Error updating profile'));
84                                         return;
85                                 }
86                         }
87                         
88                         # success!
89                         
90                         $cur->query('COMMIT');
91                         
92                         common_redirect(common_local_url('openidsettings'));
93                 }
94         }
95
96         function message($msg) {
97                 common_show_header(_t('OpenID Login'));
98                 common_element('p', NULL, $msg);
99                 common_show_footer();
100         }
101
102         function get_user($canonical) {
103                 $user = NULL;
104                 $oid = User_openid::staticGet('canonical', $canonical);
105                 if ($oid) {
106                         $user = User::staticGet('id', $oid->user_id);
107                 }
108                 return $user;
109         }
110
111         function update_user(&$user, $sreg) {
112
113                 $profile =& $user->getProfile();
114
115                 $orig_profile = clone($profile);
116
117                 if ($sreg['fullname'] && strlen($sreg['fullname']) <= 255) {
118                         $profile->fullname = $sreg['fullname'];
119                 }
120
121                 if ($sreg['country']) {
122                         if ($sreg['postcode']) {
123                                 # XXX: use postcode to get city and region
124                                 # XXX: also, store postcode somewhere -- it's valuable!
125                                 $profile->location = $sreg['postcode'] . ', ' . $sreg['country'];
126                         } else {
127                                 $profile->location = $sreg['country'];
128                         }
129                 }
130
131                 # XXX save language if it's passed
132                 # XXX save timezone if it's passed
133
134                 if (!$profile->update($orig_profile)) {
135                         common_server_error(_t('Error saving the profile.'));
136                         return false;
137                 }
138
139                 $orig_user = clone($user);
140
141                 if ($sreg['email'] && Validate::email($sreg['email'], true)) {
142                         $user->email = $sreg['email'];
143                 }
144
145                 if (!$user->update($orig_user)) {
146                         common_server_error(_t('Error saving the user.'));
147                         return false;
148                 }
149                 
150                 return true;
151         }
152 }