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+ if ($ conf ['services ' ]['web ' ] == true ) {
28+ return true ;
29+ }
30+
31+ return false ;
32+ }
33+
34+ public function onLoad () {
35+ global $ app ;
36+
37+ $ app ->plugins ->registerEvent ('web_domain_insert ' , $ this ->plugin_name , 'createSymlinkForWebDomain ' );
38+ $ app ->plugins ->registerEvent ('web_domain_update ' , $ this ->plugin_name , 'createSymlinkForWebDomain ' );
39+
40+ $ app ->plugins ->registerEvent ('shell_user_insert ' , $ this ->plugin_name , 'createSymlinkForShellUser ' );
41+ $ app ->plugins ->registerEvent ('shell_user_update ' , $ this ->plugin_name , 'createSymlinkForShellUser ' );
42+ }
43+
44+ public function createSymlinkForWebDomain ($ event_name , $ data ) {
45+ $ homeDirectories = glob (sprintf ('%s/home ' , $ data ['new ' ]['document_root ' ]) . '/* ' , GLOB_ONLYDIR );
46+
47+ foreach ($ homeDirectories as $ dir ) {
48+ $ target = sprintf ('%s/web ' , $ data ['new ' ]['document_root ' ]);
49+ $ link = sprintf ('%s/website ' , $ dir );
50+
51+ $ this ->createSymlink ($ target , $ link );
52+ }
53+ }
54+
55+ public function createSymlinkForShellUser ($ event_name , $ data ) {
56+ $ target = sprintf ('%s/web ' , $ data ['new ' ]['dir ' ]);
57+ $ link = sprintf ('%s/home/%s/website ' , $ data ['new ' ]['dir ' ], $ data ['new ' ]['username ' ]);
58+
59+ $ this ->createSymlink ($ target , $ link );
60+ }
61+
62+ private function createSymlink ($ target , $ link ) {
63+ global $ app ;
64+
65+ if (file_exists ($ link )) {
66+ $ app ->log (sprintf ('Not creating symlink because "%s" already exists ' , $ link ), LOGLEVEL_DEBUG );
67+
68+ return ;
69+ }
70+
71+ if ($ app ->system ->create_relative_link ($ target , $ link )) {
72+ $ app ->log (sprintf ('Created symlink from "%s" to "%s" ' , $ link , $ target ), LOGLEVEL_DEBUG );
73+ } else {
74+ $ app ->log (sprintf ('Failed to create symlink from "%s" to "%s" ' , $ link , $ target ), LOGLEVEL_WARN );
75+ }
76+ }
77+ }
0 commit comments