1
+ <?php
2
+ /**
3
+ * Created by PhpStorm.
4
+ * User: rayndeng
5
+ * Date: 6/8/18
6
+ * Time: 5:29 PM
7
+ */
8
+
9
+ namespace RyanDeng \GoogleReCaptcha ;
10
+
11
+ use RyanDeng \GoogleReCaptcha \Interfaces \ReCaptchaConfigV3Interface ;
12
+ use RyanDeng \GoogleReCaptcha \Interfaces \RequestClientInterface ;
13
+ use RyanDeng \GoogleReCaptcha \Core \GoogleReCaptchaV3Response ;
14
+
15
+ class GoogleReCaptchaV3
16
+ {
17
+ private $ config ;
18
+ private $ requestClient ;
19
+ private $ action ;
20
+ private $ defaultView = 'GoogleReCaptchaV3::googlerecaptcha.googlerecaptchav3 ' ;
21
+
22
+ public function __construct (ReCaptchaConfigV3Interface $ config , RequestClientInterface $ requestClient )
23
+ {
24
+ $ this ->config = $ config ;
25
+ $ this ->requestClient = $ requestClient ;
26
+ }
27
+
28
+ /**
29
+ * @param mixed ...$actions
30
+ * @return mixed
31
+ */
32
+ public function render (...$ actions )
33
+ {
34
+ if ($ this ->config ->isServiceEnabled () === false ) {
35
+ return null ;
36
+ }
37
+
38
+ $ mapping = collect ($ this ->config ->getScoreSetting ())
39
+ ->whereIn ('action ' , $ actions )
40
+ ->pluck ('id ' , 'action ' )
41
+ ->toArray ();
42
+
43
+ $ data = [
44
+ 'publicKey ' => value ($ this ->config ->getSiteKey ()),
45
+ 'rows ' => $ mapping
46
+ ];
47
+
48
+ $ view = $ this ->getView ();
49
+
50
+ return app ('view ' )->make ($ view , $ data );
51
+ }
52
+
53
+ /**
54
+ * @return mixed|string
55
+ */
56
+ protected function getView ()
57
+ {
58
+ $ configTemplate = $ this ->config ->getTemplate ();
59
+
60
+ if (!empty ($ configTemplate )) {
61
+ $ this ->defaultView = $ configTemplate ;
62
+ }
63
+ return $ this ->defaultView ;
64
+ }
65
+
66
+ /**
67
+ * @param $response
68
+ * @param null $ip
69
+ * @return GoogleReCaptchaV3Response
70
+ */
71
+ public function verifyResponse ($ response , $ ip = null )
72
+ {
73
+
74
+ if (!$ this ->config ->isServiceEnabled ()) {
75
+ $ res = new GoogleReCaptchaV3Response ([], $ ip );
76
+ $ res ->setSuccess (true );
77
+ return $ res ;
78
+ }
79
+
80
+ if (empty ($ response )) {
81
+ return new GoogleReCaptchaV3Response ([], $ ip , 'Missing input response. ' );
82
+ }
83
+
84
+ $ verifiedResponse = $ this ->requestClient ->post (
85
+ $ this ->config ->getSiteVerifyUrl (),
86
+ [
87
+ 'secret ' => $ this ->config ->getSecretKey (),
88
+ 'remoteip ' => $ ip ,
89
+ 'response ' => $ response ,
90
+ ],
91
+ [
92
+ 'curl_timeout ' => $ this ->config ->getCurlTimeout (),
93
+ 'curl_verify ' => $ this ->config ->getCurlVerify (),
94
+ ]
95
+ );
96
+
97
+ if (is_null ($ verifiedResponse ) || empty ($ verifiedResponse )) {
98
+ return new GoogleReCaptchaV3Response ([], $ ip , 'Unable to verify. ' );
99
+ }
100
+
101
+ $ decodedResponse = json_decode ($ verifiedResponse , true );
102
+ $ rawResponse = new GoogleReCaptchaV3Response ($ decodedResponse , $ ip );
103
+
104
+ if ($ rawResponse ->isSuccess () === false ) {
105
+ return $ rawResponse ;
106
+ }
107
+
108
+ if (strcasecmp ($ this ->config ->getHostName (), $ rawResponse ->getHostname ()) !== 0 ) {
109
+ $ rawResponse ->setMessage ('Hostname does not match. ' );
110
+ $ rawResponse ->setSuccess (false );
111
+ return $ rawResponse ;
112
+ }
113
+
114
+ if (isset ($ this ->action ) && strcasecmp ($ this ->action , $ rawResponse ->getAction ()) !== 0 ) {
115
+ $ rawResponse ->setMessage ('Action does not match. ' );
116
+ $ rawResponse ->setSuccess (false );
117
+ return $ rawResponse ;
118
+ }
119
+
120
+ if ($ this ->getConfig ()->isScoreEnabled ()) {
121
+ $ count = collect ($ this ->getConfig ()->getScoreSetting ())
122
+ ->where ('action ' , '= ' , $ rawResponse ->getAction ())
123
+ ->where ('is_enabled ' , '= ' , true )
124
+ ->where ('threshold ' , '> ' , $ rawResponse ->getScore ())
125
+ ->count ();
126
+
127
+
128
+ if ($ count > 0 ) {
129
+ $ rawResponse ->setSuccess (false );
130
+ $ rawResponse ->setMessage ('Score does not meet threshold. ' );
131
+ return $ rawResponse ;
132
+ }
133
+ }
134
+ $ rawResponse ->setSuccess (true );
135
+ $ rawResponse ->setMessage ('Successfully passed. ' );
136
+ return $ rawResponse ;
137
+ }
138
+
139
+
140
+ public function getConfig ()
141
+ {
142
+ return $ this ->config ;
143
+
144
+ }
145
+
146
+ /**
147
+ * @param string|null $value
148
+ * @return $this
149
+ */
150
+ public function setAction (string $ value = null )
151
+ {
152
+ $ this ->action = $ value ;
153
+ return $ this ;
154
+ }
155
+
156
+ }
0 commit comments