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