- if (!empty($cid)) {
- $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
- $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
- if (!DBA::isResult($contact)) {
- notice(L10n::t('Contact not found.'));
- DI::baseUrl()->redirect();
- }
-
- $contact_url = $contact['url'];
-
- if (!Session::isAuthenticated() // Visitors (not logged in or not remotes) can't authenticate.
- || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
- {
- $a->redirect($url ?: $contact_url);
- }
-
- if ($contact['uid'] == 0 && local_user()) {
- // Let's have a look if there is an established connection
- // between the public contact we have found and the local user.
- $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
-
- if (DBA::isResult($contact)) {
- $cid = $contact['id'];
- }
-
- if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
- // Local user is already authenticated.
- $target_url = $url ?: $contact_url;
- Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
- $a->redirect($target_url);
- }
- }
-
- if (remote_user()) {
- $host = substr(DI::baseUrl()->getUrlPath() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''), strpos(DI::baseUrl()->getUrlPath(), '://') + 3);
- $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
-
- // On a local instance we have to check if the local user has already authenticated
- // with the local contact. Otherwise the local user would ask the local contact
- // for authentification everytime he/she is visiting a profile page of the local
- // contact.
- if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
- // Remote user is already authenticated.
- $target_url = $url ?: $contact_url;
- Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
- $a->redirect($target_url);
- }
- }