View source
<?php
function spambot_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_register' || $form_id == 'user_edit') {
array_push($form['#validate'], 'spambot_username_validate');
}
}
function spambot_username_validate($element) {
$mail = urlencode($element['#post']['mail']);
$name = urlencode($element['#post']['name']);
if ($mail && _spambot_request('http://www.stopforumspam.com/api?email=' . $mail)) {
$block = true;
watchdog("spambot", t("blocked spambot: @mail", array(
"@mail" => $mail,
)));
}
else {
if ($name && _spambot_request('http://www.stopforumspam.com/api?username=' . $name)) {
$block = true;
watchdog("spambot", t("blocked spambot: @name", array(
"@name" => $name,
)));
}
}
if ($block) {
form_set_error('name', t("Blacklisted. Now go away!"));
form_set_error('mail', t("Blacklisted. Now go away!"));
}
}
function _spambot_request($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if ($output === FALSE) {
$error = curl_error($ch);
curl_close($ch);
watchdog("spambot", t("curl returned error: @error, request: @request", array(
"@error" => $error,
"@request" => $url,
)));
return false;
}
curl_close($ch);
$xml = new SimpleXMLElement($output);
if ($xml->appears == 'yes') {
return true;
}
return false;
}