]> git.mxchange.org Git - friendica.git/blob - include/redir.php
make sure that the contact is local for auto redir
[friendica.git] / include / redir.php
1 <?php
2
3 function auto_redir(&$a, $contact_nick) {
4
5         if((! $contact_nick) || ($contact_nick === $a->user['nickname']))
6                 return;
7
8         if(local_user()) {
9
10                 // We need to find out if $contact_nick is a user on this hub, and if so, if I
11                 // am a contact of that user. However, that user may have other contacts with the
12                 // same nickname as me on other hubs or other networks. Exclude these by requiring
13                 // that the contact have a local URL. I will be the only person with my nickname at
14                 // this URL, so if a result is found, then I am a contact of the $contact_nick user.
15
16                 $baseurl = $a->get_baseurl();
17                 $domain_st = strpos($baseurl, "://");
18                 if($domain_st === false)
19                         return;
20                 $baseurl = substr($baseurl, $domain_st + 3);
21
22                 $r = q("SELECT id FROM contact WHERE uid = ( SELECT uid FROM user WHERE nickname = '%s' LIMIT 1 )
23                         AND nick = '%s' AND self = 0 AND url LIKE '%%%s%%' LIMIT 1",
24                            dbesc($contact_nick),
25                            dbesc($a->user['nickname']),
26                        dbesc($baseurl)
27                 );
28
29                 if((!$r) || (! count($r)) || $r[0]['id'] == remote_user())
30                         return;
31
32
33                 $r = q("SELECT * FROM contact WHERE nick = '%s' AND network = '%s' AND uid = %d LIMIT 1",
34                        dbesc($contact_nick),
35                        dbesc(NETWORK_DFRN),
36                        intval(local_user())
37                 );
38
39                 if(! ($r && count($r)))
40                         return;
41
42                 $cid = $r[0]['id'];
43
44                 $dfrn_id = $orig_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']);
45
46                 if($r[0]['duplex'] && $r[0]['issued-id']) {
47                         $orig_id = $r[0]['issued-id'];
48                         $dfrn_id = '1:' . $orig_id;
49                 }
50                 if($r[0]['duplex'] && $r[0]['dfrn-id']) {
51                         $orig_id = $r[0]['dfrn-id'];
52                         $dfrn_id = '0:' . $orig_id;
53                 }
54
55                 $sec = random_string();
56
57                 q("INSERT INTO `profile_check` ( `uid`, `cid`, `dfrn_id`, `sec`, `expire`)
58                         VALUES( %d, %s, '%s', '%s', %d )",
59                         intval(local_user()),
60                         intval($cid),
61                         dbesc($dfrn_id),
62                         dbesc($sec),
63                         intval(time() + 45)
64                 );
65
66                 $url = curPageURL();
67
68                 logger('auto_redir: ' . $r[0]['name'] . ' ' . $sec, LOGGER_DEBUG); 
69                 $dest = (($url) ? '&destination_url=' . $url : '');
70                 goaway ($r[0]['poll'] . '?dfrn_id=' . $dfrn_id 
71                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest );
72         }
73
74         return;
75 }
76
77