2011-01-31 20:55:26 +01:00
|
|
|
<?php
|
|
|
|
|
2012-01-12 22:54:28 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @phutil-external-symbol function recaptcha_get_html
|
|
|
|
* @phutil-external-symbol function recaptcha_check_answer
|
|
|
|
*/
|
2012-03-14 00:21:04 +01:00
|
|
|
final class AphrontFormRecaptchaControl extends AphrontFormControl {
|
2011-01-31 20:55:26 +01:00
|
|
|
|
|
|
|
protected function getCustomControlClass() {
|
|
|
|
return 'aphront-form-control-recaptcha';
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function shouldRender() {
|
|
|
|
return self::isRecaptchaEnabled();
|
|
|
|
}
|
|
|
|
|
2012-01-12 21:56:11 +01:00
|
|
|
public static function isRecaptchaEnabled() {
|
2011-01-31 20:55:26 +01:00
|
|
|
return PhabricatorEnv::getEnvConfig('recaptcha.enabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function requireLib() {
|
|
|
|
$root = phutil_get_library_root('phabricator');
|
|
|
|
require_once dirname($root).'/externals/recaptcha/recaptchalib.php';
|
|
|
|
}
|
|
|
|
|
2012-01-12 21:56:11 +01:00
|
|
|
public static function hasCaptchaResponse(AphrontRequest $request) {
|
|
|
|
return $request->getBool('recaptcha_response_field');
|
|
|
|
}
|
|
|
|
|
2011-01-31 20:55:26 +01:00
|
|
|
public static function processCaptcha(AphrontRequest $request) {
|
|
|
|
if (!self::isRecaptchaEnabled()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::requireLib();
|
|
|
|
|
|
|
|
$challenge = $request->getStr('recaptcha_challenge_field');
|
|
|
|
$response = $request->getStr('recaptcha_response_field');
|
|
|
|
$resp = recaptcha_check_answer(
|
|
|
|
PhabricatorEnv::getEnvConfig('recaptcha.private-key'),
|
|
|
|
$_SERVER['REMOTE_ADDR'],
|
|
|
|
$challenge,
|
|
|
|
$response);
|
|
|
|
|
|
|
|
return (bool)@$resp->is_valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderInput() {
|
|
|
|
self::requireLib();
|
|
|
|
|
2012-08-27 23:22:04 +02:00
|
|
|
$uri = new PhutilURI(PhabricatorEnv::getEnvConfig('phabricator.base-uri'));
|
|
|
|
$protocol = $uri->getProtocol();
|
|
|
|
$use_ssl = ($protocol == 'https');
|
|
|
|
|
2011-01-31 20:55:26 +01:00
|
|
|
return recaptcha_get_html(
|
|
|
|
PhabricatorEnv::getEnvConfig('recaptcha.public-key'),
|
|
|
|
$error = null,
|
2012-08-27 23:22:04 +02:00
|
|
|
$use_ssl);
|
2011-01-31 20:55:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|