]> git.mxchange.org Git - friendica.git/blob - mod/openid.php
Merge pull request #5907 from nupplaphil/goaway_to_redirectto
[friendica.git] / mod / openid.php
1 <?php
2 /**
3  * @file mod/openid.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\Authentication;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12
13 function openid_content(App $a) {
14
15         $noid = Config::get('system','no_openid');
16         if($noid)
17                 $a->internalRedirect();
18
19         logger('mod_openid ' . print_r($_REQUEST,true), LOGGER_DATA);
20
21         if((x($_GET,'openid_mode')) && (x($_SESSION,'openid'))) {
22
23                 $openid = new LightOpenID($a->getHostName());
24
25                 if($openid->validate()) {
26
27                         $authid = $_REQUEST['openid_identity'];
28
29                         if(! strlen($authid)) {
30                                 logger(L10n::t('OpenID protocol error. No ID returned.') . EOL);
31                                 $a->internalRedirect();
32                         }
33
34                         // NOTE: we search both for normalised and non-normalised form of $authid
35                         //       because the normalization step was removed from setting
36                         //       mod/settings.php in 8367cad so it might have left mixed
37                         //       records in the user table
38                         //
39                         $r = q("SELECT *
40                                 FROM `user`
41                                 WHERE ( `openid` = '%s' OR `openid` = '%s' )
42                                 AND `blocked` = 0 AND `account_expired` = 0
43                                 AND `account_removed` = 0 AND `verified` = 1
44                                 LIMIT 1",
45                                 DBA::escape($authid), DBA::escape(normalise_openid($authid))
46                         );
47
48                         if (DBA::isResult($r)) {
49
50                                 // successful OpenID login
51
52                                 unset($_SESSION['openid']);
53
54                                 Authentication::setAuthenticatedSessionForUser($r[0],true,true);
55
56                                 // just in case there was no return url set
57                                 // and we fell through
58
59                                 $a->internalRedirect();
60                         }
61
62                         // Successful OpenID login - but we can't match it to an existing account.
63                         // New registration?
64
65                         if (intval(Config::get('config', 'register_policy')) === REGISTER_CLOSED) {
66                                 notice(L10n::t('Account not found and OpenID registration is not permitted on this site.') . EOL);
67                                 $a->internalRedirect();
68                         }
69
70                         unset($_SESSION['register']);
71                         $args = '';
72                         $attr = $openid->getAttributes();
73                         if (is_array($attr) && count($attr)) {
74                                 foreach ($attr as $k => $v) {
75                                         if ($k === 'namePerson/friendly') {
76                                                 $nick = notags(trim($v));
77                                         }
78                                         if($k === 'namePerson/first') {
79                                                 $first = notags(trim($v));
80                                         }
81                                         if($k === 'namePerson') {
82                                                 $args .= '&username=' . urlencode(notags(trim($v)));
83                                         }
84                                         if ($k === 'contact/email') {
85                                                 $args .= '&email=' . urlencode(notags(trim($v)));
86                                         }
87                                         if ($k === 'media/image/aspect11') {
88                                                 $photosq = bin2hex(trim($v));
89                                         }
90                                         if ($k === 'media/image/default') {
91                                                 $photo = bin2hex(trim($v));
92                                         }
93                                 }
94                         }
95                         if ($nick) {
96                                 $args .= '&nickname=' . urlencode($nick);
97                         }
98                         elseif ($first) {
99                                 $args .= '&nickname=' . urlencode($first);
100                         }
101
102                         if ($photosq) {
103                                 $args .= '&photo=' . urlencode($photosq);
104                         }
105                         elseif ($photo) {
106                                 $args .= '&photo=' . urlencode($photo);
107                         }
108
109                         $args .= '&openid_url=' . urlencode(notags(trim($authid)));
110
111                         $a->internalRedirect('register?' . $args);
112
113                         // NOTREACHED
114                 }
115         }
116         notice(L10n::t('Login failed.') . EOL);
117         $a->internalRedirect();
118         // NOTREACHED
119 }