Issue 7142: Prevent respawn of "remote self" items
[friendica.git/.git] / mod / redir.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\L10n;
5 use Friendica\Core\Logger;
6 use Friendica\Core\Session;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Profile;
11 use Friendica\Util\Network;
12 use Friendica\Util\Strings;
13
14 function redir_init(App $a) {
15
16         $url = defaults($_GET, 'url', '');
17         $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
18         $con_url = defaults($_GET, 'conurl', '');
19
20         if ($a->argc > 1 && intval($a->argv[1])) {
21                 $cid = intval($a->argv[1]);
22         } elseif (local_user() && !empty($con_url)) {
23                 $cid = Contact::getIdForURL($con_url, local_user());
24         } else {
25                 $cid = 0;
26         }
27
28         if (!empty($cid)) {
29                 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
30                 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
31                 if (!DBA::isResult($contact)) {
32                         notice(L10n::t('Contact not found.'));
33                         $a->internalRedirect();
34                 }
35
36                 $contact_url = $contact['url'];
37
38                 if ((!local_user() && !remote_user()) // Visitors (not logged in or not remotes) can't authenticate.
39                         || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
40                 {
41                         $a->redirect(defaults($url, $contact_url));
42                 }
43
44                 if ($contact['uid'] == 0 && local_user()) {
45                         // Let's have a look if there is an established connection
46                         // between the public contact we have found and the local user.
47                         $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
48
49                         if (DBA::isResult($contact)) {
50                                 $cid = $contact['id'];
51                         }
52
53                         if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
54                                 // Local user is already authenticated.
55                                 $target_url = defaults($url, $contact_url);
56                                 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
57                                 $a->redirect($target_url);
58                         }
59                 }
60
61                 if (remote_user()) {
62                         $host = substr($a->getBaseURL() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''), strpos($a->getBaseURL(), '://') + 3);
63                         $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
64
65                         // On a local instance we have to check if the local user has already authenticated
66                         // with the local contact. Otherwise the local user would ask the local contact
67                         // for authentification everytime he/she is visiting a profile page of the local
68                         // contact.
69                         if ($host == $remotehost
70                                 && !empty($_SESSION['remote'])
71                                 && is_array($_SESSION['remote']))
72                         {
73                                 foreach ($_SESSION['remote'] as $v) {
74                                         if (!empty($v['uid']) && !empty($v['cid']) &&
75                                             $v['uid'] == Session::get('visitor_visiting') &&
76                                             $v['cid'] == Session::get('visitor_id')) {
77                                                 // Remote user is already authenticated.
78                                                 $target_url = defaults($url, $contact_url);
79                                                 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
80                                                 $a->redirect($target_url);
81                                         }
82                                 }
83                         }
84                 }
85
86                 // When the remote page does support OWA, then we enforce the use of it
87                 $basepath = Contact::getBasepath($contact_url);
88                 if ($basepath == System::baseUrl()) {
89                         $use_magic = true;
90                 } else {
91                         $serverret = Network::curl($basepath . '/magic');
92                         $use_magic = $serverret->isSuccess();
93                 }
94
95                 // Doing remote auth with dfrn.
96                 if (local_user() && !$use_magic && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
97                         $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
98
99                         if ($contact['duplex'] && $contact['issued-id']) {
100                                 $orig_id = $contact['issued-id'];
101                                 $dfrn_id = '1:' . $orig_id;
102                         }
103                         if ($contact['duplex'] && $contact['dfrn-id']) {
104                                 $orig_id = $contact['dfrn-id'];
105                                 $dfrn_id = '0:' . $orig_id;
106                         }
107
108                         $sec = Strings::getRandomHex();
109
110                         $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
111                                 'sec' => $sec, 'expire' => time() + 45];
112                         DBA::insert('profile_check', $fields);
113
114                         Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
115
116                         $dest = (!empty($url) ? '&destination_url=' . $url : '');
117
118                         System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
119                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
120                 }
121
122                 $url = defaults($url, $contact_url);
123         }
124
125         // If we don't have a connected contact, redirect with
126         // the 'zrl' parameter.
127         if (!empty($url)) {
128                 $my_profile = Profile::getMyURL();
129
130                 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
131                         $separator = strpos($url, '?') ? '&' : '?';
132
133                         $url .= $separator . 'zrl=' . urlencode($my_profile);
134                 }
135
136                 Logger::log('redirecting to ' . $url, Logger::DEBUG);
137                 $a->redirect($url);
138         }
139
140         notice(L10n::t('Contact not found.'));
141         $a->internalRedirect();
142 }