Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2 into zf11884
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Kellner committed Nov 19, 2011
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function generate();
/**
* Display the captcha
*
* @param Zend_View_Interface $view
* @param \Zend\View\Renderer $view
* @param mixed $element
* @return string
*/
Expand All @@ -72,7 +72,7 @@ public function getName();
/**
* Get optional private decorator for this captcha type
*
* @return \Zend_Form_Decorator_Interface|string
* @return \Zend\Form\Decorator|string
*/
public function getDecorator();
}
2 changes: 1 addition & 1 deletion src/Dumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Dumb extends Word
/**
* Render the captcha
*
* @param Zend_View_Interface $view
* @param \Zend\View\Renderer $view
* @param mixed $element
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Figlet.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function generate()
/**
* Display the captcha
*
* @param Zend_View_Interface $view
* @param \Zend\View\Renderer $view
* @param mixed $element
* @return string
*/
Expand Down
56 changes: 33 additions & 23 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
* @namespace
*/
namespace Zend\Captcha;
use Zend\Captcha\Exception\NoFontProvidedException,
Zend\Captcha\Exception\ExtensionNotLoadedException,
Zend\Captcha\Exception\ImageNotLoadableException;

use Zend\Captcha\Exception,
Zend\Loader\Pluggable,
Zend\View\Renderer;

