File indexing completed on 2024-12-22 05:37:14

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_Controller
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  */
0020 
0021 /**
0022  * Zend_XmlRpc_Value
0023  */
0024 // require_once 'Zend/XmlRpc/Value.php';
0025 
0026 /**
0027  * Zend_XmlRpc_Fault
0028  */
0029 // require_once 'Zend/XmlRpc/Fault.php';
0030 
0031 /** @see Zend_Xml_Security */
0032 // require_once 'Zend/Xml/Security.php';
0033 
0034 /** @see Zend_Xml_Exception */
0035 // require_once 'Zend/Xml/Exception.php';
0036 
0037 /**
0038  * XmlRpc Response
0039  *
0040  * Container for accessing an XMLRPC return value and creating the XML response.
0041  *
0042  * @category Zend
0043  * @package  Zend_XmlRpc
0044  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0045  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0046  * @version $Id$
0047  */
0048 class Zend_XmlRpc_Response
0049 {
0050     /**
0051      * Return value
0052      * @var mixed
0053      */
0054     protected $_return;
0055 
0056     /**
0057      * Return type
0058      * @var string
0059      */
0060     protected $_type;
0061 
0062     /**
0063      * Response character encoding
0064      * @var string
0065      */
0066     protected $_encoding = 'UTF-8';
0067 
0068     /**
0069      * Fault, if response is a fault response
0070      * @var null|Zend_XmlRpc_Fault
0071      */
0072     protected $_fault = null;
0073 
0074     /**
0075      * Constructor
0076      *
0077      * Can optionally pass in the return value and type hinting; otherwise, the
0078      * return value can be set via {@link setReturnValue()}.
0079      *
0080      * @param mixed $return
0081      * @param string $type
0082      * @return void
0083      */
0084     public function __construct($return = null, $type = null)
0085     {
0086         $this->setReturnValue($return, $type);
0087     }
0088 
0089     /**
0090      * Set encoding to use in response
0091      *
0092      * @param string $encoding
0093      * @return Zend_XmlRpc_Response
0094      */
0095     public function setEncoding($encoding)
0096     {
0097         $this->_encoding = $encoding;
0098         Zend_XmlRpc_Value::setEncoding($encoding);
0099         return $this;
0100     }
0101 
0102     /**
0103      * Retrieve current response encoding
0104      *
0105      * @return string
0106      */
0107     public function getEncoding()
0108     {
0109         return $this->_encoding;
0110     }
0111 
0112     /**
0113      * Set the return value
0114      *
0115      * Sets the return value, with optional type hinting if provided.
0116      *
0117      * @param mixed $value
0118      * @param string $type
0119      * @return void
0120      */
0121     public function setReturnValue($value, $type = null)
0122     {
0123         $this->_return = $value;
0124         $this->_type = (string) $type;
0125     }
0126 
0127     /**
0128      * Retrieve the return value
0129      *
0130      * @return mixed
0131      */
0132     public function getReturnValue()
0133     {
0134         return $this->_return;
0135     }
0136 
0137     /**
0138      * Retrieve the XMLRPC value for the return value
0139      *
0140      * @return Zend_XmlRpc_Value
0141      */
0142     protected function _getXmlRpcReturn()
0143     {
0144         return Zend_XmlRpc_Value::getXmlRpcValue($this->_return);
0145     }
0146 
0147     /**
0148      * Is the response a fault response?
0149      *
0150      * @return boolean
0151      */
0152     public function isFault()
0153     {
0154         return $this->_fault instanceof Zend_XmlRpc_Fault;
0155     }
0156 
0157     /**
0158      * Returns the fault, if any.
0159      *
0160      * @return null|Zend_XmlRpc_Fault
0161      */
0162     public function getFault()
0163     {
0164         return $this->_fault;
0165     }
0166 
0167     /**
0168      * Load a response from an XML response
0169      *
0170      * Attempts to load a response from an XMLRPC response, autodetecting if it
0171      * is a fault response.
0172      *
0173      * @param string $response
0174      * @return boolean True if a valid XMLRPC response, false if a fault
0175      * response or invalid input
0176      */
0177     public function loadXml($response)
0178     {
0179         if (!is_string($response)) {
0180             $this->_fault = new Zend_XmlRpc_Fault(650);
0181             $this->_fault->setEncoding($this->getEncoding());
0182             return false;
0183         }
0184 
0185         try {
0186             $xml = Zend_Xml_Security::scan($response);
0187         } catch (Zend_Xml_Exception $e) {    
0188             // Not valid XML
0189             $this->_fault = new Zend_XmlRpc_Fault(651);
0190             $this->_fault->setEncoding($this->getEncoding());
0191             return false;
0192         }
0193 
0194         if (!empty($xml->fault)) {
0195             // fault response
0196             $this->_fault = new Zend_XmlRpc_Fault();
0197             $this->_fault->setEncoding($this->getEncoding());
0198             $this->_fault->loadXml($response);
0199             return false;
0200         }
0201 
0202         if (empty($xml->params)) {
0203             // Invalid response
0204             $this->_fault = new Zend_XmlRpc_Fault(652);
0205             $this->_fault->setEncoding($this->getEncoding());
0206             return false;
0207         }
0208 
0209         try {
0210             if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
0211                 // require_once 'Zend/XmlRpc/Value/Exception.php';
0212                 throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
0213             }
0214             $valueXml = $xml->params->param->value->asXML();
0215             $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING);
0216         } catch (Zend_XmlRpc_Value_Exception $e) {
0217             $this->_fault = new Zend_XmlRpc_Fault(653);
0218             $this->_fault->setEncoding($this->getEncoding());
0219             return false;
0220         }
0221 
0222         $this->setReturnValue($value->getValue());
0223         return true;
0224     }
0225 
0226     /**
0227      * Return response as XML
0228      *
0229      * @return string
0230      */
0231     public function saveXml()
0232     {
0233         $value = $this->_getXmlRpcReturn();
0234         $generator = Zend_XmlRpc_Value::getGenerator();
0235         $generator->openElement('methodResponse')
0236                   ->openElement('params')
0237                   ->openElement('param');
0238         $value->generateXml();
0239         $generator->closeElement('param')
0240                   ->closeElement('params')
0241                   ->closeElement('methodResponse');
0242 
0243         return $generator->flush();
0244     }
0245 
0246     /**
0247      * Return XML response
0248      *
0249      * @return string
0250      */
0251     public function __toString()
0252     {
0253         return $this->saveXML();
0254     }
0255 }