1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpXmlRpc; |
4
|
|
|
|
5
|
|
|
use PhpXmlRpc\Helper\Charset; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class provides the representation of the response of an XML-RPC server. |
9
|
|
|
* Server-side, a server method handler will construct a Response and pass it as its return value. |
10
|
|
|
* An identical Response object will be returned by the result of an invocation of the send() method of the Client class. |
11
|
|
|
*/ |
12
|
|
|
class Response |
13
|
|
|
{ |
14
|
|
|
/// @todo: do these need to be public? |
15
|
|
|
public $val = 0; |
16
|
|
|
public $valtyp; |
17
|
|
|
public $errno = 0; |
18
|
|
|
public $errstr = ''; |
19
|
|
|
public $payload; |
20
|
|
|
public $hdrs = array(); |
21
|
|
|
public $_cookies = array(); |
22
|
|
|
public $content_type = 'text/xml'; |
23
|
|
|
public $raw_data = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param mixed $val either a Value object, a php value or the xml serialization of an xmlrpc value (a string) |
27
|
|
|
* @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded |
28
|
|
|
* @param string $fString the error string, in case of an error response |
29
|
|
|
* @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let |
30
|
|
|
* the code guess the correct type. |
31
|
|
|
* |
32
|
|
|
* @todo add check that $val / $fCode / $fString is of correct type??? |
33
|
|
|
* NB: as of now we do not do it, since it might be either an xmlrpc value or a plain php val, or a complete |
34
|
|
|
* xml chunk, depending on usage of Client::send() inside which creator is called... |
35
|
|
|
*/ |
36
|
603 |
|
public function __construct($val, $fCode = 0, $fString = '', $valType = '') |
37
|
|
|
{ |
38
|
603 |
|
if ($fCode != 0) { |
39
|
|
|
// error response |
40
|
145 |
|
$this->errno = $fCode; |
41
|
145 |
|
$this->errstr = $fString; |
42
|
|
|
} else { |
43
|
|
|
// successful response |
44
|
594 |
|
$this->val = $val; |
45
|
594 |
|
if ($valType == '') { |
46
|
|
|
// user did not declare type of response value: try to guess it |
47
|
491 |
|
if (is_object($this->val) && is_a($this->val, 'PhpXmlRpc\Value')) { |
48
|
491 |
|
$this->valtyp = 'xmlrpcvals'; |
49
|
|
|
} elseif (is_string($this->val)) { |
50
|
|
|
$this->valtyp = 'xml'; |
51
|
|
|
} else { |
52
|
25 |
|
$this->valtyp = 'phpvals'; |
53
|
|
|
} |
54
|
|
|
} else { |
55
|
|
|
// user declares type of resp value: believe him |
56
|
593 |
|
$this->valtyp = $valType; |
57
|
|
|
} |
58
|
|
|
} |
59
|
603 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the error code of the response. |
63
|
|
|
* |
64
|
|
|
* @return integer the error code of this response (0 for not-error responses) |
65
|
|
|
*/ |
66
|
591 |
|
public function faultCode() |
67
|
|
|
{ |
68
|
591 |
|
return $this->errno; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the error code of the response. |
73
|
|
|
* |
74
|
|
|
* @return string the error string of this response ('' for not-error responses) |
75
|
|
|
*/ |
76
|
489 |
|
public function faultString() |
77
|
|
|
{ |
78
|
489 |
|
return $this->errstr; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the value received by the server. If the Response's faultCode is non-zero then the value returned by this |
83
|
|
|
* method should not be used (it may not even be an object). |
84
|
|
|
* |
85
|
|
|
* @return Value|string|mixed the Value object returned by the server. Might be an xml string or plain php value |
86
|
|
|
* depending on the convention adopted when creating the Response |
87
|
|
|
*/ |
88
|
591 |
|
public function value() |
89
|
|
|
{ |
90
|
591 |
|
return $this->val; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns an array with the cookies received from the server. |
95
|
|
|
* Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 => $val2, ...) |
96
|
|
|
* with attributes being e.g. 'expires', 'path', domain'. |
97
|
|
|
* NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array. |
98
|
|
|
* It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back |
99
|
|
|
* with the next request to the server (using Client::setCookie) or not. |
100
|
|
|
* |
101
|
|
|
* @return array array of cookies received from the server |
102
|
|
|
*/ |
103
|
20 |
|
public function cookies() |
104
|
|
|
{ |
105
|
20 |
|
return $this->_cookies; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns xml representation of the response. XML prologue not included. |
110
|
|
|
* |
111
|
|
|
* @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed |
112
|
|
|
* |
113
|
|
|
* @return string the xml representation of the response |
114
|
|
|
* |
115
|
|
|
* @throws \Exception |
116
|
|
|
*/ |
117
|
491 |
|
public function serialize($charsetEncoding = '') |
118
|
|
|
{ |
119
|
491 |
|
if ($charsetEncoding != '') { |
120
|
50 |
|
$this->content_type = 'text/xml; charset=' . $charsetEncoding; |
121
|
|
|
} else { |
122
|
441 |
|
$this->content_type = 'text/xml'; |
123
|
|
|
} |
124
|
491 |
|
if (PhpXmlRpc::$xmlrpc_null_apache_encoding) { |
125
|
1 |
|
$result = "<methodResponse xmlns:ex=\"" . PhpXmlRpc::$xmlrpc_null_apache_encoding_ns . "\">\n"; |
126
|
|
|
} else { |
127
|
491 |
|
$result = "<methodResponse>\n"; |
128
|
|
|
} |
129
|
491 |
|
if ($this->errno) { |
130
|
|
|
// Let non-ASCII response messages be tolerated by clients by xml-encoding non ascii chars |
131
|
|
|
$result .= "<fault>\n" . |
132
|
79 |
|
"<value>\n<struct><member><name>faultCode</name>\n<value><int>" . $this->errno . |
133
|
|
|
"</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>" . |
134
|
79 |
|
Charset::instance()->encodeEntities($this->errstr, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "</string></value>\n</member>\n" . |
135
|
79 |
|
"</struct>\n</value>\n</fault>"; |
136
|
|
|
} else { |
137
|
489 |
|
if (!is_object($this->val) || !is_a($this->val, 'PhpXmlRpc\Value')) { |
|
|
|
|
138
|
|
|
if (is_string($this->val) && $this->valtyp == 'xml') { |
|
|
|
|
139
|
|
|
$result .= "<params>\n<param>\n" . |
140
|
|
|
$this->val . |
141
|
|
|
"</param>\n</params>"; |
142
|
|
|
} else { |
143
|
|
|
/// @todo try to build something serializable? |
144
|
|
|
throw new \Exception('cannot serialize xmlrpc response objects whose content is native php values'); |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
$result .= "<params>\n<param>\n" . |
148
|
489 |
|
$this->val->serialize($charsetEncoding) . |
149
|
489 |
|
"</param>\n</params>"; |
150
|
|
|
} |
151
|
|
|
} |
152
|
491 |
|
$result .= "\n</methodResponse>"; |
153
|
491 |
|
$this->payload = $result; |
154
|
|
|
|
155
|
491 |
|
return $result; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|