/**
* Image-based captcha element
*
* Generates image displaying random word
*
* @uses \Zend\Captcha\Exception
* @uses \Zend\Captcha\Word
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
Expand Down Expand Up @@ -145,15 +144,15 @@ class Image extends Word
public function __construct($options = null)
{
if (!extension_loaded("gd")) {
throw new ExtensionNotLoadedException("Image CAPTCHA requires GD extension");
throw new Exception\ExtensionNotLoadedException("Image CAPTCHA requires GD extension");
}

if (!function_exists("imagepng")) {
throw new ExtensionNotLoadedException("Image CAPTCHA requires PNG support");
throw new Exception\ExtensionNotLoadedException("Image CAPTCHA requires PNG support");
}

if (!function_exists("imageftbbox")) {
throw new ExtensionNotLoadedException("Image CAPTCHA requires FT fonts support");
throw new Exception\ExtensionNotLoadedException("Image CAPTCHA requires FT fonts support");
}

parent::__construct($options);
Expand Down Expand Up @@ -312,7 +311,7 @@ public function setLineNoiseLevel ($lineNoiseLevel)
* Set captcha expiration
*
* @param int $expiration
* @return \Zend\Captcha\Image
* @return Image
*/
public function setExpiration($expiration)
{
Expand All @@ -324,7 +323,7 @@ public function setExpiration($expiration)
* Set garbage collection frequency
*
* @param int $gcFreq
* @return \Zend\Captcha\Image
* @return Image
*/
public function setGcFreq($gcFreq)
{
Expand All @@ -336,7 +335,7 @@ public function setGcFreq($gcFreq)
* Set captcha font
*
* @param string $font
* @return \Zend\Captcha\Image
* @return Image
*/
public function setFont($font)
{
Expand All @@ -348,7 +347,7 @@ public function setFont($font)
* Set captcha font size
*
* @param int $fsize
* @return \Zend\Captcha\Image
* @return Image
*/
public function setFontSize($fsize)
{
Expand All @@ -360,7 +359,7 @@ public function setFontSize($fsize)
* Set captcha image height
*
* @param int $height
* @return \Zend\Captcha\Image
* @return Image
*/
public function setHeight($height)
{
Expand All @@ -372,7 +371,7 @@ public function setHeight($height)
* Set captcha image storage directory
*
* @param string $imgDir
* @return \Zend\Captcha\Image
* @return Image
*/
public function setImgDir($imgDir)
{
Expand All @@ -384,7 +383,7 @@ public function setImgDir($imgDir)
* Set captcha image base URL
*
* @param string $imgUrl
* @return \Zend\Captcha\Image
* @return Image
*/
public function setImgUrl($imgUrl)
{
Expand All @@ -404,7 +403,7 @@ public function setImgAlt ($imgAlt)
* Set captch image filename suffix
*
* @param string $suffix
* @return \Zend\Captcha\Image
* @return Image
*/
public function setSuffix($suffix)
{
Expand All @@ -416,7 +415,7 @@ public function setSuffix($suffix)
* Set captcha image width
*
* @param int $width
* @return \Zend\Captcha\Image
* @return Image
*/
public function setWidth($width)
{
Expand Down Expand Up @@ -491,7 +490,7 @@ protected function _generateImage($id, $word)
$font = $this->getFont();

if (empty($font)) {
throw new NoFontProvidedException("Image CAPTCHA requires font");
throw new Exception\NoFontProvidedException("Image CAPTCHA requires font");
}

$w = $this->getWidth();
Expand All @@ -505,7 +504,7 @@ protected function _generateImage($id, $word)
// Potential error is change to exception
$img = @imagecreatefrompng($this->_startImage);
if(!$img) {
throw new ImageNotLoadableException("Can not load start image");
throw new Exception\ImageNotLoadableException("Can not load start image");
}
$w = imagesx($img);
$h = imagesy($img);
Expand Down Expand Up @@ -620,13 +619,24 @@ protected function _gc()
/**
* Display the captcha
*
* @param Zend_View_Interface $view
* @param Renderer $view
* @param mixed $element
* @return string
*/
public function render(\Zend\View\Renderer $view = null, $element = null)
public function render(Renderer $view = null, $element = null)
{
return '<img width="' . $this->getWidth() . '" height="' . $this->getHeight() . '" alt="' . $this->getImgAlt()
. '" src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '" />';
$endTag = ' />';
if ($view instanceof Pluggable) {
$doctype = $view->plugin('doctype');
if ($doctype && !$doctype->isXhtml()) {
$endTag = '>';
}
}

return '<img width="' . $this->getWidth()
. '" height="' . $this->getHeight()
. '" alt="' . $this->getImgAlt()
. '" src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '"'
. $endTag;
}
}
57 changes: 35 additions & 22 deletions src/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
*/
namespace Zend\Captcha;

use Zend\Service\ReCaptcha\ReCaptcha as ReCaptchaService;
use Traversable,
Zend\Form\Element,
Zend\Service\ReCaptcha\ReCaptcha as ReCaptchaService,
Zend\View\Renderer;

/**
* ReCaptcha adapter
Expand All @@ -33,8 +36,6 @@
*
* @see http://recaptcha.net/apidocs/captcha/
*
* @uses Zend\Captcha\AbstractAdapter
* @uses Zend\Service\ReCaptcha\ReCaptcha
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
Expand Down Expand Up @@ -114,7 +115,7 @@ public function getPubkey()
* Set ReCaptcha Private key
*
* @param string $privkey
* @return \Zend\Captcha\ReCaptcha
* @return ReCaptcha
*/
public function setPrivkey($privkey)
{
Expand All @@ -126,7 +127,7 @@ public function setPrivkey($privkey)
* Set ReCaptcha public key
*
* @param string $pubkey
* @return \Zend\Captcha\ReCaptcha
* @return ReCaptcha
*/
public function setPubkey($pubkey)
{
Expand All @@ -137,7 +138,7 @@ public function setPubkey($pubkey)
/**
* Constructor
*
* @param array|\Zend\Config\Config $options
* @param array|Config $options
* @return void
*/
public function __construct($options = null)
Expand All @@ -148,9 +149,6 @@ public function __construct($options = null)

parent::__construct($options);

if ($options instanceof \Zend\Config\Config) {
$options = $options->toArray();
}
if (!empty($options)) {
$this->setOptions($options);
}
Expand All @@ -159,8 +157,8 @@ public function __construct($options = null)
/**
* Set service object
*
* @param Zend\Service\ReCaptcha\ReCaptcha $service
* @return Zend\Captcha\ReCaptcha
* @param ReCaptchaService $service
* @return ReCaptcha
*/
public function setService(ReCaptchaService $service)
{
Expand All @@ -171,7 +169,7 @@ public function setService(ReCaptchaService $service)
/**
* Retrieve ReCaptcha service object
*
* @return Zend\Service\ReCaptcha\ReCaptcha
* @return ReCaptchaService
*/
public function getService()
{
Expand All @@ -186,7 +184,7 @@ public function getService()
*
* @param string $key
* @param mixed $value
* @return \Zend\Captcha\ReCaptcha
* @return ReCaptcha
*/
public function setOption($key, $value)
{
Expand All @@ -205,7 +203,7 @@ public function setOption($key, $value)
/**
* Generate captcha
*
* @see Zend_Form_Captcha_Adapter::generate()
* @see AbstractAdapter::generate()
* @return string
*/
public function generate()
Expand All @@ -216,14 +214,14 @@ public function generate()
/**
* Validate captcha
*
* @see Zend\Validator::isValid()
* @see \Zend\Validator\Validator::isValid()
* @param mixed $value
* @return boolean
*/
public function isValid($value, $context = null)
{
if (!is_array($value) && !is_array($context)) {
$this->_error(self::MISSING_VALUE);
$this->error(self::MISSING_VALUE);
return false;
}

Expand All @@ -232,7 +230,7 @@ public function isValid($value, $context = null)
}

if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) {
$this->_error(self::MISSING_VALUE);
$this->error(self::MISSING_VALUE);
return false;
}

Expand All @@ -241,12 +239,12 @@ public function isValid($value, $context = null)
$res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]);

if (!$res) {
$this->_error(self::ERR_CAPTCHA);
$this->error(self::ERR_CAPTCHA);
return false;
}

if (!$res->isValid()) {
$this->_error(self::BAD_CAPTCHA, $res->getErrorCode());
$this->error(self::BAD_CAPTCHA, $res->getErrorCode());
$service->setParam('error', $res->getErrorCode());
return false;
}
Expand All @@ -257,12 +255,27 @@ public function isValid($value, $context = null)
/**
* Render captcha
*
* @param Zend_View_Interface $view
* @param Renderer $view
* @param mixed $element
* @return string
*/
public function render(\Zend\View\Renderer $view = null, $element = null)
public function render(Renderer $view = null, $element = null)
{
$name = null;
if ($element instanceof Element) {
$name = $element->getBelongsTo();
}

return $this->getService()->getHTML($name);
}

/**
* Get captcha decorator
*
* @return string
*/
public function getDecorator()
{
return $this->getService()->getHTML();
return "Captcha\ReCaptcha";
}
}
Loading

0 comments on commit e59b115

Please sign in to comment.