]> git.mxchange.org Git - friendica.git/blob - mod/openid.php
Merge remote-tracking branch 'upstream/develop' into sanitize-gcontact
[friendica.git] / mod / openid.php
1 <?php
2 /**
3  * @file mod/openid.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Session;
11 use Friendica\Database\DBA;
12 use Friendica\Util\Strings;
13
14 function openid_content(App $a) {
15
16         if (Config::get('system','no_openid')) {
17                 $a->internalRedirect();
18         }
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                         $authid = $openid->identity;
28
29                         if (empty($authid)) {
30                                 Logger::log(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                         $condition = ['blocked' => false, 'account_expired' => false, 'account_removed' => false, 'verified' => true,
40                                 'openid' => [$authid, Strings::normaliseOpenID($authid)]];
41                         $user  = DBA::selectFirst('user', [], $condition);
42                         if (DBA::isResult($user)) {
43
44                                 // successful OpenID login
45
46                                 unset($_SESSION['openid']);
47
48                                 Session::setAuthenticatedForUser($a, $user, true, true);
49
50                                 // just in case there was no return url set
51                                 // and we fell through
52
53                                 $a->internalRedirect();
54                         }
55
56                         // Successful OpenID login - but we can't match it to an existing account.
57                         // New registration?
58
59                         if (intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED) {
60                                 notice(L10n::t('Account not found and OpenID registration is not permitted on this site.') . EOL);
61                                 $a->internalRedirect();
62                         }
63
64                         unset($_SESSION['register']);
65                         $args = '';
66                         $attr = $openid->getAttributes();
67                         if (is_array($attr) && count($attr)) {
68                                 foreach ($attr as $k => $v) {
69                                         if ($k === 'namePerson/friendly') {
70                                                 $nick = Strings::escapeTags(trim($v));
71                                         }
72                                         if ($k === 'namePerson/first') {
73                                                 $first = Strings::escapeTags(trim($v));
74                                         }
75                                         if ($k === 'namePerson') {
76                                                 $args .= '&username=' . urlencode(Strings::escapeTags(trim($v)));
77                                         }
78                                         if ($k === 'contact/email') {
79                                                 $args .= '&email=' . urlencode(Strings::escapeTags(trim($v)));
80                                         }
81                                         if ($k === 'media/image/aspect11') {
82                                                 $photosq = bin2hex(trim($v));
83                                         }
84                                         if ($k === 'media/image/default') {
85                                                 $photo = bin2hex(trim($v));
86                                         }
87                                 }
88                         }
89                         if (!empty($nick)) {
90                                 $args .= '&nickname=' . urlencode($nick);
91                         } elseif (!empty($first)) {
92                                 $args .= '&nickname=' . urlencode($first);
93                         }
94
95                         if (!empty($photosq)) {
96                                 $args .= '&photo=' . urlencode($photosq);
97                         } elseif (!empty($photo)) {
98                                 $args .= '&photo=' . urlencode($photo);
99                         }
100
101                         $args .= '&openid_url=' . urlencode(Strings::escapeTags(trim($authid)));
102
103                         $a->internalRedirect('register?' . $args);
104
105                         // NOTREACHED
106                 }
107         }
108         notice(L10n::t('Login failed.') . EOL);
109         $a->internalRedirect();
110         // NOTREACHED
111 }