From 19f2ce69975ed63589eca461f88a46da2bf57625 Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind@owncloud.com>
Date: Mon, 16 Jul 2012 19:56:43 +0200
Subject: [PATCH] improve webfinger for situations where owncloud is not
 installed in the server root

---
 apps/user_webfinger/.htaccess     |  5 +++++
 apps/user_webfinger/host-meta.php | 19 ++++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 apps/user_webfinger/.htaccess

diff --git a/apps/user_webfinger/.htaccess b/apps/user_webfinger/.htaccess
new file mode 100644
index 0000000000..1b13cf788f
--- /dev/null
+++ b/apps/user_webfinger/.htaccess
@@ -0,0 +1,5 @@
+<IfModule mod_rewrite.c>
+RewriteEngine on
+RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
+RewriteRule ^host-meta host-meta.php [QSA,L]
+</IfModule>
diff --git a/apps/user_webfinger/host-meta.php b/apps/user_webfinger/host-meta.php
index 32ffb51205..4ac37b1ea0 100644
--- a/apps/user_webfinger/host-meta.php
+++ b/apps/user_webfinger/host-meta.php
@@ -1,10 +1,27 @@
 <?php
+
+if(class_exists('OC')){
+	$WEBROOT=OC::$WEBROOT;
+}else{//not called trough remote.php try to guess the webroot the best we can from here
+	// calculate the root directories
+	$SERVERROOT=str_replace("\\",'/',substr(__FILE__,0,-strlen('apps/user_webfinger/host-meta.php')));
+	$WEBROOT=substr($SERVERROOT,strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
+
+	if($WEBROOT!='' and $WEBROOT[0]!=='/'){
+		$WEBROOT='/'.$WEBROOT;
+	}
+}
+
+if(substr($WEBROOT,-1)==='/'){
+	$WEBROOT=substr($WEBROOT,0,-1);
+}
+
 $hostMetaHeader = array(
 	'Access-Control-Allow-Origin' => '*',
 	'Content-Type' => 'application/xrd+json'
 );
 $serverName = $_SERVER['SERVER_NAME'];
-$hostMetaContents = '{"links":[{"rel":"lrdd","template":"http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$serverName.'/public.php?service=webfinger&q={uri}"}]}';
+$hostMetaContents = '{"links":[{"rel":"lrdd","template":"http'.(isset($_SERVER['HTTPS'])?'s':'').'://'.$serverName.$WEBROOT.'/public.php?service=webfinger&q={uri}"}]}';
 foreach($hostMetaHeader as $header => $value) {
 	header($header . ": " . $value);
 }
-- 
GitLab