7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Session;
11 use Friendica\Database\DBA;
12 use Friendica\Util\Strings;
14 function openid_content(App $a) {
16 if (Config::get('system','no_openid')) {
17 $a->internalRedirect();
20 Logger::log('mod_openid ' . print_r($_REQUEST,true), Logger::DATA);
22 if (!empty($_GET['openid_mode']) && !empty($_SESSION['openid'])) {
24 $openid = new LightOpenID($a->getHostName());
26 if ($openid->validate()) {
27 $authid = $openid->data['openid_identity'];
30 Logger::log(L10n::t('OpenID protocol error. No ID returned.') . EOL);
31 $a->internalRedirect();
34 // NOTE: we search both for normalised and non-normalised form of $authid
35 // because the normalization step was removed from setting
36 // mod/settings.php in 8367cad so it might have left mixed
37 // records in the user table
39 $condition = ['blocked' => false, 'account_expired' => false, 'account_removed' => false, 'verified' => true,
40 'openid' => [$authid, Strings::normaliseOpenID($authid)]];
41 $user = DBA::selectFirst('user', [], $condition);
42 if (DBA::isResult($user)) {
44 // successful OpenID login
46 unset($_SESSION['openid']);
48 Session::setAuthenticatedForUser($a, $user, true, true);
50 // just in case there was no return url set
51 // and we fell through
53 $a->internalRedirect();
56 // Successful OpenID login - but we can't match it to an existing account.
57 unset($_SESSION['register']);
58 Session::set('openid_attributes', $openid->getAttributes());
59 Session::set('openid_identity', $authid);
61 // Detect the server URL
62 $open_id_obj = new LightOpenID($a->getHostName());
63 $open_id_obj->identity = $authid;
64 Session::set('openid_server', $open_id_obj->discover($open_id_obj->identity));
66 if (intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
67 notice(L10n::t('Account not found. Please login to your existing account to add the OpenID to it.'));
69 notice(L10n::t('Account not found. Please register a new account or login to your existing account to add the OpenID to it.'));
72 $a->internalRedirect('login');
75 notice(L10n::t('Login failed.') . EOL);
76 $a->internalRedirect();