|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00024 require_once 'Zend/XmlRpc/Value.php'; 00025 00029 require_once 'Zend/XmlRpc/Fault.php'; 00030 00042 class Zend_XmlRpc_Response 00043 { 00048 protected $_return; 00049 00054 protected $_type; 00055 00060 protected $_encoding = 'UTF-8'; 00061 00066 protected $_fault = null; 00067 00078 public function __construct($return = null, $type = null) 00079 { 00080 $this->setReturnValue($return, $type); 00081 } 00082 00089 public function setEncoding($encoding) 00090 { 00091 $this->_encoding = $encoding; 00092 Zend_XmlRpc_Value::setEncoding($encoding); 00093 return $this; 00094 } 00095 00101 public function getEncoding() 00102 { 00103 return $this->_encoding; 00104 } 00105 00115 public function setReturnValue($value, $type = null) 00116 { 00117 $this->_return = $value; 00118 $this->_type = (string) $type; 00119 } 00120 00126 public function getReturnValue() 00127 { 00128 return $this->_return; 00129 } 00130 00136 protected function _getXmlRpcReturn() 00137 { 00138 return Zend_XmlRpc_Value::getXmlRpcValue($this->_return); 00139 } 00140 00146 public function isFault() 00147 { 00148 return $this->_fault instanceof Zend_XmlRpc_Fault; 00149 } 00150 00156 public function getFault() 00157 { 00158 return $this->_fault; 00159 } 00160 00171 public function loadXml($response) 00172 { 00173 if (!is_string($response)) { 00174 $this->_fault = new Zend_XmlRpc_Fault(650); 00175 $this->_fault->setEncoding($this->getEncoding()); 00176 return false; 00177 } 00178 00179 try { 00180 $xml = new SimpleXMLElement($response); 00181 } catch (Exception $e) { 00182 // Not valid XML 00183 $this->_fault = new Zend_XmlRpc_Fault(651); 00184 $this->_fault->setEncoding($this->getEncoding()); 00185 return false; 00186 } 00187 00188 if (!empty($xml->fault)) { 00189 // fault response 00190 $this->_fault = new Zend_XmlRpc_Fault(); 00191 $this->_fault->setEncoding($this->getEncoding()); 00192 $this->_fault->loadXml($response); 00193 return false; 00194 } 00195 00196 if (empty($xml->params)) { 00197 // Invalid response 00198 $this->_fault = new Zend_XmlRpc_Fault(652); 00199 $this->_fault->setEncoding($this->getEncoding()); 00200 return false; 00201 } 00202 00203 try { 00204 if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) { 00205 throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML'); 00206 } 00207 $valueXml = $xml->params->param->value->asXML(); 00208 $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING); 00209 } catch (Zend_XmlRpc_Value_Exception $e) { 00210 $this->_fault = new Zend_XmlRpc_Fault(653); 00211 $this->_fault->setEncoding($this->getEncoding()); 00212 return false; 00213 } 00214 00215 $this->setReturnValue($value->getValue()); 00216 return true; 00217 } 00218 00224 public function saveXml() 00225 { 00226 $value = $this->_getXmlRpcReturn(); 00227 $generator = Zend_XmlRpc_Value::getGenerator(); 00228 $generator->openElement('methodResponse') 00229 ->openElement('params') 00230 ->openElement('param'); 00231 $value->generateXml(); 00232 $generator->closeElement('param') 00233 ->closeElement('params') 00234 ->closeElement('methodResponse'); 00235 00236 return $generator->flush(); 00237 } 00238 00244 public function __toString() 00245 { 00246 return $this->saveXML(); 00247 } 00248 }