|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\GoogleRecaptcha; |
|
3
|
|
|
|
|
4
|
|
|
use Pimple\Container as PimpleContainer; |
|
5
|
|
|
use Pimple\ServiceProviderInterface; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Psr\Log\NullLogger; |
|
8
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
9
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
10
|
|
|
|
|
11
|
|
|
use ReCaptcha\ReCaptcha; |
|
12
|
|
|
|
|
13
|
|
|
class GoogleRecaptchaServiceProvider implements ServiceProviderInterface, LoggerAwareInterface |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
use LoggerAwareTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $public_key; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
public $secret_key; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $public_key |
|
32
|
|
|
* @param string $secret_key |
|
33
|
|
|
* @param LoggerInterface|null $logger |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( $public_key, $secret_key, LoggerInterface $logger = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->secret_key = $secret_key; |
|
38
|
|
|
$this->public_key = $public_key; |
|
39
|
|
|
$this->setLogger( $logger ?: new NullLogger ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param PimpleContainer $dic Pimple Instance |
|
46
|
|
|
* @implements ServiceProviderInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
public function register(PimpleContainer $dic) |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
$dic['Google.Recaptcha.Config'] = function($dic) { |
|
|
|
|
|
|
55
|
|
|
return [ |
|
56
|
|
|
'input_field' => 'g-recaptcha-response', |
|
57
|
|
|
'status_code' => 400, |
|
58
|
|
|
'request_attribute' => 'GoogleRecaptcha' |
|
59
|
|
|
]; |
|
60
|
|
|
}; |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
$dic['Google.Recaptcha.PublicKey'] = function($dic) { |
|
|
|
|
|
|
67
|
|
|
return $this->public_key; |
|
68
|
|
|
}; |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
$dic['Google.Recaptcha.SecretKey'] = function($dic) { |
|
|
|
|
|
|
75
|
|
|
return $this->secret_key; |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
$dic['Google.Recaptcha.ClientIP'] = function($dic) { |
|
|
|
|
|
|
82
|
|
|
return $_SERVER['REMOTE_ADDR']; |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return LoggerInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
$dic['Google.Recaptcha.Logger'] = function($dic) { |
|
|
|
|
|
|
90
|
|
|
return $this->logger; |
|
91
|
|
|
}; |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return ReCaptcha |
|
96
|
|
|
*/ |
|
97
|
|
|
$dic['Google.Recaptcha.Validator'] = function($dic) { |
|
98
|
|
|
$secret = $dic['Google.Recaptcha.SecretKey']; |
|
99
|
|
|
return new ReCaptcha($secret); |
|
100
|
|
|
}; |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return callable GoogleRecaptchaCallable |
|
105
|
|
|
*/ |
|
106
|
|
|
$dic['Google.Recaptcha.Validator.Callable'] = function($dic) { |
|
107
|
|
|
$php_client = $dic['Google.Recaptcha.Validator']; |
|
108
|
|
|
$logger = $dic['Google.Recaptcha.Logger']; |
|
109
|
|
|
|
|
110
|
|
|
return new GoogleRecaptchaCallable( $php_client, $logger ); |
|
111
|
|
|
}; |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return callable GoogleRecaptchaMiddleware |
|
116
|
|
|
*/ |
|
117
|
|
|
$dic['Google.Recaptcha.Middleware'] = function($dic) { |
|
118
|
|
|
|
|
119
|
|
|
$recaptcha_validator = $dic['Google.Recaptcha.Validator.Callable']; |
|
120
|
|
|
$client_ip = $dic['Google.Recaptcha.ClientIP']; |
|
121
|
|
|
$logger = $dic['Google.Recaptcha.Logger']; |
|
122
|
|
|
|
|
123
|
|
|
$config = $dic['Google.Recaptcha.Config']; |
|
124
|
|
|
$input_field = $config['input_field']; |
|
125
|
|
|
$http_status_code = $config['status_code']; |
|
126
|
|
|
$request_attribute = $config['request_attribute']; |
|
127
|
|
|
|
|
128
|
|
|
return new GoogleRecaptchaMiddleware($recaptcha_validator, $client_ip, $input_field, $request_attribute, $http_status_code, $logger); |
|
129
|
|
|
|
|
130
|
|
|
}; |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.