depage-forms
v2.0.0
html forms made easy
Toggle main menu visibility
Loading...
Searching...
No Matches
Captcha.php
Go to the documentation of this file.
1
<?php
2
12
13
namespace
Depage\HtmlForm\Elements;
14
15
use Gregwar\Captcha\CaptchaBuilder;
16
use Gregwar\Captcha\PhraseBuilder;
17
22
class
Captcha
extends
Text
23
{
27
protected
$captcha
=
null
;
28
32
protected
$sessionSlot
=
null
;
33
34
protected
$textColor
=
false
;
35
protected
$backgroundColor
=
false
;
36
protected
$width
= 250;
37
protected
$height
= 100;
38
48
protected
function
setDefaults
(): void
49
{
50
parent::setDefaults();
51
$this->defaults[
'defaultValue'
] =
false
;
52
$this->defaults[
'errorMessage'
] = _(
'Please fill in the correct phrase.'
);
53
$this->defaults[
'textColor'
] =
false
;
54
$this->defaults[
'backgroundColor'
] =
false
;
55
$this->defaults[
'width'
] = 250;
56
$this->defaults[
'height'
] = 100;
57
}
58
66
public
function
__construct
(
string
$name
, array $parameters,
object
$form)
67
{
68
$this->captcha =
new
CaptchaBuilder();
69
70
parent::__construct(
$name
, $parameters, $form);
71
72
if
(is_array($this->textColor)) {
73
$this->captcha->setTextColor($this->textColor[0], $this->textColor[1], $this->textColor[2]);
74
}
75
if
(is_array($this->backgroundColor)) {
76
$this->captcha->setBackgroundColor($this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]);
77
}
78
}
79
88
public
function
setValue
(mixed $newValue): mixed
89
{
90
if
(is_bool($newValue)) {
91
$this->value = $newValue;
92
}
else
{
93
$this->value = $newValue;
94
}
95
96
return
$this->value
;
97
}
98
104
public
function
setSessionSlot
(&
$sessionSlot
): void
105
{
106
$this->sessionSlot = &
$sessionSlot
;
107
108
}
109
118
public
function
validate
(): bool
119
{
120
if
(!$this->validated) {
121
$this->validated =
true
;
122
123
$this->valid = $this->value ===
true
|| $this->
testPhrase
($this->value);
124
}
125
126
return
$this->valid
;
127
}
128
134
protected
function
testPhrase
(mixed
$value
): bool
135
{
136
$this->captcha->setPhrase($this->sessionSlot[
'formCaptcha'
] ??
""
);
137
138
if
(!$this->captcha->testPhrase(
$value
)) {
139
$this->
getNewPhrase
();
140
141
return
false
;
142
}
143
144
return
true
;
145
}
146
151
protected
function
getNewPhrase
(): void
152
{
153
$phraseBuilder =
new
PhraseBuilder();
154
$phrase = $phraseBuilder->build();
155
156
$this->captcha->setPhrase($phrase);
157
$this->sessionSlot[
'formCaptcha'
] = $phrase;
158
}
159
164
public
function
__toString
(): string
165
{
166
$value
= $this->
htmlValue
();
167
$type
= strtolower($this->type);
168
$inputAttributes = $this->
htmlInputAttributes
();
169
$marker
= $this->
htmlMarker
();
170
$label
= $this->htmlLabel();
171
$list
= $this->
htmlList
();
172
$wrapperAttributes = $this->
htmlWrapperAttributes
();
173
$errorMessage
= $this->
htmlErrorMessage
();
174
$helpMessage
= $this->
htmlHelpMessage
();
175
176
if
(empty($this->sessionSlot[
'formCaptcha'
])) {
177
$this->
getNewPhrase
();
178
}
179
180
$this->captcha->build($this->width, $this->height);
181
182
return
"<p {$wrapperAttributes}>"
183
.
"<label>"
184
.
"<span class=\"depage-label\">{$label}{$marker}</span>"
185
.
"<span class=\"captcha-img\">"
186
.
"<img src=\""
. $this->captcha->inline() .
"\" />"
187
.
"</span>"
188
.
"<input name=\"{$this->name}\" type=\"{$type}\"{$inputAttributes} value=\"$value\">"
189
.
$list
190
.
"</label>"
191
.
$errorMessage
192
.
$helpMessage
193
.
"</p>\n"
;
194
}
195
}
196
197
// vim:set ft=php sw=4 sts=4 fdm=marker et :
Depage\HtmlForm\Abstracts\Element\$valid
$valid
Contains element validation status/result.
Definition
Element.php:41
Depage\HtmlForm\Abstracts\Element\$name
$name
Element name.
Definition
Element.php:36
Depage\HtmlForm\Abstracts\Input\$value
$value
Input elements's value.
Definition
Input.php:124
Depage\HtmlForm\Abstracts\Input\$label
$label
Input element - HTML label.
Definition
Input.php:37
Depage\HtmlForm\Abstracts\Input\$errorMessage
$errorMessage
Message that gets displayed in case of invalid input.
Definition
Input.php:218
Depage\HtmlForm\Abstracts\Input\htmlHelpMessage
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition
Input.php:685
Depage\HtmlForm\Abstracts\Input\htmlWrapperAttributes
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition
Input.php:636
Depage\HtmlForm\Abstracts\Input\htmlMarker
htmlMarker()
Returns elements' required-indicator.
Definition
Input.php:566
Depage\HtmlForm\Abstracts\Input\$type
$type
Input element type - HTML input type attribute.
Definition
Input.php:26
Depage\HtmlForm\Abstracts\Input\$helpMessage
$helpMessage
Extra help message.
Definition
Input.php:230
Depage\HtmlForm\Abstracts\Input\htmlValue
htmlValue()
Returns HTML-rendered element value.
Definition
Input.php:656
Depage\HtmlForm\Abstracts\Input\$marker
$marker
Input element - HTML marker text that marks required fields.
Definition
Input.php:92
Depage\HtmlForm\Abstracts\Input\htmlErrorMessage
htmlErrorMessage()
Returns HTML-rendered error message.
Definition
Input.php:666
Depage\HtmlForm\Elements\Captcha
Captcha Class Captcha.
Definition
Captcha.php:23
Depage\HtmlForm\Elements\Captcha\validate
validate()
validates boolean input element value
Definition
Captcha.php:118
Depage\HtmlForm\Elements\Captcha\setValue
setValue(mixed $newValue)
set the boolean element value
Definition
Captcha.php:88
Depage\HtmlForm\Elements\Captcha\$height
$height
Definition
Captcha.php:37
Depage\HtmlForm\Elements\Captcha\$width
$width
Definition
Captcha.php:36
Depage\HtmlForm\Elements\Captcha\$captcha
$captcha
captcha
Definition
Captcha.php:27
Depage\HtmlForm\Elements\Captcha\__construct
__construct(string $name, array $parameters, object $form)
__construct
Definition
Captcha.php:66
Depage\HtmlForm\Elements\Captcha\__toString
__toString()
Renders element to HTML.
Definition
Captcha.php:164
Depage\HtmlForm\Elements\Captcha\setSessionSlot
setSessionSlot(&$sessionSlot)
setSessionSlot
Definition
Captcha.php:104
Depage\HtmlForm\Elements\Captcha\getNewPhrase
getNewPhrase()
getNewPhrase
Definition
Captcha.php:151
Depage\HtmlForm\Elements\Captcha\$sessionSlot
$sessionSlot
sessionSlot
Definition
Captcha.php:32
Depage\HtmlForm\Elements\Captcha\$backgroundColor
$backgroundColor
Definition
Captcha.php:35
Depage\HtmlForm\Elements\Captcha\$textColor
$textColor
Definition
Captcha.php:34
Depage\HtmlForm\Elements\Captcha\setDefaults
setDefaults()
collects initial values across subclasses.
Definition
Captcha.php:48
Depage\HtmlForm\Elements\Captcha\testPhrase
testPhrase(mixed $value)
testPhrase
Definition
Captcha.php:134
Depage\HtmlForm\Elements\Text
HTML text input type.
Definition
Text.php:40
Depage\HtmlForm\Elements\Text\htmlInputAttributes
htmlInputAttributes()
renders text element specific HTML attributes
Definition
Text.php:160
Depage\HtmlForm\Elements\Text\$list
$list
HTML list.
Definition
Text.php:59
Depage\HtmlForm\Elements\Text\htmlList
htmlList(?array $options=null, array|string|null $value=null)
Renders HTML datalist.
Definition
Text.php:130
Elements
Captcha.php
Generated on
for depage-forms by
doxygen
1.18.0
For an overview of all docs, go to
docs.depage.net