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

Commit

Permalink
Merge branch 'milestones/exceptions' of git://git.zendframework.com/z…
Browse files Browse the repository at this point in the history
…f into serializer-exceptions

Conflicts:

	working/README-exceptions.txt
  • Loading branch information
marc-mabe committed Sep 29, 2010
11 parents cd5dffb + 5d7867c + 1f8bc83 + c5ab2f3 + 7a52da4 + bb2812c + f7972b4 + 0abbe02 + aefc063 + b47bdc4 + 3696366 commit 69b9eae
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
/**
* Exception for Zend_Form component.
*
* @uses \Zend\Exception
* @uses Zend\Exception
* @category Zend
* @package Zend_Captcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Exception extends \Zend\Exception
interface Exception
{
}
41 changes: 41 additions & 0 deletions src/Exception/ExtensionNotLoadedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Captcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id$
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Captcha\Exception;
use Zend\Captcha\Exception;

/**
* Exception for Zend_Form component.
*
* @uses Zend\Exception
* @category Zend
* @package Zend_Captcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class ExtensionNotLoadedException
extends \RuntimeException
implements Exception
{
}
41 changes: 41 additions & 0 deletions src/Exception/ImageNotLoadableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Captcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id$
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Captcha\Exception;
use Zend\Captcha\Exception;

/**
* Exception for Zend_Form component.
*
* @uses Zend\Exception
* @category Zend
* @package Zend_Captcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class ImageNotLoadableException
extends \RuntimeException
implements Exception
{
}
41 changes: 41 additions & 0 deletions src/Exception/NoFontProvidedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Captcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id$
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Captcha\Exception;
use Zend\Captcha\Exception;

/**
* Exception for Zend_Form component.
*
* @uses Zend\Exception
* @category Zend
* @package Zend_Captcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class NoFontProvidedException
extends \InvalidArgumentException
implements Exception
{
}
63 changes: 46 additions & 17 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @namespace
*/
namespace Zend\Captcha;
use Zend\Captcha\Exception\NoFontProvidedException,
Zend\Captcha\Exception\ExtensionNotLoadedException,
Zend\Captcha\Exception\ImageNotLoadableException;

/**
* Image-based captcha element
Expand Down Expand Up @@ -103,6 +106,7 @@ class Image extends Word
* @var string
*/
protected $_startImage;

/**
* How frequently to execute garbage collection
*
Expand All @@ -124,41 +128,70 @@ class Image extends Word
* @var int
*/
protected $_dotNoiseLevel = 100;

/**
* Number of noise lines on image
* Used twice - before and after transform
*
* @var int
*/
protected $_lineNoiseLevel = 5;

/**
* Constructor
*
* @param array|Zend\Config\Config $options
* @return void
*/
public function __construct($options = null)
{
if (!extension_loaded("gd")) {
throw new ExtensionNotLoadedException("Image CAPTCHA requires GD extension");
}

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

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

parent::__construct($options);
}

/**
* @return string
*/
public function getImgAlt ()
{
return $this->_imgAlt;
}

/**
* @return string
*/
public function getStartImage ()
{
return $this->_startImage;
}

/**
* @return int
*/
public function getDotNoiseLevel ()
{
return $this->_dotNoiseLevel;
}

/**
* @return int
*/
public function getLineNoiseLevel ()
{
return $this->_lineNoiseLevel;
}

/**
* Get captcha expiration
*
Expand All @@ -178,6 +211,7 @@ public function getGcFreq()
{
return $this->_gcFreq;
}

/**
* Get font to use when generating captcha
*
Expand Down Expand Up @@ -217,6 +251,7 @@ public function getImgDir()
{
return $this->_imgDir;
}

/**
* Get captcha image base URL
*
Expand All @@ -226,6 +261,7 @@ public function getImgUrl()
{
return $this->_imgUrl;
}

/**
* Get captcha image file suffix
*
Expand All @@ -235,6 +271,7 @@ public function getSuffix()
{
return $this->_suffix;
}

/**
* Get captcha image width
*
Expand All @@ -244,6 +281,7 @@ public function getWidth()
{
return $this->_width;
}

/**
* @param string $startImage
*/
Expand All @@ -252,6 +290,7 @@ public function setStartImage ($startImage)
$this->_startImage = $startImage;
return $this;
}

/**
* @param int $dotNoiseLevel
*/
Expand All @@ -260,7 +299,8 @@ public function setDotNoiseLevel ($dotNoiseLevel)
$this->_dotNoiseLevel = $dotNoiseLevel;
return $this;
}
/**

/**
* @param int $lineNoiseLevel
*/
public function setLineNoiseLevel ($lineNoiseLevel)
Expand Down Expand Up @@ -449,22 +489,10 @@ public function generate()
*/
protected function _generateImage($id, $word)
{
if (!extension_loaded("gd")) {
throw new Exception("Image CAPTCHA requires GD extension");
}

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

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

$font = $this->getFont();

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

$w = $this->getWidth();
Expand All @@ -473,11 +501,12 @@ protected function _generateImage($id, $word)

$img_file = $this->getImgDir() . $id . $this->getSuffix();
if(empty($this->_startImage)) {
$img = imagecreatetruecolor($w, $h);
$img = imagecreatetruecolor($w, $h);
} else {
$img = imagecreatefrompng($this->_startImage);
// Potential error is change to exception
$img = @imagecreatefrompng($this->_startImage);
if(!$img) {
throw new Exception("Can not load start image");
throw new ImageNotLoadableException("Can not load start image");
}
$w = imagesx($img);
$h = imagesy($img);
Expand Down
19 changes: 18 additions & 1 deletion test/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
* @namespace
*/
namespace ZendTest\Captcha;
use Zend\View\View;
use Zend\View\View,
Zend\Captcha\Image;

/**
* @category Zend
Expand Down Expand Up @@ -311,5 +312,21 @@ public function testIsValidShouldNotRequireValueToBeNestedArray()
$input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
$this->assertTrue($this->element->isValid($input));
}

public function testNoFontProvidedWillThrowException()
{
$this->setExpectedException('Zend\Captcha\Exception\NoFontProvidedException');
$captcha = new Image();
$captcha->generate();
}

public function testImageProvidedNotLoadableWillThrowException()
{
$this->setExpectedException('Zend\Captcha\Exception\ImageNotLoadableException');
$captcha = new Image(array('font' => __DIR__. '/../Pdf/_fonts/Vera.ttf',
'startImage' => 'file_not_found.png'));
$captcha->generate();
}

}

0 comments on commit 69b9eae

Please # to comment.