#! /usr/bin/perl
use File::Copy;

# Please define the location of your locate command.
# The location below id the default on RedHat 9 and RHES3
$LOCATE = '/usr/bin/locate';

# Try to get all the locations of the viewtopic.php file
@ViewtopicLocations = `$LOCATE viewtopic.php`;

foreach $Location (@ViewtopicLocations) {
	$Location =~ s/\s+$//g;
	$Location =~ s/^\s+//g;
	# Open the file
	open(VTFH, "< $Location");
	# Read the file
	@FileData = <VTFH>;
	# Close the file
	close(VTFH);
	# Try to find the BUG.
	$FoundBug = 0;
	foreach $Line (@FileData) {
		if ($Line =~ /\$words = explode\(' ', trim\(htmlspecialchars\(urldecode\(\$HTTP_GET_VARS\['highlight'\]\)\)\)\)\;/) {
			$FoundBug = 1;
		}
	}
	if ($FoundBug) {
		# Get the file data
		@FileInfo = stat($Location);
		# Make a backup copy of the file.
		copy($Location, $Location.".bak");
		# Open the file
		open(VTFH, "> $Location");
	        foreach $Line (@FileData) {
	                if ($Line =~ /\$words = explode\(' ', trim\(htmlspecialchars\(urldecode\(\$HTTP_GET_VARS\['highlight'\]\)\)\)\)\;/) {
				print(VTFH "\t\$words = explode\(' ', trim\(htmlspecialchars\(\$HTTP_GET_VARS\['highlight'\]\)\)\)\;\n");
                	} else {
				# check if it is a DOS file
				print(VTFH $Line);
			}
        	}
	        # Close the file
	        close(VTFH);
		# Change the file mode
		chmod($FileInfo[2], $Location);
		# Change the file ownwer
		chown($FileInfo[4], $FileInfo[5], $Location);
		print($Location." [ PATCHED !! ]\n");
	} else {
		print($Location." [ OK !! ]\n");
	}
}
print(" [ DONE !! ]\n");
