{"id":1821,"date":"2012-11-21T22:24:56","date_gmt":"2012-11-21T20:24:56","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=1821"},"modified":"2014-03-04T18:19:22","modified_gmt":"2014-03-04T17:19:22","slug":"checking-domain-blacklists-from-spamhaus-surbl-and-uribl-in-php","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2012\/11\/checking-domain-blacklists-from-spamhaus-surbl-and-uribl-in-php\/","title":{"rendered":"Checking Domain Blacklists from Spamhaus, SURBL and URIBL in PHP"},"content":{"rendered":"<p>I&#39;ve been speaking lately with folks from <a href=\"http:\/\/www.spamhaus.org\/\">Spamhaus<\/a> about anti spam measure in <a href=\"http:\/\/yourls.org\/\">YOURLS<\/a> and a YOURLS plugin for this. Currently the #1 result in Google for &quot;<tt>spamhaus PHP<\/tt>&quot; 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 <strong>does work<\/strong>.<\/p>\n<p>This script checks a URL (its domain part, in fact) against the 3 major black lists: <a href=\"http:\/\/www.spamhaus.org\/\">Spamhaus<\/a>, <a href=\"http:\/\/www.surbl.org\/\">SURBL<\/a> and <a href=\"http:\/\/uribl.com\/\">URIBL<\/a>.<\/p>\n<p><strong>The script:<\/strong><\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">\/**\r\n * Check a URL against the 3 major blacklists\r\n *\r\n * @param string $url The URL to check\r\n * @return mixed true if blacklisted, false if not blacklisted, &#039;malformed&#039; if URL looks weird\r\n *\/\r\nfunction ozh_is_blacklisted( $url ) {\r\n\r\n\t$parsed = parse_url( $url );\r\n\r\n\tif( !isset( $parsed[&#039;host&#039;] ) )\r\n\t\treturn &#039;malformed&#039;;\r\n\t\t\r\n\t\/\/ Remove www. from domain (but not from www.com)\r\n\t$parsed[&#039;host&#039;] = preg_replace( &#039;\/^www\\.(.+\\.)\/i&#039;, &#039;$1&#039;, $parsed[&#039;host&#039;] );\r\n\r\n\t\/\/ The 3 major blacklists\r\n\t$blacklists = array(\r\n\t\t&#039;zen.spamhaus.org&#039;,\r\n\t\t&#039;multi.surbl.org&#039;,\r\n\t\t&#039;black.uribl.com&#039;,\r\n\t);\r\n\t\r\n\t\/\/ Check against each black list, exit if blacklisted\r\n\tforeach( $blacklists as $blacklist ) {\r\n\t\t$domain = $parsed[&#039;host&#039;] . &#039;.&#039; . $blacklist . &#039;.&#039;;\r\n\t\t$record = dns_get_record( $domain );\r\n\t\t\r\n\t\tif( count( $record ) &gt; 0 )\r\n\t\t\treturn true;\r\n\t}\r\n\t\r\n\t\/\/ All clear, probably not spam\r\n\treturn false;\r\n}<\/code><\/pre><\/div>\n<p>Usage:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">if( ozh_is_blacklisted( $url ) ) {\r\n\t\/\/ do something brutal (eg die() your script, yell at user, etc...)\r\n}\r\n\r\n\/\/ all is fine *for today*, do your regular stuff.\r\n\/\/ This said, it&#039;d be nice to recheck every couple of days<\/code><\/pre><\/div>\n<p>Feel free to steal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;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 &quot;spamhaus PHP&quot; 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\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[398,2,10,27],"class_list":["post-1821","post","type-post","status-publish","format-standard","hentry","category-published","tag-blacklist","tag-code","tag-php","tag-spam"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=1821"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1821\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}