1 | <?php |
---|
2 | // by Edd Dumbill (C) 1999-2001 |
---|
3 | // <edd@usefulinc.com> |
---|
4 | // xmlrpc.inc,v 1.18 2001/07/06 18:23:57 edmundd |
---|
5 | |
---|
6 | // License is granted to use or modify this software ('XML-RPC for PHP') |
---|
7 | // for commercial or non-commercial use provided the copyright of the author |
---|
8 | // is preserved in any distributed or derivative work. |
---|
9 | |
---|
10 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR |
---|
11 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
12 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
13 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
14 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
15 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
16 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
17 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
18 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
19 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
20 | |
---|
21 | |
---|
22 | class xmlrpcmsg |
---|
23 | { |
---|
24 | var $payload; |
---|
25 | var $methodname; |
---|
26 | var $params = array(); |
---|
27 | var $debug = 0; |
---|
28 | |
---|
29 | function xmlrpcmsg($meth, $pars=0) |
---|
30 | { |
---|
31 | $this->methodname = $meth; |
---|
32 | if (is_array($pars) && sizeof($pars)>0) |
---|
33 | { |
---|
34 | for($i=0; $i<sizeof($pars); $i++) |
---|
35 | { |
---|
36 | $this->addParam($pars[$i]); |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | function xml_header() |
---|
42 | { |
---|
43 | return '<?xml version="1.0"?>' . "\n" . '<methodCall>' . "\n"; |
---|
44 | } |
---|
45 | |
---|
46 | function xml_footer() |
---|
47 | { |
---|
48 | return '</methodCall>' . "\n"; |
---|
49 | } |
---|
50 | |
---|
51 | function createPayload() |
---|
52 | { |
---|
53 | $this->payload = $this->xml_header(); |
---|
54 | $this->payload .= '<methodName>' . $this->methodname . '</methodName>' . "\n"; |
---|
55 | if (sizeof($this->params)) |
---|
56 | { |
---|
57 | $this->payload .= '<params>' . "\n"; |
---|
58 | for($i=0; $i<sizeof($this->params); $i++) |
---|
59 | { |
---|
60 | $p = $this->params[$i]; |
---|
61 | $this->payload .= '<param>' . "\n" . $p->serialize() . '</param>' . "\n"; |
---|
62 | } |
---|
63 | $this->payload .= '</params>' . "\n"; |
---|
64 | } |
---|
65 | $this->payload .= $this->xml_footer(); |
---|
66 | $this->payload = str_replace("\n", "\r\n", $this->payload); |
---|
67 | } |
---|
68 | |
---|
69 | function method($meth='') |
---|
70 | { |
---|
71 | if ($meth != '') |
---|
72 | { |
---|
73 | $this->methodname = $meth; |
---|
74 | } |
---|
75 | return $this->methodname; |
---|
76 | } |
---|
77 | |
---|
78 | function serialize() |
---|
79 | { |
---|
80 | $this->createPayload(); |
---|
81 | return $this->payload; |
---|
82 | } |
---|
83 | |
---|
84 | function addParam($par) |
---|
85 | { |
---|
86 | $this->params[] = $par; |
---|
87 | } |
---|
88 | |
---|
89 | function getParam($i) |
---|
90 | { |
---|
91 | return $this->params[$i]; |
---|
92 | } |
---|
93 | |
---|
94 | function getNumParams() |
---|
95 | { |
---|
96 | return sizeof($this->params); |
---|
97 | } |
---|
98 | |
---|
99 | function parseResponseFile($fp) |
---|
100 | { |
---|
101 | $ipd = ''; |
---|
102 | |
---|
103 | while($data = fread($fp, 32768)) |
---|
104 | { |
---|
105 | $ipd .= $data; |
---|
106 | } |
---|
107 | /* echo $ipd;exit; */ |
---|
108 | return $this->parseResponse($ipd); |
---|
109 | } |
---|
110 | |
---|
111 | function parseResponse($data='') |
---|
112 | { |
---|
113 | $parser = xml_parser_create($GLOBALS['xmlrpc_defencoding']); |
---|
114 | |
---|
115 | $GLOBALS['_xh'][$parser] = array(); |
---|
116 | $GLOBALS['_xh'][$parser]['st'] = ''; |
---|
117 | $GLOBALS['_xh'][$parser]['cm'] = 0; |
---|
118 | $GLOBALS['_xh'][$parser]['isf'] = 0; |
---|
119 | $GLOBALS['_xh'][$parser]['ac'] = ''; |
---|
120 | $GLOBALS['_xh'][$parser]['qt'] = ''; |
---|
121 | $GLOBALS['_xh'][$parser]['ha'] = ''; |
---|
122 | |
---|
123 | xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
---|
124 | xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee'); |
---|
125 | xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
---|
126 | xml_set_default_handler($parser, 'xmlrpc_dh'); |
---|
127 | // $xmlrpc_value = CreateObject('phpgwapi.xmlrpcval'); |
---|
128 | |
---|
129 | $hdrfnd = 0; |
---|
130 | if ($this->debug) |
---|
131 | { |
---|
132 | echo '<PRE style="text-align: left;">---GOT---' . "\n" . htmlspecialchars($data) . "\n" . '---END---' . "\n" . '</PRE>'; |
---|
133 | } |
---|
134 | if ($data == '') |
---|
135 | { |
---|
136 | error_log('No response received from server.'); |
---|
137 | $r = CreateObject( |
---|
138 | 'phpgwapi.xmlrpcresp', |
---|
139 | 0, |
---|
140 | $GLOBALS['xmlrpcerr']['no_data'], |
---|
141 | $GLOBALS['xmlrpcstr']['no_data'] |
---|
142 | ); |
---|
143 | xml_parser_free($parser); |
---|
144 | return $r; |
---|
145 | } |
---|
146 | |
---|
147 | // see if we got an HTTP 200 OK, else bomb |
---|
148 | // but only do this if we're using the HTTP protocol. |
---|
149 | if (ereg("^HTTP",$data) && !ereg("^HTTP/[0-9.]+ 200 ", $data)) |
---|
150 | { |
---|
151 | $errstr = substr($data, 0, strpos($data, "\n")-1); |
---|
152 | error_log('HTTP error, got response: ' .$errstr); |
---|
153 | $r = CreateObject('phpgwapi.xmlrpcresp','', $GLOBALS['xmlrpcerr']['http_error'], |
---|
154 | $GLOBALS['xmlrpcstr']['http_error'] . ' (' . $errstr . ')'); |
---|
155 | xml_parser_free($parser); |
---|
156 | return $r; |
---|
157 | } |
---|
158 | |
---|
159 | // if using HTTP, then gotta get rid of HTTP headers here |
---|
160 | // and we store them in the 'ha' bit of our data array |
---|
161 | if (ereg("^HTTP", $data)) |
---|
162 | { |
---|
163 | $ar=explode("\r\n", $data); |
---|
164 | $newdata = ''; |
---|
165 | $hdrfnd = 0; |
---|
166 | for ($i=0; $i<sizeof($ar); $i++) |
---|
167 | { |
---|
168 | if (!$hdrfnd) |
---|
169 | { |
---|
170 | if (strlen($ar[$i])>0) |
---|
171 | { |
---|
172 | $GLOBALS['_xh'][$parser]['ha'] .= $ar[$i]. "\r\n"; |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | $hdrfnd=1; |
---|
177 | } |
---|
178 | } |
---|
179 | else |
---|
180 | { |
---|
181 | $newdata.=$ar[$i] . "\r\n"; |
---|
182 | } |
---|
183 | } |
---|
184 | $data=$newdata; |
---|
185 | } |
---|
186 | |
---|
187 | if (!xml_parse($parser, $data, sizeof($data))) |
---|
188 | { |
---|
189 | // thanks to Peter Kocks <peter.kocks@baygate.com> |
---|
190 | if((xml_get_current_line_number($parser)) == 1) |
---|
191 | { |
---|
192 | $errstr = 'XML error at line 1, check URL'; |
---|
193 | } |
---|
194 | else |
---|
195 | { |
---|
196 | $errstr = sprintf('XML error: %s at line %d', |
---|
197 | xml_error_string(xml_get_error_code($parser)), |
---|
198 | xml_get_current_line_number($parser)); |
---|
199 | } |
---|
200 | error_log($errstr); |
---|
201 | $r = CreateObject('phpgwapi.xmlrpcresp', '', $GLOBALS['xmlrpcerr']['invalid_return'],$GLOBALS['xmlrpcstr']['invalid_return']); |
---|
202 | xml_parser_free($parser); |
---|
203 | return $r; |
---|
204 | } |
---|
205 | xml_parser_free($parser); |
---|
206 | if ($this->debug) |
---|
207 | { |
---|
208 | echo '<PRE style="text-align: left;">---EVALING---[' |
---|
209 | . strlen($GLOBALS['_xh'][$parser]['st']) . ' chars]---' . "\n" |
---|
210 | . htmlspecialchars($GLOBALS['_xh'][$parser]['st']) . ';' . "\n" . '---END---</PRE>'; |
---|
211 | } |
---|
212 | if (strlen($GLOBALS['_xh'][$parser]['st']) == 0) |
---|
213 | { |
---|
214 | // then something odd has happened |
---|
215 | // and it's time to generate a client side error |
---|
216 | // indicating something odd went on |
---|
217 | $r = CreateObject('phpgwapi.xmlrpcresp', '', $GLOBALS['xmlrpcerr']['invalid_return'],$GLOBALS['xmlrpcstr']['invalid_return']); |
---|
218 | } |
---|
219 | else |
---|
220 | { |
---|
221 | $code = '$v=' . $GLOBALS['_xh'][$parser]['st'] . '; $allOK=1;'; |
---|
222 | $code = str_replace(',,',",'',",$code); |
---|
223 | eval($code); |
---|
224 | if ($GLOBALS['_xh'][$parser]['isf']) |
---|
225 | { |
---|
226 | $f = $v->structmem('faultCode'); |
---|
227 | $fs = $v->structmem('faultString'); |
---|
228 | $r = CreateObject('phpgwapi.xmlrpcresp',$v, $f->scalarval(), $fs->scalarval()); |
---|
229 | } |
---|
230 | else |
---|
231 | { |
---|
232 | $r = CreateObject('phpgwapi.xmlrpcresp',$v); |
---|
233 | } |
---|
234 | } |
---|
235 | $r->hdrs = $GLOBALS['_xh'][$parser]['ha']; //split("\r?\n", $GLOBALS['_xh'][$parser]['ha'][1]); |
---|
236 | return $r; |
---|
237 | } |
---|
238 | } |
---|
239 | ?> |
---|