7 use Friendica\Core\Authentication;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Util\Strings;
15 function openid_content(App $a) {
17 $noid = Config::get('system','no_openid');
19 $a->internalRedirect();
21 Logger::log('mod_openid ' . print_r($_REQUEST,true), Logger::DATA);
23 if(!empty($_GET['openid_mode']) && !empty($_SESSION['openid'])) {
25 $openid = new LightOpenID($a->getHostName());
27 if($openid->validate()) {
29 $authid = $_REQUEST['openid_identity'];
31 if(! strlen($authid)) {
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
43 WHERE ( `openid` = '%s' OR `openid` = '%s' )
44 AND `blocked` = 0 AND `account_expired` = 0
45 AND `account_removed` = 0 AND `verified` = 1
47 DBA::escape($authid), DBA::escape(Strings::normaliseOpenID($authid))
50 if (DBA::isResult($r)) {
52 // successful OpenID login
54 unset($_SESSION['openid']);
56 Authentication::setAuthenticatedSessionForUser($r[0],true,true);
58 // just in case there was no return url set
59 // and we fell through
61 $a->internalRedirect();
64 // Successful OpenID login - but we can't match it to an existing account.
67 if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
68 notice(L10n::t('Account not found and OpenID registration is not permitted on this site.') . EOL);
69 $a->internalRedirect();
72 unset($_SESSION['register']);
74 $attr = $openid->getAttributes();
75 if (is_array($attr) && count($attr)) {
76 foreach ($attr as $k => $v) {
77 if ($k === 'namePerson/friendly') {
78 $nick = Strings::escapeTags(trim($v));
80 if($k === 'namePerson/first') {
81 $first = Strings::escapeTags(trim($v));
83 if($k === 'namePerson') {
84 $args .= '&username=' . urlencode(Strings::escapeTags(trim($v)));
86 if ($k === 'contact/email') {
87 $args .= '&email=' . urlencode(Strings::escapeTags(trim($v)));
89 if ($k === 'media/image/aspect11') {
90 $photosq = bin2hex(trim($v));
92 if ($k === 'media/image/default') {
93 $photo = bin2hex(trim($v));
98 $args .= '&nickname=' . urlencode($nick);
101 $args .= '&nickname=' . urlencode($first);
105 $args .= '&photo=' . urlencode($photosq);
108 $args .= '&photo=' . urlencode($photo);
111 $args .= '&openid_url=' . urlencode(Strings::escapeTags(trim($authid)));
113 $a->internalRedirect('register?' . $args);
118 notice(L10n::t('Login failed.') . EOL);
119 $a->internalRedirect();