]> git.mxchange.org Git - friendica.git/blob - mod/redir.php
9b4b440bbc9a1ea6e2c2ad1fb1dcd894b4243e7e
[friendica.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\DI;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Profile;
12 use Friendica\Util\Network;
13 use Friendica\Util\Strings;
14
15 function redir_init(App $a) {
16
17         $url = $_GET['url'] ?? '';
18         $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
19
20         if ($a->argc > 1 && intval($a->argv[1])) {
21                 $cid = intval($a->argv[1]);
22         } else {
23                 $cid = 0;
24         }
25
26         // Try magic auth before the legacy stuff
27         redir_magic($a, $cid, $url);
28
29         if (!empty($cid)) {
30                 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
31                 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
32                 if (!DBA::isResult($contact)) {
33                         notice(L10n::t('Contact not found.'));
34                         DI::baseUrl()->redirect();
35                 }
36
37                 $contact_url = $contact['url'];
38
39                 if (!Session::isAuthenticated() // Visitors (not logged in or not remotes) can't authenticate.
40                         || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
41                 {
42                         $a->redirect($url ?: $contact_url);
43                 }
44
45                 if ($contact['uid'] == 0 && local_user()) {
46                         // Let's have a look if there is an established connection
47                         // between the public contact we have found and the local user.
48                         $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
49
50                         if (DBA::isResult($contact)) {
51                                 $cid = $contact['id'];
52                         }
53
54                         if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
55                                 // Local user is already authenticated.
56                                 $target_url = $url ?: $contact_url;
57                                 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
58                                 $a->redirect($target_url);
59                         }
60                 }
61
62                 if (remote_user()) {
63                         $host = substr(DI::baseUrl()->getUrlPath() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''), strpos(DI::baseUrl()->getUrlPath(), '://') + 3);
64                         $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
65
66                         // On a local instance we have to check if the local user has already authenticated
67                         // with the local contact. Otherwise the local user would ask the local contact
68                         // for authentification everytime he/she is visiting a profile page of the local
69                         // contact.
70                         if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
71                                 // Remote user is already authenticated.
72                                 $target_url = $url ?: $contact_url;
73                                 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
74                                 $a->redirect($target_url);
75                         }
76                 }
77
78                 // Doing remote auth with dfrn.
79                 if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
80                         $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
81
82                         if ($contact['duplex'] && $contact['issued-id']) {
83                                 $orig_id = $contact['issued-id'];
84                                 $dfrn_id = '1:' . $orig_id;
85                         }
86                         if ($contact['duplex'] && $contact['dfrn-id']) {
87                                 $orig_id = $contact['dfrn-id'];
88                                 $dfrn_id = '0:' . $orig_id;
89                         }
90
91                         $sec = Strings::getRandomHex();
92
93                         $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
94                                 'sec' => $sec, 'expire' => time() + 45];
95                         DBA::insert('profile_check', $fields);
96
97                         Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
98
99                         $dest = (!empty($url) ? '&destination_url=' . $url : '');
100
101                         System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
102                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
103                 }
104
105                 $url = $url ?: $contact_url;
106         }
107
108         // If we don't have a connected contact, redirect with
109         // the 'zrl' parameter.
110         if (!empty($url)) {
111                 $my_profile = Profile::getMyURL();
112
113                 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
114                         $separator = strpos($url, '?') ? '&' : '?';
115
116                         $url .= $separator . 'zrl=' . urlencode($my_profile);
117                 }
118
119                 Logger::log('redirecting to ' . $url, Logger::DEBUG);
120                 $a->redirect($url);
121         }
122
123         notice(L10n::t('Contact not found.'));
124         DI::baseUrl()->redirect();
125 }
126
127 function redir_magic($a, $cid, $url)
128 {
129         $visitor = Profile::getMyURL();
130         if (!empty($visitor)) {
131                 Logger::info('Got my url', ['visitor' => $visitor]);
132         }
133
134         $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
135         if (!DBA::isResult($contact)) {
136                 Logger::info('Contact not found', ['id' => $cid]);
137                 // Shouldn't happen under normal conditions
138                 notice(L10n::t('Contact not found.'));
139                 if (!empty($url)) {
140                         System::externalRedirect($url);
141                 } else {
142                         DI::baseUrl()->redirect();
143                 }
144         } else {
145                 $contact_url = $contact['url'];
146                 $target_url = $url ?: $contact_url;
147         }
148
149         $basepath = Contact::getBasepath($contact_url);
150
151         // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
152         if (empty($visitor) || Strings::compareLink($basepath, System::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
153                 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
154                 System::externalRedirect($target_url);
155         }
156
157         // Test for magic auth on the target system
158         $serverret = Network::curl($basepath . '/magic');
159         if ($serverret->isSuccess()) {
160                 $separator = strpos($target_url, '?') ? '&' : '?';
161                 $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
162
163                 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
164                 System::externalRedirect($target_url);
165         } else {
166                 Logger::info('No magic for contact', ['contact' => $contact_url]);
167         }
168 }