Skip to content

Commit b5d9479

Browse files
authored
function remove_etc_hosts_entry
1 parent ddf34cd commit b5d9479

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/helper/site-utils.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ function sysctl_parameters() {
759759
* @param string $site_url site name.
760760
*
761761
*/
762-
function remove_etc_hosts_entry( $site_url ) {
762+
/** function remove_etc_hosts_entry( $site_url ) {
763763
$fs = new Filesystem();
764764
765765
$hosts_file = file_get_contents( '/etc/hosts' );
@@ -768,6 +768,28 @@ function remove_etc_hosts_entry( $site_url ) {
768768
$hosts_file_new = preg_replace( "/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file );
769769
770770
$fs->dumpFile( '/etc/hosts', $hosts_file_new );
771+
} */
772+
/** Custom function remove_etc_hosts_entry
773+
*/
774+
function remove_etc_hosts_entry($site_url) {
775+
$hosts_file = @file_get_contents('/etc/hosts');
776+
if ($hosts_file === false) {
777+
throw new Exception("Failed to read /etc/hosts");
778+
}
779+
780+
$site_url_escaped = preg_quote($site_url, '/');
781+
$hosts_file_new = preg_replace("/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file);
782+
783+
$context = stream_context_create([
784+
'ssl' => ['verify_peer' => false],
785+
'ftp' => ['overgrade' => true],
786+
'file' => ['privileged' => true]
787+
]);
788+
789+
$result = file_put_contents('/etc/hosts', $hosts_file_new, LOCK_EX, $context);
790+
if ($result === false) {
791+
throw new Exception("Failed to update /etc/hosts");
792+
}
771793
}
772794

773795
/**

0 commit comments

Comments
 (0)