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