1 <?php
2 namespace DevStrefa\ReCaptcha;
3
4 use DevStrefa\ReCaptcha\Senders\SenderInterface;
5
6 /**
7 * Main library class
8 *
9 * @author Cichy <d3ut3r@gmail.com>
10 * @license https://opensource.org/licenses/MIT MIT
11 * @version 1.0.0
12 */
13 class ReCaptcha
14 {
15
16 /**
17 *
18 * @var string secret_key generated in Google reCaptcha panel
19 */
20 private $secret;
21
22 /**
23 *
24 * @var SenderInterface Sender Object (must implement SenderInterface)
25 */
26 private $sender;
27
28 /**
29 *
30 * @var string Remote IP Address
31 */
32 private $remoteIp;
33
34 /**
35 *
36 * @var string Response for example from $_POST array
37 */
38 private $response;
39
40 /**
41 *
42 * @param string secret_key generated in Google reCaptcha panel
43 * @param SenderInterface Sender object (if null FgcSender will be used)
44 * @throws \InvalidArgumentException
45 */
46 public function __construct($secret, SenderInterface $sender = null)
47 {
48
49 if (empty($secret) || !is_string($secret))
50 {
51 throw new \InvalidArgumentException('Invalid value of secret key.');
52 }
53
54 $this->secret=$secret;
55
56 if (!isset($sender))
57 {
58 $this->sender=new Senders\FgcSender();
59 }
60 else
61 {
62 $this->sender=$sender;
63 }
64
65 }
66
67 /**
68 * Setting response of captcha to verify.
69 *
70 * @param string $response
71 * @return \DevStrefa\ReCaptcha\ReCaptcha
72 * @throws \InvalidArgumentException
73 */
74 public function setResponse($response)
75 {
76 if (empty($response) || !is_string($response))
77 {
78 throw new \InvalidArgumentException('Invalid value for g-recaptcha-response');
79 }
80
81 $this->response=$response;
82 return $this;
83
84 }
85
86 /**
87 * Setting Remote IP Address (optional)
88 *
89 * @param string $remoteIp
90 * @return \DevStrefa\ReCaptcha\ReCaptcha
91 * @throws \InvalidArgumentException
92 */
93 public function setRemoteIp($remoteIp)
94 {
95 if (!filter_var($remoteIp,FILTER_VALIDATE_IP))
96 {
97 throw new \InvalidArgumentException('Invalid IP address');
98 }
99
100 $this->remoteIp=$remoteIp;
101
102 return $this;
103 }
104
105 /**
106 * Verify given response by using sender provided in constructor.
107 * @return Response Response Object
108 */
109 public function verify()
110 {
111 return $this->sender->send($this->secret,$this->response,$this->remoteIp);
112 }
113
114
115
116
117 }
118