Skip to content

Commit 5adedbf

Browse files
committed
Add component classes
1 parent 7bfd2b8 commit 5adedbf

2 files changed

Lines changed: 166 additions & 0 deletions

File tree

src/TGRecaptcha.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
class TGRecaptcha extends TField implements AdiantiWidgetInterface
4+
{
5+
protected $name;
6+
protected $form;
7+
protected $required;
8+
protected $labelRequired;
9+
protected $validator;
10+
protected $validatorLabel;
11+
protected $width;
12+
protected $height;
13+
14+
public function __construct($name)
15+
{
16+
parent::__construct($name);
17+
$this->setTagName('div');
18+
$this->setName($name);
19+
$this->id = 'tgrecaptcha'.mt_rand(1000000000, 1999999999);
20+
}
21+
22+
public function setId($id)
23+
{
24+
$this->id = $id;
25+
}
26+
27+
public function getId($id)
28+
{
29+
return $this->id;
30+
}
31+
32+
public function setName($name)
33+
{
34+
$this->name = $name;
35+
}
36+
37+
public function getName($name)
38+
{
39+
return $this->name;
40+
}
41+
42+
public function setRequired($required)
43+
{
44+
$this->required = $required;
45+
}
46+
47+
public function getRequired($required)
48+
{
49+
return $this->required;
50+
}
51+
52+
public function setLabelRequired($labelRequired)
53+
{
54+
$this->labelRequired = $labelRequired;
55+
}
56+
57+
public function getLabelRequired($labelRequired)
58+
{
59+
return $this->labelRequired;
60+
}
61+
62+
public function setValidator($validator)
63+
{
64+
$this->validator = $validator;
65+
}
66+
67+
public function getValidator($validator)
68+
{
69+
return $this->validator;
70+
}
71+
72+
public function setValidatorLabel($validatorLabel)
73+
{
74+
$this->validatorLabel = $validatorLabel;
75+
}
76+
77+
public function getValidatorLabel($validatorLabel)
78+
{
79+
return $this->validatorLabel;
80+
}
81+
82+
public function setWidth($width)
83+
{
84+
$this->width = $width;
85+
}
86+
87+
public function getWidth($width)
88+
{
89+
return $this->width;
90+
}
91+
92+
public function setHeight($height)
93+
{
94+
$this->height = $height;
95+
}
96+
97+
public function getHeight($height)
98+
{
99+
return $this->height;
100+
}
101+
102+
public function getSize()
103+
{
104+
return [
105+
$this->width ?? null,
106+
$this->height ?? null
107+
];
108+
}
109+
110+
public function getPostData()
111+
{
112+
return $_REQUEST['g-recaptcha-response'] ?? null;
113+
}
114+
115+
public function show()
116+
{
117+
$this->tag->id = $this->id;
118+
$this->tag->name = $this->name;
119+
120+
$recaptcha_config = require_once('app/config/recaptcha.php');
121+
122+
$this->tag->style = 'padding-left:50px; margin-bottom: -25px;';
123+
$this->tag->add("
124+
<script src='https://www.google.com/recaptcha/api.js' async defer ></script>
125+
<div class='g-recaptcha' data-sitekey='{$recaptcha_config['chave_site']}' ></div>
126+
");
127+
128+
if(!empty($this->width))
129+
{
130+
$width = (strstr((string) $this->width, '%') !== false) ? $this->width : "{$this->width}";
131+
$this->tag->style .= "width:{$width}";
132+
}
133+
134+
if(!empty($this->height))
135+
{
136+
$height = (strstr((string) $this->height, '%') !== false) ? $this->height : "{$this->height}";
137+
$this->tag->style .= "height:{$height}";
138+
}
139+
140+
$this->tag->class = '';
141+
142+
$this->tag->show();
143+
}
144+
}

src/TGRecaptchaValidator.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
class TGRecaptchaValidator extends TFieldValidator
4+
{
5+
public function validate($label, $value, $params = null)
6+
{
7+
if(!value)
8+
{
9+
throw new Exception("O campo {$label} é obrigatório");
10+
}
11+
12+
$recaptcha_config = require_once('app/config/recaptcha.php');
13+
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_config['chave_secreta']);
14+
$resp = $recaptcha->setExpectedHostname($_SERVER["SERVER_NAME"])
15+
->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
16+
17+
if (!$resp->isSuccess())
18+
{
19+
throw new Exception("O {$label} é inválido!");
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)