4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
8 require_once('library/openid.php');
10 function openid_content(App $a) {
12 $noid = Config::get('system','no_openid');
14 goaway(System::baseUrl());
16 logger('mod_openid ' . print_r($_REQUEST,true), LOGGER_DATA);
18 if((x($_GET,'openid_mode')) && (x($_SESSION,'openid'))) {
20 $openid = new LightOpenID;
22 if($openid->validate()) {
24 $authid = $_REQUEST['openid_identity'];
26 if(! strlen($authid)) {
27 logger( t('OpenID protocol error. No ID returned.') . EOL);
28 goaway(System::baseUrl());
31 // NOTE: we search both for normalised and non-normalised form of $authid
32 // because the normalization step was removed from setting
33 // mod/settings.php in 8367cad so it might have left mixed
34 // records in the user table
36 $r = q("SELECT *, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey` FROM `user`
37 WHERE ( `openid` = '%s' OR `openid` = '%s' )
38 AND `blocked` = 0 AND `account_expired` = 0
39 AND `account_removed` = 0 AND `verified` = 1
41 dbesc($authid), dbesc(normalise_openid($authid))
44 if (DBM::is_result($r)) {
46 // successful OpenID login
48 unset($_SESSION['openid']);
50 require_once('include/security.php');
51 authenticate_success($r[0],true,true);
53 // just in case there was no return url set
54 // and we fell through
56 goaway(System::baseUrl());
59 // Successful OpenID login - but we can't match it to an existing account.
62 if ($a->config['register_policy'] == REGISTER_CLOSED) {
63 notice( t('Account not found and OpenID registration is not permitted on this site.') . EOL);
64 goaway(System::baseUrl());
67 unset($_SESSION['register']);
69 $attr = $openid->getAttributes();
70 if (is_array($attr) && count($attr)) {
71 foreach ($attr as $k => $v) {
72 if ($k === 'namePerson/friendly') {
73 $nick = notags(trim($v));
75 if($k === 'namePerson/first') {
76 $first = notags(trim($v));
78 if($k === 'namePerson') {
79 $args .= '&username=' . urlencode(notags(trim($v)));
81 if ($k === 'contact/email') {
82 $args .= '&email=' . urlencode(notags(trim($v)));
84 if ($k === 'media/image/aspect11') {
85 $photosq = bin2hex(trim($v));
87 if ($k === 'media/image/default') {
88 $photo = bin2hex(trim($v));
93 $args .= '&nickname=' . urlencode($nick);
96 $args .= '&nickname=' . urlencode($first);
100 $args .= '&photo=' . urlencode($photosq);
103 $args .= '&photo=' . urlencode($photo);
106 $args .= '&openid_url=' . urlencode(notags(trim($authid)));
108 goaway(System::baseUrl() . '/register?' . $args);
113 notice( t('Login failed.') . EOL);
114 goaway(System::baseUrl());