1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @package Zend_XmlRpc
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Fault.php 20208 2010-01-11 22:37:37Z lars $
*/
/**
* Zend_XmlRpc_Value
*/
require_once 'Zend/XmlRpc/Value.php';
/**
* XMLRPC Faults
*
* Container for XMLRPC faults, containing both a code and a message;
* additionally, has methods for determining if an XML response is an XMLRPC
* fault, as well as generating the XML for an XMLRPC fault response.
*
* To allow method chaining, you may only use the {@link getInstance()} factory
* to instantiate a Zend_XmlRpc_Server_Fault.
*
* @category Zend
* @package Zend_XmlRpc
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_XmlRpc_Fault
{
/**
* Fault code
* @var int
*/
protected $_code;
/**
* Fault character encoding
* @var string
*/
protected $_encoding = 'UTF-8';
/**
* Fault message
* @var string
*/
protected $_message;
/**
* Internal fault codes => messages
* @var array
*/
protected $_internal = array(
404 => 'Unknown Error',
// 610 - 619 reflection errors
610 => 'Invalid method class',
611 => 'Unable to attach function or callback; not callable',
612 => 'Unable to load array; not an array',
613 => 'One or more method records are corrupt or otherwise unusable',
// 620 - 629 dispatch errors
620 => 'Method does not exist',
621 => 'Error instantiating class to invoke method',
622 => 'Method missing implementation',
623 => 'Calling parameters do not match signature',
// 630 - 639 request errors
630 => 'Unable to read request',
631 => 'Failed to parse request',
632 => 'Invalid request, no method passed; request must contain a \'methodName\' tag',
633 => 'Param must contain a value',
634 => 'Invalid method name',
635 => 'Invalid XML provided to request',
636 => 'Error creating xmlrpc value',
// 640 - 649 system.* errors
640 => 'Method does not exist',
// 650 - 659 response errors
650 => 'Invalid XML provided for response',
651 => 'Failed to parse response',
652 => 'Invalid response',
653 => 'Invalid XMLRPC value in response',
);
/**
* Constructor
*
* @return Zend_XmlRpc_Fault
*/
public function __construct($code = 404, $message = '')
{
$this->setCode($code);
$code = $this->getCode();
if (empty($message) && isset($this->_internal[$code])) {
$message = $this->_internal[$code];
} elseif (empty($message)) {
$message = 'Unknown error';
}
$this->setMessage($message);
}
/**
* Set the fault code
*
* @param int $code
* @return Zend_XmlRpc_Fault
*/
public function setCode($code)
{
$this->_code = (int) $code;
return $this;
}
/**
* Return fault code
*
* @return int
*/
public function getCode()
{
return $this->_code;
}
/**
* Retrieve fault message
*
* @param string
* @return Zend_XmlRpc_Fault
*/
public function setMessage($message)
{
$this->_message = (string) $message;
return $this;
}
/**
* Retrieve fault message
*
* @return string
*/
public function getMessage()
{
return $this->_message;
}
/**
* Set encoding to use in fault response
*
* @param string $encoding
* @return Zend_XmlRpc_Fault
*/
public function setEncoding($encoding)
{
$this->_encoding = $encoding;
Zend_XmlRpc_Value::setEncoding($encoding);
return $this;
}
/**
* Retrieve current fault encoding
*
* @return string
*/
public function getEncoding()
{
return $this->_encoding;
}
/**
* Load an XMLRPC fault from XML
*
* @param string $fault
* @return boolean Returns true if successfully loaded fault response, false
* if response was not a fault response
* @throws Zend_XmlRpc_Exception if no or faulty XML provided, or if fault
* response does not contain either code or message
*/
public function loadXml($fault)
{
if (!is_string($fault)) {
require_once 'Zend/XmlRpc/Exception.php';
throw new Zend_XmlRpc_Exception('Invalid XML provided to fault');
}
try {
$xml = @new SimpleXMLElement($fault);
} catch (Exception $e) {
// Not valid XML
require_once 'Zend/XmlRpc/Exception.php';
throw new Zend_XmlRpc_Exception('Failed to parse XML fault: ' . $e->getMessage(), 500, $e);
}
// Check for fault
if (!$xml->fault) {
// Not a fault
return false;
}
if (!$xml->fault->value->struct) {
// not a proper fault
require_once 'Zend/XmlRpc/Exception.php';
throw new Zend_XmlRpc_Exception('Invalid fault structure', 500);
}
$structXml = $xml->fault->value->asXML();
$struct = Zend_XmlRpc_Value::getXmlRpcValue($structXml, Zend_XmlRpc_Value::XML_STRING);
$struct = $struct->getValue();
if (isset($struct['faultCode'])) {
$code = $struct['faultCode'];
}
if (isset($struct['faultString'])) {
$message = $struct['faultString'];
}
if (empty($code) && empty($message)) {
require_once 'Zend/XmlRpc/Exception.php';
throw new Zend_XmlRpc_Exception('Fault code and string required');
}
if (empty($code)) {
$code = '404';
}
if (empty($message)) {
if (isset($this->_internal[$code])) {
$message = $this->_internal[$code];
} else {
$message = 'Unknown Error';
}
}
$this->setCode($code);
$this->setMessage($message);
return true;
}
/**
* Determine if an XML response is an XMLRPC fault
*
* @param string $xml
* @return boolean
*/
public static function isFault($xml)
{
$fault = new self();
require_once 'Zend/XmlRpc/Exception.php';
try {
$isFault = $fault->loadXml($xml);
} catch (Zend_XmlRpc_Exception $e) {
$isFault = false;
}
return $isFault;
}
/**
* Serialize fault to XML
*
* @return string
*/
public function saveXml()
{
// Create fault value
$faultStruct = array(
'faultCode' => $this->getCode(),
'faultString' => $this->getMessage()
);
$value = Zend_XmlRpc_Value::getXmlRpcValue($faultStruct);
$generator = Zend_XmlRpc_Value::getGenerator();
$generator->openElement('methodResponse')
->openElement('fault');
$value->generateXml();
$generator->closeElement('fault')
->closeElement('methodResponse');
return $generator->flush();
}
/**
* Return XML fault response
*
* @return string
*/
public function __toString()
{
return $this->saveXML();
}
}
|