adding zend project folders into old campcaster.

This commit is contained in:
naomiaro 2010-12-07 14:19:27 -05:00
parent 56abfaf28e
commit 7ef0c18b26
4045 changed files with 1054952 additions and 0 deletions

View file

@ -0,0 +1,76 @@
<?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
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Validate_Interface */
require_once 'Zend/Validate/Interface.php';
/**
* Generic Captcha adapter interface
*
* Each specific captcha implementation should implement this interface
*
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Adapter.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
interface Zend_Captcha_Adapter extends Zend_Validate_Interface
{
/**
* Generate a new captcha
*
* @return string new captcha ID
*/
public function generate();
/**
* Display the captcha
*
* @param Zend_View_Interface $view
* @param mixed $element
* @return string
*/
public function render(Zend_View_Interface $view = null, $element = null);
/**
* Set captcha name
*
* @param string $name
* @return Zend_Captcha_Adapter
*/
public function setName($name);
/**
* Get captcha name
*
* @return string
*/
public function getName();
/**
* Get optional private decorator for this captcha type
*
* @return Zend_Form_Decorator_Interface|string
*/
public function getDecorator();
}

View file

@ -0,0 +1,176 @@
<?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
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Captcha_Adapter */
require_once 'Zend/Captcha/Adapter.php';
/** @see Zend_Validate_Abstract */
require_once 'Zend/Validate/Abstract.php';
/**
* Base class for Captcha adapters
*
* Provides some utility functionality to build on
*
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Base.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
abstract class Zend_Captcha_Base extends Zend_Validate_Abstract implements Zend_Captcha_Adapter
{
/**
* Element name
*
* Useful to generate/check form fields
*
* @var string
*/
protected $_name;
/**
* Captcha options
*
* @var array
*/
protected $_options = array();
/**
* Options to skip when processing options
* @var array
*/
protected $_skipOptions = array(
'options',
'config',
);
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->_name;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->_name = $name;
return $this;
}
/**
* Constructor
*
* @param array|Zend_Config $options
* @return void
*/
public function __construct($options = null)
{
// Set options
if (is_array($options)) {
$this->setOptions($options);
} else if ($options instanceof Zend_Config) {
$this->setConfig($options);
}
}
/**
* Set single option for the object
*
* @param string $key
* @param string $value
* @return Zend_Form_Element
*/
public function setOption($key, $value)
{
if (in_array(strtolower($key), $this->_skipOptions)) {
return $this;
}
$method = 'set' . ucfirst ($key);
if (method_exists ($this, $method)) {
// Setter exists; use it
$this->$method ($value);
$this->_options[$key] = $value;
} elseif (property_exists($this, $key)) {
// Assume it's metadata
$this->$key = $value;
$this->_options[$key] = $value;
}
return $this;
}
/**
* Set object state from options array
*
* @param array $options
* @return Zend_Form_Element
*/
public function setOptions($options = null)
{
foreach ($options as $key => $value) {
$this->setOption($key, $value);
}
return $this;
}
/**
* Retrieve options representing object state
*
* @return array
*/
public function getOptions()
{
return $this->_options;
}
/**
* Set object state from config object
*
* @param Zend_Config $config
* @return Zend_Captcha_Base
*/
public function setConfig(Zend_Config $config)
{
return $this->setOptions($config->toArray());
}
/**
* Get optional decorator
*
* By default, return null, indicating no extra decorator needed.
*
* @return null
*/
public function getDecorator()
{
return null;
}
}

View file

@ -0,0 +1,52 @@
<?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
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Captcha_Word */
require_once 'Zend/Captcha/Word.php';
/**
* Example dumb word-based captcha
*
* Note that only rendering is necessary for word-based captcha
*
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Dumb.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
class Zend_Captcha_Dumb extends Zend_Captcha_Word
{
/**
* Render the captcha
*
* @param Zend_View_Interface $view
* @param mixed $element
* @return string
*/
public function render(Zend_View_Interface $view = null, $element = null)
{
return 'Please type this word backwards: <b>'
. strrev($this->getWord())
. '</b>';
}
}

