web/lib/Zend/Service/ReCaptcha.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Service
    16  * @package    Zend_Service
    17  * @subpackage ReCaptcha
    17  * @subpackage ReCaptcha
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  */
    20  */
    21 
    21 
    22 /** @see Zend_Service_Abstract */
    22 /** @see Zend_Service_Abstract */
    23 require_once 'Zend/Service/Abstract.php';
    23 require_once 'Zend/Service/Abstract.php';
    32  * Zend_Service_ReCaptcha
    32  * Zend_Service_ReCaptcha
    33  *
    33  *
    34  * @category   Zend
    34  * @category   Zend
    35  * @package    Zend_Service
    35  * @package    Zend_Service
    36  * @subpackage ReCaptcha
    36  * @subpackage ReCaptcha
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  * @version    $Id: ReCaptcha.php 20096 2010-01-06 02:05:09Z bkarwin $
    39  * @version    $Id: ReCaptcha.php 25153 2012-11-28 11:56:23Z cogo $
    40  */
    40  */
    41 class Zend_Service_ReCaptcha extends Zend_Service_Abstract
    41 class Zend_Service_ReCaptcha extends Zend_Service_Abstract
    42 {
    42 {
    43     /**
    43     /**
    44      * URI to the regular API
    44      * URI to the regular API
    45      *
    45      *
    46      * @var string
    46      * @var string
    47      */
    47      */
    48     const API_SERVER = 'http://api.recaptcha.net';
    48     const API_SERVER = 'http://www.google.com/recaptcha/api';
    49 
    49 
    50     /**
    50     /**
    51      * URI to the secure API
    51      * URI to the secure API
    52      *
    52      *
    53      * @var string
    53      * @var string
    54      */
    54      */
    55     const API_SECURE_SERVER = 'https://api-secure.recaptcha.net';
    55     const API_SECURE_SERVER = 'https://www.google.com/recaptcha/api';
    56 
    56 
    57     /**
    57     /**
    58      * URI to the verify server
    58      * URI to the verify server
    59      *
    59      *
    60      * @var string
    60      * @var string
    61      */
    61      */
    62     const VERIFY_SERVER = 'http://api-verify.recaptcha.net/verify';
    62     const VERIFY_SERVER = 'http://www.google.com/recaptcha/api/verify';
    63 
    63 
    64     /**
    64     /**
    65      * Public key used when displaying the captcha
    65      * Public key used when displaying the captcha
    66      *
    66      *
    67      * @var string
    67      * @var string
   371     /**
   371     /**
   372      * Get the HTML code for the captcha
   372      * Get the HTML code for the captcha
   373      *
   373      *
   374      * This method uses the public key to fetch a recaptcha form.
   374      * This method uses the public key to fetch a recaptcha form.
   375      *
   375      *
       
   376      * @param  null|string $name Base name for recaptcha form elements
   376      * @return string
   377      * @return string
   377      * @throws Zend_Service_ReCaptcha_Exception
   378      * @throws Zend_Service_ReCaptcha_Exception
   378      */
   379      */
   379     public function getHtml()
   380     public function getHtml($name = null)
   380     {
   381     {
   381         if ($this->_publicKey === null) {
   382         if ($this->_publicKey === null) {
   382             /** @see Zend_Service_ReCaptcha_Exception */
   383             /** @see Zend_Service_ReCaptcha_Exception */
   383             require_once 'Zend/Service/ReCaptcha/Exception.php';
   384             require_once 'Zend/Service/ReCaptcha/Exception.php';
   384 
   385 
   412             $reCaptchaOptions = <<<SCRIPT
   413             $reCaptchaOptions = <<<SCRIPT
   413 <script type="text/javascript">
   414 <script type="text/javascript">
   414     var RecaptchaOptions = {$encoded};
   415     var RecaptchaOptions = {$encoded};
   415 </script>
   416 </script>
   416 SCRIPT;
   417 SCRIPT;
       
   418         }
       
   419         $challengeField = 'recaptcha_challenge_field';
       
   420         $responseField  = 'recaptcha_response_field';
       
   421         if (!empty($name)) {
       
   422             $challengeField = $name . '[' . $challengeField . ']';
       
   423             $responseField  = $name . '[' . $responseField . ']';
   417         }
   424         }
   418 
   425 
   419         $return = $reCaptchaOptions;
   426         $return = $reCaptchaOptions;
   420         $return .= <<<HTML
   427         $return .= <<<HTML
   421 <script type="text/javascript"
   428 <script type="text/javascript"
   424 HTML;
   431 HTML;
   425         $return .= <<<HTML
   432         $return .= <<<HTML
   426 <noscript>
   433 <noscript>
   427    <iframe src="{$host}/noscript?k={$this->_publicKey}{$errorPart}"
   434    <iframe src="{$host}/noscript?k={$this->_publicKey}{$errorPart}"
   428        height="300" width="500" frameborder="0"></iframe>{$htmlBreak}
   435        height="300" width="500" frameborder="0"></iframe>{$htmlBreak}
   429    <textarea name="recaptcha_challenge_field" rows="3" cols="40">
   436    <textarea name="{$challengeField}" rows="3" cols="40">
   430    </textarea>
   437    </textarea>
   431    <input type="hidden" name="recaptcha_response_field"
   438    <input type="hidden" name="{$responseField}"
   432        value="manual_challenge"{$htmlInputClosing}
   439        value="manual_challenge"{$htmlInputClosing}
   433 </noscript>
   440 </noscript>
   434 HTML;
   441 HTML;
   435 
   442 
   436         return $return;
   443         return $return;
   458             require_once 'Zend/Service/ReCaptcha/Exception.php';
   465             require_once 'Zend/Service/ReCaptcha/Exception.php';
   459 
   466 
   460             throw new Zend_Service_ReCaptcha_Exception('Missing ip address');
   467             throw new Zend_Service_ReCaptcha_Exception('Missing ip address');
   461         }
   468         }
   462 
   469 
   463         if (empty($challengeField)) {
       
   464             /** @see Zend_Service_ReCaptcha_Exception */
       
   465             require_once 'Zend/Service/ReCaptcha/Exception.php';
       
   466             throw new Zend_Service_ReCaptcha_Exception('Missing challenge field');
       
   467         }
       
   468 
       
   469         if (empty($responseField)) {
       
   470             /** @see Zend_Service_ReCaptcha_Exception */
       
   471             require_once 'Zend/Service/ReCaptcha/Exception.php';
       
   472 
       
   473             throw new Zend_Service_ReCaptcha_Exception('Missing response field');
       
   474         }
       
   475 
       
   476         /* Fetch an instance of the http client */
   470         /* Fetch an instance of the http client */
   477         $httpClient = self::getHttpClient();
   471         $httpClient = self::getHttpClient();
       
   472         $httpClient->resetParameters(true);
   478 
   473 
   479         $postParams = array('privatekey' => $this->_privateKey,
   474         $postParams = array('privatekey' => $this->_privateKey,
   480                             'remoteip'   => $this->_ip,
   475                             'remoteip'   => $this->_ip,
   481                             'challenge'  => $challengeField,
   476                             'challenge'  => $challengeField,
   482                             'response'   => $responseField);
   477                             'response'   => $responseField);