From 27ab16b6dc649fbda0ee329b42f1882623f9cba6 Mon Sep 17 00:00:00 2001
From: Andrew Dolgov <noreply@fakecake.org>
Date: Thu, 11 Mar 2021 07:44:58 +0300
Subject: [PATCH] add Config::LOCAL_OVERRIDE_JS

---
 classes/config.php         | 17 ++++++++++++++++-
 classes/handler/public.php |  4 +---
 include/functions.php      |  2 ++
 include/login_form.php     |  4 +---
 index.php                  |  4 +---
 prefs.php                  |  4 +---
 6 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/classes/config.php b/classes/config.php
index a0113bd24..2307665f3 100644
--- a/classes/config.php
+++ b/classes/config.php
@@ -98,7 +98,10 @@ class Config {
 	// available options: sql (default, event log), syslog, stdout (for debugging)
 
 	const LOCAL_OVERRIDE_STYLESHEET = "LOCAL_OVERRIDE_STYLESHEET";
-	// link this stylesheet on all pages
+	// link this stylesheet on all pages (if it exists), should be placed in themes.local
+
+	const LOCAL_OVERRIDE_JS = "LOCAL_OVERRIDE_JS";
+	// same but this javascript file (you can use that for polyfills), should be placed in themes.local
 
 	const DAEMON_MAX_CHILD_RUNTIME = "DAEMON_MAX_CHILD_RUNTIME";
 	// in seconds, terminate update tasks that ran longer than this interval
@@ -194,6 +197,8 @@ class Config {
 		Config::LOG_DESTINATION => [ Logger::LOG_DEST_SQL,			Config::T_STRING ],
 		Config::LOCAL_OVERRIDE_STYLESHEET => [ "local-overrides.css",
 																					Config::T_STRING ],
+		Config::LOCAL_OVERRIDE_JS => [ "local-overrides.js",
+																					Config::T_STRING ],
 		Config::DAEMON_MAX_CHILD_RUNTIME => [ 1800,					Config::T_INT ],
 		Config::DAEMON_MAX_JOBS => [ 2,									Config::T_INT ],
 		Config::FEED_FETCH_TIMEOUT => [ 45,								Config::T_INT ],
@@ -610,4 +615,14 @@ class Config {
 	private static function format_error($msg) {
 		return "<div class=\"alert alert-danger\">$msg</div>";
 	}
+
+	static function get_override_links() {
+		$rv = "";
+
+		$local_css = get_theme_path(self::get(self::LOCAL_OVERRIDE_STYLESHEET));
+		if ($local_css) $rv .= stylesheet_tag($local_css);
+
+		$local_js = get_theme_path(self::get(self::LOCAL_OVERRIDE_JS));
+		if ($local_js) $rv .= javascript_tag($local_js);
+	}
 }
diff --git a/classes/handler/public.php b/classes/handler/public.php
index 2de073cc2..d5319c306 100755
--- a/classes/handler/public.php
+++ b/classes/handler/public.php
@@ -638,9 +638,7 @@ class Handler_Public extends Handler {
 
 			} ?>
 
-			<?php if (theme_exists(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET))) {
-				echo stylesheet_tag(get_theme_path(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET)));
-			} ?>
+			<?= Config::get_override_links() ?>
 
 			<style type="text/css">
 				@media (prefers-color-scheme: dark) {
diff --git a/include/functions.php b/include/functions.php
index 73d963803..e8f41d56a 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -420,6 +420,8 @@
 
 		$check = "themes.local/$theme";
 		if (file_exists($check)) return $check;
+
+		return "";
 	}
 
 	function theme_exists($theme) {
diff --git a/include/login_form.php b/include/login_form.php
index 9efe0e238..2e0eb077f 100755
--- a/include/login_form.php
+++ b/include/login_form.php
@@ -15,9 +15,7 @@
 
 	} ?>
 
-	<?php if (theme_exists(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET))) {
-		echo stylesheet_tag(get_theme_path(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET)));
-	} ?>
+	<?= Config::get_override_links() ?>
 
 	<style type="text/css">
 		@media (prefers-color-scheme: dark) {
diff --git a/index.php b/index.php
index ba6e03376..fd3eebafa 100644
--- a/index.php
+++ b/index.php
@@ -35,9 +35,7 @@
 		}
 	} ?>
 
-	<?php if (theme_exists(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET))) {
-		echo stylesheet_tag(get_theme_path(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET)));
-	} ?>
+	<?= Config::get_override_links() ?>
 
 	<script type="text/javascript">
 		const __csrf_token = "<?= $_SESSION["csrf_token"]; ?>";
diff --git a/prefs.php b/prefs.php
index 3f6eb47c6..982726740 100644
--- a/prefs.php
+++ b/prefs.php
@@ -27,9 +27,7 @@
 		}
 	} ?>
 
-	<?php if (theme_exists(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET))) {
-		echo stylesheet_tag(get_theme_path(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET)));
-	} ?>
+	<?= Config::get_override_links() ?>
 
 	<script type="text/javascript">
 		const __csrf_token = "<?= $_SESSION["csrf_token"]; ?>";
-- 
GitLab