1 | <?php |
---|
2 | /* |
---|
3 | |
---|
4 | SOAPx4 |
---|
5 | by Dietrich Ayala (C) 2001 dietrich@ganx4.com |
---|
6 | |
---|
7 | This project began based on code from the 2 projects below, |
---|
8 | and still contains some original code. The licenses of both must be respected. |
---|
9 | |
---|
10 | XML-RPC for PHP |
---|
11 | originally by Edd Dumbill (C) 1999-2000 |
---|
12 | |
---|
13 | SOAP for PHP |
---|
14 | by Victor Zou (C) 2000-2001 <victor@gigaideas.com.cn> |
---|
15 | |
---|
16 | */ |
---|
17 | |
---|
18 | /* changelog: |
---|
19 | 2001-07-04 |
---|
20 | - abstract type system to support either 1999 or 2001 schema (arg, typing still needs much |
---|
21 | solidification.) |
---|
22 | - implemented proxy support, based on sample code from miles lott <milos@speakeasy.net> |
---|
23 | - much general cleanup of code & cleaned out what was left of original xml-rpc/gigaideas code |
---|
24 | - implemented a transport argument into send() that allows you to specify different transports |
---|
25 | (assuming you have implemented the function, and added it to the conditional statement in send() |
---|
26 | - abstracted the determination of charset in Content-type header |
---|
27 | 2001-07-5 |
---|
28 | - fixed more weird type/namespace issues |
---|
29 | */ |
---|
30 | |
---|
31 | // $path can be a complete endpoint url, with the other parameters left blank: |
---|
32 | // $soap_client = new soap_client("http://path/to/soap/server"); |
---|
33 | |
---|
34 | |
---|
35 | // soap value object |
---|
36 | class soapval |
---|
37 | { |
---|
38 | // function soapval($name='',$type=False,$value=-1,$namespace=False,$type_namespace=False) |
---|
39 | function soapval($name='',$type=False,$value=0,$namespace=False,$type_namespace=False) |
---|
40 | { |
---|
41 | // detect type if not passed |
---|
42 | if(!$type) |
---|
43 | { |
---|
44 | if(is_array($value) && count($value) >= 1) |
---|
45 | { |
---|
46 | if(ereg("[a-zA-Z0-9\-]*",key($v))) |
---|
47 | { |
---|
48 | $type = 'struct'; |
---|
49 | } |
---|
50 | else |
---|
51 | { |
---|
52 | $type = 'array'; |
---|
53 | } |
---|
54 | } |
---|
55 | elseif(is_int($v)) |
---|
56 | { |
---|
57 | $type = 'int'; |
---|
58 | } |
---|
59 | elseif(is_float($v) || $v == 'NaN' || $v == 'INF') |
---|
60 | { |
---|
61 | $type = 'float'; |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | $type = gettype($value); |
---|
66 | } |
---|
67 | } |
---|
68 | // php type name mangle |
---|
69 | if($type == 'integer') |
---|
70 | { |
---|
71 | $type = 'int'; |
---|
72 | } |
---|
73 | |
---|
74 | $this->soapTypes = $GLOBALS['soapTypes']; |
---|
75 | $this->name = $name; |
---|
76 | $this->value = ''; |
---|
77 | $this->type = $type; |
---|
78 | $this->type_code = 0; |
---|
79 | $this->type_prefix = false; |
---|
80 | $this->array_type = ''; |
---|
81 | $this->debug_flag = False; |
---|
82 | $this->debug_str = ''; |
---|
83 | $this->debug("Entering soapval - name: '$name' type: '$type'"); |
---|
84 | |
---|
85 | if($namespace) |
---|
86 | { |
---|
87 | $this->namespace = $namespace; |
---|
88 | if(!isset($GLOBALS['namespaces'][$namespace])) |
---|
89 | { |
---|
90 | $GLOBALS['namespaces'][$namespace] = "ns".(count($GLOBALS['namespaces'])+1); |
---|
91 | } |
---|
92 | $this->prefix = $GLOBALS['namespaces'][$namespace]; |
---|
93 | } |
---|
94 | |
---|
95 | // get type prefix |
---|
96 | if(ereg(":",$type)) |
---|
97 | { |
---|
98 | $this->type = substr(strrchr($type,':'),1,strlen(strrchr($type,':'))); |
---|
99 | $this->type_prefix = substr($type,0,strpos($type,':')); |
---|
100 | } |
---|
101 | elseif($type_namespace) |
---|
102 | { |
---|
103 | if(!isset($GLOBALS['namespaces'][$type_namespace])) |
---|
104 | { |
---|
105 | $GLOBALS['namespaces'][$type_namespace] = 'ns'.(count($GLOBALS['namespaces'])+1); |
---|
106 | } |
---|
107 | $this->type_prefix = $GLOBALS['namespaces'][$type_namespace]; |
---|
108 | } |
---|
109 | |
---|
110 | // if type namespace was not explicitly passed, and we're not in a method struct: |
---|
111 | elseif(!$this->type_prefix && !isset($this->namespace)) |
---|
112 | { |
---|
113 | // try to get type prefix from typeMap |
---|
114 | if(!$this->type_prefix = $this->verify_type($type)) |
---|
115 | { |
---|
116 | // else default to method namespace |
---|
117 | $this->type_prefix = $GLOBALS['namespaces'][$GLOBALS['methodNamespace']]; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | // if scalar |
---|
122 | if($this->soapTypes[$this->type] == 1) |
---|
123 | { |
---|
124 | $this->type_code = 1; |
---|
125 | $this->addScalar($value,$this->type,$name); |
---|
126 | // if array |
---|
127 | } |
---|
128 | elseif($this->soapTypes[$this->type] == 2) |
---|
129 | { |
---|
130 | $this->type_code = 2; |
---|
131 | $this->addArray($value); |
---|
132 | // if struct |
---|
133 | } |
---|
134 | elseif($this->soapTypes[$this->type] == 3) |
---|
135 | { |
---|
136 | $this->type_code = 3; |
---|
137 | $this->addStruct($value); |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | //if($namespace == $GLOBALS['methodNamespace']){ |
---|
142 | $this->type_code = 3; |
---|
143 | $this->addStruct($value); |
---|
144 | //} |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | function addScalar($value, $type, $name="") |
---|
149 | { |
---|
150 | $this->debug("adding scalar '$name' of type '$type'"); |
---|
151 | |
---|
152 | // if boolean, change value to 1 or 0 |
---|
153 | if ($type == "boolean") |
---|
154 | { |
---|
155 | if((strcasecmp($value,"true") == 0) || ($value == 1)) |
---|
156 | { |
---|
157 | $value = 1; |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | $value = 0; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | $this->value = $value; |
---|
166 | return true; |
---|
167 | } |
---|
168 | |
---|
169 | function addArray($vals) |
---|
170 | { |
---|
171 | $this->debug("adding array '$this->name' with ".count($vals)." vals"); |
---|
172 | $this->value = array(); |
---|
173 | if(is_array($vals) && count($vals) >= 1) |
---|
174 | { |
---|
175 | @reset($vals); |
---|
176 | while(list($k,$v) = @each($vals)) |
---|
177 | /* foreach($vals as $k => $v) */ |
---|
178 | { |
---|
179 | $this->debug("checking value $k : $v"); |
---|
180 | // if soapval, add.. |
---|
181 | if(get_class($v) == 'soapval') |
---|
182 | { |
---|
183 | $this->value[] = $v; |
---|
184 | $this->debug($v->debug_str); |
---|
185 | // else make obj and serialize |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | if(is_array($v)) |
---|
190 | { |
---|
191 | if(ereg("[a-zA-Z\-]*",key($v))) |
---|
192 | { |
---|
193 | $type = 'struct'; |
---|
194 | } |
---|
195 | else |
---|
196 | { |
---|
197 | $type = 'array'; |
---|
198 | } |
---|
199 | } |
---|
200 | elseif(!ereg("^[0-9]*$",$k) && in_array($k,array_keys($this->soapTypes))) |
---|
201 | { |
---|
202 | $type = $k; |
---|
203 | } |
---|
204 | elseif(is_int($v)) |
---|
205 | { |
---|
206 | $type = 'int'; |
---|
207 | } |
---|
208 | elseif(is_float($v) || $v == 'NaN' || $v == 'INF') |
---|
209 | { |
---|
210 | $type = 'float'; |
---|
211 | } |
---|
212 | else |
---|
213 | { |
---|
214 | $type = gettype($v); |
---|
215 | } |
---|
216 | $new_val = CreateObject('phpgwapi.soapval','item',$type,$v); |
---|
217 | $this->debug($new_val->debug_str); |
---|
218 | $this->value[] = $new_val; |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | return true; |
---|
223 | } |
---|
224 | |
---|
225 | function addStruct($vals) |
---|
226 | { |
---|
227 | $this->debug("adding struct '$this->name' with ".count($vals).' vals'); |
---|
228 | if(is_array($vals) && count($vals) >= 1) |
---|
229 | { |
---|
230 | @reset($vals); |
---|
231 | while(list($k,$v) = @each($vals)) |
---|
232 | /* foreach($vals as $k => $v) */ |
---|
233 | { |
---|
234 | // if serialize, if soapval |
---|
235 | if(get_class($v) == 'soapval') |
---|
236 | { |
---|
237 | $this->value[] = $v; |
---|
238 | $this->debug($v->debug_str); |
---|
239 | // else make obj and serialize |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | if(is_array($v)) |
---|
244 | { |
---|
245 | @reset($v); |
---|
246 | while(list($a,$b) = @each($v)) |
---|
247 | /* foreach($v as $a => $b) */ |
---|
248 | { |
---|
249 | if($a == "0") |
---|
250 | { |
---|
251 | $type = 'array'; |
---|
252 | } |
---|
253 | else |
---|
254 | { |
---|
255 | $type = 'struct'; |
---|
256 | } |
---|
257 | break; |
---|
258 | } |
---|
259 | } |
---|
260 | // elseif(is_array($k) && in_array($k,array_keys($this->soapTypes))) |
---|
261 | elseif(is_array($k,in_array($k,array_keys($this->soapTypes)))) |
---|
262 | { |
---|
263 | $this->debug("got type '$type' for value '$v' from soapTypes array!"); |
---|
264 | $type = $k; |
---|
265 | } |
---|
266 | elseif(is_int($v)) |
---|
267 | { |
---|
268 | $type = 'int'; |
---|
269 | } |
---|
270 | elseif(is_float($v) || $v == "NaN" || $v == "INF") |
---|
271 | { |
---|
272 | $type = 'float'; |
---|
273 | } |
---|
274 | else |
---|
275 | { |
---|
276 | $type = gettype($v); |
---|
277 | $this->debug("got type '$type' for value '$v' from php gettype()!"); |
---|
278 | } |
---|
279 | $new_val = CreateObject('phpgwapi.soapval',$k,$type,$v); |
---|
280 | $this->debug($new_val->debug_str); |
---|
281 | $this->value[] = $new_val; |
---|
282 | } |
---|
283 | } |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | $this->value = array(); |
---|
288 | } |
---|
289 | return true; |
---|
290 | } |
---|
291 | |
---|
292 | // turn soapvals into xml, woohoo! |
---|
293 | function serializeval($soapval=false) |
---|
294 | { |
---|
295 | if(!$soapval) |
---|
296 | { |
---|
297 | $soapval = $this; |
---|
298 | } |
---|
299 | $this->debug("serializing '$soapval->name' of type '$soapval->type'"); |
---|
300 | if($soapval->name == '') |
---|
301 | { |
---|
302 | $soapval->name = 'return'; |
---|
303 | } |
---|
304 | |
---|
305 | switch($soapval->type_code) |
---|
306 | { |
---|
307 | case 3: |
---|
308 | // struct |
---|
309 | $this->debug('got a struct'); |
---|
310 | if($soapval->prefix && $soapval->type_prefix) |
---|
311 | { |
---|
312 | $xml .= "<$soapval->prefix:$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">\n"; |
---|
313 | } |
---|
314 | elseif($soapval->type_prefix) |
---|
315 | { |
---|
316 | $xml .= "<$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">\n"; |
---|
317 | } |
---|
318 | elseif($soapval->prefix) |
---|
319 | { |
---|
320 | $xml .= "<$soapval->prefix:$soapval->name>\n"; |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | $xml .= "<$soapval->name>\n"; |
---|
325 | } |
---|
326 | if(is_array($soapval->value)) |
---|
327 | { |
---|
328 | @reset($soapval->value); |
---|
329 | while(list($k,$v) = @each($soapval->value)) |
---|
330 | /* foreach($soapval->value as $k => $v) */ |
---|
331 | { |
---|
332 | $xml .= $this->serializeval($v); |
---|
333 | } |
---|
334 | } |
---|
335 | if($soapval->prefix) |
---|
336 | { |
---|
337 | $xml .= "</$soapval->prefix:$soapval->name>\n"; |
---|
338 | } |
---|
339 | else |
---|
340 | { |
---|
341 | $xml .= "</$soapval->name>\n"; |
---|
342 | } |
---|
343 | break; |
---|
344 | case 2: |
---|
345 | // array |
---|
346 | @reset($soapval->value); |
---|
347 | while(list($null,$array_val) = @each($soapval->value)) |
---|
348 | /* foreach($soapval->value as $array_val) */ |
---|
349 | { |
---|
350 | $array_types[$array_val->type] = 1; |
---|
351 | $xml .= $this->serializeval($array_val); |
---|
352 | } |
---|
353 | if(count($array_types) > 1) |
---|
354 | { |
---|
355 | $array_type = 'xsd:ur-type'; |
---|
356 | } |
---|
357 | elseif(count($array_types) >= 1) |
---|
358 | { |
---|
359 | $array_type = $array_val->type_prefix.":".$array_val->type; |
---|
360 | } |
---|
361 | |
---|
362 | $xml = "<$soapval->name xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"".$array_type."[".sizeof($soapval->value)."]\">\n".$xml."</$soapval->name>\n"; |
---|
363 | break; |
---|
364 | case 1: |
---|
365 | $xml .= "<$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">$soapval->value</$soapval->name>\n"; |
---|
366 | break; |
---|
367 | default: |
---|
368 | break; |
---|
369 | } |
---|
370 | return $xml; |
---|
371 | } |
---|
372 | |
---|
373 | // serialize |
---|
374 | function serialize() |
---|
375 | { |
---|
376 | return $this->serializeval($this); |
---|
377 | } |
---|
378 | |
---|
379 | function decode($soapval=false) |
---|
380 | { |
---|
381 | if(!$soapval) |
---|
382 | { |
---|
383 | $soapval = $this; |
---|
384 | } |
---|
385 | // scalar decode |
---|
386 | if($soapval->type_code == 1) |
---|
387 | { |
---|
388 | return $soapval->value; |
---|
389 | // array decode |
---|
390 | } |
---|
391 | elseif($soapval->type_code == 2) |
---|
392 | { |
---|
393 | if(is_array($soapval->value)) |
---|
394 | { |
---|
395 | @reset($soapval->value); |
---|
396 | while(list($null,$item) = @each($soapval->value)) |
---|
397 | /* foreach($soapval->value as $item) */ |
---|
398 | { |
---|
399 | $return[] = $this->decode($item); |
---|
400 | } |
---|
401 | return $return; |
---|
402 | } |
---|
403 | else |
---|
404 | { |
---|
405 | return array(); |
---|
406 | } |
---|
407 | // struct decode |
---|
408 | } |
---|
409 | elseif($soapval->type_code == 3) |
---|
410 | { |
---|
411 | if(is_array($soapval->value)) |
---|
412 | { |
---|
413 | @reset($soapval->value); |
---|
414 | while(list($null,$item) = @each($soapval->value)) |
---|
415 | /* foreach($soapval->value as $item) */ |
---|
416 | { |
---|
417 | $return[$item->name] = $this->decode($item); |
---|
418 | } |
---|
419 | return $return; |
---|
420 | } |
---|
421 | else |
---|
422 | { |
---|
423 | return array(); |
---|
424 | } |
---|
425 | } |
---|
426 | } |
---|
427 | |
---|
428 | // verify type |
---|
429 | function verify_type($type) |
---|
430 | { |
---|
431 | if ($type) |
---|
432 | { |
---|
433 | // global $GLOBALS['namespaces'],$GLOBALS['soapTypes'],$GLOBALS['typemap']; |
---|
434 | // global $GLOBALS['namespaces'],$GLOBALS['typemap']; |
---|
435 | |
---|
436 | @reset($GLOBALS['typemap']); |
---|
437 | while(list($namespace,$types) = @each($GLOBALS['typemap'])) |
---|
438 | /* foreach($GLOBALS['typemap'] as $namespace => $types) */ |
---|
439 | { |
---|
440 | if(in_array($type,$types)) |
---|
441 | { |
---|
442 | return $GLOBALS['namespaces'][$namespace]; |
---|
443 | } |
---|
444 | } |
---|
445 | } |
---|
446 | return false; |
---|
447 | } |
---|
448 | |
---|
449 | // alias for verify_type() - pass it a type, and it returns it's prefix |
---|
450 | function get_prefix($type) |
---|
451 | { |
---|
452 | if($prefix = $this->verify_type($type)) |
---|
453 | { |
---|
454 | return $prefix; |
---|
455 | } |
---|
456 | return false; |
---|
457 | } |
---|
458 | |
---|
459 | function debug($string) |
---|
460 | { |
---|
461 | if($this->debug_flag) |
---|
462 | { |
---|
463 | $this->debug_str .= "$string\n"; |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | ?> |
---|