1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - XML-RPC Server * |
---|
4 | * This file written by Miles Lott <milos@groupwhere.org> * |
---|
5 | * Copyright (C) 2003 Miles Lott * |
---|
6 | * -------------------------------------------------------------------------* |
---|
7 | * This library is free software; you can redistribute it and/or modify it * |
---|
8 | * under the terms of the GNU Lesser General Public License as published by * |
---|
9 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
10 | * or any later version. * |
---|
11 | * This library is distributed in the hope that it will be useful, but * |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
14 | * See the GNU Lesser General Public License for more details. * |
---|
15 | * You should have received a copy of the GNU Lesser General Public License * |
---|
16 | * along with this library; if not, write to the Free Software Foundation, * |
---|
17 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
18 | \**************************************************************************/ |
---|
19 | |
---|
20 | |
---|
21 | class xmlrpc_server extends xmlrpc_server_shared |
---|
22 | { |
---|
23 | var $server = ''; |
---|
24 | var $authed = True; |
---|
25 | var $log = False; //'/tmp/xmlrpc.log'; |
---|
26 | var $last_method = ''; |
---|
27 | |
---|
28 | function xmlrpc_server($dispMap='', $serviceNow=0) |
---|
29 | { |
---|
30 | $this->server = xmlrpc_server_create(); |
---|
31 | if($dispMap) |
---|
32 | { |
---|
33 | $this->dmap = $dispMap; |
---|
34 | if($serviceNow) |
---|
35 | { |
---|
36 | $this->service(); |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | function serializeDebug() |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | function service($r = False) |
---|
46 | { |
---|
47 | if (!$r) // do we have a response, or we need to parse the request |
---|
48 | { |
---|
49 | $r = $this->parseRequest(); |
---|
50 | } |
---|
51 | if(!$r) |
---|
52 | { |
---|
53 | header('WWW-Authenticate: Basic realm="eGroupWare xmlrpc"'); |
---|
54 | header('HTTP/1.0 401 Unauthorized'); |
---|
55 | // for the log: |
---|
56 | $payload = "WWW-Authenticate: Basic realm=\"eGroupWare xmlrpc\"\nHTTP/1.0 401 Unauthorized\n"; |
---|
57 | echo $payload; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | // $payload = '<?xml version="1.0"?\>' . "\n" . $this->serializeDebug() . $r->serialize(); |
---|
62 | // Header("Content-type: text/xml\r\nContent-length: " . strlen($payload)); |
---|
63 | // print $payload; |
---|
64 | echo $r; |
---|
65 | } |
---|
66 | |
---|
67 | if($this->log) |
---|
68 | { |
---|
69 | $fp = fopen($this->log,'a+'); |
---|
70 | fwrite($fp,"\n\n" . date('Y-m-d H:i:s') . " authorized=" |
---|
71 | . ($this->authed ? $GLOBALS['phpgw_info']['user']['account_lid'] : 'False') |
---|
72 | . ", method='$this->last_method'\n"); |
---|
73 | fwrite($fp,"==== GOT ============================\n" . $GLOBALS['HTTP_RAW_POST_DATA'] |
---|
74 | . "\n==== RETURNED =======================\n"); |
---|
75 | fputs($fp,$payload); |
---|
76 | fclose($fp); |
---|
77 | } |
---|
78 | |
---|
79 | if($this->debug) |
---|
80 | { |
---|
81 | $this->echoInput(); |
---|
82 | |
---|
83 | $fp = fopen('/tmp/xmlrpc_debug.out','a+'); |
---|
84 | fputs($fp,$payload); |
---|
85 | fclose($fp); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | function add_to_map($methodname,$function,$sig,$doc) |
---|
90 | { |
---|
91 | xmlrpc_server_register_method($this->server,$methodname,$function); |
---|
92 | // xmlrpc_server_register_method($this->server,$methodname,'xmlrpc_call_wrapper'); |
---|
93 | // $descr = array( |
---|
94 | // 'function' => $function, |
---|
95 | // 'signature' => $sig, |
---|
96 | // 'docstring' => $doc |
---|
97 | // ); |
---|
98 | // xmlrpc_server_set_method_description($this->server,$methodname,$descr); |
---|
99 | |
---|
100 | $this->dmap[$methodname] = array( |
---|
101 | 'function' => $function, |
---|
102 | 'signature' => $sig, |
---|
103 | 'docstring' => $doc |
---|
104 | ); |
---|
105 | } |
---|
106 | |
---|
107 | function verifySignature($in, $sig) |
---|
108 | { |
---|
109 | return array(1); |
---|
110 | |
---|
111 | for($i=0; $i<sizeof($sig); ++$i) |
---|
112 | { |
---|
113 | // check each possible signature in turn |
---|
114 | $cursig = $sig[$i]; |
---|
115 | if (sizeof($cursig) == $in->getNumParams()+1) |
---|
116 | { |
---|
117 | $itsOK = 1; |
---|
118 | for($n=0; $n<$in->getNumParams(); ++$n) |
---|
119 | { |
---|
120 | $p = $in->getParam($n); |
---|
121 | // print "<!-- $p -->\n"; |
---|
122 | if ($p->kindOf() == 'scalar') |
---|
123 | { |
---|
124 | $pt = $p->scalartyp(); |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | $pt = $p->kindOf(); |
---|
129 | } |
---|
130 | // $n+1 as first type of sig is return type |
---|
131 | if ($pt != $cursig[$n+1]) |
---|
132 | { |
---|
133 | $itsOK = 0; |
---|
134 | $pno = $n+1; |
---|
135 | $wanted = $cursig[$n+1]; |
---|
136 | $got = $pt; |
---|
137 | break; |
---|
138 | } |
---|
139 | } |
---|
140 | if ($itsOK) |
---|
141 | { |
---|
142 | return array(1); |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | return array(0, "Wanted $wanted, got $got at param $pno)"); |
---|
147 | } |
---|
148 | |
---|
149 | function parseRequest($data='') |
---|
150 | { |
---|
151 | global $HTTP_RAW_POST_DATA; |
---|
152 | |
---|
153 | if($data == '') |
---|
154 | { |
---|
155 | $data = $HTTP_RAW_POST_DATA; |
---|
156 | } |
---|
157 | // return $this->echoInput($data); |
---|
158 | |
---|
159 | /* Decode to extract methodName */ |
---|
160 | $params = xmlrpc_decode_request($data, &$methName); |
---|
161 | $this->last_method = $methName; |
---|
162 | $syscall = 0; |
---|
163 | |
---|
164 | /* Setup dispatch map based on the function, if this is a system call */ |
---|
165 | if(preg_match('/^system\./', $methName)) |
---|
166 | { |
---|
167 | foreach($GLOBALS['_xmlrpcs_dmap'] as $meth => $dat) |
---|
168 | { |
---|
169 | $this->add_to_map($meth,$dat['function'],$dat['signature'],$dat['docstring']); |
---|
170 | } |
---|
171 | $sysCall = 1; |
---|
172 | $dmap = $this->dmap; |
---|
173 | } |
---|
174 | elseif(preg_match('/^examples\./',$methName) || |
---|
175 | preg_match('/^validator1\./',$methName) || |
---|
176 | preg_match('/^interopEchoTests\./', $methName) |
---|
177 | ) |
---|
178 | { |
---|
179 | $dmap = $this->dmap; |
---|
180 | $sysCall = 1; |
---|
181 | } |
---|
182 | |
---|
183 | /* verify dispatch map, or try to fix it for non-trivial system calls */ |
---|
184 | if(!isset($this->dmap[$methName]['function'])) |
---|
185 | { |
---|
186 | if($sysCall) |
---|
187 | { |
---|
188 | /* Bad or non-existent system call, return error */ |
---|
189 | $r = CreateObject('phpgwapi.xmlrpcresp', |
---|
190 | '', |
---|
191 | $GLOBALS['xmlrpcerr']['unknown_method'], |
---|
192 | $GLOBALS['xmlrpcstr']['unknown_method'] . ': ' . $methName |
---|
193 | ); |
---|
194 | return $r; |
---|
195 | } |
---|
196 | if($this->authed) |
---|
197 | { |
---|
198 | $method = $methName; |
---|
199 | list($app,$class,$method) = explode('.',$methName); |
---|
200 | |
---|
201 | switch($app) |
---|
202 | { |
---|
203 | case 'server': |
---|
204 | case 'phpgwapi': |
---|
205 | /* Server role functions only - api access */ |
---|
206 | if($GLOBALS['phpgw']->acl->get_role() >= PHPGW_ACL_SERVER) |
---|
207 | { |
---|
208 | $dmap = ExecMethod(sprintf('%s.%s.%s','phpgwapi',$class,'list_methods'),'xmlrpc'); |
---|
209 | } |
---|
210 | break; |
---|
211 | case 'service': |
---|
212 | /* Service functions, user-level */ |
---|
213 | $t = 'phpgwapi.' . $class . '.exec'; |
---|
214 | $dmap = ExecMethod($t,array($service,'list_methods','xmlrpc')); |
---|
215 | break; |
---|
216 | default: |
---|
217 | /* User-level application access */ |
---|
218 | if($GLOBALS['phpgw']->acl->check('run',PHPGW_ACL_READ,$app)) |
---|
219 | { |
---|
220 | $dmap = ExecMethod(sprintf('',$app,$class,'list_methods'),'xmlrpc'); |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | $r = CreateObject('phpgwapi.xmlrpcresp', |
---|
225 | '', |
---|
226 | $GLOBALS['xmlrpcerr']['no_access'], |
---|
227 | $GLOBALS['xmlrpcstr']['no_access'] |
---|
228 | ); |
---|
229 | return $r; |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | /* add the functions from preset $dmap OR list_methods() to the server map */ |
---|
236 | foreach($dmap as $meth => $dat) |
---|
237 | { |
---|
238 | $this->add_to_map($meth,$dat['function'],$dat['signature'],$dat['docstring']); |
---|
239 | } |
---|
240 | |
---|
241 | /* _debug_array($this->dmap);exit; */ |
---|
242 | |
---|
243 | /* Now make the call */ |
---|
244 | if(isset($dmap[$methName]['function'])) |
---|
245 | { |
---|
246 | // dispatch if exists |
---|
247 | if(isset($dmap[$methName]['signature'])) |
---|
248 | { |
---|
249 | $sr = $this->verifySignature($m, $dmap[$methName]['signature'] ); |
---|
250 | } |
---|
251 | if((!isset($dmap[$methName]['signature'])) || $sr[0]) |
---|
252 | { |
---|
253 | // if no signature or correct signature |
---|
254 | $r = xmlrpc_server_call_method($this->server,$data,$params); |
---|
255 | } |
---|
256 | else |
---|
257 | { |
---|
258 | $r = CreateObject('phpgwapi.xmlrpcresp', |
---|
259 | '', |
---|
260 | $GLOBALS['xmlrpcerr']['incorrect_params'], |
---|
261 | $GLOBALS['xmlrpcstr']['incorrect_params'] . ': ' . $sr[1] |
---|
262 | ); |
---|
263 | } |
---|
264 | } |
---|
265 | else |
---|
266 | { |
---|
267 | // else prepare error response |
---|
268 | if(!$this->authed) |
---|
269 | { |
---|
270 | // $r = False; |
---|
271 | // send 401 header to force authorization |
---|
272 | $r = CreateObject('phpgwapi.xmlrpcresp', |
---|
273 | CreateObject('phpgwapi.xmlrpcval', |
---|
274 | 'UNAUTHORIZED', |
---|
275 | 'string' |
---|
276 | ) |
---|
277 | ); |
---|
278 | } |
---|
279 | else |
---|
280 | { |
---|
281 | $r = CreateObject('phpgwapi.xmlrpcresp', |
---|
282 | '', |
---|
283 | $GLOBALS['xmlrpcerr']['unknown_method'], |
---|
284 | $GLOBALS['xmlrpcstr']['unknown_method'] . ': ' . $methName |
---|
285 | ); |
---|
286 | } |
---|
287 | } |
---|
288 | xmlrpc_server_destroy($xmlrpc_server); |
---|
289 | return $r; |
---|
290 | } |
---|
291 | |
---|
292 | function echoInput() |
---|
293 | { |
---|
294 | global $HTTP_RAW_POST_DATA; |
---|
295 | |
---|
296 | // a debugging routine: just echos back the input |
---|
297 | // packet as a string value |
---|
298 | |
---|
299 | /* TODO */ |
---|
300 | // $r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval',"'Aha said I: '" . $HTTP_RAW_POST_DATA,'string')); |
---|
301 | return $HTTP_RAW_POST_DATA; |
---|
302 | } |
---|
303 | |
---|
304 | function xmlrpc_custom_error($error_number, $error_string, $filename, $line, $vars) |
---|
305 | { |
---|
306 | if(error_reporting() & $error_number) |
---|
307 | { |
---|
308 | $error_string .= sprintf("\nFilename: %s\nLine: %s",$filename,$line); |
---|
309 | |
---|
310 | xmlrpc_error(1005,$error_string); |
---|
311 | } |
---|
312 | } |
---|
313 | /* |
---|
314 | function xmlrpc_error($error_number, $error_string) |
---|
315 | { |
---|
316 | $values = array( |
---|
317 | 'faultString' => $error_string, |
---|
318 | 'faultCode' => $error_number |
---|
319 | ); |
---|
320 | |
---|
321 | echo xmlrpc_encode_request(NULL,$values); |
---|
322 | |
---|
323 | xmlrpc_server_destroy($GLOBALS['xmlrpc_server']); |
---|
324 | exit; |
---|
325 | } |
---|
326 | */ |
---|
327 | function xmlrpc_error($error_number, $error_string) |
---|
328 | { |
---|
329 | $r = CreateObject('phpgwapi.xmlrpcresp', |
---|
330 | '', |
---|
331 | $error_number, |
---|
332 | $error_string . ': ' . $this->last_method |
---|
333 | ); |
---|
334 | $this->service($r); |
---|
335 | xmlrpc_server_destroy($GLOBALS['xmlrpc_server']); |
---|
336 | exit; |
---|
337 | } |
---|
338 | } |
---|