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