Source of file TextCaptchaField.php
Size: 4,187 Bytes - Last Modified: 2021-12-23T10:51:46+00:00
/var/www/docs.ssmods.com/process/src/code/TextCaptchaField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 | <?php class TextCaptchaField extends FormField { public function Field($properties = array()) { $key = $this->config()->ApiKey; $question = $this->getQuestion(); $attributes = array( 'type' => 'text', 'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''), 'id' => $this->id(), 'name' => $this->getName(), 'value' => '', 'title' => $this->Title(), 'tabindex' => $this->getAttribute('tabindex'), 'maxlength' => ($this->maxLength) ? $this->maxLength : null, 'size' => ($this->maxLength) ? min($this->maxLength, 30) : null ); $html = $this->createTag('input', $attributes); return $html; } public function FieldHolder($properties=array()) { $Title = $this->XML_val('Title'); $Message = $this->XML_val('Message'); $MessageType = $this->XML_val('MessageType'); $Type = $this->XML_val('Type'); $extraClass = $this->XML_val('extraClass'); $Name = $this->XML_val('Name'); $label = $this->getQuestion(); $Field = $this->XML_val('Field'); $messageBlock = (!empty($Message)) ? "<span class=\"message $MessageType\">$Message</span>" : ""; return <<<HTML <div id="$Name" class="field $Type $extraClass"><label for="$Name"><strong>Please answer the following question:</strong> $label</label>{$Field}{$messageBlock}</div> HTML; } public function validate($validator) { $ans = strtolower(trim($this->Value())); $ans = md5($ans); $captcha = Session::get('captcha'); if (in_array($ans, Session::get('captcha'))) { Session::clear('setCaptcha'); Session::clear('question'); Session::clear('captchaFail'); return true; } else { Session::clear('setCaptcha'); Session::clear('question'); Session::set("captchaFail", true); //$this->Message('Captcha', 'Catcha error', 'bad'); $validator->validationError( $this->name, _t( 'Captcha.INCORRECTSOLUTION', "Please check the Captcha field and try again", "Mollom Captcha provides words in an image, and expects a user to type them in a textfield" ), "validation", false ); return false; } } public function getCaptcha() { $key = TextCaptchaField::config()->ApiKey; $url = 'http://api.textcaptcha.com/'.$key; //if(Session::get('setCaptcha') == true && Session::get('question') === null && Session::get('captchaFail') != true) {} try { $xml = @new SimpleXMLElement($url, null, true); } catch (Exception $e) { // if there is a problem, use static fallback.. $fallback = '<captcha>'. '<question>Is ice hot or cold?</question>'. '<answer>'.md5('cold').'</answer></captcha>'; $xml = new SimpleXMLElement($fallback); } // display question as part of form $question = (string) $xml->question; // store answers in session $ans = array(); foreach ($xml->answer as $hash) { $ans[] = (string) $hash; } Session::set('captcha', $ans); Session::set('question', $question); Session::set('setCaptcha', true); Session::clear('captchaFail'); return true; } public function getQuestion() { if (Session::get('setCaptcha') === true && Session::get('captchaFail') != true) { return Session::get('question'); } else { $this->getCaptcha(); return Session::get('question'); } } public static function CaptchaAPI($key = "demo") { $key = TextCaptchaField::config()->ApiKey; } } |