1+ <?php
2+
3+ /**
4+ * When installed, this plugin will create a relative symlink from $HOME/website to /web when:
5+ * - domain is created or updated
6+ * - shell user is created or updated
7+ *
8+ * Its purpose is to make it easier for users to navigate to their web folder. If a file named website/ already exists
9+ * it is not overwritten.
10+ *
11+ * Example:
12+ *
13+ * $ ls -al /var/www/domain.com/home/username
14+ * total 12
15+ * drwxr-x--- 3 web1 client1 4096 Nov 4 22:19 .
16+ * drwxr-xr-x 4 root root 4096 Nov 4 22:19 ..
17+ * lrwxrwxrwx 1 root root 9 Nov 4 22:19 website -> ../../web
18+ */
19+ class website_symlink_plugin {
20+
21+ var $ plugin_name = 'website_symlink_plugin ' ;
22+ var $ class_name = 'website_symlink_plugin ' ;
23+
24+ public function onInstall () {
25+ global $ conf ;
26+
27+ // Enable the following code section to activate the plugin automatically at install time
28+ /*
29+ if ($conf['services']['web'] == true) {
30+ return true;
31+ }
32+ */
33+
34+ return false ;
35+ }
36+
37+ public function onLoad () {
38+ global $ app ;
39+
40+ $ app ->plugins ->registerEvent ('web_domain_insert ' , $ this ->plugin_name , 'createSymlinkForWebDomain ' );
41+ $ app ->plugins ->registerEvent ('web_domain_update ' , $ this ->plugin_name , 'createSymlinkForWebDomain ' );
42+
43+ $ app ->plugins ->registerEvent ('shell_user_insert ' , $ this ->plugin_name , 'createSymlinkForShellUser ' );
44+ $ app ->plugins ->registerEvent ('shell_user_update ' , $ this ->plugin_name , 'createSymlinkForShellUser ' );
45+ }
46+
47+ public function createSymlinkForWebDomain ($ event_name , $ data ) {
48+ $ homeDirectories = glob (sprintf ('%s/home ' , $ data ['new ' ]['document_root ' ]) . '/* ' , GLOB_ONLYDIR );
49+
50+ foreach ($ homeDirectories as $ dir ) {
51+ $ target = sprintf ('%s/web ' , $ data ['new ' ]['document_root ' ]);
52+ $ link = sprintf ('%s/website ' , $ dir );
53+
54+ $ this ->createSymlink ($ target , $ link );
55+ }
56+ }
57+
58+ public function createSymlinkForShellUser ($ event_name , $ data ) {
59+ $ target = sprintf ('%s/web ' , $ data ['new ' ]['dir ' ]);
60+ $ link = sprintf ('%s/home/%s/website ' , $ data ['new ' ]['dir ' ], $ data ['new ' ]['username ' ]);
61+
62+ $ this ->createSymlink ($ target , $ link );
63+ }
64+
65+ private function createSymlink ($ target , $ link ) {
66+ global $ app ;
67+
68+ if (file_exists ($ link )) {
69+ $ app ->log (sprintf ('Not creating symlink because "%s" already exists ' , $ link ), LOGLEVEL_DEBUG );
70+
71+ return ;
72+ }
73+
74+ if ($ app ->system ->create_relative_link ($ target , $ link )) {
75+ $ app ->log (sprintf ('Created symlink from "%s" to "%s" ' , $ link , $ target ), LOGLEVEL_DEBUG );
76+ } else {
77+ $ app ->log (sprintf ('Failed to create symlink from "%s" to "%s" ' , $ link , $ target ), LOGLEVEL_WARN );
78+ }
79+ }
80+ }
0 commit comments