]> git.mxchange.org Git - friendica.git/blob - mod/openid.php
Fixes:
[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\System;
10 use Friendica\Database\DBM;
11
12 function openid_content(App $a) {
13
14         $noid = Config::get('system','no_openid');
15         if($noid)
16                 goaway(System::baseUrl());
17
18         logger('mod_openid ' . print_r($_REQUEST,true), LOGGER_DATA);
19
20         if((x($_GET,'openid_mode')) && (x($_SESSION,'openid'))) {
21
22                 $openid = new LightOpenID($a->get_hostname());
23
24                 if($openid->validate()) {
25
26                         $authid = $_REQUEST['openid_identity'];
27
28                         if(! strlen($authid)) {
29                                 logger(L10n::t('OpenID protocol error. No ID returned.') . EOL);
30                                 goaway(System::baseUrl());
31                         }
32
33                         // NOTE: we search both for normalised and non-normalised form of $authid
34                         //       because the normalization step was removed from setting
35                         //       mod/settings.php in 8367cad so it might have left mixed
36                         //       records in the user table
37                         //
38                         $r = q("SELECT *
39                                 FROM `user`
40                                 WHERE ( `openid` = '%s' OR `openid` = '%s' )
41                                 AND `blocked` = 0 AND `account_expired` = 0
42                                 AND `account_removed` = 0 AND `verified` = 1
43                                 LIMIT 1",
44                                 dbesc($authid), dbesc(normalise_openid($authid))
45                         );
46
47                         if (DBM::is_result($r)) {
48
49                                 // successful OpenID login
50
51                                 unset($_SESSION['openid']);
52
53                                 require_once('include/security.php');
54                                 authenticate_success($r[0],true,true);
55
56                                 // just in case there was no return url set
57                                 // and we fell through
58
59                                 goaway(System::baseUrl());
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                                 goaway(System::baseUrl());
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                         goaway(System::baseUrl() . '/register?' . $args);
112
113                         // NOTREACHED
114                 }
115         }
116         notice(L10n::t('Login failed.') . EOL);
117         goaway(System::baseUrl());
118         // NOTREACHED
119 }