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) {
48 // This means the authentication succeeded; extract the
49 // identity URL and Simple Registration data (if it was
51 $display = $response->getDisplayIdentifier();
52 $canonical = ($response->endpoint->canonicalID) ?
53 $response->endpoint->canonicalID : $response->getDisplayIdentifier();
55 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
58 $sreg = $sreg_resp->contents();
61 $user = $this->get_user($canonical);
64 $this->message(_t('This OpenID is already associated with user "') . $user->nickname . _t('"'));
66 $user = common_current_user();
67 if (!$this->connect_user($user, $display, $canonical)) {
68 $this->message(_t('Error connecting user'));
72 if (!$this->update_user($user, $sreg)) {
73 $this->message(_t('Error updating profile'));
78 common_redirect(common_local_url('openidsettings'));
83 function message($msg) {
84 common_show_header(_t('OpenID Login'));
85 common_element('p', NULL, $msg);
89 function get_user($canonical) {
91 $oid = User_openid::staticGet('canonical', $canonical);
93 $user = User::staticGet('id', $oid->user_id);
98 function update_user($user, $sreg) {
100 $profile = $user->getProfile();
102 $orig_profile = clone($profile);
104 if ($sreg['fullname'] && strlen($sreg['fullname']) <= 255) {
105 $profile->fullname = $sreg['fullname'];
108 if ($sreg['country']) {
109 if ($sreg['postcode']) {
110 # XXX: use postcode to get city and region
111 # XXX: also, store postcode somewhere -- it's valuable!
112 $profile->location = $sreg['postcode'] . ', ' . $sreg['country'];
114 $profile->location = $sreg['country'];
118 # XXX save language if it's passed
119 # XXX save timezone if it's passed
121 if (!$profile->update($orig_profile)) {
122 common_server_error(_t('Error saving the profile.'));
126 $orig_user = clone($user);
128 if ($sreg['email'] && Validate::email($sreg['email'], true)) {
129 $user->email = $sreg['email'];
132 if (!$user->update($orig_user)) {
133 common_server_error(_t('Error saving the user.'));
140 function connect_user($user, $display, $canonical) {
142 $oid = new User_openid();
143 $oid->display = $display;
144 $oid->canonical = $canonical;
145 $oid->user_id = $user->id;
146 $oid->created = DB_DataObject_Cast::dateTime();
148 if (!$oid->insert()) {