]> git.mxchange.org Git - friendica.git/blob - mod/redir.php
Merge pull request #6181 from JonnyTischbein/feature_admin_subsubpages
[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\Protocol;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Profile;
11 use Friendica\Util\Strings;
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 ($contact['network'] !== Protocol::DFRN // Authentication isn't supported for non DFRN contacts.
38                         || (!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                                 && x($_SESSION, 'remote')
71                                 && is_array($_SESSION['remote']))
72                         {
73                                 foreach ($_SESSION['remote'] as $v) {
74                                         if ($v['uid'] == $_SESSION['visitor_visiting'] && $v['cid'] == $_SESSION['visitor_id']) {
75                                                 // Remote user is already authenticated.
76                                                 $target_url = defaults($url, $contact_url);
77                                                 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
78                                                 $a->redirect($target_url);
79                                         }
80                                 }
81                         }
82                 }
83
84                 // Doing remote auth with dfrn.
85                 if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
86                         $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
87
88                         if ($contact['duplex'] && $contact['issued-id']) {
89                                 $orig_id = $contact['issued-id'];
90                                 $dfrn_id = '1:' . $orig_id;
91                         }
92                         if ($contact['duplex'] && $contact['dfrn-id']) {
93                                 $orig_id = $contact['dfrn-id'];
94                                 $dfrn_id = '0:' . $orig_id;
95                         }
96
97                         $sec = Strings::getRandomHex();
98
99                         $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
100                                 'sec' => $sec, 'expire' => time() + 45];
101                         DBA::insert('profile_check', $fields);
102
103                         Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
104
105                         $dest = (!empty($url) ? '&destination_url=' . $url : '');
106
107                         System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
108                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
109                 }
110
111                 $url = defaults($url, $contact_url);
112         }
113
114         // If we don't have a connected contact, redirect with
115         // the 'zrl' parameter.
116         if (!empty($url)) {
117                 $my_profile = Profile::getMyURL();
118
119                 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
120                         $separator = strpos($url, '?') ? '&' : '?';
121
122                         $url .= $separator . 'zrl=' . urlencode($my_profile);
123                 }
124
125                 Logger::log('redirecting to ' . $url, Logger::DEBUG);
126                 $a->redirect($url);
127         }
128
129         notice(L10n::t('Contact not found.'));
130         $a->internalRedirect();
131 }