From ce5ad7e7f48751b25ef9dd377fbbface2adb30b4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Julius=20H=C3=A4rtl?= <jus@bitgrid.net>
Date: Tue, 23 May 2017 15:20:54 +0200
Subject: [PATCH] Prefer custom theme over theming app
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Julius Härtl <jus@bitgrid.net>
---
 apps/theming/appinfo/app.php    | 62 ++++++++++++++++++---------------
 apps/theming/lib/Util.php       | 13 +++++++
 apps/theming/tests/UtilTest.php | 20 +++++++++++
 core/js/mimetype.js             |  2 +-
 lib/private/URLGenerator.php    | 22 +++++-------
 5 files changed, 77 insertions(+), 42 deletions(-)

diff --git a/apps/theming/appinfo/app.php b/apps/theming/appinfo/app.php
index 152504c4179..941df6f73df 100644
--- a/apps/theming/appinfo/app.php
+++ b/apps/theming/appinfo/app.php
@@ -23,33 +23,39 @@
  *
  */
 
-$linkToCSS = \OC::$server->getURLGenerator()->linkToRoute(
-	'theming.Theming.getStylesheet',
-	[
-		'v' => \OC::$server->getConfig()->getAppValue('theming', 'cachebuster', '0'),
-	]
-);
-\OCP\Util::addHeader(
-	'link',
-	[
-		'rel' => 'stylesheet',
-		'href' => $linkToCSS,
-	]
-);
+$app = new \OCP\AppFramework\App('theming');
+/** @var \OCA\Theming\Util $util */
+$util = $app->getContainer()->query(\OCA\Theming\Util::class);
+if(!$util->isAlreadyThemed()) {
 
-$linkToJs = \OC::$server->getURLGenerator()->linkToRoute(
-	'theming.Theming.getJavascript',
-	[
-		'v' => \OC::$server->getConfig()->getAppValue('theming', 'cachebuster', '0'),
-	]
-);
-\OCP\Util::addHeader(
-	'script',
-	[
-		'src' => $linkToJs,
-		'nonce' => \OC::$server->getContentSecurityPolicyNonceManager()->getNonce()
-	], ''
-);
+	$app->getContainer()->registerCapability(\OCA\Theming\Capabilities::class);
 
-$app = new \OCP\AppFramework\App('theming');
-$app->getContainer()->registerCapability(\OCA\Theming\Capabilities::class);
+	$linkToCSS = \OC::$server->getURLGenerator()->linkToRoute(
+		'theming.Theming.getStylesheet',
+		[
+			'v' => \OC::$server->getConfig()->getAppValue('theming', 'cachebuster', '0'),
+		]
+	);
+	\OCP\Util::addHeader(
+		'link',
+		[
+			'rel' => 'stylesheet',
+			'href' => $linkToCSS,
+		]
+	);
+
+	$linkToJs = \OC::$server->getURLGenerator()->linkToRoute(
+		'theming.Theming.getJavascript',
+		[
+			'v' => \OC::$server->getConfig()->getAppValue('theming', 'cachebuster', '0'),
+		]
+	);
+	\OCP\Util::addHeader(
+		'script',
+		[
+			'src' => $linkToJs,
+			'nonce' => \OC::$server->getContentSecurityPolicyNonceManager()->getNonce()
+		], ''
+	);
+
+}
\ No newline at end of file
diff --git a/apps/theming/lib/Util.php b/apps/theming/lib/Util.php
index 286756a4849..1df16ea4976 100644
--- a/apps/theming/lib/Util.php
+++ b/apps/theming/lib/Util.php
@@ -199,4 +199,17 @@ class Util {
 		return $svg;
 	}
 
+	/**
+	 * Check if a custom theme is set in the server configuration
+	 * 
+	 * @return bool
+	 */
+	public function isAlreadyThemed() {
+		$theme = $this->config->getSystemValue('theme', '');
+		if ($theme !== '') {
+			return true;
+		}
+		return false;
+	}
+
 }
