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