]> git.mxchange.org Git - friendica.git/blob - include/redir.php
Merge https://github.com/friendica/friendica into pull
[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                 // We also have to make sure that I'm a legitimate contact--I'm not blocked or pending.
17
18                 $baseurl = $a->get_baseurl();
19                 $domain_st = strpos($baseurl, "://");
20                 if($domain_st === false)
21                         return;
22                 $baseurl = substr($baseurl, $domain_st + 3);
23
24                 $r = q("SELECT id FROM contact WHERE uid = ( SELECT uid FROM user WHERE nickname = '%s' LIMIT 1 )
25                         AND nick = '%s' AND self = 0 AND url LIKE '%%%s%%' AND blocked = 0 AND pending = 0 LIMIT 1",
26                            dbesc($contact_nick),
27                            dbesc($a->user['nickname']),
28                        dbesc($baseurl)
29                 );
30
31                 if((!$r) || (! count($r)) || $r[0]['id'] == remote_user())
32                         return;
33
34
35                 $r = q("SELECT * FROM contact WHERE nick = '%s'
36                         AND network = '%s' AND uid = %d  AND url LIKE '%%%s%%' LIMIT 1",
37                        dbesc($contact_nick),
38                        dbesc(NETWORK_DFRN),
39                        intval(local_user()),
40                        dbesc($baseurl)
41                 );
42
43                 if(! ($r && count($r)))
44                         return;
45
46                 $cid = $r[0]['id'];
47
48                 $dfrn_id = $orig_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']);
49
50                 if($r[0]['duplex'] && $r[0]['issued-id']) {
51                         $orig_id = $r[0]['issued-id'];
52                         $dfrn_id = '1:' . $orig_id;
53                 }
54                 if($r[0]['duplex'] && $r[0]['dfrn-id']) {
55                         $orig_id = $r[0]['dfrn-id'];
56                         $dfrn_id = '0:' . $orig_id;
57                 }
58
59                 $sec = random_string();
60
61                 q("INSERT INTO `profile_check` ( `uid`, `cid`, `dfrn_id`, `sec`, `expire`)
62                         VALUES( %d, %s, '%s', '%s', %d )",
63                         intval(local_user()),
64                         intval($cid),
65                         dbesc($dfrn_id),
66                         dbesc($sec),
67                         intval(time() + 45)
68                 );
69
70                 $url = curPageURL();
71
72                 logger('auto_redir: ' . $r[0]['name'] . ' ' . $sec, LOGGER_DEBUG); 
73                 $dest = (($url) ? '&destination_url=' . $url : '');
74                 goaway ($r[0]['poll'] . '?dfrn_id=' . $dfrn_id 
75                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest );
76         }
77
78         return;
79 }
80
81