]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/finishaddopenid.php
Merge branch 'more-notice-info'
[quix0rs-gnu-social.git] / plugins / OpenID / finishaddopenid.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Complete adding an OpenID
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2008-2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
35
36 /**
37  * Complete adding an OpenID
38  *
39  * Handle the return from an OpenID verification
40  *
41  * @category Settings
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class FinishaddopenidAction extends Action
49 {
50     var $msg = null;
51
52     /**
53      * Handle the redirect back from OpenID confirmation
54      *
55      * Check to see if the user's logged in, and then try
56      * to use the OpenID login system.
57      *
58      * @param array $args $_REQUEST arguments
59      *
60      * @return void
61      */
62
63     function handle($args)
64     {
65         parent::handle($args);
66         if (!common_logged_in()) {
67             $this->clientError(_m('Not logged in.'));
68         } else {
69             $this->tryLogin();
70         }
71     }
72
73     /**
74      * Try to log in using OpenID
75      *
76      * Check the OpenID for validity; potentially store it.
77      *
78      * @return void
79      */
80
81     function tryLogin()
82     {
83         $consumer = oid_consumer();
84
85         $response = $consumer->complete(common_local_url('finishaddopenid'));
86
87         if ($response->status == Auth_OpenID_CANCEL) {
88             $this->message(_m('OpenID authentication cancelled.'));
89             return;
90         } else if ($response->status == Auth_OpenID_FAILURE) {
91             // Authentication failed; display the error message.
92             $this->message(sprintf(_m('OpenID authentication failed: %s'),
93                                    $response->message));
94         } else if ($response->status == Auth_OpenID_SUCCESS) {
95
96             $display   = $response->getDisplayIdentifier();
97             $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
98               $response->endpoint->canonicalID : $display;
99
100             $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
101
102             if ($sreg_resp) {
103                 $sreg = $sreg_resp->contents();
104             }
105
106             $cur = common_current_user();
107
108             $other = oid_get_user($canonical);
109
110             if ($other) {
111                 if ($other->id == $cur->id) {
112                     $this->message(_m('You already have this OpenID!'));
113                 } else {
114                     $this->message(_m('Someone else already has this OpenID.'));
115                 }
116                 return;
117             }
118
119             // start a transaction
120
121             $cur->query('BEGIN');
122
123             $result = oid_link_user($cur->id, $canonical, $display);
124
125             if (!$result) {
126                 $this->message(_m('Error connecting user.'));
127                 return;
128             }
129             if (Event::handle('StartOpenIDUpdateUser', array($cur, $canonical, &$sreg))) {
130                 if ($sreg) {
131                     if (!oid_update_user($cur, $sreg)) {
132                         $this->message(_m('Error updating profile'));
133                         return;
134                     }
135                 }
136             }
137             Event::handle('EndOpenIDUpdateUser', array($cur, $canonical, $sreg));
138
139             // success!
140
141             $cur->query('COMMIT');
142
143             oid_set_last($display);
144
145             common_redirect(common_local_url('openidsettings'), 303);
146         }
147     }
148
149     /**
150      * Show a failure message
151      *
152      * Something went wrong. Save the message, and show the page.
153      *
154      * @param string $msg Error message to show
155      *
156      * @return void
157      */
158
159     function message($msg)
160     {
161         $this->message = $msg;
162         $this->showPage();
163     }
164
165     /**
166      * Title of the page
167      *
168      * @return string title
169      */
170
171     function title()
172     {
173         return _m('OpenID Login');
174     }
175
176     /**
177      * Show error message
178      *
179      * @return void
180      */
181
182     function showPageNotice()
183     {
184         if ($this->message) {
185             $this->element('p', 'error', $this->message);
186         }
187     }
188 }