Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / view / form / control / AphrontFormRecaptchaControl.php
blob4152f7d8b7df31639193e93b18b728457632da0c
1 <?php
3 final class AphrontFormRecaptchaControl extends AphrontFormControl {
5 protected function getCustomControlClass() {
6 return 'aphront-form-control-recaptcha';
9 protected function shouldRender() {
10 return self::isRecaptchaEnabled();
13 public static function isRecaptchaEnabled() {
14 return PhabricatorEnv::getEnvConfig('recaptcha.enabled');
17 public static function hasCaptchaResponse(AphrontRequest $request) {
18 return $request->getBool('g-recaptcha-response');
21 public static function processCaptcha(AphrontRequest $request) {
22 if (!self::isRecaptchaEnabled()) {
23 return true;
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(),
33 list($body) = id(new HTTPSFuture($uri, $params))
34 ->setMethod('POST')
35 ->resolvex();
37 $json = phutil_json_decode($body);
38 return (bool)idx($json, 'success');
41 protected function renderInput() {
42 $js = 'https://www.google.com/recaptcha/api.js';
43 $pubkey = PhabricatorEnv::getEnvConfig('recaptcha.public-key');
45 CelerityAPI::getStaticResourceResponse()
46 ->addContentSecurityPolicyURI('script-src', $js)
47 ->addContentSecurityPolicyURI('script-src', 'https://www.gstatic.com/')
48 ->addContentSecurityPolicyURI('frame-src', 'https://www.google.com/');
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 )),