Path: blob/master/src/view/form/control/AphrontFormRecaptchaControl.php
12256 views
<?php12final class AphrontFormRecaptchaControl extends AphrontFormControl {34protected function getCustomControlClass() {5return 'aphront-form-control-recaptcha';6}78protected function shouldRender() {9return self::isRecaptchaEnabled();10}1112public static function isRecaptchaEnabled() {13return PhabricatorEnv::getEnvConfig('recaptcha.enabled');14}1516public static function hasCaptchaResponse(AphrontRequest $request) {17return $request->getBool('g-recaptcha-response');18}1920public static function processCaptcha(AphrontRequest $request) {21if (!self::isRecaptchaEnabled()) {22return true;23}2425$uri = 'https://www.google.com/recaptcha/api/siteverify';26$params = array(27'secret' => PhabricatorEnv::getEnvConfig('recaptcha.private-key'),28'response' => $request->getStr('g-recaptcha-response'),29'remoteip' => $request->getRemoteAddress(),30);3132list($body) = id(new HTTPSFuture($uri, $params))33->setMethod('POST')34->resolvex();3536$json = phutil_json_decode($body);37return (bool)idx($json, 'success');38}3940protected function renderInput() {41$js = 'https://www.google.com/recaptcha/api.js';42$pubkey = PhabricatorEnv::getEnvConfig('recaptcha.public-key');4344CelerityAPI::getStaticResourceResponse()45->addContentSecurityPolicyURI('script-src', $js)46->addContentSecurityPolicyURI('script-src', 'https://www.gstatic.com/')47->addContentSecurityPolicyURI('frame-src', 'https://www.google.com/');4849return array(50phutil_tag(51'div',52array(53'class' => 'g-recaptcha',54'data-sitekey' => $pubkey,55)),56phutil_tag(57'script',58array(59'type' => 'text/javascript',60'src' => $js,61)),62);63}64}656667