From 7c0a2ab2029b98086f02da80adf6a7ebc303fd6f Mon Sep 17 00:00:00 2001
From: Andrew Dolgov <noreply@fakecake.org>
Date: Fri, 5 Jun 2015 17:54:52 +0300
Subject: [PATCH] pluginhost: allow loading user plugins from plugins.local

---
 .gitignore               |  1 +
 classes/pluginhost.php   | 20 +++++++++++++++++---
 include/functions2.php   |  8 ++++++++
 plugins.local/index.html |  0
 4 files changed, 26 insertions(+), 3 deletions(-)
 create mode 100644 plugins.local/index.html

diff --git a/.gitignore b/.gitignore
index e7e5407d0..09fccffa5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,7 @@
 *.DS_Store
 #*
 .idea/*
+plugins.local/*
 config.php
 feed-icons/*
 cache/*/*
diff --git a/classes/pluginhost.php b/classes/pluginhost.php
index 457698291..13da7e7ab 100644
--- a/classes/pluginhost.php
+++ b/classes/pluginhost.php
@@ -129,7 +129,15 @@ class PluginHost {
 		}
 	}
 	function load_all($kind, $owner_uid = false) {
-		$plugins = array_map("basename", glob("plugins/*"));
+		$plugins = array_map("basename", array_filter(glob("plugins/*"), "is_dir"));
+
+		if (is_dir("plugins.local")) {
+			$plugins = array_merge($plugins, array_map("basename",
+				array_filter(glob("plugins.local/*"), "is_dir")));
+		}
+
+		asort($plugins);
+
 		$this->load(join(",", $plugins), $kind, $owner_uid);
 	}
 
@@ -142,9 +150,15 @@ class PluginHost {
 			$class = trim($class);
 			$class_file = strtolower(basename($class));
 
-			if (!is_dir(dirname(__FILE__)."/../plugins/$class_file")) continue;
+			if (!is_dir(__DIR__."/../plugins/$class_file") &&
+					!is_dir(__DIR__."/../plugins.local/$class_file")) continue;
+
+			// try system plugin directory first
+			$file = __DIR__ . "/../plugins/$class_file/init.php";
 
-			$file = dirname(__FILE__)."/../plugins/$class_file/init.php";
+			if (!file_exists($file)) {
+				$file = __DIR__ . "/../plugins.local/$class_file/init.php";
+			}
 
 			if (!isset($this->plugins[$class])) {
 				if (file_exists($file)) require_once $file;
diff --git a/include/functions2.php b/include/functions2.php
index 27b1933d0..4f8a0523b 100644
--- a/include/functions2.php
+++ b/include/functions2.php
@@ -2471,4 +2471,12 @@
 			array("code" => $code, "message" => $message)));
 
 	}
+
+	function abs_to_rel_path($dir) {
+		$tmp = str_replace(dirname(__DIR__), "", $dir);
+
+		if (strlen($tmp) > 0 && substr($tmp, 0, 1) == "/") $tmp = substr($tmp, 1);
+
+		return $tmp;
+	}
 ?>
diff --git a/plugins.local/index.html b/plugins.local/index.html
new file mode 100644
index 000000000..e69de29bb
-- 
GitLab