View file

@ -0,0 +1,37 @@
<?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: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Exception
*/
require_once 'Zend/Exception.php';
/**
* Exception for Zend_Form component.
*
* @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 Zend_Captcha_Exception extends Zend_Exception
{
}

View file

@ -0,0 +1,85 @@
<?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
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Captcha_Word */
require_once 'Zend/Captcha/Word.php';
/** @see Zend_Text_Figlet */
require_once 'Zend/Text/Figlet.php';
/**
* Captcha based on figlet text rendering service
*
* Note that this engine seems not to like numbers
*
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Figlet.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
class Zend_Captcha_Figlet extends Zend_Captcha_Word
{
/**
* Figlet text renderer
*
* @var Zend_Text_Figlet
*/
protected $_figlet;
/**
* Constructor
*
* @param null|string|array|Zend_Config $options
* @return void
*/
public function __construct($options = null)
{
parent::__construct($options);
$this->_figlet = new Zend_Text_Figlet($options);
}
/**
* Generate new captcha
*
* @return string
*/
public function generate()
{
$this->_useNumbers = false;
return parent::generate();
}
/**
* Display the captcha
*
* @param Zend_View_Interface $view
* @param mixed $element
* @return string
*/
public function render(Zend_View_Interface $view = null, $element = null)
{
return '<pre>'
. $this->_figlet->render($this->getWord())
. "</pre>\n";
}
}

View file

