/** * {@inheritDoc} */ public function render(ElementInterface $element, $options = array()) { $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new \DomainException(sprintf('%s requires that the element has a "captcha" attribute' . ' implementing Zend\\Captcha\\AdapterInterface; none found', __METHOD__)); } $helper = $captcha->getHelperName(); $renderer = $this->getView(); if (!method_exists($renderer, 'plugin')) { throw new \DomainException(sprintf('%s requires that the renderer implements plugin();' . ' it does not', __METHOD__)); } $helper = $renderer->plugin($helper); // Custom CAPTCHA view $separator = null; if (isset($options['separator'])) { $separator = $options['separator']; } elseif ($element->getOption('separator')) { $separator = $element->getOption('separator'); } if (null !== $separator) { $helper->setSeparator($separator); } $position = null; if (isset($options['captcha_position'])) { $position = $options['captcha_position']; } elseif ($element->getOption('captcha_position')) { $position = $element->getOption('captcha_position'); } if (null !== $position) { $helper->setCaptchaPosition($position); } return $helper($element); }
/** * Override * * Render the captcha * * @param ElementInterface $element * @throws Exception\DomainException * @return string */ public function render(ElementInterface $element) { //we could also set the separator here to break between the inputs and the image. //$this->setSeparator('') $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new DomainException(sprintf('%s requires that the element has a "captcha" attribute of type Recruitment\\Model\\CaptchaImage; none found', __METHOD__)); } $captcha->generate(); $imgAttributes = array('width' => $captcha->getWidth(), 'height' => $captcha->getHeight(), 'alt' => $captcha->getImgAlt(), 'src' => $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix(), 'class' => 'thumbnail center-block'); if ($element->hasAttribute('id')) { $imgAttributes['id'] = $element->getAttribute('id') . '-image'; } $closingBracket = $this->getInlineClosingBracket(); $img = sprintf('<img %s%s ', $this->createAttributesString($imgAttributes), $closingBracket); $position = $this->getCaptchaPosition(); $separator = $this->getSeparator(); $captchaInput = $this->renderCaptchaInputs($element); $pattern = '%s' . '<div class="input-group">' . '%s%s' . '<span class="input-group-btn">' . '<button id="' . $element->getAttribute('id') . '-refresh' . '" class="btn btn-default" type="button">' . '<i class="ion-loop"></i>' . '</button>' . '</span>' . '</div>'; if ($position == self::CAPTCHA_PREPEND) { return sprintf($pattern, $captchaInput, $separator, $img); } return sprintf($pattern, $img, $separator, $captchaInput); }
/** * {@inheritDoc} */ public function render(ElementInterface $element) { $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new Exception\DomainException(sprintf('%s requires that the element has a "captcha" attribute' . ' of type Zend\\Captcha\\Image; none found', __METHOD__)); } // Generates ID, but NOT word and image $captcha->generate(); // Generates URL to access image, and image won't be generated until the URL is accessed $imgSrc = $captcha->getImgUrl() . '?id=' . $captcha->getId(); $imgAttributes = array('width' => $captcha->getWidth(), 'height' => $captcha->getHeight(), 'src' => $imgSrc, 'onclick' => sprintf('this.src=\'%s&refresh=\'+Math.random()', $imgSrc), 'style' => 'cursor: pointer; vertical-align: middle;', 'alt' => __('CAPTCHA image'), 'title' => __('Click to refresh CAPTCHA')); if ($element->hasAttribute('id')) { $imgAttributes['id'] = $element->getAttribute('id') . '-image'; } else { $imgAttributes['id'] = $captcha->getId() . '-image'; } $closingBracket = $this->getInlineClosingBracket(); $img = sprintf('<img %s%s', $this->createAttributesString($imgAttributes), $closingBracket); $position = $this->getCaptchaPosition(); $separator = $this->getSeparator(); $captchaInput = $this->renderCaptchaInputs($element); $pattern = '%s%s%s'; if ($position == static::CAPTCHA_PREPEND) { return sprintf($pattern, $captchaInput, $separator, $img); } return sprintf($pattern, $img, $separator, $captchaInput); }
/** * @param ElementInterface $element * * @return string */ public function render(ElementInterface $element) { /** @var \Zf3Php7Recaptcha\ReCaptcha $captcha */ $captcha = $element->getCaptcha(); $pubkey = $captcha->getPubkey(); $theme = $captcha->getTheme(); $html = '<div class="g-recaptcha" data-sitekey="' . $pubkey . '" data-theme="' . $theme . '"></div>'; //Add the js for the recaptcha api $this->view->headScript()->appendFile('https://www.google.com/recaptcha/api.js'); return $html; }
/** * Render a form captcha for an element * * @param ElementInterface $element * @throws Exception\DomainException if the element does not compose a captcha, or the renderer does not implement plugin() * @return string */ public function render(ElementInterface $element) { $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new Exception\DomainException(sprintf('%s requires that the element has a "captcha" attribute implementing Zend\\Captcha\\AdapterInterface; none found', __METHOD__)); } $helper = $captcha->getHelperName(); $renderer = $this->getView(); if (!method_exists($renderer, 'plugin')) { throw new Exception\DomainException(sprintf('%s requires that the renderer implements plugin(); it does not', __METHOD__)); } $helper = $renderer->plugin($helper); return $helper($element); }
/** * Render captcha form elements for the given element * * Creates and returns: * - Hidden input with captcha identifier (name[id]) * - Text input for entering captcha value (name[input]) * * More specific renderers will consume this and render it. * * @param ElementInterface $element * @throws Exception\DomainException * @return string */ protected function renderCaptchaInputs(ElementInterface $element) { $name = $element->getName(); if ($name === null || $name === '') { throw new Exception\DomainException(sprintf('%s requires that the element has an assigned name; none discovered', __METHOD__)); } $attributes = $element->getAttributes(); $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new Exception\DomainException(sprintf('%s requires that the element has a "captcha" attribute implementing Zend\\Captcha\\AdapterInterface; none found', __METHOD__)); } $hidden = $this->renderCaptchaHidden($captcha, $attributes); $input = $this->renderCaptchaInput($captcha, $attributes); return $hidden . $input; }
/** * @param ElementInterface $element * * @return string */ public function render(ElementInterface $element) { $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof AdapterInterface) { throw new Exception\DomainException(sprintf('%s requires that the element has a "captcha" attribute implementing Zend\\Captcha\\AdapterInterface', __METHOD__)); } $name = $element->getName(); $id = $element->getAttribute('id') ? $element->getAttribute('id') : $name; $captchaPattern = '<div %s></div>'; $captchaAttributes = $this->createAttributesString(array('class' => 'g-recaptcha ' . $element->getAttribute('class'), 'data-sitekey' => $captcha->getSiteKey(), 'data-theme' => $captcha->getTheme(), 'data-type' => $captcha->getType(), 'data-callback' => $captcha->getCallback())); $captchaElement = sprintf($captchaPattern, $captchaAttributes); $input = $this->renderHiddenInput($id, $name); $js = $this->renderJsCallback($captcha->getCallback(), $id); return $captchaElement . $input . $js; }
/** * Render ReCaptcha form elements * * @param ElementInterface $element * @throws Exception\DomainException * @return string */ public function render(ElementInterface $element) { $attributes = $element->getAttributes(); $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new Exception\DomainException(sprintf('%s requires that the element has a "captcha" attribute implementing Zend\\Captcha\\AdapterInterface; none found', __METHOD__)); } $name = $element->getName(); $id = isset($attributes['id']) ? $attributes['id'] : $name; $responseName = empty($name) ? 'recaptcha_response_field' : $name . '[recaptcha_response_field]'; $responseId = $id . '-response'; $markup = $captcha->getService()->getHtml($name); $hidden = $this->renderHiddenInput($responseName, $responseId); $js = $this->renderJsEvents($responseId); return $hidden . $markup . $js; }
/** * Render the captcha * * @param ElementInterface $element * @throws Exception\DomainException * @return string */ public function render(ElementInterface $element) { $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new Exception\DomainException(sprintf('%s requires that the element has a "captcha" attribute of type Zend\\Captcha\\Figlet; none found', __METHOD__)); } $captcha->generate(); $figlet = sprintf('<pre>%s</pre>', $captcha->getFiglet()->render($captcha->getWord())); $position = $this->getCaptchaPosition(); $separator = $this->getSeparator(); $captchaInput = $this->renderCaptchaInputs($element); $pattern = '%s%s%s'; if ($position == self::CAPTCHA_PREPEND) { return sprintf($pattern, $captchaInput, $separator, $figlet); } return sprintf($pattern, $figlet, $separator, $captchaInput); }
/** * Render the captcha * * @param ElementInterface $element * @throws Exception\DomainException * @return string */ public function render(ElementInterface $element) { $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new Exception\DomainException(sprintf('%s requires that the element has a "captcha" attribute of type Zend\\Captcha\\Image; none found', __METHOD__)); } $captcha->generate(); $imgAttributes = array('width' => $captcha->getWidth(), 'height' => $captcha->getHeight(), 'alt' => $captcha->getImgAlt(), 'src' => $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix()); $closingBracket = $this->getInlineClosingBracket(); $img = sprintf('<img %s%s', $this->createAttributesString($imgAttributes), $closingBracket); $position = $this->getCaptchaPosition(); $separator = $this->getSeparator(); $captchaInput = $this->renderCaptchaInputs($element); $pattern = '%s%s%s'; if ($position == self::CAPTCHA_PREPEND) { return sprintf($pattern, $captchaInput, $separator, $img); } return sprintf($pattern, $img, $separator, $captchaInput); }
/** * {@inheritDoc} */ public function render(ElementInterface $element) { $captcha = $element->getCaptcha(); if ($captcha === null || !$captcha instanceof CaptchaAdapter) { throw new Exception\DomainException(sprintf('%s requires that the element has a "captcha" attribute ' . 'implementing %s; none found', __METHOD__, CaptchaAdapter::class)); } $helper = $captcha->getHelperName(); $renderer = $this->getView(); if (!method_exists($renderer, 'plugin')) { throw new Exception\DomainException(sprintf('%s requires that the renderer implements plugin(); it does not', __METHOD__)); } $helper = $renderer->plugin($helper); if ($helper instanceof TranslatorAwareInterface) { $rollbackTextDomain = $helper->getTranslatorTextDomain(); $helper->setTranslatorTextDomain($this->getTranslatorTextDomain()); } $markup = parent::render($element); if ($helper instanceof TranslatorAwareInterface) { $helper->setTranslatorTextDomain($rollbackTextDomain); } return $markup; }