-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCaptchaField.php
More file actions
84 lines (67 loc) · 1.73 KB
/
CaptchaField.php
File metadata and controls
84 lines (67 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Gregwar\Formidable\Fields;
use Gregwar\Captcha\CaptchaBuilder;
/**
* Captcha field
*
* @author Grégoire Passault <g.passault@gmail.com>
*/
class CaptchaField extends Field
{
/**
* Captcha value
*/
protected $builder = null;
/**
* Captcha is required
*/
protected $required = true;
/**
* Field type (text)
*/
protected $type = 'text';
public function __wakeup()
{
$this->generate();
}
public function __construct()
{
if (!class_exists(CaptchaBuilder::class)) {
throw new \LogicException('You should install gregwar/captcha to use the CAPTCHA type');
}
$this->generate();
}
protected function generate()
{
$this->builder = new CaptchaBuilder;
$this->builder->build();
}
public function push($var, $value = null)
{
if ($var !== 'type') {
parent::push($var, $value);
}
}
public function getCaptchaValue()
{
return $this->builder->getPhrase();
}
public function check()
{
$this->value = strtolower($this->value);
if (!isset($_SESSION['Formidable_Captcha']) || $_SESSION['Formidable_Captcha']!=$this->value) {
return array('bad_captcha');
}
unset($_SESSION["Formidable_Captcha"]);
}
public function getHtml()
{
$temp = $this->value;
$this->value = '';
$input_html = parent::getHtml();
$this->value = $temp;
$_SESSION['Formidable_Captcha'] = $this->getCaptchaValue();
$html = '<img src="'.$this->builder->inline().'" class="Formidable_Captcha" alt="Code visuel" ><br />'.$input_html;
return $html;
}
}