7 use Friendica\Core\Authentication;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Logger;
11 use Friendica\Database\DBA;
12 use Friendica\Util\Strings;
14 function openid_content(App $a) {
16 $noid = Config::get('system','no_openid');
18 $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()) {
28 $authid = $_REQUEST['openid_identity'];
30 if(! strlen($authid)) {
31 Logger::log(L10n::t('OpenID protocol error. No ID returned.') . EOL);
32 $a->internalRedirect();
35 // NOTE: we search both for normalised and non-normalised form of $authid
36 // because the normalization step was removed from setting
37 // mod/settings.php in 8367cad so it might have left mixed
38 // records in the user table
42 WHERE ( `openid` = '%s' OR `openid` = '%s' )
43 AND `blocked` = 0 AND `account_expired` = 0
44 AND `account_removed` = 0 AND `verified` = 1
46 DBA::escape($authid), DBA::escape(Strings::normaliseOpenID($authid))
49 if (DBA::isResult($r)) {
51 // successful OpenID login
53 unset($_SESSION['openid']);
55 Authentication::setAuthenticatedSessionForUser($r[0],true,true);
57 // just in case there was no return url set
58 // and we fell through
60 $a->internalRedirect();
63 // Successful OpenID login - but we can't match it to an existing account.
66 if (intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
67 notice(L10n::t('Account not found and OpenID registration is not permitted on this site.') . EOL);
68 $a->internalRedirect();
71 unset($_SESSION['register']);
73 $attr = $openid->getAttributes();
74 if (is_array($attr) && count($attr)) {
75 foreach ($attr as $k => $v) {
76 if ($k === 'namePerson/friendly') {
77 $nick = Strings::escapeTags(trim($v));
79 if($k === 'namePerson/first') {
80 $first = Strings::escapeTags(trim($v));
82 if($k === 'namePerson') {
83 $args .= '&username=' . urlencode(Strings::escapeTags(trim($v)));
85 if ($k === 'contact/email') {
86 $args .= '&email=' . urlencode(Strings::escapeTags(trim($v)));
88 if ($k === 'media/image/aspect11') {
89 $photosq = bin2hex(trim($v));
91 if ($k === 'media/image/default') {
92 $photo = bin2hex(trim($v));
97 $args .= '&nickname=' . urlencode($nick);
99 elseif (!empty($first)) {
100 $args .= '&nickname=' . urlencode($first);
103 if (!empty($photosq)) {
104 $args .= '&photo=' . urlencode($photosq);
106 elseif (!empty($photo)) {
107 $args .= '&photo=' . urlencode($photo);
110 $args .= '&openid_url=' . urlencode(Strings::escapeTags(trim($authid)));
112 $a->internalRedirect('register?' . $args);
117 notice(L10n::t('Login failed.') . EOL);
118 $a->internalRedirect();