]> git.mxchange.org Git - friendica.git/blob - mod/openid.php
Merge pull request #6977 from annando/fix-reshare
[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\Logger;
11 use Friendica\Database\DBA;
12 use Friendica\Util\Strings;
13
14 function openid_content(App $a) {
15
16         $noid = Config::get('system','no_openid');
17         if($noid)
18                 $a->internalRedirect();
19
20         Logger::log('mod_openid ' . print_r($_REQUEST,true), Logger::DATA);
21
22         if(!empty($_GET['openid_mode']) && !empty($_SESSION['openid'])) {
23
24                 $openid = new LightOpenID($a->getHostName());
25
26                 if($openid->validate()) {
27
28                         $authid = $_REQUEST['openid_identity'];
29
30                         if(! strlen($authid)) {
31                                 Logger::log(L10n::t('OpenID protocol error. No ID returned.') . EOL);
32                                 $a->internalRedirect();
33                         }
34
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
39                         //
40                         $r = q("SELECT *
41                                 FROM `user`
42                                 WHERE ( `openid` = '%s' OR `openid` = '%s' )
43                                 AND `blocked` = 0 AND `account_expired` = 0
44                                 AND `account_removed` = 0 AND `verified` = 1
45                                 LIMIT 1",
46                                 DBA::escape($authid), DBA::escape(Strings::normaliseOpenID($authid))
47                         );
48
49                         if (DBA::isResult($r)) {
50
51                                 // successful OpenID login
52
53                                 unset($_SESSION['openid']);
54
55                                 Authentication::setAuthenticatedSessionForUser($r[0],true,true);
56
57                                 // just in case there was no return url set
58                                 // and we fell through
59
60                                 $a->internalRedirect();
61                         }
62
63                         // Successful OpenID login - but we can't match it to an existing account.
64                         // New registration?
65
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();
69                         }
70
71                         unset($_SESSION['register']);
72                         $args = '';
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));
78                                         }
79                                         if($k === 'namePerson/first') {
80                                                 $first = Strings::escapeTags(trim($v));
81                                         }
82                                         if($k === 'namePerson') {
83                                                 $args .= '&username=' . urlencode(Strings::escapeTags(trim($v)));
84                                         }
85                                         if ($k === 'contact/email') {
86                                                 $args .= '&email=' . urlencode(Strings::escapeTags(trim($v)));
87                                         }
88                                         if ($k === 'media/image/aspect11') {
89                                                 $photosq = bin2hex(trim($v));
90                                         }
91                                         if ($k === 'media/image/default') {
92                                                 $photo = bin2hex(trim($v));
93                                         }
94                                 }
95                         }
96                         if (!empty($nick)) {
97                                 $args .= '&nickname=' . urlencode($nick);
98                         }
99                         elseif (!empty($first)) {
100                                 $args .= '&nickname=' . urlencode($first);
101                         }
102
103                         if (!empty($photosq)) {
104                                 $args .= '&photo=' . urlencode($photosq);
105                         }
106                         elseif (!empty($photo)) {
107                                 $args .= '&photo=' . urlencode($photo);
108                         }
109
110                         $args .= '&openid_url=' . urlencode(Strings::escapeTags(trim($authid)));
111
112                         $a->internalRedirect('register?' . $args);
113
114                         // NOTREACHED
115                 }
116         }
117         notice(L10n::t('Login failed.') . EOL);
118         $a->internalRedirect();
119         // NOTREACHED
120 }