web/enmi/Zend/Service/ReCaptcha/Response.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service
       
    17  * @subpackage ReCaptcha
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  */
       
    21 
       
    22 /**
       
    23  * Zend_Service_ReCaptcha_Response
       
    24  *
       
    25  * @category   Zend
       
    26  * @package    Zend_Service
       
    27  * @subpackage ReCaptcha
       
    28  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    29  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    30  * @version    $Id: Response.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    31  */
       
    32 class Zend_Service_ReCaptcha_Response
       
    33 {
       
    34     /**
       
    35      * Status
       
    36      *
       
    37      * true if the response is valid or false otherwise
       
    38      *
       
    39      * @var boolean
       
    40      */
       
    41     protected $_status = null;
       
    42 
       
    43     /**
       
    44      * Error code
       
    45      *
       
    46      * The error code if the status is false. The different error codes can be found in the
       
    47      * recaptcha API docs.
       
    48      *
       
    49      * @var string
       
    50      */
       
    51     protected $_errorCode = null;
       
    52 
       
    53     /**
       
    54      * Class constructor used to construct a response
       
    55      *
       
    56      * @param string $status
       
    57      * @param string $errorCode
       
    58      * @param Zend_Http_Response $httpResponse If this is set the content will override $status and $errorCode
       
    59      */
       
    60     public function __construct($status = null, $errorCode = null, Zend_Http_Response $httpResponse = null)
       
    61     {
       
    62         if ($status !== null) {
       
    63             $this->setStatus($status);
       
    64         }
       
    65 
       
    66         if ($errorCode !== null) {
       
    67             $this->setErrorCode($errorCode);
       
    68         }
       
    69 
       
    70         if ($httpResponse !== null) {
       
    71             $this->setFromHttpResponse($httpResponse);
       
    72         }
       
    73     }
       
    74 
       
    75     /**
       
    76      * Set the status
       
    77      *
       
    78      * @param string $status
       
    79      * @return Zend_Service_ReCaptcha_Response
       
    80      */
       
    81     public function setStatus($status)
       
    82     {
       
    83         if ($status === 'true') {
       
    84             $this->_status = true;
       
    85         } else {
       
    86             $this->_status = false;
       
    87         }
       
    88 
       
    89         return $this;
       
    90     }
       
    91 
       
    92     /**
       
    93      * Get the status
       
    94      *
       
    95      * @return boolean
       
    96      */
       
    97     public function getStatus()
       
    98     {
       
    99         return $this->_status;
       
   100     }
       
   101 
       
   102     /**
       
   103      * Alias for getStatus()
       
   104      *
       
   105      * @return boolean
       
   106      */
       
   107     public function isValid()
       
   108     {
       
   109         return $this->getStatus();
       
   110     }
       
   111 
       
   112     /**
       
   113      * Set the error code
       
   114      *
       
   115      * @param string $errorCode
       
   116      * @return Zend_Service_ReCaptcha_Response
       
   117      */
       
   118     public function setErrorCode($errorCode)
       
   119     {
       
   120         $this->_errorCode = $errorCode;
       
   121 
       
   122         return $this;
       
   123     }
       
   124 
       
   125     /**
       
   126      * Get the error code
       
   127      *
       
   128      * @return string
       
   129      */
       
   130     public function getErrorCode()
       
   131     {
       
   132         return $this->_errorCode;
       
   133     }
       
   134 
       
   135     /**
       
   136      * Populate this instance based on a Zend_Http_Response object
       
   137      *
       
   138      * @param Zend_Http_Response $response
       
   139      * @return Zend_Service_ReCaptcha_Response
       
   140      */
       
   141     public function setFromHttpResponse(Zend_Http_Response $response)
       
   142     {
       
   143         $body = $response->getBody();
       
   144 
       
   145         $parts = explode("\n", $body, 2);
       
   146 
       
   147         if (count($parts) !== 2) {
       
   148             $status = 'false';
       
   149             $errorCode = '';
       
   150         } else {
       
   151             list($status, $errorCode) = $parts;
       
   152         }
       
   153 
       
   154         $this->setStatus($status);
       
   155         $this->setErrorCode($errorCode);
       
   156 
       
   157         return $this;
       
   158     }
       
   159 }