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