2 // This file is part of GNU social - https://www.gnu.org/software/social
4 // GNU social is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // GNU social is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
18 * OpenID Sync completion
21 * @author Bruno Casteleiro <brunoccast@fc.up.pt>
22 * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
23 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
26 defined('GNUSOCIAL') || die();
28 require_once(INSTALLDIR . '/plugins/OpenID/openid.php');
31 * Action that handles OpenID Sync completion.
33 * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
34 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
36 class FinishsyncopenidAction extends Action
41 * Handle the redirect back from OpenID confirmation
43 * Check to see if the user's logged in, and then try
44 * to use the OpenID login system.
46 * @param array $args $_REQUEST arguments
50 public function handle()
53 if (!common_logged_in()) {
54 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
55 $this->clientError(_m('Not logged in.'));
62 * Try to log in using OpenID
64 * Check the OpenID for validity; potentially store it.
68 public function tryLogin()
70 $consumer = oid_consumer();
72 $response = $consumer->complete(common_local_url('finishsyncopenid'));
74 if ($response->status == Auth_OpenID_CANCEL) {
75 // TRANS: Status message in case the response from the OpenID provider is that the logon attempt was cancelled.
76 $this->message(_m('OpenID authentication cancelled.'));
78 } elseif ($response->status == Auth_OpenID_FAILURE) {
79 // TRANS: OpenID authentication failed; display the error message.
80 // TRANS: %s is the error message.
81 $this->message(sprintf(
82 _m('OpenID authentication failed: %s.'),
85 } elseif ($response->status == Auth_OpenID_SUCCESS) {
86 $display = $response->getDisplayIdentifier();
87 $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
88 $response->endpoint->canonicalID : $display;
90 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
93 $sreg = $sreg_resp->contents();
96 // Launchpad teams extension
97 if (!oid_check_teams($response)) {
98 // TRANS: OpenID authentication error.
99 $this->message(_m('OpenID authentication aborted: You are not allowed to login to this site.'));
103 $cur = common_current_user();
105 // start a transaction
107 $cur->query('BEGIN');
109 if (Event::handle('StartOpenIDUpdateUser', [$cur, $canonical, &$sreg])) {
110 if (!oid_update_user($cur, $sreg)) {
111 // TRANS: Message in case the user or the user profile cannot be saved in StatusNet.
112 $this->message(_m('Error updating profile.'));
116 Event::handle('EndOpenIDUpdateUser', [$cur, $canonical, $sreg]);
120 $cur->query('COMMIT');
122 oid_set_last($display);
124 common_redirect(common_local_url('openidsettings'), 303);
129 * Show a failure message
131 * Something went wrong. Save the message, and show the page.
133 * @param string $msg Error message to show
137 public function message($msg)
139 $this->message = $msg;
146 * @return string title
148 public function title()
150 // TRANS: Title after getting the status of the OpenID authorisation request.
151 // TODO update after understanding the function ^
152 return _m('OpenID Synchronization');
160 public function showPageNotice()
162 if ($this->message) {
163 $this->element('p', 'error', $this->message);