]> git.mxchange.org Git - friendica.git/blob - mod/openid.php
Overwrite constructor of Memory session handling so no session ini-setting in backend...
[friendica.git] / mod / openid.php
1 <?php
2 /**
3  * @file mod/openid.php
4  */
5
6 use Friendica\App;
7 use Friendica\BaseObject;
8 use Friendica\App\Authentication;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Session;
13 use Friendica\Database\DBA;
14 use Friendica\Util\Strings;
15
16 function openid_content(App $a) {
17
18         if (Config::get('system','no_openid')) {
19                 $a->internalRedirect();
20         }
21
22         Logger::log('mod_openid ' . print_r($_REQUEST,true), Logger::DATA);
23
24         if (!empty($_GET['openid_mode']) && !empty($_SESSION['openid'])) {
25
26                 $openid = new LightOpenID($a->getHostName());
27
28                 if ($openid->validate()) {
29                         $authid = $openid->data['openid_identity'];
30
31                         if (empty($authid)) {
32                                 Logger::log(L10n::t('OpenID protocol error. No ID returned.') . EOL);
33                                 $a->internalRedirect();
34                         }
35
36                         // NOTE: we search both for normalised and non-normalised form of $authid
37                         //       because the normalization step was removed from setting
38                         //       mod/settings.php in 8367cad so it might have left mixed
39                         //       records in the user table
40                         //
41                         $condition = ['blocked' => false, 'account_expired' => false, 'account_removed' => false, 'verified' => true,
42                                 'openid' => [$authid, Strings::normaliseOpenID($authid)]];
43                         $user  = DBA::selectFirst('user', [], $condition);
44                         if (DBA::isResult($user)) {
45
46                                 // successful OpenID login
47
48                                 unset($_SESSION['openid']);
49
50                                 /** @var Authentication $authentication */
51                                 $authentication = BaseObject::getClass(Authentication::class);
52                                 $authentication->setForUser($a, $user, true, true);
53
54                                 // just in case there was no return url set
55                                 // and we fell through
56
57                                 $a->internalRedirect();
58                         }
59
60                         // Successful OpenID login - but we can't match it to an existing account.
61                         unset($_SESSION['register']);
62                         Session::set('openid_attributes', $openid->getAttributes());
63                         Session::set('openid_identity', $authid);
64
65                         // Detect the server URL
66                         $open_id_obj = new LightOpenID($a->getHostName());
67                         $open_id_obj->identity = $authid;
68                         Session::set('openid_server', $open_id_obj->discover($open_id_obj->identity));
69
70                         if (intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
71                                 notice(L10n::t('Account not found. Please login to your existing account to add the OpenID to it.'));
72                         } else {
73                                 notice(L10n::t('Account not found. Please register a new account or login to your existing account to add the OpenID to it.'));
74                         }
75
76                         $a->internalRedirect('login');
77                 }
78         }
79         notice(L10n::t('Login failed.') . EOL);
80         $a->internalRedirect();
81         // NOTREACHED
82 }