From b9634eb8b8cab7329dca8d8173ce381a8c1f81f4 Mon Sep 17 00:00:00 2001
From: Andrew Dolgov <noreply@fakecake.org>
Date: Fri, 5 Jun 2015 18:08:19 +0300
Subject: [PATCH] support loading themes from themes.local

---
 .gitignore             |  2 +-
 classes/pref/prefs.php |  6 ++++--
 include/functions2.php | 16 +++++++++++++---
 index.php              |  2 +-
 prefs.php              |  2 +-
 themes.local/.empty    |  0
 6 files changed, 20 insertions(+), 8 deletions(-)
 create mode 100644 themes.local/.empty

diff --git a/.gitignore b/.gitignore
index 09fccffa5..97ba8f64f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,12 +4,12 @@
 #*
 .idea/*
 plugins.local/*
+themes.local/*
 config.php
 feed-icons/*
 cache/*/*
 lock/*
 tags
-plugins/fever
 cache/htmlpurifier/*/*ser
 lib/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer/*/*ser
 web.config
diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php
index 096260bfb..bc7d9212f 100644
--- a/classes/pref/prefs.php
+++ b/classes/pref/prefs.php
@@ -570,8 +570,10 @@ class Pref_Prefs extends Handler_Protected {
 
 			} else if ($pref_name == "USER_CSS_THEME") {
 
-				$themes = array_filter(array_map("basename", glob("themes/*.css")),
-					"theme_valid");
+				$themes = array_merge(glob("themes/*.css"), glob("themes.local/*.css"));
+				$themes = array_map("basename", $themes);
+				$themes = array_filter($themes, "theme_valid");
+				asort($themes);
 
 				print_select($pref_name, $value, $themes,
 					'dojoType="dijit.form.Select"');
diff --git a/include/functions2.php b/include/functions2.php
index 4f8a0523b..314c12873 100644
--- a/include/functions2.php
+++ b/include/functions2.php
@@ -2444,9 +2444,19 @@
 		return LABEL_BASE_INDEX - 1 + abs($feed);
 	}
 
-	function theme_valid($file) {
-		if ($file == "default.css" || $file == "night.css") return true; // needed for array_filter
-		$file = "themes/" . basename($file);
+	function get_theme_path($theme) {
+		$check = "themes/$theme";
+		if (file_exists($check)) return $check;
+
+		$check = "themes.local/$theme";
+		if (file_exists($check)) return $check;
+	}
+
+	function theme_valid($theme) {
+		if ($theme == "default.css" || $theme == "night.css") return true; // needed for array_filter
+		$file = "themes/" . basename($theme);
+
+		if (!file_exists($file)) $file = "themes.local/" . basename($theme);
 
 		if (file_exists($file) && is_readable($file)) {
 			$fh = fopen($file, "r");
diff --git a/index.php b/index.php
index 360a6dc19..f7ff97c68 100644
--- a/index.php
+++ b/index.php
@@ -66,7 +66,7 @@
 	<?php if ($_SESSION["uid"]) {
 		$theme = get_pref( "USER_CSS_THEME", $_SESSION["uid"], false);
 		if ($theme && theme_valid("$theme")) {
-			echo stylesheet_tag("themes/$theme");
+			echo stylesheet_tag(get_theme_path($theme));
 		} else {
 			echo stylesheet_tag("themes/default.css");
 		}
diff --git a/prefs.php b/prefs.php
index 191f452c4..b20678abe 100644
--- a/prefs.php
+++ b/prefs.php
@@ -42,7 +42,7 @@
 	<?php if ($_SESSION["uid"]) {
 		$theme = get_pref( "USER_CSS_THEME", $_SESSION["uid"], false);
 		if ($theme && theme_valid("$theme")) {
-			echo stylesheet_tag("themes/$theme");
+			echo stylesheet_tag(get_theme_path($theme));
 		} else {
 			echo stylesheet_tag("themes/default.css");
 		}
diff --git a/themes.local/.empty b/themes.local/.empty
new file mode 100644
index 000000000..e69de29bb
-- 
GitLab