|
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 try { |
|
180 $useInternalXmlErrors = libxml_use_internal_errors(true); |
|
181 $xml = new SimpleXMLElement($response); |
|
182 libxml_use_internal_errors($useInternalXmlErrors); |
|
183 } catch (Exception $e) { |
|
184 libxml_use_internal_errors($useInternalXmlErrors); |
|
185 // Not valid XML |
|
186 $this->_fault = new Zend_XmlRpc_Fault(651); |
|
187 $this->_fault->setEncoding($this->getEncoding()); |
|
188 return false; |
|
189 } |
|
190 |
|
191 if (!empty($xml->fault)) { |
|
192 // fault response |
|
193 $this->_fault = new Zend_XmlRpc_Fault(); |
|
194 $this->_fault->setEncoding($this->getEncoding()); |
|
195 $this->_fault->loadXml($response); |
|
196 return false; |
|
197 } |
|
198 |
|
199 if (empty($xml->params)) { |
|
200 // Invalid response |
|
201 $this->_fault = new Zend_XmlRpc_Fault(652); |
|
202 $this->_fault->setEncoding($this->getEncoding()); |
|
203 return false; |
|
204 } |
|
205 |
|
206 try { |
|
207 if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) { |
|
208 throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML'); |
|
209 } |
|
210 $valueXml = $xml->params->param->value->asXML(); |
|
211 $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING); |
|
212 } catch (Zend_XmlRpc_Value_Exception $e) { |
|
213 $this->_fault = new Zend_XmlRpc_Fault(653); |
|
214 $this->_fault->setEncoding($this->getEncoding()); |
|
215 return false; |
|
216 } |
|
217 |
|
218 $this->setReturnValue($value->getValue()); |
|
219 return true; |
|
220 } |
|
221 |
|
222 /** |
|
223 * Return response as XML |
|
224 * |
|
225 * @return string |
|
226 */ |
|
227 public function saveXml() |
|
228 { |
|
229 $value = $this->_getXmlRpcReturn(); |
|
230 $generator = Zend_XmlRpc_Value::getGenerator(); |
|
231 $generator->openElement('methodResponse') |
|
232 ->openElement('params') |
|
233 ->openElement('param'); |
|
234 $value->generateXml(); |
|
235 $generator->closeElement('param') |
|
236 ->closeElement('params') |
|
237 ->closeElement('methodResponse'); |
|
238 |
|
239 return $generator->flush(); |
|
240 } |
|
241 |
|
242 /** |
|
243 * Return XML response |
|
244 * |
|
245 * @return string |
|
246 */ |
|
247 public function __toString() |
|
248 { |
|
249 return $this->saveXML(); |
|
250 } |
|
251 } |