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