Current File : /home/k/a/r/karenpetzb/www/items/category/ReCaptcha.php.tar |
home/karenpetzb/library/Zend/Service/ReCaptcha.php 0000604 00000030243 15071332314 0016145 0 ustar 00 <?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_Service
* @subpackage ReCaptcha
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Service_Abstract */
require_once 'Zend/Service/Abstract.php';
/** @see Zend_Json */
require_once 'Zend/Json.php';
/** @see Zend_Service_ReCaptcha_Response */
require_once 'Zend/Service/ReCaptcha/Response.php';
/**
* Zend_Service_ReCaptcha
*
* @category Zend
* @package Zend_Service
* @subpackage ReCaptcha
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
class Zend_Service_ReCaptcha extends Zend_Service_Abstract
{
/**
* URI to the regular API
*
* @var string
*/
const API_SERVER = 'http://api.recaptcha.net';
/**
* URI to the secure API
*
* @var string
*/
const API_SECURE_SERVER = 'https://api-secure.recaptcha.net';
/**
* URI to the verify server
*
* @var string
*/
const VERIFY_SERVER = 'http://api-verify.recaptcha.net/verify';
/**
* Public key used when displaying the captcha
*
* @var string
*/
protected $_publicKey = null;
/**
* Private key used when verifying user input
*
* @var string
*/
protected $_privateKey = null;
/**
* Ip address used when verifying user input
*
* @var string
*/
protected $_ip = null;
/**
* Parameters for the object
*
* @var array
*/
protected $_params = array(
'ssl' => false, /* Use SSL or not when generating the recaptcha */
'error' => null, /* The error message to display in the recaptcha */
'xhtml' => false /* Enable XHTML output (this will not be XHTML Strict
compliant since the IFRAME is necessary when
Javascript is disabled) */
);
/**
* Options for tailoring reCaptcha
*
* See the different options on http://recaptcha.net/apidocs/captcha/client.html
*
* @var array
*/
protected $_options = array(
'theme' => 'red',
'lang' => 'en',
);
/**
* Response from the verify server
*
* @var Zend_Service_ReCaptcha_Response
*/
protected $_response = null;
/**
* Class constructor
*
* @param string $publicKey
* @param string $privateKey
* @param array $params
* @param array $options
* @param string $ip
* @param array|Zend_Config $params
*/
public function __construct($publicKey = null, $privateKey = null,
$params = null, $options = null, $ip = null)
{
if ($publicKey !== null) {
$this->setPublicKey($publicKey);
}
if ($privateKey !== null) {
$this->setPrivateKey($privateKey);
}
if ($ip !== null) {
$this->setIp($ip);
} else if (isset($_SERVER['REMOTE_ADDR'])) {
$this->setIp($_SERVER['REMOTE_ADDR']);
}
if ($params !== null) {
$this->setParams($params);
}
if ($options !== null) {
$this->setOptions($options);
}
}
/**
* Serialize as string
*
* When the instance is used as a string it will display the recaptcha.
* Since we can't throw exceptions within this method we will trigger
* a user warning instead.
*
* @return string
*/
public function __toString()
{
try {
$return = $this->getHtml();
} catch (Exception $e) {
$return = '';
trigger_error($e->getMessage(), E_USER_WARNING);
}
return $return;
}
/**
* Set the ip property
*
* @param string $ip
* @return Zend_Service_ReCaptcha
*/
public function setIp($ip)
{
$this->_ip = $ip;
return $this;
}
/**
* Get the ip property
*
* @return string
*/
public function getIp()
{
return $this->_ip;
}
/**
* Set a single parameter
*
* @param string $key
* @param string $value
* @return Zend_Service_ReCaptcha
*/
public function setParam($key, $value)
{
$this->_params[$key] = $value;
return $this;
}
/**
* Set parameters
*
* @param array|Zend_Config $params
* @return Zend_Service_ReCaptcha
* @throws Zend_Service_ReCaptcha_Exception
*/
public function setParams($params)
{
if ($params instanceof Zend_Config) {
$params = $params->toArray();
}
if (is_array($params)) {
foreach ($params as $k => $v) {
$this->setParam($k, $v);
}
} else {
/** @see Zend_Service_ReCaptcha_Exception */
require_once 'Zend/Service/ReCaptcha/Exception.php';
throw new Zend_Service_ReCaptcha_Exception(
'Expected array or Zend_Config object'
);
}
return $this;
}
/**
* Get the parameter array
*
* @return array
*/
public function getParams()
{
return $this->_params;
}
/**
* Get a single parameter
*
* @param string $key
* @return mixed
*/
public function getParam($key)
{
return $this->_params[$key];
}
/**
* Set a single option
*
* @param string $key
* @param string $value
* @return Zend_Service_ReCaptcha
*/
public function setOption($key, $value)
{
$this->_options[$key] = $value;
return $this;
}
/**
* Set options
*
* @param array|Zend_Config $options
* @return Zend_Service_ReCaptcha
* @throws Zend_Service_ReCaptcha_Exception
*/
public function setOptions($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (is_array($options)) {
foreach ($options as $k => $v) {
$this->setOption($k, $v);
}
} else {
/** @see Zend_Service_ReCaptcha_Exception */
require_once 'Zend/Service/ReCaptcha/Exception.php';
throw new Zend_Service_ReCaptcha_Exception(
'Expected array or Zend_Config object'
);
}
return $this;
}
/**
* Get the options array
*
* @return array
*/
public function getOptions()
{
return $this->_options;
}
/**
* Get a single option
*
* @param string $key
* @return mixed
*/
public function getOption($key)
{
return $this->_options[$key];
}
/**
* Get the public key
*
* @return string
*/
public function getPublicKey()
{
return $this->_publicKey;
}
/**
* Set the public key
*
* @param string $publicKey
* @return Zend_Service_ReCaptcha
*/
public function setPublicKey($publicKey)
{
$this->_publicKey = $publicKey;
return $this;
}
/**
* Get the private key
*
* @return string
*/
public function getPrivateKey()
{
return $this->_privateKey;
}
/**
* Set the private key
*
* @param string $privateKey
* @return Zend_Service_ReCaptcha
*/
public function setPrivateKey($privateKey)
{
$this->_privateKey = $privateKey;
return $this;
}
/**
* Get the HTML code for the captcha
*
* This method uses the public key to fetch a recaptcha form.
*
* @return string
* @throws Zend_Service_ReCaptcha_Exception
*/
public function getHtml()
{
if ($this->_publicKey === null) {
/** @see Zend_Service_ReCaptcha_Exception */
require_once 'Zend/Service/ReCaptcha/Exception.php';
throw new Zend_Service_ReCaptcha_Exception('Missing public key');
}
$host = self::API_SERVER;
if ($this->_params['ssl'] === true) {
$host = self::API_SECURE_SERVER;
}
$htmlBreak = '<br>';
$htmlInputClosing = '>';
if ($this->_params['xhtml'] === true) {
$htmlBreak = '<br />';
$htmlInputClosing = '/>';
}
$errorPart = '';
if (!empty($this->_params['error'])) {
$errorPart = '&error=' . urlencode($this->_params['error']);
}
$reCaptchaOptions = '';
if (!empty($this->_options)) {
$encoded = Zend_Json::encode($this->_options);
$reCaptchaOptions = <<<SCRIPT
<script type="text/javascript">
var RecaptchaOptions = {$encoded};
</script>
SCRIPT;
}
$return = $reCaptchaOptions;
$return .= <<<HTML
<script type="text/javascript"
src="{$host}/challenge?k={$this->_publicKey}{$errorPart}">
</script>
HTML;
$return .= <<<HTML
<noscript>
<iframe src="{$host}/noscript?k={$this->_publicKey}{$errorPart}"
height="300" width="500" frameborder="0"></iframe>{$htmlBreak}
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge"{$htmlInputClosing}
</noscript>
HTML;
return $return;
}
/**
* Post a solution to the verify server
*
* @param string $challengeField
* @param string $responseField
* @return Zend_Http_Response
* @throws Zend_Service_ReCaptcha_Exception
*/
protected function _post($challengeField, $responseField)
{
if ($this->_privateKey === null) {
/** @see Zend_Service_ReCaptcha_Exception */
require_once 'Zend/Service/ReCaptcha/Exception.php';
throw new Zend_Service_ReCaptcha_Exception('Missing private key');
}
if ($this->_ip === null) {
/** @see Zend_Service_ReCaptcha_Exception */
require_once 'Zend/Service/ReCaptcha/Exception.php';
throw new Zend_Service_ReCaptcha_Exception('Missing ip address');
}
if (empty($challengeField)) {
/** @see Zend_Service_ReCaptcha_Exception */
require_once 'Zend/Service/ReCaptcha/Exception.php';
throw new Zend_Service_ReCaptcha_Exception('Missing challenge field');
}
if (empty($responseField)) {
/** @see Zend_Service_ReCaptcha_Exception */
require_once 'Zend/Service/ReCaptcha/Exception.php';
throw new Zend_Service_ReCaptcha_Exception('Missing response field');
}
/* Fetch an instance of the http client */
$httpClient = self::getHttpClient();
$postParams = array('privatekey' => $this->_privateKey,
'remoteip' => $this->_ip,
'challenge' => $challengeField,
'response' => $responseField);
/* Make the POST and return the response */
return $httpClient->setUri(self::VERIFY_SERVER)
->setParameterPost($postParams)
->request(Zend_Http_Client::POST);
}
/**
* Verify the user input
*
* This method calls up the post method and returns a
* Zend_Service_ReCaptcha_Response object.
*
* @param string $challengeField
* @param string $responseField
* @return Zend_Service_ReCaptcha_Response
*/
public function verify($challengeField, $responseField)
{
$response = $this->_post($challengeField, $responseField);
return new Zend_Service_ReCaptcha_Response(null, null, $response);
}
}
home/karenpetzb/library/Zend/Captcha/ReCaptcha.php 0000604 00000014202 15071515502 0016107 0 ustar 00 <?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-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** Zend_Captcha_Base */
require_once 'Zend/Captcha/Base.php';
/** 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: $
*/
class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
{
/**
* Recaptcha public key
*
* @var string
*/
protected $_pubkey;
/**
* Recaptcha private key
*
* @var string
*/
protected $_privkey;
/**@+
* 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();
/**#@+
* Error codes
* @const string
*/
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->_privkey;
}
/**
* Retrieve ReCaptcha Public key
*
* @return string
*/
public function getPubkey()
{
return $this->_pubkey;
}
/**
* Set ReCaptcha Private key
*
* @param string $_privkey
* @return Zend_Captcha_ReCaptcha
*/
public function setPrivkey($privkey)
{
$this->_privkey = $privkey;
return $this;
}
/**
* Set ReCaptcha public key
*
* @param string $_pubkey
* @return Zend_Captcha_ReCaptcha
*/
public function setPubkey($pubkey)
{
$this->_pubkey = $pubkey;
return $this;
}
/**
* Constructor
*
* @param array|Zend_Config $options
* @return void
*/
public function __construct($options = null)
{
parent::__construct($options);
$this->setService(new Zend_Service_ReCaptcha($this->getPubKey(), $this->getPrivKey()));
$this->_serviceParams = $this->getService()->getParams();
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.
*
* @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;
}
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 $view
* @param mixed $element
* @return string
*/
public function render(Zend_View_Interface $view, $element = null)
{
return $this->getService()->getHTML();
}
}