@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 65 lines 1.8 kB view raw
1<?php 2 3final class AphrontFormRecaptchaControl extends AphrontFormControl { 4 5 protected function getCustomControlClass() { 6 return 'aphront-form-control-recaptcha'; 7 } 8 9 protected function shouldRender() { 10 return self::isRecaptchaEnabled(); 11 } 12 13 public static function isRecaptchaEnabled() { 14 return PhabricatorEnv::getEnvConfig('recaptcha.enabled'); 15 } 16 17 public static function hasCaptchaResponse(AphrontRequest $request) { 18 return $request->getBool('g-recaptcha-response'); 19 } 20 21 public static function processCaptcha(AphrontRequest $request) { 22 if (!self::isRecaptchaEnabled()) { 23 return true; 24 } 25 26 $uri = 'https://www.google.com/recaptcha/api/siteverify'; 27 $params = array( 28 'secret' => PhabricatorEnv::getEnvConfig('recaptcha.private-key'), 29 'response' => $request->getStr('g-recaptcha-response'), 30 'remoteip' => $request->getRemoteAddress(), 31 ); 32 33 list($body) = id(new HTTPSFuture($uri, $params)) 34 ->setMethod('POST') 35 ->resolvex(); 36 37 $json = phutil_json_decode($body); 38 return (bool)idx($json, 'success'); 39 } 40 41 protected function renderInput() { 42 $js = 'https://www.google.com/recaptcha/api.js'; 43 $pubkey = PhabricatorEnv::getEnvConfig('recaptcha.public-key'); 44 45 CelerityAPI::getStaticResourceResponse() 46 ->addContentSecurityPolicyURI('script-src', $js) 47 ->addContentSecurityPolicyURI('script-src', 'https://www.gstatic.com/') 48 ->addContentSecurityPolicyURI('frame-src', 'https://www.google.com/'); 49 50 return array( 51 phutil_tag( 52 'div', 53 array( 54 'class' => 'g-recaptcha', 55 'data-sitekey' => $pubkey, 56 )), 57 phutil_tag( 58 'script', 59 array( 60 'type' => 'text/javascript', 61 'src' => $js, 62 )), 63 ); 64 } 65}