]> git.mxchange.org Git - friendica.git/blob - mod/openid.php
refactor openid logins/registrations
[friendica.git] / mod / openid.php
1 <?php
2
3
4 require_once('library/openid.php');
5
6
7 function openid_content(&$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                 $openid = new LightOpenID;
17
18                 if($openid->validate()) {
19
20                         $authid = normalise_openid($_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                         $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey` 
28                                 FROM `user` WHERE `openid` = '%s' AND `blocked` = 0 
29                                 AND `account_expired` = 0 AND `verified` = 1 LIMIT 1",
30                                 dbesc($authid)
31                         );
32
33                         if($r && count($r)) {
34                                 unset($_SESSION['openid']);
35
36                                 require_once('include/security.php');
37                                 authenticate_success($r[0],true,true);
38
39                                 // just in case there was no return url set 
40                                 // and we fell through
41
42                                 goaway(z_root());
43                         }
44
45                         // new registration?
46
47                         if($a->config['register_policy'] == REGISTER_CLOSED) {
48                                 notice( t('Account not found and OpenID registration is not permitted on this site.') . EOL);
49                                 goaway(z_root());
50                         }
51
52                         unset($_SESSION['register']);
53                         $args = '';
54                         $attr = $openid->getAttributes();
55                         if(is_array($attr) && count($attr)) {
56                                 foreach($attr as $k => $v) {
57                                         if($k === 'namePerson/friendly')
58                                                 $nick = notags(trim($v));
59                                         if($k === 'namePerson/first')
60                                                 $first = notags(trim($v));
61                                         if($k === 'namePerson')
62                                                 $args .= '&username=' . notags(trim($v));
63                                         if($k === 'contact/email')
64                                                 $args .= '&email=' . notags(trim($v));
65                                         if($k === 'media/image/aspect11')
66                                                 $photosq = bin2hex(trim($v));
67                                         if($k === 'media/image/default')
68                                                 $photo = bin2hex(trim($v));
69                                 }
70                         }
71                         if($nick)
72                                 $args .= '&nickname=' . $nick;
73                         elseif($first)
74                                 $args .= '&nickname=' . $first;
75
76                         if($photosq)
77                                 $args .= '&photo=' . $photosq;
78                         elseif($photo)
79                                 $args .= '&photo=' . $photo;
80
81                         $args .= '&openid_url=' . notags(trim($authid));
82
83                         goaway($a->get_baseurl() . '/register' . $args);
84
85                         // NOTREACHED
86                 }
87         }
88         notice( t('Login failed.') . EOL);
89         goaway(z_root());
90         // NOTREACHED
91 }