@@ -1744,12 +1744,26 @@ function web_folder_user($event_name,$data) {
17441744
17451745 //* Create the .htaccess file
17461746 //if(!is_file($folder_path.'.htaccess')) {
1747- $ ht_file = "AuthType Basic \nAuthName \"Members Only \"\nAuthUserFile " .$ folder_path .".htpasswd \nrequire valid-user " ;
1748- $ app ->system ->file_put_contents ($ folder_path .'.htaccess ' ,$ ht_file );
1747+ $ begin_marker = '### ISPConfig folder protection begin ### ' ;
1748+ $ end_marker = '### ISPConfig folder protection end ### ' ;
1749+ $ ht_file = $ begin_marker ."\nAuthType Basic \nAuthName \"Members Only \"\nAuthUserFile " .$ folder_path .".htpasswd \nrequire valid-user \n" .$ end_marker ."\n\n" ;
1750+
1751+ if (file_exists ($ folder_path .'.htaccess ' )) {
1752+ $ old_content = $ app ->system ->file_get_contents ($ folder_path .'.htaccess ' );
1753+
1754+ if (preg_match ('/ ' . preg_quote ($ begin_marker , '/ ' ) . '(.*?) ' . preg_quote ($ end_marker , '/ ' ) . '/s ' , $ old_content , $ matches )) {
1755+ $ ht_file = str_replace ($ matches [0 ], $ ht_file , $ old_content );
1756+ } else {
1757+ $ ht_file .= $ old_content ;
1758+ }
1759+ }
1760+ unset($ old_content );
1761+
1762+ $ app ->system ->file_put_contents ($ folder_path .'.htaccess ' ,$ ht_file );
17491763 $ app ->system ->chmod ($ folder_path .'.htaccess ' ,0755 );
17501764 $ app ->system ->chown ($ folder_path .'.htaccess ' ,$ website ['system_user ' ]);
17511765 $ app ->system ->chgrp ($ folder_path .'.htaccess ' ,$ website ['system_group ' ]);
1752- $ app ->log ('Created file ' .$ folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1766+ $ app ->log ('Created/modified file ' .$ folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
17531767 //}
17541768
17551769 }
@@ -1791,8 +1805,22 @@ function web_folder_delete($event_name,$data) {
17911805
17921806 //* Remove .htaccess file
17931807 if (is_file ($ folder_path .'.htaccess ' )) {
1794- $ app ->system ->unlink ($ folder_path .'.htaccess ' );
1795- $ app ->log ('Removed file ' .$ folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1808+ $ begin_marker = '### ISPConfig folder protection begin ### ' ;
1809+ $ end_marker = '### ISPConfig folder protection end ### ' ;
1810+
1811+ $ ht_file = $ app ->system ->file_get_contents ($ folder_path .'.htaccess ' );
1812+
1813+ if (preg_match ('/ ' . preg_quote ($ begin_marker , '/ ' ) . '(.*?) ' . preg_quote ($ end_marker , '/ ' ) . '/s ' , $ ht_file , $ matches )) {
1814+ $ ht_file = str_replace ($ matches [0 ], '' , $ ht_file );
1815+ }
1816+
1817+ if (trim ($ ht_file ) == '' ) {
1818+ $ app ->system ->unlink ($ folder_path .'.htaccess ' );
1819+ $ app ->log ('Removed file ' .$ folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1820+ } else {
1821+ $ app ->system ->file_put_contents ($ folder_path .'.htaccess ' , $ ht_file );
1822+ $ app ->log ('Removed protection content from file ' .$ folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1823+ }
17961824 }
17971825 }
17981826
@@ -1844,6 +1872,9 @@ function web_folder_update($event_name,$data) {
18441872 //* Create the folder path, if it does not exist
18451873 if (!is_dir ($ new_folder_path )) $ app ->system ->mkdirpath ($ new_folder_path );
18461874
1875+ $ begin_marker = '### ISPConfig folder protection begin ### ' ;
1876+ $ end_marker = '### ISPConfig folder protection end ### ' ;
1877+
18471878 if ($ data ['old ' ]['path ' ] != $ data ['new ' ]['path ' ]) {
18481879
18491880
@@ -1855,26 +1886,59 @@ function web_folder_update($event_name,$data) {
18551886
18561887 //* delete old .htaccess file
18571888 if (is_file ($ old_folder_path .'.htaccess ' )) {
1858- $ app ->system ->unlink ($ old_folder_path .'.htaccess ' );
1859- $ app ->log ('Deleted file ' .$ old_folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1889+ $ ht_file = $ app ->system ->file_get_contents ($ old_folder_path .'.htaccess ' );
1890+
1891+ if (preg_match ('/ ' . preg_quote ($ begin_marker , '/ ' ) . '(.*?) ' . preg_quote ($ end_marker , '/ ' ) . '/s ' , $ ht_file , $ matches )) {
1892+ $ ht_file = str_replace ($ matches [0 ], '' , $ ht_file );
1893+ }
1894+
1895+ if (trim ($ ht_file ) == '' ) {
1896+ $ app ->system ->unlink ($ old_folder_path .'.htaccess ' );
1897+ $ app ->log ('Removed file ' .$ old_folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1898+ } else {
1899+ $ app ->system ->file_put_contents ($ old_folder_path .'.htaccess ' , $ ht_file );
1900+ $ app ->log ('Removed protection content from file ' .$ old_folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1901+ }
18601902 }
18611903
18621904 }
18631905
18641906 //* Create the .htaccess file
18651907 if ($ data ['new ' ]['active ' ] == 'y ' ) {
1866- $ ht_file = "AuthType Basic \nAuthName \"Members Only \"\nAuthUserFile " .$ new_folder_path .".htpasswd \nrequire valid-user " ;
1867- $ app ->system ->file_put_contents ($ new_folder_path .'.htaccess ' ,$ ht_file );
1868- $ app ->system ->chmod ($ new_folder_path .'.htpasswd ' ,0755 );
1869- $ app ->system ->chown ($ folder_path .'.htpasswd ' ,$ website ['system_user ' ]);
1870- $ app ->system ->chgrp ($ folder_path .'.htpasswd ' ,$ website ['system_group ' ]);
1871- $ app ->log ('Created file ' .$ new_folder_path .'.htpasswd ' ,LOGLEVEL_DEBUG );
1908+ $ ht_file = $ begin_marker ."\nAuthType Basic \nAuthName \"Members Only \"\nAuthUserFile " .$ new_folder_path .".htpasswd \nrequire valid-user \n" .$ end_marker ."\n\n" ;
1909+
1910+ if (file_exists ($ new_folder_path .'.htaccess ' )) {
1911+ $ old_content = $ app ->system ->file_get_contents ($ new_folder_path .'.htaccess ' );
1912+
1913+ if (preg_match ('/ ' . preg_quote ($ begin_marker , '/ ' ) . '(.*?) ' . preg_quote ($ end_marker , '/ ' ) . '/s ' , $ old_content , $ matches )) {
1914+ $ ht_file = str_replace ($ matches [0 ], $ ht_file , $ old_content );
1915+ } else {
1916+ $ ht_file .= $ old_content ;
1917+ }
1918+ }
1919+
1920+ $ app ->system ->file_put_contents ($ new_folder_path .'.htaccess ' ,$ ht_file );
1921+ $ app ->system ->chmod ($ new_folder_path .'.htaccess ' ,0755 );
1922+ $ app ->system ->chown ($ new_folder_path .'.htaccess ' ,$ website ['system_user ' ]);
1923+ $ app ->system ->chgrp ($ new_folder_path .'.htaccess ' ,$ website ['system_group ' ]);
1924+ $ app ->log ('Created/modified file ' .$ new_folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
18721925 }
18731926
18741927 //* Remove .htaccess file
18751928 if ($ data ['new ' ]['active ' ] == 'n ' && is_file ($ new_folder_path .'.htaccess ' )) {
1876- $ app ->system ->unlink ($ new_folder_path .'.htaccess ' );
1877- $ app ->log ('Removed file ' .$ new_folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1929+ $ ht_file = $ app ->system ->file_get_contents ($ new_folder_path .'.htaccess ' );
1930+
1931+ if (preg_match ('/ ' . preg_quote ($ begin_marker , '/ ' ) . '(.*?) ' . preg_quote ($ end_marker , '/ ' ) . '/s ' , $ ht_file , $ matches )) {
1932+ $ ht_file = str_replace ($ matches [0 ], '' , $ ht_file );
1933+ }
1934+
1935+ if (trim ($ ht_file ) == '' ) {
1936+ $ app ->system ->unlink ($ new_folder_path .'.htaccess ' );
1937+ $ app ->log ('Removed file ' .$ new_folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1938+ } else {
1939+ $ app ->system ->file_put_contents ($ new_folder_path .'.htaccess ' , $ ht_file );
1940+ $ app ->log ('Removed protection content from file ' .$ new_folder_path .'.htaccess ' ,LOGLEVEL_DEBUG );
1941+ }
18781942 }
18791943
18801944
0 commit comments