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->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.
59 if (intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
60 notice(L10n::t('Account not found and OpenID registration is not permitted on this site.') . EOL);
61 $a->internalRedirect();
64 unset($_SESSION['register']);
66 $attr = $openid->getAttributes();
67 if (is_array($attr) && count($attr)) {
68 foreach ($attr as $k => $v) {
69 if ($k === 'namePerson/friendly') {
70 $nick = Strings::escapeTags(trim($v));
72 if ($k === 'namePerson/first') {
73 $first = Strings::escapeTags(trim($v));
75 if ($k === 'namePerson') {
76 $args .= '&username=' . urlencode(Strings::escapeTags(trim($v)));
78 if ($k === 'contact/email') {
79 $args .= '&email=' . urlencode(Strings::escapeTags(trim($v)));
81 if ($k === 'media/image/aspect11') {
82 $photosq = bin2hex(trim($v));
84 if ($k === 'media/image/default') {
85 $photo = bin2hex(trim($v));
90 $args .= '&nickname=' . urlencode($nick);
91 } elseif (!empty($first)) {
92 $args .= '&nickname=' . urlencode($first);
95 if (!empty($photosq)) {
96 $args .= '&photo=' . urlencode($photosq);
97 } elseif (!empty($photo)) {
98 $args .= '&photo=' . urlencode($photo);
101 $args .= '&openid_url=' . urlencode(Strings::escapeTags(trim($authid)));
103 $a->internalRedirect('register?' . $args);
108 notice(L10n::t('Login failed.') . EOL);
109 $a->internalRedirect();