]> git.mxchange.org Git - friendica.git/blobdiff - mod/redir.php
Unified JSON exit
[friendica.git] / mod / redir.php
index cb1ca20e0e0f8d2c9bef12f53fbd90882fdf4178..dc086ae19a2347387c3541df6db5bff6e65f9cdb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -27,7 +27,8 @@ use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
-use Friendica\Util\Network;
+use Friendica\Network\HTTPClient\Client\HttpClient;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Util\Strings;
 
 function redir_init(App $a) {
@@ -36,10 +37,9 @@ function redir_init(App $a) {
        }
 
        $url = $_GET['url'] ?? '';
-       $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
 
-       if ($a->argc > 1 && intval($a->argv[1])) {
-               $cid = intval($a->argv[1]);
+       if (DI::args()->getArgc() > 1 && intval(DI::args()->getArgv()[1])) {
+               $cid = intval(DI::args()->getArgv()[1]);
        } else {
                $cid = 0;
        }
@@ -48,10 +48,10 @@ function redir_init(App $a) {
        redir_magic($a, $cid, $url);
 
        if (empty($cid)) {
-               throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
+               throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
        }
 
-       $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
+       $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name'];
        $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
        if (!DBA::isResult($contact)) {
                throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
@@ -59,7 +59,7 @@ function redir_init(App $a) {
 
        $contact_url = $contact['url'];
 
-       if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
+       if (!empty($a->getContactId()) && $a->getContactId() == $cid) {
                // Local user is already authenticated.
                redir_check_url($contact_url, $url);
                $a->redirect($url ?: $contact_url);
@@ -74,11 +74,11 @@ function redir_init(App $a) {
                        $cid = $contact['id'];
                }
 
-               if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
+               if (!empty($a->getContactId()) && $a->getContactId() == $cid) {
                        // Local user is already authenticated.
                        redir_check_url($contact_url, $url);
                        $target_url = $url ?: $contact_url;
-                       Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
+                       Logger::info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
                        $a->redirect($target_url);
                }
        }
@@ -95,43 +95,13 @@ function redir_init(App $a) {
                        // Remote user is already authenticated.
                        redir_check_url($contact_url, $url);
                        $target_url = $url ?: $contact_url;
-                       Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
+                       Logger::info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
                        $a->redirect($target_url);
                }
        }
 
-       // Doing remote auth with dfrn.
-       if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
-               $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
-
-               if ($contact['duplex'] && $contact['issued-id']) {
-                       $orig_id = $contact['issued-id'];
-                       $dfrn_id = '1:' . $orig_id;
-               }
-               if ($contact['duplex'] && $contact['dfrn-id']) {
-                       $orig_id = $contact['dfrn-id'];
-                       $dfrn_id = '0:' . $orig_id;
-               }
-
-               $sec = Strings::getRandomHex();
-
-               $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
-                       'sec' => $sec, 'expire' => time() + 45];
-               DBA::insert('profile_check', $fields);
-
-               Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
-
-               $dest = (!empty($url) ? '&destination_url=' . $url : '');
-
-               System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
-                       . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
-
-               redir_check_url($contact_url, $url);
-               $url = $url ?: $contact_url;
-       }
-
        if (empty($url)) {
-               throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
+               throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
        }
 
        // If we don't have a connected contact, redirect with
@@ -144,7 +114,7 @@ function redir_init(App $a) {
                $url .= $separator . 'zrl=' . urlencode($my_profile);
        }
 
-       Logger::log('redirecting to ' . $url, Logger::DEBUG);
+       Logger::info('redirecting to ' . $url);
        $a->redirect($url);
 }
 
@@ -174,7 +144,7 @@ function redir_magic($a, $cid, $url)
        }
 
        // Test for magic auth on the target system
-       $serverret = Network::curl($basepath . '/magic');
+       $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClient::ACCEPT_HTML]);
        if ($serverret->isSuccess()) {
                $separator = strpos($target_url, '?') ? '&' : '?';
                $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
@@ -193,6 +163,10 @@ function redir_check_url(string $contact_url, string $url)
        }
 
        $url_host = parse_url($url, PHP_URL_HOST);
+       if (empty($url_host)) {
+               $url_host = parse_url(DI::baseUrl(), PHP_URL_HOST);
+       }
+
        $contact_url_host = parse_url($contact_url, PHP_URL_HOST);
 
        if ($url_host == $contact_url_host) {
@@ -201,4 +175,4 @@ function redir_check_url(string $contact_url, string $url)
 
        Logger::error('URL check host mismatch', ['contact' => $contact_url, 'url' => $url]);
        throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
-}
\ No newline at end of file
+}