]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/finishaddopenid.php
Normalize execution guards in OpenID plugin files; avoids annoying fatal errors when...
[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')) {
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             // TRANS: Client error message
68             $this->clientError(_m('Not logged in.'));
69         } else {
70             $this->tryLogin();
71         }
72     }
73
74     /**
75      * Try to log in using OpenID
76      *
77      * Check the OpenID for validity; potentially store it.
78      *
79      * @return void
80      */
81
82     function tryLogin()
83     {
84         $consumer = oid_consumer();
85
86         $response = $consumer->complete(common_local_url('finishaddopenid'));
87
88         if ($response->status == Auth_OpenID_CANCEL) {
89             // TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled.
90             $this->message(_m('OpenID authentication cancelled.'));
91             return;
92         } else if ($response->status == Auth_OpenID_FAILURE) {
93             // TRANS: OpenID authentication failed; display the error message.
94             // TRANS: %s is the error message.
95             $this->message(sprintf(_m('OpenID authentication failed: %s'),
96                                    $response->message));
97         } else if ($response->status == Auth_OpenID_SUCCESS) {
98
99             $display   = $response->getDisplayIdentifier();
100             $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
101               $response->endpoint->canonicalID : $display;
102
103             $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
104
105             if ($sreg_resp) {
106                 $sreg = $sreg_resp->contents();
107             }
108
109             // Launchpad teams extension
110             if (!oid_check_teams($response)) {
111                 $this->message(_m('OpenID authentication aborted: you are not allowed to login to this site.'));
112                 return;
113             }
114
115             $cur = common_current_user();
116
117             $other = oid_get_user($canonical);
118
119             if ($other) {
120                 if ($other->id == $cur->id) {
121                     // TRANS: message in case a user tries to add an OpenID that is already connected to them.
122                     $this->message(_m('You already have this OpenID!'));
123                 } else {
124                     // TRANS: message in case a user tries to add an OpenID that is already used by another user.
125                     $this->message(_m('Someone else already has this OpenID.'));
126                 }
127                 return;
128             }
129
130             // start a transaction
131
132             $cur->query('BEGIN');
133
134             $result = oid_link_user($cur->id, $canonical, $display);
135
136             if (!$result) {
137                 // TRANS: message in case the OpenID object cannot be connected to the user.
138                 $this->message(_m('Error connecting user.'));
139                 return;
140             }
141             if (Event::handle('StartOpenIDUpdateUser', array($cur, $canonical, &$sreg))) {
142                 if ($sreg) {
143                     if (!oid_update_user($cur, $sreg)) {
144                         // TRANS: message in case the user or the user profile cannot be saved in StatusNet.
145                         $this->message(_m('Error updating profile'));
146                         return;
147                     }
148                 }
149             }
150             Event::handle('EndOpenIDUpdateUser', array($cur, $canonical, $sreg));
151
152             // success!
153
154             $cur->query('COMMIT');
155
156             oid_set_last($display);
157
158             common_redirect(common_local_url('openidsettings'), 303);
159         }
160     }
161
162     /**
163      * Show a failure message
164      *
165      * Something went wrong. Save the message, and show the page.
166      *
167      * @param string $msg Error message to show
168      *
169      * @return void
170      */
171
172     function message($msg)
173     {
174         $this->message = $msg;
175         $this->showPage();
176     }
177
178     /**
179      * Title of the page
180      *
181      * @return string title
182      */
183
184     function title()
185     {
186         // TRANS: Title after getting the status of the OpenID authorisation request.
187         return _m('OpenID Login');
188     }
189
190     /**
191      * Show error message
192      *
193      * @return void
194      */
195
196     function showPageNotice()
197     {
198         if ($this->message) {
199             $this->element('p', 'error', $this->message);
200         }
201     }
202 }