From 491fc5f78aadb787a57a0f0630779f457ff1bcb4 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Tue, 4 Dec 2018 07:12:55 +0000
Subject: [PATCH] Issue 6168: "redir" now mostly use OWA

---
 mod/dfrn_poll.php          |  6 +-----
 mod/redir.php              | 12 ++++++++++--
 src/Model/Contact.php      | 20 ++++++++++++++++++++
 src/Network/CurlResult.php |  2 +-
 src/Network/Probe.php      |  2 +-
 src/Util/LDSignature.php   |  2 +-
 6 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/mod/dfrn_poll.php b/mod/dfrn_poll.php
index 646047ea5..001202d5e 100644
--- a/mod/dfrn_poll.php
+++ b/mod/dfrn_poll.php
@@ -561,11 +561,7 @@ function dfrn_poll_content(App $a)
 					break;
 				default:
 					$appendix = (strstr($destination_url, '?') ? '&f=&redir=1' : '?f=&redir=1');
-					if (filter_var($url, FILTER_VALIDATE_URL)) {
-						System::externalRedirect($destination_url . $appendix);
-					} else {
-						$a->internalRedirect($destination_url . $appendix);
-					}
+					$a->redirect($destination_url . $appendix);
 					break;
 			}
 			// NOTREACHED
diff --git a/mod/redir.php b/mod/redir.php
index 9f579a9dc..f8dfa3ab9 100644
--- a/mod/redir.php
+++ b/mod/redir.php
@@ -9,6 +9,7 @@ use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
 use Friendica\Util\Strings;
+use Friendica\Util\Network;
 
 function redir_init(App $a) {
 
@@ -34,8 +35,7 @@ function redir_init(App $a) {
 
 		$contact_url = $contact['url'];
 
-		if ($contact['network'] !== Protocol::DFRN // Authentication isn't supported for non DFRN contacts.
-			|| (!local_user() && !remote_user()) // Visitors (not logged in or not remotes) can't authenticate.
+		if ((!local_user() && !remote_user()) // 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(defaults($url, $contact_url));
@@ -81,6 +81,14 @@ function redir_init(App $a) {
 			}
 		}
 
+		// When the remote page does support OWA, then we enforce the use of it
+		$basepath = Contact::getBasepath($contact_url);
+		$serverret = Network::curl($basepath . '/magic');
+		if ($serverret->isSuccess()) {
+			$contact['issued-id'] = '';
+			$contact['dfrn-id'] = '';
+		}
+
 		// 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']);
diff --git a/src/Model/Contact.php b/src/Model/Contact.php
index 539246285..5507ba3d1 100644
--- a/src/Model/Contact.php
+++ b/src/Model/Contact.php
@@ -98,6 +98,26 @@ class Contact extends BaseObject
 	 * @}
 	 */
 
+	/**
+	 * @brief Get the basepath for a given contact link
+	 *
+	 * @param string $url The contact link
+	 *
+	 * @return string basepath
+	 */
+	public static function getBasepath($url)
+	{
+		$data = Probe::uri($url);
+		if (!empty($data['baseurl'])) {
+			return $data['baseurl'];
+		}
+
+		$urlarr = explode('/profile/', $url);
+                $basepath = $urlarr[0];
+
+		return $basepath;
+	}
+
 	/**
 	 * @brief Returns the contact id for the user and the public contact id for a given contact id
 	 *
diff --git a/src/Network/CurlResult.php b/src/Network/CurlResult.php
index b2587799d..eb446af4e 100644
--- a/src/Network/CurlResult.php
+++ b/src/Network/CurlResult.php
@@ -177,7 +177,7 @@ class CurlResult
 
 			$this->redirectUrl = Network::unparseURL($redirect_parts);
 
-			$this->isRedirectUrl = filter_var($this->redirectUrl, FILTER_VALIDATE_URL) !== false;
+			$this->isRedirectUrl = true;
 		} else {
 			$this->isRedirectUrl = false;
 		}
diff --git a/src/Network/Probe.php b/src/Network/Probe.php
index 5d392a2af..f4e3d1d43 100644
--- a/src/Network/Probe.php
+++ b/src/Network/Probe.php
@@ -164,7 +164,7 @@ class Probe
 			}
 		}
 
-		self::$baseurl = "http://".$host;
+		self::$baseurl = $host_url;
 
 		Logger::log("Probing successful for ".$host, Logger::DEBUG);
 
diff --git a/src/Util/LDSignature.php b/src/Util/LDSignature.php
index c9fde441f..5acdfd901 100644
--- a/src/Util/LDSignature.php
+++ b/src/Util/LDSignature.php
@@ -27,7 +27,7 @@ class LDSignature
 		}
 
 		$actor = JsonLD::fetchElement($data, 'actor', 'id');
-		if (empty($actor)) {
+		if (empty($actor) || !is_string($actor)) {
 			return false;
 		}