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