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