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