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;
16 function openid_content(App $a) {
18 if (Config::get('system','no_openid')) {
19 $a->internalRedirect();
22 Logger::log('mod_openid ' . print_r($_REQUEST,true), Logger::DATA);
24 if (!empty($_GET['openid_mode']) && !empty($_SESSION['openid'])) {
26 $openid = new LightOpenID($a->getHostName());
28 if ($openid->validate()) {
29 $authid = $openid->data['openid_identity'];
32 Logger::log(L10n::t('OpenID protocol error. No ID returned.') . EOL);
33 $a->internalRedirect();
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
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)) {
46 // successful OpenID login
48 unset($_SESSION['openid']);
50 /** @var Authentication $authentication */
51 $authentication = BaseObject::getClass(Authentication::class);
52 $authentication->setForUser($a, $user, true, true);
54 // just in case there was no return url set
55 // and we fell through
57 $a->internalRedirect();
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);
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));
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.'));
73 notice(L10n::t('Account not found. Please register a new account or login to your existing account to add the OpenID to it.'));
76 $a->internalRedirect('login');
79 notice(L10n::t('Login failed.') . EOL);
80 $a->internalRedirect();