depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Captcha.php
Go to the documentation of this file.
1<?php
2
12
13namespace Depage\HtmlForm\Elements;
14
15use Gregwar\Captcha\CaptchaBuilder;
16use Gregwar\Captcha\PhraseBuilder;
17
22class Captcha extends Text
23{
27 protected $captcha = null;
28
32 protected $sessionSlot = null;
33
43 protected function setDefaults(): void
44 {
45 parent::setDefaults();
46 $this->defaults['defaultValue'] = false;
47 $this->defaults['errorMessage'] = _('Please fill in the correct phrase.');
48 $this->defaults['textColor'] = false;
49 $this->defaults['backgroundColor'] = false;
50 $this->defaults['width'] = 250;
51 $this->defaults['height'] = 100;
52 }
53
61 public function __construct(string $name, array $parameters, object $form)
62 {
63 $this->captcha = new CaptchaBuilder();
64
65 parent::__construct($name, $parameters, $form);
66
67 if (is_array($this->textColor)) {
68 $this->captcha->setTextColor($this->textColor[0], $this->textColor[1], $this->textColor[2]);
69 }
70 if (is_array($this->backgroundColor)) {
71 $this->captcha->setBackgroundColor($this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]);
72 }
73 }
74
83 public function setValue(mixed $newValue): mixed
84 {
85 if (is_bool($newValue)) {
86 $this->value = $newValue;
87 } else {
88 $this->value = $newValue;
89 }
90
91 return $this->value;
92 }
93
99 public function setSessionSlot(&$sessionSlot): void
100 {
101 $this->sessionSlot = &$sessionSlot;
102
103 }
104
113 public function validate(): bool
114 {
115 if (!$this->validated) {
116 $this->validated = true;
117
118 $this->valid = $this->value === true || $this->testPhrase($this->value);
119 }
120
121 return $this->valid;
122 }
123
129 protected function testPhrase(mixed $value): bool
130 {
131 $this->captcha->setPhrase($this->sessionSlot['formCaptcha'] ?? "");
132
133 if (!$this->captcha->testPhrase($value)) {
134 $this->getNewPhrase();
135
136 return false;
137 }
138
139 return true;
140 }
141
146 protected function getNewPhrase(): void
147 {
148 $phraseBuilder = new PhraseBuilder();
149 $phrase = $phraseBuilder->build();
150
151 $this->captcha->setPhrase($phrase);
152 $this->sessionSlot['formCaptcha'] = $phrase;
153 }
154
159 public function __toString(): string
160 {
161 $value = $this->htmlValue();
162 $type = strtolower($this->type);
163 $inputAttributes = $this->htmlInputAttributes();
164 $marker = $this->htmlMarker();
165 $label = $this->htmlLabel();
166 $list = $this->htmlList();
167 $wrapperAttributes = $this->htmlWrapperAttributes();
169 $helpMessage = $this->htmlHelpMessage();
170
171 if (empty($this->sessionSlot['formCaptcha'])) {
172 $this->getNewPhrase();
173 }
174
175 $this->captcha->build($this->width, $this->height);
176
177 return "<p {$wrapperAttributes}>" .
178 "<label>" .
179 "<span class=\"depage-label\">{$label}{$marker}</span>" .
180 "<span class=\"captcha-img\">" .
181 "<img src=\"" . $this->captcha->inline() . "\" />" .
182 "</span>" .
183 "<input name=\"{$this->name}\" type=\"{$type}\"{$inputAttributes} value=\"$value\">" .
184 $list .
185 "</label>" .
188 "</p>\n";
189 }
190}
191
192
193
194// vim:set ft=php sw=4 sts=4 fdm=marker et :
$valid
Contains element validation status/result.
Definition Element.php:39
$value
Input elements's value.
Definition Input.php:124
$label
Input element - HTML label.
Definition Input.php:37
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:218
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:685
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:636
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:566
$type
Input element type - HTML input type attribute.
Definition Input.php:26
$helpMessage
Extra help message.
Definition Input.php:230
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:656
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:92
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:666
Captcha Class Captcha.
Definition Captcha.php:23
validate()
validates boolean input element value
Definition Captcha.php:113
setValue(mixed $newValue)
set the boolean element value
Definition Captcha.php:83
__construct(string $name, array $parameters, object $form)
__construct
Definition Captcha.php:61
__toString()
Renders element to HTML.
Definition Captcha.php:159
setSessionSlot(&$sessionSlot)
setSessionSlot
Definition Captcha.php:99
setDefaults()
collects initial values across subclasses.
Definition Captcha.php:43
testPhrase(mixed $value)
testPhrase
Definition Captcha.php:129
HTML text input type.
Definition Text.php:40
htmlInputAttributes()
renders text element specific HTML attributes
Definition Text.php:160
htmlList($options=null)
Renders HTML datalist.
Definition Text.php:130