$line) { preg_match('/"(GET|POST).+" [0-9]+ [0-9]+ "(.+)"/Ui', $line, $matches); if (!preg_match('/^(|-)$/', $matches[2])) { if (!stristr($matches[2], $good_ref_host)) { $ref_url = parse_url($matches[2]); // Normalize the referrers $ref_url['mainhost'] = preg_replace('/^www\./Ui', '', $ref_url['host']); $ref_url['mainhost'] = preg_replace('/\.[a-z]{2,7}$/Ui', '', $ref_url['mainhost']); $access_details[$ref_url['mainhost']] = isset($access_details[$ref_url['mainhost']]) ? ($access_details[$ref_url['mainhost']] + 1) : 1; } } } $threshold = ($num_accesses / 100) * $threshold_percent; $num_valid = 0; // Start looking for spam foreach ($access_details as $address => $number) { if ($number <= $threshold || preg_match('/^(|-)$/', trim($address))) { unset($access_details[$address]); $num_valid++; } } // Read the .htaccess file $htaccess = @file($htaccess_file); $htaccess = is_array($htaccess) ? $htaccess : array(); // First run of this script? If yes, append the necessary code if (!stristr($htaccess[0], '### REFSPAM FUCKER 3000 ### START')) { array_unshift($htaccess, '### REFSPAM FUCKER 3000 ### START ### DO NOT REMOVE OR MOVE THIS BLOCK', 'order deny,allow', 'deny from env=BadReferrer', '### REFSPAM FUCKER 3000 ### END', '### (c) CARLO ZOTTMANN, http://G-Spotting.net/', '', ''); } // Prepare for adding the new spammers: get rid of '### REFSPAM FUCKER 3000 ### START' array_shift($htaccess); // Append new anti-spam rules foreach ($access_details as $key => $val) { $new_rule = 'SetEnvIfNoCase Referer ".*'.str_replace('.', '\.', $key).'.*" BadReferrer'; if (!in_array("$new_rule\n", $htaccess) && !preg_grep("/^# (WATCH|NOBLOCK|IGNORE) ".str_replace('.', '\.', $key)."\s?$/", $htaccess)) { array_unshift($htaccess, $new_rule); } } array_unshift($htaccess, '### REFSPAM FUCKER 3000 ### START'); // Write .htaccess file if ($fp = fopen($htaccess_file, 'w')) { for ($a = 0; $a < count($htaccess); $a++) { fwrite($fp, trim($htaccess[$a])."\n"); } fclose($fp); } // Done! ?>