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