Subject: | Doesn't support new API |
Google has introduced a new API, which supports the new, easier to solve captchas. It needs to be supported.
See https://developers.google.com/recaptcha/ for the documentation.
It's actually easier to use. I updated the squirrelmail php plugin in a few minutes. For reference, here's the PHP code (The globals are configurable options; the script ideally goes in <head>, but can be placed *anywhere*):
Display:
$output .= "<script src=\"https://www.google.com/recaptcha/api.js";
if ($recaptcha_fallback) // force old-style captcha
{
$output .= "?fallback=true";
if (!empty($recaptcha_language)) // force language (default is autodetect)
{
$output .= "&hl=" . $recaptcha_language;
}
} elseif( !empty($recaptcha_language) ) {
$output .= "?hl=" . $recaptcha_language;
}
$output .= "\" async defer></script>";
$output .= "<div class = \"g-recaptcha\" data-SiteKey = \"" . $recaptcha_public_key . "\"";
if (!empty($recaptcha_theme))
{
$output .= " data-theme=\"" . $recaptcha_theme . "\"";
}
if (!empty($recaptcha_type))
{
$output .= " data-type=\"" . $recaptcha_type . "\"";
}
$output .= "> </div>\n";
if ($recaptcha_noscript) // Not recommended if site requires JS
{
$output .= "<noscript>
<div style=\"width: 302px; height: 352px;\">
<div style=\"width: 302px; height: 352px; position: relative;\">
<div style=\"width: 302px; height: 352px; position: absolute;\">
<iframe src=\"https://www.google.com/recaptcha/api/fallback?k=" . urlencode($recaptcha_public_key) . "\"
frameborder=\"0\" scrolling=\"no\"
style=\"width: 302px; height:352px; border-style: none;\">
</iframe>
</div>
<div style=\"width: 250px; height: 80px; position: absolute; border-style: none;
bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;\">
<textarea id=\"g-recaptcha-response\" name=\"g-recaptcha-response\"
class=\"g-recaptcha-response\"
style=\"width: 250px; height: 80px; border: 1px solid #c1c1c1;
margin: 0px; padding: 0px; resize: none;\" value=\"\">
</textarea>
</div>
</div>
</div>
</noscript>\n";
}
Validate:
if (!sqGetGlobalVar('g-recaptcha-response', $recaptcha_response_field, SQ_FORM)
|| !sqGetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER))
return FALSE;
// test for no user input
//
if ($recaptcha_response_field === '')
return NULL;
global $recaptcha_private_key;
$recapcha_verify_host = 'www.google.com';
// set up GET payload
//
$payload = '?secret=' . urlencode($recaptcha_private_key)
. '&remoteip=' . urlencode($remote_addr)
. '&response=' . urlencode($recaptcha_response_field);
ini_set( "user_agent", "SquirrelMail CAPTCHA Plugin" );
// ask reCAPTCHA server if response was correct
//
$srv = fopen( "https://" . $recapcha_verify_host .
"/recaptcha/api/siteverify" .
$payload, "r" );
if( !$srv ) {
return FALSE;
}
// get response
//
$verify_response = '';
while (!feof($srv)) $verify_response .= fgets($srv, 4096);
fclose($srv);
$rsp = json_decode( $verify_response, FALSE );
if (!$rsp )
{
return FALSE;
}
if( $rsp->{'success'} == 'true' ) {
return TRUE;
}
// $rsp->{'error-codes'} : [ ... ]
/* Error code Description
missing-input-secret The secret parameter is missing.
invalid-input-secret The secret parameter is invalid or malformed.
missing-input-response The response parameter is missing.
invalid-input-response The response parameter is invalid or malformed.
*/
return FALSE;