@ -0,0 +1,600 @@
<?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
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Image.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/** @see Zend_Captcha_Word */
require_once 'Zend/Captcha/Word.php';
/**
* Image-based captcha element
*
* Generates image displaying random word
*
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
* @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 Zend_Captcha_Image extends Zend_Captcha_Word
{
/**
* Directory for generated images
*
* @var string
*/
protected $_imgDir = "./images/captcha/";
/**
* URL for accessing images
*
* @var string
*/
protected $_imgUrl = "/images/captcha/";
/**
* Image's alt tag content
*
* @var string
*/
protected $_imgAlt = "";
/**
* Image suffix (including dot)
*
* @var string
*/
protected $_suffix = ".png";
/**
* Image width
*
* @var int
*/
protected $_width = 200;
/**
* Image height
*
* @var int
*/
protected $_height = 50;
/**
* Font size
*
* @var int
*/
protected $_fsize = 24;
/**
* Image font file
*
* @var string
*/
protected $_font;
/**
* Image to use as starting point
* Default is blank image. If provided, should be PNG image.
*
* @var string
*/
protected $_startImage;
/**
* How frequently to execute garbage collection
*
* @var int
*/
protected $_gcFreq = 10;
/**
* How long to keep generated images
*
* @var int
*/
protected $_expiration = 600;
/**
* Number of noise dots on image
* Used twice - before and after transform
*
* @var int
*/
protected $_dotNoiseLevel = 100;
/**
* Number of noise lines on image
* Used twice - before and after transform
*
* @var int
*/
protected $_lineNoiseLevel = 5;
/**
* @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
*
* @return int
*/
public function getExpiration()
{
return $this->_expiration;
}
/**
* Get garbage collection frequency
*
* @return int
*/
public function getGcFreq()
{
return $this->_gcFreq;
}
/**
* Get font to use when generating captcha
*
* @return string
*/
public function getFont()
{
return $this->_font;
}
/**
* Get font size
*
* @return int
*/
public function getFontSize()
{
return $this->_fsize;
}
/**
* Get captcha image height
*
* @return int
*/
public function getHeight()
{
return $this->_height;
}
/**
* Get captcha image directory
*
* @return string
*/
public function getImgDir()
{
return $this->_imgDir;
}
/**
* Get captcha image base URL
*
* @return string
*/
public function getImgUrl()
{
return $this->_imgUrl;
}
/**
* Get captcha image file suffix
*
* @return string
*/
public function getSuffix()
{
return $this->_suffix;
}
/**
* Get captcha image width
*
* @return int
*/
public function getWidth()
{
return $this->_width;
}
/**
* @param string $startImage
*/
public function setStartImage ($startImage)
{
$this->_startImage = $startImage;
return $this;
}
/**
* @param int $dotNoiseLevel
*/
public function setDotNoiseLevel ($dotNoiseLevel)
{
$this->_dotNoiseLevel = $dotNoiseLevel;
return $this;
}
/**
* @param int $lineNoiseLevel
*/
public function setLineNoiseLevel ($lineNoiseLevel)
{
$this->_lineNoiseLevel = $lineNoiseLevel;
return $this;
}
/**
* Set captcha expiration
*
* @param int $expiration
* @return Zend_Captcha_Image
*/
public function setExpiration($expiration)
{
$this->_expiration = $expiration;
return $this;
}
/**
* Set garbage collection frequency
*
* @param int $gcFreq
* @return Zend_Captcha_Image
*/
public function setGcFreq($gcFreq)
{
$this->_gcFreq = $gcFreq;
return $this;
}
/**
* Set captcha font
*
* @param string $font
* @return Zend_Captcha_Image
*/
public function setFont($font)
{
$this->_font = $font;
return $this;
}
/**
* Set captcha font size
*
* @param int $fsize
* @return Zend_Captcha_Image
*/
public function setFontSize($fsize)
{
$this->_fsize = $fsize;
return $this;
}
/**
* Set captcha image height
*
* @param int $height
* @return Zend_Captcha_Image
*/
public function setHeight($height)
{
$this->_height = $height;
return $this;
}
/**
* Set captcha image storage directory
*
* @param string $imgDir
* @return Zend_Captcha_Image
*/
public function setImgDir($imgDir)
{
$this->_imgDir = rtrim($imgDir, "/\\") . '/';
return $this;
}
/**
* Set captcha image base URL
*
* @param string $imgUrl
* @return Zend_Captcha_Image
*/
public function setImgUrl($imgUrl)
{
$this->_imgUrl = rtrim($imgUrl, "/\\") . '/';
return $this;
}
/**
* @param string $imgAlt
*/
public function setImgAlt ($imgAlt)
{
$this->_imgAlt = $imgAlt;
return $this;
}
/**
* Set captch image filename suffix
*
* @param string $suffix
* @return Zend_Captcha_Image
*/
public function setSuffix($suffix)
{
$this->_suffix = $suffix;
return $this;
}
/**
* Set captcha image width
*
* @param int $width
* @return Zend_Captcha_Image
*/
public function setWidth($width)
{
$this->_width = $width;
return $this;
}
/**
* Generate random frequency
*
* @return float
*/
protected function _randomFreq()
{
return mt_rand(700000, 1000000) / 15000000;
}
/**
* Generate random phase
*
* @return float
*/
protected function _randomPhase()
{
// random phase from 0 to pi
return mt_rand(0, 3141592) / 1000000;
}
/**
* Generate random character size
*
* @return int
*/
protected function _randomSize()
{
return mt_rand(300, 700) / 100;
}
/**
* Generate captcha
*
* @return string captcha ID
*/
public function generate()
{
$id = parent::generate();
$tries = 5;
// If there's already such file, try creating a new ID
while($tries-- && file_exists($this->getImgDir() . $id . $this->getSuffix())) {
$id = $this->_generateRandomId();
$this->_setId($id);
}
$this->_generateImage($id, $this->getWord());
if (mt_rand(1, $this->getGcFreq()) == 1) {
$this->_gc();
}
return $id;
}
/**
* Generate image captcha
*
* Override this function if you want different image generator
* Wave transform from http://www.captcha.ru/captchas/multiwave/
*
* @param string $id Captcha ID
* @param string $word Captcha word
*/
protected function _generateImage($id, $word)
{
if (!extension_loaded("gd")) {
require_once 'Zend/Captcha/Exception.php';
throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension");
}
if (!function_exists("imagepng")) {
require_once 'Zend/Captcha/Exception.php';
throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support");
}
if (!function_exists("imageftbbox")) {
require_once 'Zend/Captcha/Exception.php';
throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support");
}
$font = $this->getFont();
if (empty($font)) {
require_once 'Zend/Captcha/Exception.php';
throw new Zend_Captcha_Exception("Image CAPTCHA requires font");
}
$w = $this->getWidth();
$h = $this->getHeight();
$fsize = $this->getFontSize();
$img_file = $this->getImgDir() . $id . $this->getSuffix();
if(empty($this->_startImage)) {
$img = imagecreatetruecolor($w, $h);
} else {
$img = imagecreatefrompng($this->_startImage);
if(!$img) {
require_once 'Zend/Captcha/Exception.php';
throw new Zend_Captcha_Exception("Can not load start image");
}
$w = imagesx($img);
$h = imagesy($img);
}
$text_color = imagecolorallocate($img, 0, 0, 0);
$bg_color = imagecolorallocate($img, 255, 255, 255);
imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);
$textbox = imageftbbox($fsize, 0, $font, $word);
$x = ($w - ($textbox[2] - $textbox[0])) / 2;
$y = ($h - ($textbox[7] - $textbox[1])) / 2;
imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word);
// generate noise
for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
}
for($i=0; $i<$this->_lineNoiseLevel; $i++) {
imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
}
// transformed image
$img2 = imagecreatetruecolor($w, $h);
$bg_color = imagecolorallocate($img2, 255, 255, 255);
imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
// apply wave transforms
$freq1 = $this->_randomFreq();
$freq2 = $this->_randomFreq();
$freq3 = $this->_randomFreq();
$freq4 = $this->_randomFreq();
$ph1 = $this->_randomPhase();
$ph2 = $this->_randomPhase();
$ph3 = $this->_randomPhase();
$ph4 = $this->_randomPhase();
$szx = $this->_randomSize();
$szy = $this->_randomSize();
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx;
$sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy;
if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
continue;
} else {
$color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF;
$color_x = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF;
$color_y = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF;
$color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;
}
if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {
// ignore background
continue;
} elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {
// transfer inside of the image as-is
$newcolor = 0;
} else {
// do antialiasing for border items
$frac_x = $sx-floor($sx);
$frac_y = $sy-floor($sy);
$frac_x1 = 1-$frac_x;
$frac_y1 = 1-$frac_y;
$newcolor = $color * $frac_x1 * $frac_y1
+ $color_x * $frac_x * $frac_y1
+ $color_y * $frac_x1 * $frac_y
+ $color_xy * $frac_x * $frac_y;
}
imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));
}
}
// generate noise
for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
}
for ($i=0; $i<$this->_lineNoiseLevel; $i++) {
imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
}
imagepng($img2, $img_file);
imagedestroy($img);
imagedestroy($img2);
}
/**
* Remove old files from image directory
*
*/
protected function _gc()
{
$expire = time() - $this->getExpiration();
$imgdir = $this->getImgDir();
if(!$imgdir || strlen($imgdir) < 2) {
// safety guard
return;
}
foreach (new DirectoryIterator($imgdir) as $file) {
if (!$file->isDot() && !$file->isDir()) {
if ($file->getMTime() < $expire) {
unlink($file->getPathname());
}
}
}
}
/**
* Display the captcha
*
* @param Zend_View_Interface $view
* @param mixed $element
* @return string
*/
public function render(Zend_View_Interface $view = null, $element = null)
{
return '<img width="'.$this->getWidth().'" height="'.$this->getHeight().'" alt="'.$this->getImgAlt().'" src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '"/><br/>';
}
}

