File indexing completed on 2024-06-16 05:30:25

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service
0017  * @subpackage ReCaptcha
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /**
0023  * Zend_Service_ReCaptcha_Response
0024  *
0025  * @category   Zend
0026  * @package    Zend_Service
0027  * @subpackage ReCaptcha
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  * @version    $Id$
0031  */
0032 class Zend_Service_ReCaptcha_Response
0033 {
0034     /**
0035      * Status
0036      *
0037      * true if the response is valid or false otherwise
0038      *
0039      * @var boolean
0040      */
0041     protected $_status = null;
0042 
0043     /**
0044      * Error code
0045      *
0046      * The error code if the status is false. The different error codes can be found in the
0047      * recaptcha API docs.
0048      *
0049      * @var string
0050      */
0051     protected $_errorCode = null;
0052 
0053     /**
0054      * Class constructor used to construct a response
0055      *
0056      * @param string $status
0057      * @param string $errorCode
0058      * @param Zend_Http_Response $httpResponse If this is set the content will override $status and $errorCode
0059      */
0060     public function __construct($status = null, $errorCode = null, Zend_Http_Response $httpResponse = null)
0061     {
0062         if ($status !== null) {
0063             $this->setStatus($status);
0064         }
0065 
0066         if ($errorCode !== null) {
0067             $this->setErrorCode($errorCode);
0068         }
0069 
0070         if ($httpResponse !== null) {
0071             $this->setFromHttpResponse($httpResponse);
0072         }
0073     }
0074 
0075     /**
0076      * Set the status
0077      *
0078      * @param string $status
0079      * @return Zend_Service_ReCaptcha_Response
0080      */
0081     public function setStatus($status)
0082     {
0083         if ($status === 'true') {
0084             $this->_status = true;
0085         } else {
0086             $this->_status = false;
0087         }
0088 
0089         return $this;
0090     }
0091 
0092     /**
0093      * Get the status
0094      *
0095      * @return boolean
0096      */
0097     public function getStatus()
0098     {
0099         return $this->_status;
0100     }
0101 
0102     /**
0103      * Alias for getStatus()
0104      *
0105      * @return boolean
0106      */
0107     public function isValid()
0108     {
0109         return $this->getStatus();
0110     }
0111 
0112     /**
0113      * Set the error code
0114      *
0115      * @param string $errorCode
0116      * @return Zend_Service_ReCaptcha_Response
0117      */
0118     public function setErrorCode($errorCode)
0119     {
0120         $this->_errorCode = $errorCode;
0121 
0122         return $this;
0123     }
0124 
0125     /**
0126      * Get the error code
0127      *
0128      * @return string
0129      */
0130     public function getErrorCode()
0131     {
0132         return $this->_errorCode;
0133     }
0134 
0135     /**
0136      * Populate this instance based on a Zend_Http_Response object
0137      *
0138      * @param Zend_Http_Response $response
0139      * @return Zend_Service_ReCaptcha_Response
0140      */
0141     public function setFromHttpResponse(Zend_Http_Response $response)
0142     {
0143         $body = $response->getBody();
0144 
0145         // Default status and error code
0146         $status = 'false';
0147         $errorCode = '';
0148 
0149         $parts = explode("\n", $body);
0150 
0151         if ($parts[0] === 'true') {
0152             $status = 'true';
0153         }
0154 
0155         if (!empty($parts[1])) {
0156             $errorCode = $parts[1];
0157         }
0158 
0159         $this->setStatus($status);
0160         $this->setErrorCode($errorCode);
0161 
0162         return $this;
0163     }
0164 }