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