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/openid.php');
24 class FinishaddopenidAction extends Action {
26 function handle($args) {
27 parent::handle($args);
28 if (!common_logged_in()) {
29 common_user_error(_t('Not logged in.'));
35 function try_login() {
37 $consumer =& oid_consumer();
39 $response = $consumer->complete(common_local_url('finishaddopenid'));
41 if ($response->status == Auth_OpenID_CANCEL) {
42 $this->message(_t('OpenID authentication cancelled.'));
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) {
49 $display = $response->getDisplayIdentifier();
50 $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
51 $response->endpoint->canonicalID : $display;
53 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
56 $sreg = $sreg_resp->contents();
59 $cur =& common_current_user();
60 $other = $this->get_user($canonical);
63 if ($other->id == $cur->id) {
64 $this->message(_t('You already have this OpenID!'));
66 $this->message(_t('Someone else already has this OpenID.'));
75 $result = oid_link_user($cur->id, $display, $canonical);
78 $this->message(_t('Error connecting user.'));
82 if (!$this->update_user($cur, $sreg)) {
83 $this->message(_t('Error updating profile'));
90 $cur->query('COMMIT');
92 oid_set_last($display);
94 common_redirect(common_local_url('openidsettings'));
98 function message($msg) {
99 common_show_header(_t('OpenID Login'));
100 common_element('p', NULL, $msg);
101 common_show_footer();
104 function get_user($canonical) {
106 $oid = User_openid::staticGet('canonical', $canonical);
108 $user = User::staticGet('id', $oid->user_id);
113 function update_user(&$user, $sreg) {
115 $profile =& $user->getProfile();
117 $orig_profile = clone($profile);
119 if ($sreg['fullname'] && strlen($sreg['fullname']) <= 255) {
120 $profile->fullname = $sreg['fullname'];
123 if ($sreg['country']) {
124 if ($sreg['postcode']) {
125 # XXX: use postcode to get city and region
126 # XXX: also, store postcode somewhere -- it's valuable!
127 $profile->location = $sreg['postcode'] . ', ' . $sreg['country'];
129 $profile->location = $sreg['country'];
133 # XXX save language if it's passed
134 # XXX save timezone if it's passed
136 if (!$profile->update($orig_profile)) {
137 common_server_error(_t('Error saving the profile.'));
141 $orig_user = clone($user);
143 if ($sreg['email'] && Validate::email($sreg['email'], true)) {
144 $user->email = $sreg['email'];
147 if (!$user->update($orig_user)) {
148 common_server_error(_t('Error saving the user.'));