]> git.mxchange.org Git - friendica.git/blob - include/redir.php
Merge pull request #4169 from annando/comment-dfrn
[friendica.git] / include / redir.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5 use Friendica\Database\DBM;
6
7 function auto_redir(App $a, $contact_nick)
8 {
9         // prevent looping
10         if (x($_REQUEST,'redir') && intval($_REQUEST['redir'])) {
11                 return;
12         }
13
14         if ((! $contact_nick) || ($contact_nick === $a->user['nickname'])) {
15                 return;
16         }
17
18         if (local_user()) {
19                 // We need to find out if $contact_nick is a user on this hub, and if so, if I
20                 // am a contact of that user. However, that user may have other contacts with the
21                 // same nickname as me on other hubs or other networks. Exclude these by requiring
22                 // that the contact have a local URL. I will be the only person with my nickname at
23                 // this URL, so if a result is found, then I am a contact of the $contact_nick user.
24                 //
25                 // We also have to make sure that I'm a legitimate contact--I'm not blocked or pending.
26
27                 $baseurl = System::baseUrl();
28                 $domain_st = strpos($baseurl, "://");
29                 if ($domain_st === false) {
30                         return;
31                 }
32                 $baseurl = substr($baseurl, $domain_st + 3);
33                 $nurl = normalise_link($baseurl);
34
35                 /// @todo Why is there a query for "url" *and* "nurl"? Especially this normalising is strange.
36                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = (SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1)
37                         AND `nick` = '%s' AND NOT `self` AND (`url` LIKE '%%%s%%' OR `nurl` LIKE '%%%s%%') AND NOT `blocked` AND NOT `pending` LIMIT 1",
38                                 dbesc($contact_nick),
39                                 dbesc($a->user['nickname']),
40                                 dbesc($baseurl),
41                                 dbesc($nurl)
42                 );
43                 if ((! DBM::is_result($r)) || $r[0]['id'] == remote_user()) {
44                         return;
45                 }
46
47                 $r = q("SELECT * FROM contact WHERE nick = '%s'
48                         AND network = '%s' AND uid = %d  AND url LIKE '%%%s%%' LIMIT 1",
49                        dbesc($contact_nick),
50                        dbesc(NETWORK_DFRN),
51                        intval(local_user()),
52                        dbesc($baseurl)
53                 );
54                 if (! DBM::is_result($r)) {
55                         return;
56                 }
57
58                 $cid = $r[0]['id'];
59
60                 $dfrn_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']);
61
62                 if ($r[0]['duplex'] && $r[0]['issued-id']) {
63                         $orig_id = $r[0]['issued-id'];
64                         $dfrn_id = '1:' . $orig_id;
65                 }
66                 if ($r[0]['duplex'] && $r[0]['dfrn-id']) {
67                         $orig_id = $r[0]['dfrn-id'];
68                         $dfrn_id = '0:' . $orig_id;
69                 }
70
71                 // ensure that we've got a valid ID. There may be some edge cases with forums and non-duplex mode
72                 // that may have triggered some of the "went to {profile/intro} and got an RSS feed" issues
73
74                 if (strlen($dfrn_id) < 3) {
75                         return;
76                 }
77
78                 $sec = random_string();
79
80                 q("INSERT INTO `profile_check` ( `uid`, `cid`, `dfrn_id`, `sec`, `expire`)
81                         VALUES( %d, %s, '%s', '%s', %d )",
82                         intval(local_user()),
83                         intval($cid),
84                         dbesc($dfrn_id),
85                         dbesc($sec),
86                         intval(time() + 45)
87                 );
88
89                 $url = curPageURL();
90
91                 logger('auto_redir: ' . $r[0]['name'] . ' ' . $sec, LOGGER_DEBUG);
92                 $dest = (($url) ? '&destination_url=' . $url : '');
93                 goaway ($r[0]['poll'] . '?dfrn_id=' . $dfrn_id
94                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest );
95         }
96
97         return;
98 }
99
100