View file

@ -0,0 +1,266 @@
<?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
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Captcha_Base */
require_once 'Zend/Captcha/Base.php';
/** @see Zend_Service_ReCaptcha */
require_once 'Zend/Service/ReCaptcha.php';
/**
* ReCaptcha adapter
*
* Allows to insert captchas driven by ReCaptcha service
*
* @see http://recaptcha.net/apidocs/captcha/
*
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: ReCaptcha.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
{
/**@+
* ReCaptcha Field names
* @var string
*/
protected $_CHALLENGE = 'recaptcha_challenge_field';
protected $_RESPONSE = 'recaptcha_response_field';
/**@-*/
/**
* Recaptcha service object
*
* @var Zend_Service_Recaptcha
*/
protected $_service;
/**
* Parameters defined by the service
*
* @var array
*/
protected $_serviceParams = array();
/**
* Options defined by the service
*
* @var array
*/
protected $_serviceOptions = array();
/**#@+
* Error codes
*/
const MISSING_VALUE = 'missingValue';
const ERR_CAPTCHA = 'errCaptcha';
const BAD_CAPTCHA = 'badCaptcha';
/**#@-*/
/**
* Error messages
* @var array
*/
protected $_messageTemplates = array(
self::MISSING_VALUE => 'Missing captcha fields',
self::ERR_CAPTCHA => 'Failed to validate captcha',
self::BAD_CAPTCHA => 'Captcha value is wrong: %value%',
);
/**
* Retrieve ReCaptcha Private key
*
* @return string
*/
public function getPrivkey()
{
return $this->getService()->getPrivateKey();
}
/**
* Retrieve ReCaptcha Public key
*
* @return string
*/
public function getPubkey()
{
return $this->getService()->getPublicKey();
}
/**
* Set ReCaptcha Private key
*
* @param string $privkey
* @return Zend_Captcha_ReCaptcha
*/
public function setPrivkey($privkey)
{
$this->getService()->setPrivateKey($privkey);
return $this;
}
/**
* Set ReCaptcha public key
*
* @param string $pubkey
* @return Zend_Captcha_ReCaptcha
*/
public function setPubkey($pubkey)
{
$this->getService()->setPublicKey($pubkey);
return $this;
}
/**
* Constructor
*
* @param array|Zend_Config $options
* @return void
*/
public function __construct($options = null)
{
$this->setService(new Zend_Service_ReCaptcha());
$this->_serviceParams = $this->getService()->getParams();
$this->_serviceOptions = $this->getService()->getOptions();
parent::__construct($options);
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (!empty($options)) {
$this->setOptions($options);
}
}
/**
* Set service object
*
* @param Zend_Service_ReCaptcha $service
* @return Zend_Captcha_ReCaptcha
*/
public function setService(Zend_Service_ReCaptcha $service)
{
$this->_service = $service;
return $this;
}
/**
* Retrieve ReCaptcha service object
*
* @return Zend_Service_ReCaptcha
*/
public function getService()
{
return $this->_service;
}
/**
* Set option
*
* If option is a service parameter, proxies to the service. The same
* goes for any service options (distinct from service params)
*
* @param string $key
* @param mixed $value
* @return Zend_Captcha_ReCaptcha
*/
public function setOption($key, $value)
{
$service = $this->getService();
if (isset($this->_serviceParams[$key])) {
$service->setParam($key, $value);
return $this;
}
if (isset($this->_serviceOptions[$key])) {
$service->setOption($key, $value);
return $this;
}
return parent::setOption($key, $value);
}
/**
* Generate captcha
*
* @see Zend_Form_Captcha_Adapter::generate()
* @return string
*/
public function generate()
{
return "";
}
/**
* Validate captcha
*
* @see Zend_Validate_Interface::isValid()
* @param mixed $value
* @return boolean
*/
public function isValid($value, $context = null)
{
if (!is_array($value) && !is_array($context)) {
$this->_error(self::MISSING_VALUE);
return false;
}
if (!is_array($value) && is_array($context)) {
$value = $context;
}
if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) {
$this->_error(self::MISSING_VALUE);
return false;
}
$service = $this->getService();
$res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]);
if (!$res) {
$this->_error(self::ERR_CAPTCHA);
return false;
}
if (!$res->isValid()) {
$this->_error(self::BAD_CAPTCHA, $res->getErrorCode());
$service->setParam('error', $res->getErrorCode());
return false;
}
return true;
}
/**
* Render captcha
*
* @param Zend_View_Interface $view
* @param mixed $element
* @return string
*/
public function render(Zend_View_Interface $view = null, $element = null)
{
return $this->getService()->getHTML();
}
}

