From 7fe2864dbf921b6aea191e6af90c3924c6899390 Mon Sep 17 00:00:00 2001
From: Till Brehm <tbrehm@ispconfig.org>
Date: Mon, 5 Nov 2018 14:57:26 +0000
Subject: [PATCH] Merge branch 'master' into 'master'

Add website_symlink_plugin.inc.php

See merge request ispconfig/ispconfig3!838

(cherry picked from commit ea9f8ef673ac835db11af4e4633849f3396bef93)

2cd49f48 Add website_symlink_plugin.inc.php
f9626973 Update website_symlink_plugin.inc.php
---
 .../website_symlink_plugin.inc.php            | 80 +++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100644 server/plugins-available/website_symlink_plugin.inc.php

diff --git a/server/plugins-available/website_symlink_plugin.inc.php b/server/plugins-available/website_symlink_plugin.inc.php
new file mode 100644
index 0000000000..a17732c5e4
--- /dev/null
+++ b/server/plugins-available/website_symlink_plugin.inc.php
@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * When installed, this plugin will create a relative symlink from $HOME/website to /web when:
+ * - domain is created or updated
+ * - shell user is created or updated
+ *
+ * Its purpose is to make it easier for users to navigate to their web folder. If a file named website/ already exists
+ * it is not overwritten.
+ *
+ * Example:
+ *
+ * $ ls -al /var/www/domain.com/home/username
+ * total 12
+ * drwxr-x--- 3 web1    client1    4096 Nov  4 22:19 .
+ * drwxr-xr-x 4 root    root       4096 Nov  4 22:19 ..
+ * lrwxrwxrwx 1 root    root          9 Nov  4 22:19 website -> ../../web
+ */
+class website_symlink_plugin {
+
+	var $plugin_name = 'website_symlink_plugin';
+	var $class_name = 'website_symlink_plugin';
+
+	public function onInstall() {
+		global $conf;
+
+		// Enable the following code section to activate the plugin automatically at install time
+		/*
+		if ($conf['services']['web'] == true) {
+			return true;
+		}
+		*/
+
+		return false;
+	}
+
+	public function onLoad() {
+		global $app;
+
+		$app->plugins->registerEvent('web_domain_insert', $this->plugin_name, 'createSymlinkForWebDomain');
+		$app->plugins->registerEvent('web_domain_update', $this->plugin_name, 'createSymlinkForWebDomain');
+
+		$app->plugins->registerEvent('shell_user_insert', $this->plugin_name, 'createSymlinkForShellUser');
+		$app->plugins->registerEvent('shell_user_update', $this->plugin_name, 'createSymlinkForShellUser');
+	}
+
+	public function createSymlinkForWebDomain($event_name, $data) {
+		$homeDirectories = glob(sprintf('%s/home', $data['new']['document_root']) . '/*', GLOB_ONLYDIR);
+
+		foreach ($homeDirectories as $dir) {
+			$target = sprintf('%s/web', $data['new']['document_root']);
+			$link = sprintf('%s/website', $dir);
+
+			$this->createSymlink($target, $link);
+		}
+	}
+
+	public function createSymlinkForShellUser($event_name, $data) {
+		$target = sprintf('%s/web', $data['new']['dir']);
+		$link = sprintf('%s/home/%s/website', $data['new']['dir'], $data['new']['username']);
+
+		$this->createSymlink($target, $link);
+	}
+
+	private function createSymlink($target, $link) {
+		global $app;
+
+		if (file_exists($link)) {
+			$app->log(sprintf('Not creating symlink because "%s" already exists', $link), LOGLEVEL_DEBUG);
+
+			return;
+		}
+
+		if ($app->system->create_relative_link($target, $link)) {
+			$app->log(sprintf('Created symlink from "%s" to "%s"', $link, $target), LOGLEVEL_DEBUG);
+		} else {
+			$app->log(sprintf('Failed to create symlink from "%s" to "%s"', $link, $target), LOGLEVEL_WARN);
+		}
+	}
+}
\ No newline at end of file
-- 
GitLab