I've been speaking lately with folks from Spamhaus about anti spam measure in YOURLS and a YOURLS plugin for this. Currently the #1 result in Google for "spamhaus PHP" is a post on Lockergnome which gets it totally wrong and provides a script that does not work, so here is a PHP script that does work.
This script checks a URL (its domain part, in fact) against the 3 major black lists: Spamhaus, SURBL and URIBL.
The script:
- /**
- * Check a URL against the 3 major blacklists
- *
- * @param string $url The URL to check
- * @return mixed true if blacklisted, false if not blacklisted, 'malformed' if URL looks weird
- */
- function ozh_is_blacklisted( $url ) {
- $parsed = parse_url( $url );
- if( !isset( $parsed['host'] ) )
- return 'malformed';
- // Remove www. from domain (but not from www.com)
- $parsed['host'] = preg_replace( '/^www\.(.+\.)/i', '$1', $parsed['host'] );
- // The 3 major blacklists
- $blacklists = array(
- 'zen.spamhaus.org',
- 'multi.surbl.org',
- 'black.uribl.com',
- );
- // Check against each black list, exit if blacklisted
- foreach( $blacklists as $blacklist ) {
- $domain = $parsed['host'] . '.' . $blacklist . '.';
- $record = dns_get_record( $domain );
- if( count( $record ) > 0 )
- return true;
- }
- // All clear, probably not spam
- return false;
- }
Usage:
- if( ozh_is_blacklisted( $url ) ) {
- // do something brutal (eg die() your script, yell at user, etc...)
- }
- // all is fine *for today*, do your regular stuff.
- // This said, it'd be nice to recheck every couple of days
Feel free to steal.