diff --git a/apps/theming/tests/UtilTest.php b/apps/theming/tests/UtilTest.php
index de6690ffe0d..d81c253f98a 100644
--- a/apps/theming/tests/UtilTest.php
+++ b/apps/theming/tests/UtilTest.php
@@ -180,4 +180,24 @@ class UtilTest extends TestCase {
 		$this->assertEquals($expected, $result);
 	}
 
+	public function testIsAlreadyThemedFalse() {
+		$theme = $this->config->getSystemValue('theme', '');
+		$this->config->expects($this->once())
+			->method('getSystemValue')
+			->with('theme', '')
+			->willReturn('');
+		$actual = $this->util->isAlreadyThemed();
+		$this->assertFalse($actual);
+	}
+
+	public function testIsAlreadyThemedTrue() {
+		$theme = $this->config->getSystemValue('theme', '');
+		$this->config->expects($this->once())
+			->method('getSystemValue')
+			->with('theme', '')
+			->willReturn('example');
+		$actual = $this->util->isAlreadyThemed();
+		$this->assertTrue($actual);
+	}
+
 }
diff --git a/core/js/mimetype.js b/core/js/mimetype.js
index 8920fe09a7e..ed4fedc7f8a 100644
--- a/core/js/mimetype.js
+++ b/core/js/mimetype.js
@@ -91,7 +91,7 @@ OC.MimeType = {
 				path += icon;
 			}
 		}
-		if(OCA.Theming) {
+		if(OCA.Theming && gotIcon === null) {
 			path = OC.generateUrl('/apps/theming/img/core/filetypes/');
 			path += OC.MimeType._getFile(mimeType, OC.MimeTypeList.files);
 			gotIcon = true;
diff --git a/lib/private/URLGenerator.php b/lib/private/URLGenerator.php
index 073d40b0de8..9c73ba4cbc7 100644
--- a/lib/private/URLGenerator.php
+++ b/lib/private/URLGenerator.php
@@ -166,19 +166,7 @@ class URLGenerator implements IURLGenerator {
 		// Check if the app is in the app folder
 		$path = '';
 		$themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming');
-		if($themingEnabled && $image === 'favicon.ico' && \OC::$server->getThemingDefaults()->shouldReplaceIcons()) {
-			$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
-			if($app === '') { $app = 'core'; }
-			$path = $this->linkToRoute('theming.Icon.getFavicon', [ 'app' => $app ]) . '?v='. $cacheBusterValue;
-		} elseif($themingEnabled && $image === 'favicon-touch.png' && \OC::$server->getThemingDefaults()->shouldReplaceIcons()) {
-			$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
-			if($app === '') { $app = 'core'; }
-			$path = $this->linkToRoute('theming.Icon.getTouchIcon', [ 'app' => $app ]) . '?v='. $cacheBusterValue;
-		} elseif($themingEnabled && $image === 'favicon-fb.png' && \OC::$server->getThemingDefaults()->shouldReplaceIcons()) {
-			$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
-			if($app === '') { $app = 'core'; }
-			$path = $this->linkToRoute('theming.Icon.getTouchIcon', [ 'app' => $app ]) . '?v='. $cacheBusterValue;
-		} elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
+		if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
 			$path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image";
 		} elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg")
 			&& file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.png")) {
@@ -193,6 +181,14 @@ class URLGenerator implements IURLGenerator {
 		} elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
 			&& file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
 			$path =  \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
+		} elseif($themingEnabled && $image === "favicon.ico" && \OC::$server->getThemingDefaults()->shouldReplaceIcons()) {
+			$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
+			if($app==="") { $app = "core"; }
+			$path = $this->linkToRoute('theming.Icon.getFavicon', [ 'app' => $app ]) . '?v='. $cacheBusterValue;
+		} elseif($themingEnabled && $image === "favicon-touch.png" && \OC::$server->getThemingDefaults()->shouldReplaceIcons()) {
+			$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
+			if($app==="") { $app = "core"; }
+			$path = $this->linkToRoute('theming.Icon.getTouchIcon', [ 'app' => $app ]) . '?v='. $cacheBusterValue;
 		} elseif ($appPath && file_exists($appPath . "/img/$image")) {
 			$path =  \OC_App::getAppWebPath($app) . "/img/$image";
 		} elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
-- 
GitLab