View file

@ -0,0 +1,396 @@
<?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
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Captcha_Base */
require_once 'Zend/Captcha/Base.php';
/**
* Word-based captcha adapter
*
* Generates random word which user should recognise
*
* @category Zend
* @package Zend_Captcha
* @subpackage Adapter
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Word.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
abstract class Zend_Captcha_Word extends Zend_Captcha_Base
{
/**#@+
* @var array Character sets
*/
static $V = array("a", "e", "i", "o", "u", "y");
static $VN = array("a", "e", "i", "o", "u", "y","2","3","4","5","6","7","8","9");
static $C = array("b","c","d","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","z");
static $CN = array("b","c","d","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","z","2","3","4","5","6","7","8","9");
/**#@-*/
/**
* Random session ID
*
* @var string
*/
protected $_id;
/**
* Generated word
*
* @var string
*/
protected $_word;
/**
* Session
*
* @var Zend_Session_Namespace
*/
protected $_session;
/**
* Class name for sessions
*
* @var string
*/
protected $_sessionClass = 'Zend_Session_Namespace';
/**
* Should the numbers be used or only letters
*
* @var boolean
*/
protected $_useNumbers = true;
/**
* Should both cases be used or only lowercase
*
* @var boolean
*/
// protected $_useCase = false;
/**
* Session lifetime for the captcha data
*
* @var integer
*/
protected $_timeout = 300;
/**
* Should generate() keep session or create a new one?
*
* @var boolean
*/
protected $_keepSession = false;
/**#@+
* Error codes
*/
const MISSING_VALUE = 'missingValue';
const MISSING_ID = 'missingID';
const BAD_CAPTCHA = 'badCaptcha';
/**#@-*/
/**
* Error messages
* @var array
*/
protected $_messageTemplates = array(
self::MISSING_VALUE => 'Empty captcha value',
self::MISSING_ID => 'Captcha ID field is missing',
self::BAD_CAPTCHA => 'Captcha value is wrong',
);
/**
* Length of the word to generate
*
* @var integer
*/
protected $_wordlen = 8;
/**
* Retrieve session class to utilize
*
* @return string
*/
public function getSessionClass()
{
return $this->_sessionClass;
}
/**
* Set session class for persistence
*
* @param string $_sessionClass
* @return Zend_Captcha_Word
*/
public function setSessionClass($_sessionClass)
{
$this->_sessionClass = $_sessionClass;
return $this;
}
/**
* Retrieve word length to use when genrating captcha
*
* @return integer
*/
public function getWordlen()
{
return $this->_wordlen;
}
/**
* Set word length of captcha
*
* @param integer $wordlen
* @return Zend_Captcha_Word
*/
public function setWordlen($wordlen)
{
$this->_wordlen = $wordlen;
return $this;
}
/**
* Retrieve captcha ID
*
* @return string
*/
public function getId ()
{
if (null === $this->_id) {
$this->_setId($this->_generateRandomId());
}
return $this->_id;
}
/**
* Set captcha identifier
*
* @param string $id
* return Zend_Captcha_Word
*/
protected function _setId ($id)
{
$this->_id = $id;
return $this;
}
/**
* Set timeout for session token
*
* @param int $ttl
* @return Zend_Captcha_Word
*/
public function setTimeout($ttl)
{
$this->_timeout = (int) $ttl;
return $this;
}
/**
* Get session token timeout
*
* @return int
*/
public function getTimeout()
{
return $this->_timeout;
}
/**
* Sets if session should be preserved on generate()
*
* @param $keepSession Should session be kept on generate()?
* @return Zend_Captcha_Word
*/
public function setKeepSession($keepSession)
{
$this->_keepSession = $keepSession;
return $this;
}
/**
* Get session object
*
* @return Zend_Session_Namespace
*/
public function getSession()
{
if (!isset($this->_session) || (null === $this->_session)) {
$id = $this->getId();
if (!class_exists($this->_sessionClass)) {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($this->_sessionClass);
}
$this->_session = new $this->_sessionClass('Zend_Form_Captcha_' . $id);
$this->_session->setExpirationHops(1, null, true);
$this->_session->setExpirationSeconds($this->getTimeout());
}
return $this->_session;
}
/**
* Set session namespace object
*
* @param Zend_Session_Namespace $session
* @return Zend_Captcha_Word
*/
public function setSession(Zend_Session_Namespace $session)
{
$this->_session = $session;
if($session) {
$this->_keepSession = true;
}
return $this;
}
/**
* Get captcha word
*
* @return string
*/
public function getWord()
{
if (empty($this->_word)) {
$session = $this->getSession();
$this->_word = $session->word;
}
return $this->_word;
}
/**
* Set captcha word
*
* @param string $word
* @return Zend_Captcha_Word
*/
protected function _setWord($word)
{
$session = $this->getSession();
$session->word = $word;
$this->_word = $word;
return $this;
}
/**
* Generate new random word
*
* @return string
*/
protected function _generateWord()
{
$word = '';
$wordLen = $this->getWordLen();
$vowels = $this->_useNumbers ? self::$VN : self::$V;
$consonants = $this->_useNumbers ? self::$CN : self::$C;
for ($i=0; $i < $wordLen; $i = $i + 2) {
// generate word with mix of vowels and consonants
$consonant = $consonants[array_rand($consonants)];
$vowel = $vowels[array_rand($vowels)];
$word .= $consonant . $vowel;
}
if (strlen($word) > $wordLen) {
$word = substr($word, 0, $wordLen);
}
return $word;
}
/**
* Generate new session ID and new word
*
* @return string session ID
*/
public function generate()
{
if(!$this->_keepSession) {
$this->_session = null;
}
$id = $this->_generateRandomId();
$this->_setId($id);
$word = $this->_generateWord();
$this->_setWord($word);
return $id;
}
protected function _generateRandomId()
{
return md5(mt_rand(0, 1000) . microtime(true));
}
/**
* Validate the word
*
* @see Zend_Validate_Interface::isValid()
* @param mixed $value
* @return boolean
*/
public function isValid($value, $context = null)
{
if (!is_array($value) && !is_array($context)) {
$this->_error(self::MISSING_VALUE);
return false;
}
if (!is_array($value) && is_array($context)) {
$value = $context;
}
$name = $this->getName();
if (isset($value[$name])) {
$value = $value[$name];
}
if (!isset($value['input'])) {
$this->_error(self::MISSING_VALUE);
return false;
}
$input = strtolower($value['input']);
$this->_setValue($input);
if (!isset($value['id'])) {
$this->_error(self::MISSING_ID);
return false;
}
$this->_id = $value['id'];
if ($input !== $this->getWord()) {
$this->_error(self::BAD_CAPTCHA);
return false;
}
return true;
}
/**
* Get captcha decorator
*
* @return string
*/
public function getDecorator()
{
return "Captcha_Word";
}
}