PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Form/Element/Captcha.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 302 lines | 176 code | 22 blank | 104 comment | 23 complexity | 0752ad99fb0f43536c91367c70a494df MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage Element
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** @see Zend_Form_Element_Xhtml */
  23. #require_once 'Zend/Form/Element/Xhtml.php';
  24. /** @see Zend_Captcha_Adapter */
  25. #require_once 'Zend/Captcha/Adapter.php';
  26. /**
  27. * Generic captcha element
  28. *
  29. * This element allows to insert CAPTCHA into the form in order
  30. * to validate that human is submitting the form. The actual
  31. * logic is contained in the captcha adapter.
  32. *
  33. * @see http://en.wikipedia.org/wiki/Captcha
  34. *
  35. * @category Zend
  36. * @package Zend_Form
  37. * @subpackage Element
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Form_Element_Captcha extends Zend_Form_Element_Xhtml
  42. {
  43. /**
  44. * Captcha plugin type constant
  45. */
  46. const CAPTCHA = 'CAPTCHA';
  47. /**
  48. * Captcha adapter
  49. *
  50. * @var Zend_Captcha_Adapter
  51. */
  52. protected $_captcha;
  53. /**
  54. * Get captcha adapter
  55. *
  56. * @return Zend_Captcha_Adapter
  57. */
  58. public function getCaptcha()
  59. {
  60. return $this->_captcha;
  61. }
  62. /**
  63. * Set captcha adapter
  64. *
  65. * @param string|array|Zend_Captcha_Adapter $captcha
  66. * @param array $options
  67. */
  68. public function setCaptcha($captcha, $options = array())
  69. {
  70. if ($captcha instanceof Zend_Captcha_Adapter) {
  71. $instance = $captcha;
  72. } else {
  73. if (is_array($captcha)) {
  74. if (array_key_exists('captcha', $captcha)) {
  75. $name = $captcha['captcha'];
  76. unset($captcha['captcha']);
  77. } else {
  78. $name = array_shift($captcha);
  79. }
  80. $options = array_merge($options, $captcha);
  81. } else {
  82. $name = $captcha;
  83. }
  84. $name = $this->getPluginLoader(self::CAPTCHA)->load($name);
  85. if (empty($options)) {
  86. $instance = new $name;
  87. } else {
  88. $r = new ReflectionClass($name);
  89. if ($r->hasMethod('__construct')) {
  90. $instance = $r->newInstanceArgs(array($options));
  91. } else {
  92. $instance = $r->newInstance();
  93. }
  94. }
  95. }
  96. $this->_captcha = $instance;
  97. $this->_captcha->setName($this->getName());
  98. return $this;
  99. }
  100. /**
  101. * Constructor
  102. *
  103. * $spec may be:
  104. * - string: name of element
  105. * - array: options with which to configure element
  106. * - Zend_Config: Zend_Config with options for configuring element
  107. *
  108. * @param string|array|Zend_Config $spec
  109. * @return void
  110. */
  111. public function __construct($spec, $options = null)
  112. {
  113. parent::__construct($spec, $options);
  114. $this->setAllowEmpty(true)
  115. ->setRequired(true)
  116. ->setAutoInsertNotEmptyValidator(false)
  117. ->addValidator($this->getCaptcha(), true);
  118. }
  119. /**
  120. * Set options
  121. *
  122. * Overrides to allow passing captcha options
  123. *
  124. * @param array $options
  125. * @return Zend_Form_Element_Captcha
  126. */
  127. public function setOptions(array $options)
  128. {
  129. $captcha = null;
  130. $captchaOptions = array();
  131. if (array_key_exists('captcha', $options)) {
  132. $captcha = $options['captcha'];
  133. if (array_key_exists('captchaOptions', $options)) {
  134. $captchaOptions = $options['captchaOptions'];
  135. unset($options['captchaOptions']);
  136. }
  137. unset($options['captcha']);
  138. }
  139. parent::setOptions($options);
  140. if(null !== $captcha) {
  141. $this->setCaptcha($captcha, $captchaOptions);
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Render form element
  147. *
  148. * @param Zend_View_Interface $view
  149. * @return string
  150. */
  151. public function render(Zend_View_Interface $view = null)
  152. {
  153. $captcha = $this->getCaptcha();
  154. $captcha->setName($this->getFullyQualifiedName());
  155. if (!$this->loadDefaultDecoratorsIsDisabled()) {
  156. $decorators = $this->getDecorators();
  157. $decorator = $captcha->getDecorator();
  158. $key = get_class($this->_getDecorator($decorator, null));
  159. if (!empty($decorator) && !array_key_exists($key, $decorators)) {
  160. array_unshift($decorators, $decorator);
  161. }
  162. $decorator = array('Captcha', array('captcha' => $captcha));
  163. $key = get_class($this->_getDecorator($decorator[0], $decorator[1]));
  164. if ($captcha instanceof Zend_Captcha_Word && !array_key_exists($key, $decorators)) {
  165. array_unshift($decorators, $decorator);
  166. }
  167. $this->setDecorators($decorators);
  168. }
  169. $this->setValue($this->getCaptcha()->generate());
  170. return parent::render($view);
  171. }
  172. /**
  173. * Retrieve plugin loader for validator or filter chain
  174. *
  175. * Support for plugin loader for Captcha adapters
  176. *
  177. * @param string $type
  178. * @return Zend_Loader_PluginLoader
  179. * @throws Zend_Loader_Exception on invalid type.
  180. */
  181. public function getPluginLoader($type)
  182. {
  183. $type = strtoupper($type);
  184. if ($type == self::CAPTCHA) {
  185. if (!isset($this->_loaders[$type])) {
  186. #require_once 'Zend/Loader/PluginLoader.php';
  187. $this->_loaders[$type] = new Zend_Loader_PluginLoader(
  188. array('Zend_Captcha' => 'Zend/Captcha/')
  189. );
  190. }
  191. return $this->_loaders[$type];
  192. } else {
  193. return parent::getPluginLoader($type);
  194. }
  195. }
  196. /**
  197. * Add prefix path for plugin loader for captcha adapters
  198. *
  199. * This method handles the captcha type, the rest is handled by
  200. * the parent
  201. * @param string $prefix
  202. * @param string $path
  203. * @param string $type
  204. * @return Zend_Form_Element
  205. * @see Zend_Form_Element::addPrefixPath
  206. */
  207. public function addPrefixPath($prefix, $path, $type = null)
  208. {
  209. $type = strtoupper($type);
  210. switch ($type) {
  211. case null:
  212. $loader = $this->getPluginLoader(self::CAPTCHA);
  213. $nsSeparator = (false !== strpos($prefix, '\\'))?'\\':'_';
  214. $cPrefix = rtrim($prefix, $nsSeparator) . $nsSeparator . 'Captcha';
  215. $cPath = rtrim($path, '/\\') . '/Captcha';
  216. $loader->addPrefixPath($cPrefix, $cPath);
  217. return parent::addPrefixPath($prefix, $path);
  218. case self::CAPTCHA:
  219. $loader = $this->getPluginLoader($type);
  220. $loader->addPrefixPath($prefix, $path);
  221. return $this;
  222. default:
  223. return parent::addPrefixPath($prefix, $path, $type);
  224. }
  225. }
  226. /**
  227. * Load default decorators
  228. *
  229. * @return Zend_Form_Element_Captcha
  230. */
  231. public function loadDefaultDecorators()
  232. {
  233. if ($this->loadDefaultDecoratorsIsDisabled()) {
  234. return $this;
  235. }
  236. $decorators = $this->getDecorators();
  237. if (empty($decorators)) {
  238. $this->addDecorator('Errors')
  239. ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
  240. ->addDecorator('HtmlTag', array('tag' => 'dd', 'id' => $this->getName() . '-element'))
  241. ->addDecorator('Label', array('tag' => 'dt'));
  242. }
  243. return $this;
  244. }
  245. /**
  246. * Is the captcha valid?
  247. *
  248. * @param mixed $value
  249. * @param mixed $context
  250. * @return boolean
  251. */
  252. public function isValid($value, $context = null)
  253. {
  254. $this->getCaptcha()->setName($this->getName());
  255. $belongsTo = $this->getBelongsTo();
  256. if (empty($belongsTo) || !is_array($context)) {
  257. return parent::isValid($value, $context);
  258. }
  259. $name = $this->getFullyQualifiedName();
  260. $root = substr($name, 0, strpos($name, '['));
  261. $segments = substr($name, strpos($name, '['));
  262. $segments = ltrim($segments, '[');
  263. $segments = rtrim($segments, ']');
  264. $segments = explode('][', $segments);
  265. array_unshift($segments, $root);
  266. array_pop($segments);
  267. $newContext = $context;
  268. foreach ($segments as $segment) {
  269. if (array_key_exists($segment, $newContext)) {
  270. $newContext = $newContext[$segment];
  271. }
  272. }
  273. return parent::isValid($value, $newContext);
  274. }
  275. }