recaptcha icon indicating copy to clipboard operation
recaptcha copied to clipboard

Fix PHP 8.1 deprecations are being triggered in Response.php.

Open anprok opened this issue 2 years ago • 10 comments

Fixes issue #490

anprok avatar Apr 19 '22 15:04 anprok

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

For more information, open the CLA check for this pull request.

google-cla[bot] avatar Apr 19 '22 15:04 google-cla[bot]

@googlebot Would have been nice to get this out with a new tag. This error creates problems.

3kbest avatar Jun 21 '22 21:06 3kbest

Would be great to have this merged.

AlbertMN avatar Jul 01 '22 13:07 AlbertMN

I want to upgrade the PHP version for some of my websites and this bug is a problem that prevents me from doing it! It seems there is a missing CLA that prevents the process of merging this PR. I think as it's an old PR the best way to fix this problem is to create a new PR almost identical to this one!

moeBabazadeh avatar Jul 12 '22 14:07 moeBabazadeh

All contributors are covered under a CLA with Google, see https://github.com/google/recaptcha/pull/491/checks @rowan-m Could you help to merge this PR?

anprok avatar Jul 12 '22 14:07 anprok

@anprok My apologies, I simply clicked on the link in the "google-cla" comment and forgot to hit the refresh button.

moeBabazadeh avatar Jul 12 '22 14:07 moeBabazadeh

Thanks for the update - taking a look!

rowan-m avatar Jul 20 '22 11:07 rowan-m

4 months have passed. This is a super easy fix. What is the problem with merging this?....

3kbest avatar Aug 20 '22 23:08 3kbest

@rowan-m How about merging this, so people can resolve this error and not have to deal with forking this repo.

oojacoboo avatar Sep 21 '22 03:09 oojacoboo

Please merge

8ctopus avatar Sep 22 '22 10:09 8ctopus

@rowan-m please!

NiklasBr avatar Oct 04 '22 12:10 NiklasBr

Google wonders why none of their products are a success. It's because they don't support anything. At this point... why invest in adopting or using a Google product when in short time, it's going to be abandoned. Relevant and timely article just the other day on this topic:

https://techcrunch.com/2022/10/01/stadia-died-because-no-one-trusts-google/

oojacoboo avatar Oct 04 '22 15:10 oojacoboo

Another +1 for merge, please!

Tigatok avatar Oct 27 '22 18:10 Tigatok

+1 please merge

DaanBaars avatar Nov 04 '22 14:11 DaanBaars

7 months have passed. Please merge. @rowan-m @google-ospo-team @google-admin ?

3kbest avatar Nov 22 '22 22:11 3kbest

@rowan-m need merge this!

Ch0c0Cube avatar Nov 23 '22 18:11 Ch0c0Cube

I'm also waiting for the merge, but in the meantime I recommend using @ before the method :)

 $recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_secret_key);
 $response = @$recaptcha->setExpectedHostname($hostname)->verify($recaptcha_response, $client_ip);

schalkt avatar Dec 31 '22 18:12 schalkt

For anyone stumbling on this PR since Google doesn't seem to pay attention to this repository, if you're willing to use the cweagans/composer-patches package in your app, adding this patch will address or silence the deprecations coming from changes in PHP 8.1 or 8.2.

diff --git a/src/ReCaptcha/ReCaptcha.php b/src/ReCaptcha/ReCaptcha.php
index 31ec44a..af8f465 100644
--- a/src/ReCaptcha/ReCaptcha.php
+++ b/src/ReCaptcha/ReCaptcha.php
@@ -37,6 +37,7 @@ namespace ReCaptcha;
 /**
  * reCAPTCHA client.
  */
+#[\AllowDynamicProperties]
 class ReCaptcha
 {
     /**
diff --git a/src/ReCaptcha/Response.php b/src/ReCaptcha/Response.php
index 55838c0..e5ae53d 100644
--- a/src/ReCaptcha/Response.php
+++ b/src/ReCaptcha/Response.php
@@ -95,11 +95,11 @@ class Response
             return new Response(false, array(ReCaptcha::E_INVALID_JSON));
         }
 
-        $hostname = isset($responseData['hostname']) ? $responseData['hostname'] : null;
-        $challengeTs = isset($responseData['challenge_ts']) ? $responseData['challenge_ts'] : null;
-        $apkPackageName = isset($responseData['apk_package_name']) ? $responseData['apk_package_name'] : null;
+        $hostname = $responseData['hostname'] ?? '';
+        $challengeTs = $responseData['challenge_ts'] ?? '';
+        $apkPackageName = $responseData['apk_package_name'] ?? '';
         $score = isset($responseData['score']) ? floatval($responseData['score']) : null;
-        $action = isset($responseData['action']) ? $responseData['action'] : null;
+        $action = $responseData['action'] ?? '';
 
         if (isset($responseData['success']) && $responseData['success'] == true) {
             return new Response(true, array(), $hostname, $challengeTs, $apkPackageName, $score, $action);

I guess one of the 2023 changes I'll be rolling out to our clients is removing this package as a dependency due to lack of maintenance.

mbabker avatar Jan 03 '23 17:01 mbabker

For those not willing to use composer patches, I've created a fork with a branch that implements all changes needed for php 8.0, 8.1 and 8.2 compatibility here: https://github.com/8ctopus/recaptcha/commits/php8.2

To use it, you will need to update composer.json as follows:

{
    "require": {
        "google/recaptcha": "dev-php8.2"
    },
    "repositories": [{
        "type": "git",
        "url": "https://github.com/8ctopus/recaptcha.git"
    }]
}

It uses PR #460, #491 and #514.

8ctopus avatar Jan 27 '23 07:01 8ctopus