Version: current

      1 <?php
      2 /**
      3  * Provides an {@link FormField} which allows form to validate for non-bot submissions
      4  * by giving them a challenge to decrypt an image.
      5  *
      6  * @module phpcaptcha
      7  */
      8 class PhpCaptchaField extends SpamProtectorField {
      9
     10     /**
     11      * Use this singelton to applay you custon configured PhpCaptcha class.
     12      * If this will be left unset the fields constructor will initialise
     13      * it with a defaul PhpCaptcha class.
     14      *
     15      * @var PhpCaptcha
     16      */
     17     public static $prototype;
     18
     19     public function __construct($name = "PhpCaptchaField", $title = "Captcha",
     20         $value = null, $form = null, $rightTitle = null) {
     21         parent::__construct($name, $title, $value, $form, $rightTitle);
     22         require_once Director::baseFolder().'/phpcaptcha/code/php-captcha.inc.php';
     23
     24         if(!PhpCaptchaField::$prototype) {
     25             $f = array(
     26                 Director::baseFolder().'/phpcaptcha/fonts/VeraIt.ttf',
     27                 Director::baseFolder().'/phpcaptcha/fonts/VeraMono.ttf',
     28                 Director::baseFolder().'/phpcaptcha/fonts/VeraSe.ttf',
     29                 Director::baseFolder().'/phpcaptcha/fonts/VeraSeBd.ttf'
     30             );
     31             $p = new PhpCaptcha($f, 140, 40);
     32             $p->SetBackgroundImages(
     33                 Director::baseFolder()."/phpcaptcha/images/bg1.jpeg");
     34             $p->UseColour(true);
     35             $p->SetCharSet('0-9');
     36             PhpCaptchaField::$prototype = $p;
     37         }
     38     }
     39
     40     public function Field() {
     41         $attributes = array(
     42             'type' => 'text',
     43             'class' => 'text' . ($this->extraClass() ? $this->extraClass() : ''),
     44             'id' => $this->id(),
     45             'name' => $this->Name(),
     46             'value' => $this->Value(),
     47             'tabindex' => $this->getTabIndex(),
     48             'maxlength' => ($this->maxLength) ? $this->maxLength : null,
     49             'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null
     50         );
     51
     52         if($this->disabled) $attributes['disabled'] = 'disabled';
     53
     54 //TODO: использовать шаблон, сделать кнопку "обновить изображение"
     55         return '<img src="'.Director::baseURL().
     56             'PhpCaptchaField/genimage?t=' . time() . '" alt="" /><br />'.
     57             $this->createTag('input', $attributes);
     58     }
     59
     60     /**
     61      * Needed do satisfy the request handler
     62      */
     63     public function setSession($session) {
     64
     65     }
     66
     67     /**
     68      * Validate the PhpCaptcha code
     69      *
     70      * @param Validator $validator
     71      * @return boolean
     72      */
     73     public function validate($validator) {
     74
     75         if(!PhpCaptcha::Validate(@$_REQUEST[$this->name])) {
     76             if ($validator) {
     77             $validator->validationError(
     78                 $this->name,
     79                 _t(
     80                     'PhpCaptchaField.WRONG',
     81                     "The text you entered doesn't match the captcha. Please try again"
     82                 ),
     83                 "validation",
     84                 false
     85             );
     86             }
     87             return false;
     88         }
     89         else {
     90             return true;
     91         }
     92     }
     93
     94     /**
     95      * This action is requested by the field to generate the captcha image
     96      * and stream it straight to the reqests response
     97      */
     98     public function genimage() {
     99         HTTP::set_cache_age(0);
    100         HTTP::add_cache_headers();
    101         header('Last-Modified: ' . HTTP::gmt_date(time()));
    102         PhpCaptchaField::$prototype->Create();
    103     }
    104 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -