4 use Friendica\Core\Config;
5 use Friendica\Core\System;
7 require_once('library/openid.php');
9 function openid_content(App $a) {
11 $noid = Config::get('system','no_openid');
13 goaway(System::baseUrl());
15 logger('mod_openid ' . print_r($_REQUEST,true), LOGGER_DATA);
17 if((x($_GET,'openid_mode')) && (x($_SESSION,'openid'))) {
19 $openid = new LightOpenID;
21 if($openid->validate()) {
23 $authid = $_REQUEST['openid_identity'];
25 if(! strlen($authid)) {
26 logger( t('OpenID protocol error. No ID returned.') . EOL);
27 goaway(System::baseUrl());
30 // NOTE: we search both for normalised and non-normalised form of $authid
31 // because the normalization step was removed from setting
32 // mod/settings.php in 8367cad so it might have left mixed
33 // records in the user table
35 $r = q("SELECT *, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey` FROM `user`
36 WHERE ( `openid` = '%s' OR `openid` = '%s' )
37 AND `blocked` = 0 AND `account_expired` = 0
38 AND `account_removed` = 0 AND `verified` = 1
40 dbesc($authid), dbesc(normalise_openid($authid))
43 if (dbm::is_result($r)) {
45 // successful OpenID login
47 unset($_SESSION['openid']);
49 require_once('include/security.php');
50 authenticate_success($r[0],true,true);
52 // just in case there was no return url set
53 // and we fell through
55 goaway(System::baseUrl());
58 // Successful OpenID login - but we can't match it to an existing account.
61 if ($a->config['register_policy'] == REGISTER_CLOSED) {
62 notice( t('Account not found and OpenID registration is not permitted on this site.') . EOL);
63 goaway(System::baseUrl());
66 unset($_SESSION['register']);
68 $attr = $openid->getAttributes();
69 if (is_array($attr) && count($attr)) {
70 foreach ($attr as $k => $v) {
71 if ($k === 'namePerson/friendly') {
72 $nick = notags(trim($v));
74 if($k === 'namePerson/first') {
75 $first = notags(trim($v));
77 if($k === 'namePerson') {
78 $args .= '&username=' . urlencode(notags(trim($v)));
80 if ($k === 'contact/email') {
81 $args .= '&email=' . urlencode(notags(trim($v)));
83 if ($k === 'media/image/aspect11') {
84 $photosq = bin2hex(trim($v));
86 if ($k === 'media/image/default') {
87 $photo = bin2hex(trim($v));
92 $args .= '&nickname=' . urlencode($nick);
95 $args .= '&nickname=' . urlencode($first);
99 $args .= '&photo=' . urlencode($photosq);
102 $args .= '&photo=' . urlencode($photo);
105 $args .= '&openid_url=' . urlencode(notags(trim($authid)));
107 goaway(System::baseUrl() . '/register?' . $args);
112 notice( t('Login failed.') . EOL);
113 goaway(System::baseUrl());