�&ǐk�@'bJ�h�ۊL'}T� :��'2�Z#$��n�a��� �>a��`��_3d�Qpt�/�P -��#5�,�M��� �pA:©�q�����NW��ډ�A���� �9nʺج���� �TSM��{J6?7��r�@�\����D��� �׶���s�f�TJj?"��D��`?��̒� b�#�%�C*v�$�{�$����5Ծ�F�s��y�e/8��h-�f�̰&(����Gj�L:U� 2�� ����v�_k����Y��gp,�k�WF�R������_C�R��N@���R�@�ߔ?A�w9���F("iNa-S���Q�o�3tDMLh*�#4k�T/iQ��Y*�G��m����)��8�hBm/�I�,g�ﯖ���Z��}�Cz�q@´��d.����L�ŕ�,��1�Z�܌�: ̪���F+J-'��c�tvJ8��]Q-��b��y �6;*J`r_�d ��'�G ~p��)'�C,�%F��E(��2�k�����lР�z�!�=t ��_�0��f7��� ;�p�|�U �%new(); my $hosts_lock = Cpanel::SafeFile::safeopen( $hosts_fh, '+<', $file ); if ($hosts_lock) { my %SEENIP; my @ETCHOSTS; while ( my $line = readline $hosts_fh ) { chomp $line; if ( $line =~ m/^\s*#/ || $line =~ m/^\s*$/ ) { push @ETCHOSTS, { 'raw' => $line }; } else { my ( $ip, $hosts ) = split( /\s+/, $line, 2 ); my @line_hosts; foreach my $host ( split( /\s+/, $hosts ) ) { next if ( $ip ne $mainip && $host eq $hostname ); # Drop mismatch hostnames for the main IP next if ( $ip eq $mainip && ( $host eq $hostname || $host eq $shorthostname ) ); # These will be re-added push @line_hosts, $host if ( !grep { $_ eq $host } @line_hosts ); # Drop duplicates while preserving order } if ( !@line_hosts ) { ## Drop empty lines next if $ip eq $mainip; # Prevent duplicate commented lines for the main IP push @ETCHOSTS, { 'raw' => '# ' . $line }; # Preserve lines that were commented out in full } else { ## Save valid entries push @ETCHOSTS, { 'ip' => $ip, 'hosts' => \@line_hosts }; $SEENIP{$ip} = 1; } } } seek( $hosts_fh, 0, 0 ); my $added_hostname = 0; foreach my $line_ref (@ETCHOSTS) { if ( exists $line_ref->{'raw'} ) { print {$hosts_fh} $line_ref->{'raw'} . "\n"; } else { my $ip = $line_ref->{'ip'}; my $hosts_ref = $line_ref->{'hosts'}; if ( $ip eq $mainip && !$added_hostname ) { $added_hostname = 1; unshift @{$hosts_ref}, $hostname, $shorthostname; } elsif ( Cpanel::IP::Loopback::is_loopback($ip) ) { if ( !grep { $_ eq 'localhost' } @{$hosts_ref} ) { unshift @{$hosts_ref}, 'localhost'; } } print {$hosts_fh} $ip . "\t\t" . join( ' ', @{$hosts_ref} ) . "\n"; } } if ( !exists $SEENIP{'127.0.0.1'} ) { print {$hosts_fh} "127.0.0.1\t\tlocalhost\n"; } if ( !exists $SEENIP{$mainip} ) { print {$hosts_fh} $mainip . "\t\t" . $hostname . ' ' . $shorthostname . "\n"; } truncate( $hosts_fh, tell($hosts_fh) ); Cpanel::SafeFile::safeclose( $hosts_fh, $hosts_lock ); } return; }