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
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();
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>"
193 . "</p>\n";
194 }
195}
196
197// 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:118
setValue(mixed $newValue)
set the boolean element value
Definition Captcha.php:88
__construct(string $name, array $parameters, object $form)
__construct
Definition Captcha.php:66
__toString()
Renders element to HTML.
Definition Captcha.php:164
setSessionSlot(&$sessionSlot)
setSessionSlot
Definition Captcha.php:104
setDefaults()
collects initial values across subclasses.
Definition Captcha.php:48
testPhrase(mixed $value)
testPhrase
Definition Captcha.php:134
HTML text input type.
Definition Text.php:40
htmlInputAttributes()
renders text element specific HTML attributes
Definition Text.php:160
htmlList(?array $options=null, array|string|null $value=null)
Renders HTML datalist.
Definition Text.php:130