1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Captcha; |
4
|
|
|
|
5
|
|
|
use Shetabit\Captcha\Contracts\DriverInterface; |
6
|
|
|
use Shetabit\Captcha\Exceptions\DriverNotFoundException; |
7
|
|
|
use Shetabit\Captcha\Provider\CaptchaServiceProvider; |
8
|
|
|
|
9
|
|
|
class CaptchaManager |
10
|
|
|
{ |
11
|
|
|
protected $serviceProvider; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Captcha Configuration. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $config; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Captcha Driver Settings. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $settings; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Captcha Driver Name. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $driver; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Captcha Driver Instance. |
36
|
|
|
* |
37
|
|
|
* @var object |
38
|
|
|
*/ |
39
|
|
|
protected $driverInstance; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* CaptchaManager constructor. |
43
|
|
|
* |
44
|
|
|
* CaptchaManager constructor. |
45
|
|
|
* @param CaptchaServiceProvider $serviceProvider |
46
|
|
|
* @param $config |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
|
|
public function __construct(CaptchaServiceProvider $serviceProvider, $config) |
50
|
|
|
{ |
51
|
|
|
$this->serviceProvider = $serviceProvider; |
52
|
|
|
$this->config = $config; |
53
|
|
|
|
54
|
|
|
$this->via($this->config['default']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set driver. |
59
|
|
|
* |
60
|
|
|
* @param $driver |
61
|
|
|
* @return $this |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
private function via($driver) |
65
|
|
|
{ |
66
|
|
|
$this->driver = $driver; |
67
|
|
|
$this->validateDriver(); |
68
|
|
|
$this->settings = $this->config['drivers'][$driver]; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Prepare driver's instance to load requirements if needed. |
75
|
|
|
* |
76
|
|
|
* @return mixed|object |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
|
public function prepareDriver() |
80
|
|
|
{ |
81
|
|
|
$this->driverInstance = $this->getDriverInstance(); |
82
|
|
|
|
83
|
|
|
return $this->driverInstance; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Generate the captcha |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
|
|
public function generate() |
93
|
|
|
{ |
94
|
|
|
return $this->getDriverInstance()->generate(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Verify CAPTCHA with given token. |
99
|
|
|
* |
100
|
|
|
* @param null|$token |
101
|
|
|
* @return mixed |
102
|
|
|
* @throws \Exception |
103
|
|
|
*/ |
104
|
|
|
public function verify($token = null) |
105
|
|
|
{ |
106
|
|
|
return $this->getDriverInstance()->verify($token); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retrieve current driver instance or generate new one. |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
|
|
protected function getDriverInstance() |
116
|
|
|
{ |
117
|
|
|
if (!empty($this->driverInstance)) { |
118
|
|
|
return $this->driverInstance; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->getFreshDriverInstance(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get new driver instance |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
* @throws \Exception |
129
|
|
|
*/ |
130
|
|
|
protected function getFreshDriverInstance() |
131
|
|
|
{ |
132
|
|
|
$this->validateDriver(); |
133
|
|
|
$class = $this->config['map'][$this->driver]; |
134
|
|
|
|
135
|
|
|
if (!empty($this->callbackUrl)) { // use custom callbackUrl if exists |
136
|
|
|
$this->settings['callbackUrl'] = $this->callbackUrl; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return new $class($this->serviceProvider, $this->settings); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Validate driver. |
144
|
|
|
* |
145
|
|
|
* @throws \Exception |
146
|
|
|
*/ |
147
|
|
|
protected function validateDriver() |
148
|
|
|
{ |
149
|
|
|
if (empty($this->driver)) { |
150
|
|
|
throw new DriverNotFoundException('Driver not selected or default driver does not exist.'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (empty($this->config['drivers'][$this->driver]) || empty($this->config['map'][$this->driver])) { |
154
|
|
|
throw new DriverNotFoundException('Driver not found in config file. Try updating the package.'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!class_exists($this->config['map'][$this->driver])) { |
158
|
|
|
throw new DriverNotFoundException('Driver source not found. Please update the package.'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$reflect = new \ReflectionClass($this->config['map'][$this->driver]); |
162
|
|
|
|
163
|
|
|
if (!$reflect->implementsInterface(DriverInterface::class)) { |
164
|
|
|
throw new \Exception("Driver must be an instance of Contracts\DriverInterface."); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|