]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/actions/finishsynchopenid.php
[OpenID] s/sync/synch
[quix0rs-gnu-social.git] / plugins / OpenID / actions / finishsynchopenid.php
1 <?php
2 // This file is part of GNU social - https://www.gnu.org/software/social
3 //
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.
8 //
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.
13 //
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/>.
16
17 /**
18  * OpenID Synch completion
19  *
20  * @package   GNUsocial
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
24  */
25
26 defined('GNUSOCIAL') || die();
27
28 require_once(INSTALLDIR . '/plugins/OpenID/openid.php');
29
30 /**
31  * Action that handles OpenID Synch completion.
32  *
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
35  */
36 class FinishsynchopenidAction extends Action
37 {
38     public $msg = null;
39
40     /**
41      * Handle the redirect back from OpenID confirmation
42      *
43      * Check to see if the user's logged in, and then try
44      * to use the OpenID login system.
45      *
46      * @param array $args $_REQUEST arguments
47      *
48      * @return void
49      */
50     public function handle()
51     {
52         parent::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.'));
56         } else {
57             $this->tryLogin();
58         }
59     }
60
61     /**
62      * Try to log in using OpenID
63      *
64      * Check the OpenID for validity; potentially store it.
65      *
66      * @return void
67      */
68     public function tryLogin()
69     {
70         $consumer = oid_consumer();
71
72         $response = $consumer->complete(common_local_url('finishsynchopenid'));
73
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.'));
77             return;
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.'),
83                 $response->message
84             ));
85         } elseif ($response->status == Auth_OpenID_SUCCESS) {
86             $display   = $response->getDisplayIdentifier();
87             $canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
88               $response->endpoint->canonicalID : $display;
89
90             $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
91
92             if ($sreg_resp) {
93                 $sreg = $sreg_resp->contents();
94             }
95
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.'));
100                 return;
101             }
102
103             $cur = common_current_user();
104
105             // start a transaction
106
107             $cur->query('BEGIN');
108
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.'));
113                     return;
114                 }
115             }
116             Event::handle('EndOpenIDUpdateUser', [$cur, $canonical, $sreg]);
117             
118             // success!
119
120             $cur->query('COMMIT');
121
122             oid_set_last($display);
123
124             common_redirect(common_local_url('openidsettings'), 303);
125         }
126     }
127
128     /**
129      * Show a failure message
130      *
131      * Something went wrong. Save the message, and show the page.
132      *
133      * @param string $msg Error message to show
134      *
135      * @return void
136      */
137     public function message($msg)
138     {
139         $this->message = $msg;
140         $this->showPage();
141     }
142
143     /**
144      * Title of the page
145      *
146      * @return string title
147      */
148     public function title()
149     {
150         // TRANS: Title after getting the status of the OpenID authorisation request.
151         // TODO update after understanding the function ^
152         return _m('OpenID Synchronization');
153     }
154
155     /**
156      * Show error message
157      *
158      * @return void
159      */
160     public function showPageNotice()
161     {
162         if ($this->message) {
163             $this->element('p', 'error', $this->message);
164         }
165     }
166 }