]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/actions/finishaddopenid.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / OpenID / actions / 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 class FinishaddopenidAction extends Action
48 {
49     var $msg = null;
50
51     /**
52      * Handle the redirect back from OpenID confirmation
53      *
54      * Check to see if the user's logged in, and then try
55      * to use the OpenID login system.
56      *
57      * @param array $args $_REQUEST arguments
58      *
59      * @return void
60      */
61     function handle($args)
62     {
63         parent::handle($args);
64         if (!common_logged_in()) {
65             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
66             $this->clientError(_m('Not logged in.'));
67         } else {
68             $this->tryLogin();
69         }
70     }
71
72     /**
73      * Try to log in using OpenID
74      *
75      * Check the OpenID for validity; potentially store it.
76      *
77      * @return void
78      */
79     function tryLogin()
80     {
81         $consumer = oid_consumer();
82
83         $response = $consumer->complete(common_local_url('finishaddopenid'));
84
85         if ($response->status == Auth_OpenID_CANCEL) {
86             // TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled.
87             $this->message(_m('OpenID authentication cancelled.'));
88             return;
89         } else if ($response->status == Auth_OpenID_FAILURE) {
90             // TRANS: OpenID authentication failed; display the error message.
91             // TRANS: %s is 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             // Launchpad teams extension
107             if (!oid_check_teams($response)) {
108                 // TRANS: OpenID authentication error.
109                 $this->message(_m('OpenID authentication aborted: You are not allowed to login to this site.'));
110                 return;
111             }
112
113             $cur = common_current_user();
114
115             $other = oid_get_user($canonical);
116
117             if ($other) {
118                 if ($other->id == $cur->id) {
119                     // TRANS: Message in case a user tries to add an OpenID that is already connected to them.
120                     $this->message(_m('You already have this OpenID!'));
121                 } else {
122                     // TRANS: Message in case a user tries to add an OpenID that is already used by another user.
123                     $this->message(_m('Someone else already has this OpenID.'));
124                 }
125                 return;
126             }
127
128             // start a transaction
129
130             $cur->query('BEGIN');
131
132             $result = oid_link_user($cur->id, $canonical, $display);
133
134             if (!$result) {
135                 // TRANS: Message in case the OpenID object cannot be connected to the user.
136                 $this->message(_m('Error connecting user.'));
137                 return;
138             }
139             if (Event::handle('StartOpenIDUpdateUser', array($cur, $canonical, &$sreg))) {
140                 if ($sreg) {
141                     if (!oid_update_user($cur, $sreg)) {
142                         // TRANS: Message in case the user or the user profile cannot be saved in StatusNet.
143                         $this->message(_m('Error updating profile.'));
144                         return;
145                     }
146                 }
147             }
148             Event::handle('EndOpenIDUpdateUser', array($cur, $canonical, $sreg));
149
150             // success!
151
152             $cur->query('COMMIT');
153
154             oid_set_last($display);
155
156             common_redirect(common_local_url('openidsettings'), 303);
157         }
158     }
159
160     /**
161      * Show a failure message
162      *
163      * Something went wrong. Save the message, and show the page.
164      *
165      * @param string $msg Error message to show
166      *
167      * @return void
168      */
169     function message($msg)
170     {
171         $this->message = $msg;
172         $this->showPage();
173     }
174
175     /**
176      * Title of the page
177      *
178      * @return string title
179      */
180     function title()
181     {
182         // TRANS: Title after getting the status of the OpenID authorisation request.
183         return _m('OpenID Login');
184     }
185
186     /**
187      * Show error message
188      *
189      * @return void
190      */
191     function showPageNotice()
192     {
193         if ($this->message) {
194             $this->element('p', 'error', $this->message);
195         }
196     }
197 }