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