File: //scripts.20110531.215904.25158/regsrep.pl
#!/usr/bin/perl
# cpanel - regsrep.pl Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use Cpanel::SafeFile;
use Cpanel::Logger ();
my $logger = Cpanel::Logger->new();
sub regsrep {
my($file,$old,$new,$useregex,$search,$replace) = @_;
my(@CFILE);
my $filelock = Cpanel::SafeFile::safeopen(\*FH,"+<","$file");
if( !$filelock ) {
$logger->warn("Could not edit $file");
return;
}
while(<FH>) {
if ($_ =~ /^$/ || $_ eq "\n" || $_ eq "\r\n") {
push(@CFILE,$_);
} elsif($_ =~ /$old/) {
my $result = $1;
my $mnew = $new;
$mnew =~ s/\$1/${result}/g;
next if ($new eq "-1");
if ($search ne "") {
$mnew =~ s/${search}/${replace}/g;
}
if ($useregex) {
$mnew = $_;
$mnew =~ s/${old}/${new}/g;
$mnew =~ s/\n//g;
}
push(@CFILE,"${mnew}\n");
} else {
push(@CFILE,$_);
}
}
seek(FH,0,0);
print FH join("",@CFILE);
truncate(FH,tell(FH));
Cpanel::SafeFile::safeclose(\*FH,$filelock);
}
1;