]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/finishaddopenid.php
move instructions to top in openid pages
[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                         // This means the authentication succeeded; extract the
49                         // identity URL and Simple Registration data (if it was
50                         // returned).
51                         $display = $response->getDisplayIdentifier();
52                         $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
53                           $response->endpoint->canonicalID : $display;
54
55                         $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
56
57                         if ($sreg_resp) {
58                                 $sreg = $sreg_resp->contents();
59                         }
60
61                         common_debug('response = ' .print_r($response, TRUE), __FILE__);
62                         common_debug('sreg = ' .print_r($sreg, TRUE), __FILE__);
63                         common_debug('display = ' .print_r($display, TRUE), __FILE__);
64                         common_debug('canonical = ' .print_r($canonical, TRUE), __FILE__);
65                         
66                         $other =& $this->get_user($canonical);
67
68                         if ($other) {
69                                 $this->message(_t('This OpenID is already associated with user "') . $other->nickname . _t('"'));
70                         } else {
71                                 $cur =& common_current_user();
72                                 common_debug('cur = ' .print_r($cur, TRUE), __FILE__);
73                                 $result = oid_link_user($cur->id, $display, $canonical);
74                                 if (!$result) {
75                                         $this->message(_t('Error connecting user.'));
76                                         return;
77                                 }
78                                 if ($sreg) {
79                                         if (!$this->update_user($cur, $sreg)) {
80                                                 $this->message(_t('Error updating profile'));
81                                                 return;
82                                         }
83                                 }
84                                 # success!
85                                 common_redirect(common_local_url('openidsettings'));
86                         }
87                 }
88         }
89
90         function message($msg) {
91                 common_show_header(_t('OpenID Login'));
92                 common_element('p', NULL, $msg);
93                 common_show_footer();
94         }
95
96         function get_user($canonical) {
97                 $user = NULL;
98                 $oid = User_openid::staticGet('canonical', $canonical);
99                 if ($oid) {
100                         $user = User::staticGet('id', $oid->user_id);
101                 }
102                 return $user;
103         }
104
105         function update_user(&$user, $sreg) {
106
107                 $profile =& $user->getProfile();
108
109                 $orig_profile = clone($profile);
110
111                 if ($sreg['fullname'] && strlen($sreg['fullname']) <= 255) {
112                         $profile->fullname = $sreg['fullname'];
113                 }
114
115                 if ($sreg['country']) {
116                         if ($sreg['postcode']) {
117                                 # XXX: use postcode to get city and region
118                                 # XXX: also, store postcode somewhere -- it's valuable!
119                                 $profile->location = $sreg['postcode'] . ', ' . $sreg['country'];
120                         } else {
121                                 $profile->location = $sreg['country'];
122                         }
123                 }
124
125                 # XXX save language if it's passed
126                 # XXX save timezone if it's passed
127
128                 if (!$profile->update($orig_profile)) {
129                         common_server_error(_t('Error saving the profile.'));
130                         return false;
131                 }
132
133                 $orig_user = clone($user);
134
135                 if ($sreg['email'] && Validate::email($sreg['email'], true)) {
136                         $user->email = $sreg['email'];
137                 }
138
139                 if (!$user->update($orig_user)) {
140                         common_server_error(_t('Error saving the user.'));
141                         return false;
142                 }
143                 
144                 return true;
145         }
146 }