]> git.mxchange.org Git - friendica.git/blob - mod/openid.php
Issue-#3873
[friendica.git] / mod / openid.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6
7 require_once('library/openid.php');
8
9 function openid_content(App $a) {
10
11         $noid = Config::get('system','no_openid');
12         if($noid)
13                 goaway(System::baseUrl());
14
15         logger('mod_openid ' . print_r($_REQUEST,true), LOGGER_DATA);
16
17         if((x($_GET,'openid_mode')) && (x($_SESSION,'openid'))) {
18
19                 $openid = new LightOpenID;
20
21                 if($openid->validate()) {
22
23                         $authid = $_REQUEST['openid_identity'];
24
25                         if(! strlen($authid)) {
26                                 logger( t('OpenID protocol error. No ID returned.') . EOL);
27                                 goaway(System::baseUrl());
28                         }
29
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
34                         //
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
39                                 LIMIT 1",
40                                 dbesc($authid), dbesc(normalise_openid($authid))
41                         );
42
43                         if (dbm::is_result($r)) {
44
45                                 // successful OpenID login
46
47                                 unset($_SESSION['openid']);
48
49                                 require_once('include/security.php');
50                                 authenticate_success($r[0],true,true);
51
52                                 // just in case there was no return url set
53                                 // and we fell through
54
55                                 goaway(System::baseUrl());
56                         }
57
58                         // Successful OpenID login - but we can't match it to an existing account.
59                         // New registration?
60
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());
64                         }
65
66                         unset($_SESSION['register']);
67                         $args = '';
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));
73                                         }
74                                         if($k === 'namePerson/first') {
75                                                 $first = notags(trim($v));
76                                         }
77                                         if($k === 'namePerson') {
78                                                 $args .= '&username=' . urlencode(notags(trim($v)));
79                                         }
80                                         if ($k === 'contact/email') {
81                                                 $args .= '&email=' . urlencode(notags(trim($v)));
82                                         }
83                                         if ($k === 'media/image/aspect11') {
84                                                 $photosq = bin2hex(trim($v));
85                                         }
86                                         if ($k === 'media/image/default') {
87                                                 $photo = bin2hex(trim($v));
88                                         }
89                                 }
90                         }
91                         if ($nick) {
92                                 $args .= '&nickname=' . urlencode($nick);
93                         }
94                         elseif ($first) {
95                                 $args .= '&nickname=' . urlencode($first);
96                         }
97
98                         if ($photosq) {
99                                 $args .= '&photo=' . urlencode($photosq);
100                         }
101                         elseif ($photo) {
102                                 $args .= '&photo=' . urlencode($photo);
103                         }
104
105                         $args .= '&openid_url=' . urlencode(notags(trim($authid)));
106
107                         goaway(System::baseUrl() . '/register?' . $args);
108
109                         // NOTREACHED
110                 }
111         }
112         notice( t('Login failed.') . EOL);
113         goaway(System::baseUrl());
114         // NOTREACHED
115 }