4 use Friendica\Core\System;
6 require_once('library/openid.php');
8 function openid_content(App $a) {
10 $noid = get_config('system','no_openid');
12 goaway(System::baseUrl());
14 logger('mod_openid ' . print_r($_REQUEST,true), LOGGER_DATA);
16 if((x($_GET,'openid_mode')) && (x($_SESSION,'openid'))) {
18 $openid = new LightOpenID;
20 if($openid->validate()) {
22 $authid = $_REQUEST['openid_identity'];
24 if(! strlen($authid)) {
25 logger( t('OpenID protocol error. No ID returned.') . EOL);
26 goaway(System::baseUrl());
29 // NOTE: we search both for normalised and non-normalised form of $authid
30 // because the normalization step was removed from setting
31 // mod/settings.php in 8367cad so it might have left mixed
32 // records in the user table
34 $r = q("SELECT *, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey` FROM `user`
35 WHERE ( `openid` = '%s' OR `openid` = '%s' )
36 AND `blocked` = 0 AND `account_expired` = 0
37 AND `account_removed` = 0 AND `verified` = 1
39 dbesc($authid), dbesc(normalise_openid($authid))
42 if (dbm::is_result($r)) {
44 // successful OpenID login
46 unset($_SESSION['openid']);
48 require_once('include/security.php');
49 authenticate_success($r[0],true,true);
51 // just in case there was no return url set
52 // and we fell through
54 goaway(System::baseUrl());
57 // Successful OpenID login - but we can't match it to an existing account.
60 if ($a->config['register_policy'] == REGISTER_CLOSED) {
61 notice( t('Account not found and OpenID registration is not permitted on this site.') . EOL);
62 goaway(System::baseUrl());
65 unset($_SESSION['register']);
67 $attr = $openid->getAttributes();
68 if (is_array($attr) && count($attr)) {
69 foreach ($attr as $k => $v) {
70 if ($k === 'namePerson/friendly') {
71 $nick = notags(trim($v));
73 if($k === 'namePerson/first') {
74 $first = notags(trim($v));
76 if($k === 'namePerson') {
77 $args .= '&username=' . urlencode(notags(trim($v)));
79 if ($k === 'contact/email') {
80 $args .= '&email=' . urlencode(notags(trim($v)));
82 if ($k === 'media/image/aspect11') {
83 $photosq = bin2hex(trim($v));
85 if ($k === 'media/image/default') {
86 $photo = bin2hex(trim($v));
91 $args .= '&nickname=' . urlencode($nick);
94 $args .= '&nickname=' . urlencode($first);
98 $args .= '&photo=' . urlencode($photosq);
101 $args .= '&photo=' . urlencode($photo);
104 $args .= '&openid_url=' . urlencode(notags(trim($authid)));
106 goaway(System::baseUrl() . '/register?' . $args);
111 notice( t('Login failed.') . EOL);
112 goaway(System::baseUrl());