]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Magic.php
New user account type "Channel Relay"
[friendica.git] / src / Module / Magic.php
index 5276252de25c57930f5803f766a9effd10a3a3e5..7b60299e20ded83dd6aef031283e2745aec0c706 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2023, the Friendica project
+ * @copyright Copyright (C) 2010-2024, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -29,12 +29,14 @@ use Friendica\Core\Session\Capability\IHandleUserSessions;
 use Friendica\Core\System;
 use Friendica\Database\Database;
 use Friendica\Model\Contact;
+use Friendica\Model\GServer;
 use Friendica\Model\User;
 use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Util\HTTPSignature;
 use Friendica\Util\Profiler;
 use Friendica\Util\Strings;
+use GuzzleHttp\Psr7\Uri;
 use Psr\Log\LoggerInterface;
 
 /**
@@ -83,6 +85,8 @@ class Magic extends BaseModule
                        $this->logger->debug('bdest detected', ['dest' => $dest]);
                }
 
+               $target = $dest ?: $addr;
+
                if ($addr ?: $dest) {
                        $contact = Contact::getByURL($addr ?: $dest);
                }
@@ -110,17 +114,24 @@ class Magic extends BaseModule
                // OpenWebAuth
                $owner = User::getOwnerDataById($this->userSession->getLocalUserId());
 
-               $gserver = $this->dba->selectFirst('gserver', ['url'], ['id' => $contact['gsid']]);
-               if (empty($gserver)) {
-                       $this->logger->notice('Server not found, redirecting to destination.', ['gsid' => $contact['gsid'], 'dest' => $dest]);
+               if (!empty($contact['gsid'])) {
+                       $gserver = $this->dba->selectFirst('gserver', ['url'], ['id' => $contact['gsid']]);
+                       if (empty($gserver)) {
+                               $this->logger->notice('Server not found, redirecting to destination.', ['gsid' => $contact['gsid'], 'dest' => $dest]);
+                               System::externalRedirect($dest);
+                       }
+
+                       $basepath = $gserver['url'];
+               } elseif (GServer::check($target)) {
+                       $basepath = (string)GServer::cleanUri(new Uri($target));
+               } else {
+                       $this->logger->notice('The target is not a server path, redirecting to destination.', ['target' => $target]);
                        System::externalRedirect($dest);
                }
 
-               $basepath = $gserver['url'];
-
                $header = [
-                       'Accept'          => ['application/x-dfrn+json', 'application/x-zot+json'],
-                       'X-Open-Web-Auth' => [Strings::getRandomHex()],
+                       'Accept'          => 'application/x-dfrn+json, application/x-zot+json',
+                       'X-Open-Web-Auth' => Strings::getRandomHex()
                ];
 
                // Create a header that is signed with the local users private key.
@@ -144,7 +155,7 @@ class Magic extends BaseModule
                        System::externalRedirect($dest);
                }
 
-               $j = json_decode($curlResult->getBody(), true);
+               $j = json_decode($curlResult->getBodyString(), true);
                if (empty($j) || !$j['success']) {
                        $this->logger->notice('Invalid JSON, redirecting to destination.', ['json' => $j, 'dest' => $dest]);
                        $this->app->redirect($dest);