Captcha.php
Go to the documentation of this file.
1<?php
13
14use Gregwar\Captcha\CaptchaBuilder;
15use Gregwar\Captcha\PhraseBuilder;
16
21class Captcha extends Text
22{
26 protected $captcha = null;
27
31 protected $sessionSlot = null;
32
42 protected function setDefaults()
43 {
44 parent::setDefaults();
45 $this->defaults['defaultValue'] = false;
46 $this->defaults['errorMessage'] = _('Please fill in the correct phrase.');
47 $this->defaults['textColor'] = false;
48 $this->defaults['backgroundColor'] = false;
49 $this->defaults['width'] = 250;
50 $this->defaults['height'] = 100;
51 }
60 public function __construct($name, $parameters, $form)
61 {
62 $this->captcha = new CaptchaBuilder();
63
64 parent::__construct($name, $parameters, $form);
65
66 if (is_array($this->textColor)) {
67 $this->captcha->setTextColor($this->textColor[0], $this->textColor[1], $this->textColor[2]);
68 }
69 if (is_array($this->backgroundColor)) {
70 $this->captcha->setBackgroundColor($this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]);
71 }
72 }
82 public function setValue($newValue)
83 {
84 if (is_bool($newValue)) {
85 $this->value = $newValue;
86 } else {
87 $this->value = $newValue;
88 }
89
90 return $this->value;
91 }
98 public function setSessionSlot(&$sessionSlot)
99 {
100 $this->sessionSlot = &$sessionSlot;
101
102 }
112 public function validate()
113 {
114 if (!$this->validated) {
115 $this->validated = true;
116
117 $this->valid = $this->value === true || $this->testPhrase($this->value);
118 }
119
120 return $this->valid;
121 }
128 protected function testPhrase($value)
129 {
130 $this->captcha->setPhrase($this->sessionSlot['formCaptcha'] ?? "");
131
132 if (!$this->captcha->testPhrase($value)) {
133 $this->getNewPhrase();
134
135 return false;
136 }
137
138 return true;
139 }
145 protected function getNewPhrase()
146 {
147 $phraseBuilder = new PhraseBuilder();
148 $phrase = $phraseBuilder->build();
149
150 $this->captcha->setPhrase($phrase);
151 $this->sessionSlot['formCaptcha'] = $phrase;
152 }
158 public function __toString()
159 {
160 $value = $this->htmlValue();
161 $type = strtolower($this->type);
162 $inputAttributes = $this->htmlInputAttributes();
163 $marker = $this->htmlMarker();
164 $label = $this->htmlLabel();
165 $list = $this->htmlList();
166 $wrapperAttributes = $this->htmlWrapperAttributes();
168 $helpMessage = $this->htmlHelpMessage();
169
170 if (empty($this->sessionSlot['formCaptcha'])) {
171 $this->getNewPhrase();
172 }
173
174 $this->captcha->build($this->width, $this->height);
175
176 return "<p {$wrapperAttributes}>" .
177 "<label>" .
178 "<span class=\"depage-label\">{$label}{$marker}</span>" .
179 "<span class=\"captcha-img\">" .
180 "<img src=\"" . $this->captcha->inline() . "\" />" .
181 "</span>" .
182 "<input name=\"{$this->name}\" type=\"{$type}\"{$inputAttributes} value=\"$value\">" .
183 $list .
184 "</label>" .
187 "</p>\n";
188 }
189}
190
191
192
193// vim:set ft=php sw=4 sts=4 fdm=marker et :
$valid
Contains element validation status/result.
Definition Element.php:30
$value
Input elements's value.
Definition Input.php:123
$label
Input element - HTML label.
Definition Input.php:36
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:217
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:676
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:627
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:565
$type
Input element type - HTML input type attribute.
Definition Input.php:25
$helpMessage
Extra help message.
Definition Input.php:229
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:647
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:91
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:657
Captcha Class Captcha.
Definition Captcha.php:22
testPhrase($value)
testPhrase
Definition Captcha.php:128
validate()
validates boolean input element value
Definition Captcha.php:112
__construct($name, $parameters, $form)
__construct
Definition Captcha.php:60
__toString()
Renders element to HTML.
Definition Captcha.php:158
setSessionSlot(&$sessionSlot)
setSessionSlot
Definition Captcha.php:98
setValue($newValue)
set the boolean element value
Definition Captcha.php:82
setDefaults()
collects initial values across subclasses.
Definition Captcha.php:42
HTML text input type.
Definition Text.php:39
htmlInputAttributes()
renders text element specific HTML attributes
Definition Text.php:159
htmlList($options=null)
Renders HTML datalist.
Definition Text.php:129
Classes for HTML input-elements.
Definition Address.php:9