|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/XmlRpc/Value.php'; 00026 00042 class Zend_XmlRpc_Fault 00043 { 00048 protected $_code; 00049 00054 protected $_encoding = 'UTF-8'; 00055 00060 protected $_message; 00061 00066 protected $_internal = array( 00067 404 => 'Unknown Error', 00068 00069 // 610 - 619 reflection errors 00070 610 => 'Invalid method class', 00071 611 => 'Unable to attach function or callback; not callable', 00072 612 => 'Unable to load array; not an array', 00073 613 => 'One or more method records are corrupt or otherwise unusable', 00074 00075 // 620 - 629 dispatch errors 00076 620 => 'Method does not exist', 00077 621 => 'Error instantiating class to invoke method', 00078 622 => 'Method missing implementation', 00079 623 => 'Calling parameters do not match signature', 00080 00081 // 630 - 639 request errors 00082 630 => 'Unable to read request', 00083 631 => 'Failed to parse request', 00084 632 => 'Invalid request, no method passed; request must contain a \'methodName\' tag', 00085 633 => 'Param must contain a value', 00086 634 => 'Invalid method name', 00087 635 => 'Invalid XML provided to request', 00088 636 => 'Error creating xmlrpc value', 00089 00090 // 640 - 649 system.* errors 00091 640 => 'Method does not exist', 00092 00093 // 650 - 659 response errors 00094 650 => 'Invalid XML provided for response', 00095 651 => 'Failed to parse response', 00096 652 => 'Invalid response', 00097 653 => 'Invalid XMLRPC value in response', 00098 ); 00099 00105 public function __construct($code = 404, $message = '') 00106 { 00107 $this->setCode($code); 00108 $code = $this->getCode(); 00109 00110 if (empty($message) && isset($this->_internal[$code])) { 00111 $message = $this->_internal[$code]; 00112 } elseif (empty($message)) { 00113 $message = 'Unknown error'; 00114 } 00115 $this->setMessage($message); 00116 } 00117 00124 public function setCode($code) 00125 { 00126 $this->_code = (int) $code; 00127 return $this; 00128 } 00129 00135 public function getCode() 00136 { 00137 return $this->_code; 00138 } 00139 00146 public function setMessage($message) 00147 { 00148 $this->_message = (string) $message; 00149 return $this; 00150 } 00151 00157 public function getMessage() 00158 { 00159 return $this->_message; 00160 } 00161 00168 public function setEncoding($encoding) 00169 { 00170 $this->_encoding = $encoding; 00171 Zend_XmlRpc_Value::setEncoding($encoding); 00172 return $this; 00173 } 00174 00180 public function getEncoding() 00181 { 00182 return $this->_encoding; 00183 } 00184 00194 public function loadXml($fault) 00195 { 00196 if (!is_string($fault)) { 00197 require_once 'Zend/XmlRpc/Exception.php'; 00198 throw new Zend_XmlRpc_Exception('Invalid XML provided to fault'); 00199 } 00200 00201 try { 00202 $xml = @new SimpleXMLElement($fault); 00203 } catch (Exception $e) { 00204 // Not valid XML 00205 require_once 'Zend/XmlRpc/Exception.php'; 00206 throw new Zend_XmlRpc_Exception('Failed to parse XML fault: ' . $e->getMessage(), 500, $e); 00207 } 00208 00209 // Check for fault 00210 if (!$xml->fault) { 00211 // Not a fault 00212 return false; 00213 } 00214 00215 if (!$xml->fault->value->struct) { 00216 // not a proper fault 00217 require_once 'Zend/XmlRpc/Exception.php'; 00218 throw new Zend_XmlRpc_Exception('Invalid fault structure', 500); 00219 } 00220 00221 $structXml = $xml->fault->value->asXML(); 00222 $struct = Zend_XmlRpc_Value::getXmlRpcValue($structXml, Zend_XmlRpc_Value::XML_STRING); 00223 $struct = $struct->getValue(); 00224 00225 if (isset($struct['faultCode'])) { 00226 $code = $struct['faultCode']; 00227 } 00228 if (isset($struct['faultString'])) { 00229 $message = $struct['faultString']; 00230 } 00231 00232 if (empty($code) && empty($message)) { 00233 require_once 'Zend/XmlRpc/Exception.php'; 00234 throw new Zend_XmlRpc_Exception('Fault code and string required'); 00235 } 00236 00237 if (empty($code)) { 00238 $code = '404'; 00239 } 00240 00241 if (empty($message)) { 00242 if (isset($this->_internal[$code])) { 00243 $message = $this->_internal[$code]; 00244 } else { 00245 $message = 'Unknown Error'; 00246 } 00247 } 00248 00249 $this->setCode($code); 00250 $this->setMessage($message); 00251 00252 return true; 00253 } 00254 00261 public static function isFault($xml) 00262 { 00263 $fault = new self(); 00264 require_once 'Zend/XmlRpc/Exception.php'; 00265 try { 00266 $isFault = $fault->loadXml($xml); 00267 } catch (Zend_XmlRpc_Exception $e) { 00268 $isFault = false; 00269 } 00270 00271 return $isFault; 00272 } 00273 00279 public function saveXml() 00280 { 00281 // Create fault value 00282 $faultStruct = array( 00283 'faultCode' => $this->getCode(), 00284 'faultString' => $this->getMessage() 00285 ); 00286 $value = Zend_XmlRpc_Value::getXmlRpcValue($faultStruct); 00287 00288 $generator = Zend_XmlRpc_Value::getGenerator(); 00289 $generator->openElement('methodResponse') 00290 ->openElement('fault'); 00291 $value->generateXml(); 00292 $generator->closeElement('fault') 00293 ->closeElement('methodResponse'); 00294 00295 return $generator->flush(); 00296 } 00297 00303 public function __toString() 00304 { 00305 return $this->saveXML(); 00306 } 00307 }