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_Controller
17: * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18: * @license http://framework.zend.com/license/new-bsd New BSD License
19: */
20:
21: /**
22: * Zend_XmlRpc_Value
23: */
24: #require_once 'Zend/XmlRpc/Value.php';
25:
26: /**
27: * Zend_XmlRpc_Fault
28: */
29: #require_once 'Zend/XmlRpc/Fault.php';
30:
31: /**
32: * XmlRpc Response
33: *
34: * Container for accessing an XMLRPC return value and creating the XML response.
35: *
36: * @category Zend
37: * @package Zend_XmlRpc
38: * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
39: * @license http://framework.zend.com/license/new-bsd New BSD License
40: * @version $Id: Response.php 21359 2010-03-07 00:54:02Z lars $
41: */
42: class Zend_XmlRpc_Response
43: {
44: /**
45: * Return value
46: * @var mixed
47: */
48: protected $_return;
49:
50: /**
51: * Return type
52: * @var string
53: */
54: protected $_type;
55:
56: /**
57: * Response character encoding
58: * @var string
59: */
60: protected $_encoding = 'UTF-8';
61:
62: /**
63: * Fault, if response is a fault response
64: * @var null|Zend_XmlRpc_Fault
65: */
66: protected $_fault = null;
67:
68: /**
69: * Constructor
70: *
71: * Can optionally pass in the return value and type hinting; otherwise, the
72: * return value can be set via {@link setReturnValue()}.
73: *
74: * @param mixed $return
75: * @param string $type
76: * @return void
77: */
78: public function __construct($return = null, $type = null)
79: {
80: $this->setReturnValue($return, $type);
81: }
82:
83: /**
84: * Set encoding to use in response
85: *
86: * @param string $encoding
87: * @return Zend_XmlRpc_Response
88: */
89: public function setEncoding($encoding)
90: {
91: $this->_encoding = $encoding;
92: Zend_XmlRpc_Value::setEncoding($encoding);
93: return $this;
94: }
95:
96: /**
97: * Retrieve current response encoding
98: *
99: * @return string
100: */
101: public function getEncoding()
102: {
103: return $this->_encoding;
104: }
105:
106: /**
107: * Set the return value
108: *
109: * Sets the return value, with optional type hinting if provided.
110: *
111: * @param mixed $value
112: * @param string $type
113: * @return void
114: */
115: public function setReturnValue($value, $type = null)
116: {
117: $this->_return = $value;
118: $this->_type = (string) $type;
119: }
120:
121: /**
122: * Retrieve the return value
123: *
124: * @return mixed
125: */
126: public function getReturnValue()
127: {
128: return $this->_return;
129: }
130:
131: /**
132: * Retrieve the XMLRPC value for the return value
133: *
134: * @return Zend_XmlRpc_Value
135: */
136: protected function _getXmlRpcReturn()
137: {
138: return Zend_XmlRpc_Value::getXmlRpcValue($this->_return);
139: }
140:
141: /**
142: * Is the response a fault response?
143: *
144: * @return boolean
145: */
146: public function isFault()
147: {
148: return $this->_fault instanceof Zend_XmlRpc_Fault;
149: }
150:
151: /**
152: * Returns the fault, if any.
153: *
154: * @return null|Zend_XmlRpc_Fault
155: */
156: public function getFault()
157: {
158: return $this->_fault;
159: }
160:
161: /**
162: * Load a response from an XML response
163: *
164: * Attempts to load a response from an XMLRPC response, autodetecting if it
165: * is a fault response.
166: *
167: * @param string $response
168: * @return boolean True if a valid XMLRPC response, false if a fault
169: * response or invalid input
170: */
171: public function loadXml($response)
172: {
173: if (!is_string($response)) {
174: $this->_fault = new Zend_XmlRpc_Fault(650);
175: $this->_fault->setEncoding($this->getEncoding());
176: return false;
177: }
178:
179: $loadEntities = libxml_disable_entity_loader(true);
180: $useInternalXmlErrors = libxml_use_internal_errors(true);
181: try {
182: $xml = new SimpleXMLElement($response);
183: libxml_disable_entity_loader($loadEntities);
184: libxml_use_internal_errors($useInternalXmlErrors);
185: } catch (Exception $e) {
186: libxml_disable_entity_loader($loadEntities);
187: libxml_use_internal_errors($useInternalXmlErrors);
188: // Not valid XML
189: $this->_fault = new Zend_XmlRpc_Fault(651);
190: $this->_fault->setEncoding($this->getEncoding());
191: return false;
192: }
193:
194: if (!empty($xml->fault)) {
195: // fault response
196: $this->_fault = new Zend_XmlRpc_Fault();
197: $this->_fault->setEncoding($this->getEncoding());
198: $this->_fault->loadXml($response);
199: return false;
200: }
201:
202: if (empty($xml->params)) {
203: // Invalid response
204: $this->_fault = new Zend_XmlRpc_Fault(652);
205: $this->_fault->setEncoding($this->getEncoding());
206: return false;
207: }
208:
209: try {
210: if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
211: throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
212: }
213: $valueXml = $xml->params->param->value->asXML();
214: $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING);
215: } catch (Zend_XmlRpc_Value_Exception $e) {
216: $this->_fault = new Zend_XmlRpc_Fault(653);
217: $this->_fault->setEncoding($this->getEncoding());
218: return false;
219: }
220:
221: $this->setReturnValue($value->getValue());
222: return true;
223: }
224:
225: /**
226: * Return response as XML
227: *
228: * @return string
229: */
230: public function saveXml()
231: {
232: $value = $this->_getXmlRpcReturn();
233: $generator = Zend_XmlRpc_Value::getGenerator();
234: $generator->openElement('methodResponse')
235: ->openElement('params')
236: ->openElement('param');
237: $value->generateXml();
238: $generator->closeElement('param')
239: ->closeElement('params')
240: ->closeElement('methodResponse');
241:
242: return $generator->flush();
243: }
244:
245: /**
246: * Return XML response
247: *
248: * @return string
249: */
250: public function __toString()
251: {
252: return $this->saveXML();
253: }
254: }
255: