web/lib/Zend/Form/Element/Captcha.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Form
    16  * @package    Zend_Form
    17  * @subpackage Element
    17  * @subpackage Element
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Captcha.php 22328 2010-05-30 15:09:06Z bittarman $
    20  * @version    $Id: Captcha.php 24848 2012-05-31 19:28:48Z rob $
    21  */
    21  */
    22 
    22 
    23 /** @see Zend_Form_Element_Xhtml */
    23 /** @see Zend_Form_Element_Xhtml */
    24 require_once 'Zend/Form/Element/Xhtml.php';
    24 require_once 'Zend/Form/Element/Xhtml.php';
    25 
    25 
    36  * @see http://en.wikipedia.org/wiki/Captcha
    36  * @see http://en.wikipedia.org/wiki/Captcha
    37  *
    37  *
    38  * @category   Zend
    38  * @category   Zend
    39  * @package    Zend_Form
    39  * @package    Zend_Form
    40  * @subpackage Element
    40  * @subpackage Element
    41  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    41  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    43  */
    43  */
    44 class Zend_Form_Element_Captcha extends Zend_Form_Element_Xhtml
    44 class Zend_Form_Element_Captcha extends Zend_Form_Element_Xhtml
    45 {
    45 {
    46     /**
    46     /**
   125              ->setAutoInsertNotEmptyValidator(false)
   125              ->setAutoInsertNotEmptyValidator(false)
   126              ->addValidator($this->getCaptcha(), true);
   126              ->addValidator($this->getCaptcha(), true);
   127     }
   127     }
   128 
   128 
   129     /**
   129     /**
   130      * Return all attributes
       
   131      *
       
   132      * @return array
       
   133      */
       
   134     public function getAttribs()
       
   135     {
       
   136         $attribs = get_object_vars($this);
       
   137         unset($attribs['helper']);
       
   138         foreach ($attribs as $key => $value) {
       
   139             if ('_' == substr($key, 0, 1)) {
       
   140                 unset($attribs[$key]);
       
   141             }
       
   142         }
       
   143 
       
   144         return $attribs;
       
   145     }
       
   146 
       
   147     /**
       
   148      * Set options
   130      * Set options
   149      *
   131      *
   150      * Overrides to allow passing captcha options
   132      * Overrides to allow passing captcha options
   151      *
   133      *
   152      * @param  array $options
   134      * @param  array $options
   153      * @return Zend_Form_Element_Captcha
   135      * @return Zend_Form_Element_Captcha
   154      */
   136      */
   155     public function setOptions(array $options)
   137     public function setOptions(array $options)
   156     {
   138     {
       
   139         $captcha        = null;
       
   140         $captchaOptions = array();
       
   141 
   157         if (array_key_exists('captcha', $options)) {
   142         if (array_key_exists('captcha', $options)) {
       
   143             $captcha = $options['captcha'];
   158             if (array_key_exists('captchaOptions', $options)) {
   144             if (array_key_exists('captchaOptions', $options)) {
   159                 $this->setCaptcha($options['captcha'], $options['captchaOptions']);
   145                 $captchaOptions = $options['captchaOptions'];
   160                 unset($options['captchaOptions']);
   146                 unset($options['captchaOptions']);
   161             } else {
       
   162                 $this->setCaptcha($options['captcha']);
       
   163             }
   147             }
   164             unset($options['captcha']);
   148             unset($options['captcha']);
   165         }
   149         }
   166         parent::setOptions($options);
   150         parent::setOptions($options);
       
   151 
       
   152         if(null !== $captcha) {
       
   153             $this->setCaptcha($captcha, $captchaOptions);
       
   154         }
   167         return $this;
   155         return $this;
   168     }
   156     }
   169 
   157 
   170     /**
   158     /**
   171      * Render form element
   159      * Render form element
   176     public function render(Zend_View_Interface $view = null)
   164     public function render(Zend_View_Interface $view = null)
   177     {
   165     {
   178         $captcha    = $this->getCaptcha();
   166         $captcha    = $this->getCaptcha();
   179         $captcha->setName($this->getFullyQualifiedName());
   167         $captcha->setName($this->getFullyQualifiedName());
   180 
   168 
   181         $decorators = $this->getDecorators();
   169         if (!$this->loadDefaultDecoratorsIsDisabled()) {
   182 
   170             $decorators = $this->getDecorators();
   183         $decorator  = $captcha->getDecorator();
   171             $decorator  = $captcha->getDecorator();
   184         if (!empty($decorator)) {
   172             $key        = get_class($this->_getDecorator($decorator, null));
   185             array_unshift($decorators, $decorator);
   173 
   186         }
   174             if (!empty($decorator) && !array_key_exists($key, $decorators)) {
   187 
   175                 array_unshift($decorators, $decorator);
   188         $decorator = array('Captcha', array('captcha' => $captcha));
   176             }
   189         array_unshift($decorators, $decorator);
   177 
   190 
   178             $decorator = array('Captcha', array('captcha' => $captcha));
   191         $this->setDecorators($decorators);
   179             $key       = get_class($this->_getDecorator($decorator[0], $decorator[1]));
       
   180 
       
   181             if ($captcha instanceof Zend_Captcha_Word && !array_key_exists($key, $decorators)) {
       
   182                 array_unshift($decorators, $decorator);
       
   183             }
       
   184 
       
   185             $this->setDecorators($decorators);
       
   186         }
   192 
   187 
   193         $this->setValue($this->getCaptcha()->generate());
   188         $this->setValue($this->getCaptcha()->generate());
   194 
   189 
   195         return parent::render($view);
   190         return parent::render($view);
   196     }
   191     }
   235     {
   230     {
   236         $type = strtoupper($type);
   231         $type = strtoupper($type);
   237         switch ($type) {
   232         switch ($type) {
   238             case null:
   233             case null:
   239                 $loader = $this->getPluginLoader(self::CAPTCHA);
   234                 $loader = $this->getPluginLoader(self::CAPTCHA);
   240                 $cPrefix = rtrim($prefix, '_') . '_Captcha';
   235                 $nsSeparator = (false !== strpos($prefix, '\\'))?'\\':'_';
       
   236                 $cPrefix = rtrim($prefix, $nsSeparator) . $nsSeparator . 'Captcha';
   241                 $cPath   = rtrim($path, '/\\') . '/Captcha';
   237                 $cPath   = rtrim($path, '/\\') . '/Captcha';
   242                 $loader->addPrefixPath($cPrefix, $cPath);
   238                 $loader->addPrefixPath($cPrefix, $cPath);
   243                 return parent::addPrefixPath($prefix, $path);
   239                 return parent::addPrefixPath($prefix, $path);
   244             case self::CAPTCHA:
   240             case self::CAPTCHA:
   245                 $loader = $this->getPluginLoader($type);
   241                 $loader = $this->getPluginLoader($type);
   251     }
   247     }
   252 
   248 
   253     /**
   249     /**
   254      * Load default decorators
   250      * Load default decorators
   255      *
   251      *
   256      * @return void
   252      * @return Zend_Form_Element_Captcha
   257      */
   253      */
   258     public function loadDefaultDecorators()
   254     public function loadDefaultDecorators()
   259     {
   255     {
   260         if ($this->loadDefaultDecoratorsIsDisabled()) {
   256         if ($this->loadDefaultDecoratorsIsDisabled()) {
   261             return $this;
   257             return $this;