-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoapserver.php
1762 lines (1612 loc) · 52.8 KB
/
soapserver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*##################### Pagix Content Management System #######################
$Id: soapserver.php 23 2002-10-26 14:32:40Z skulawik $
$Revision: 1.1 $
$Author: skulawik $
$Date: 2002-10-26 16:32:40 +0200 (Sat, 26 Oct 2002) $
###############################################################################
$Log: soapserver.php,v $
Revision 1.1 2002/10/26 14:21:53 skulawik
*** empty log message ***
Revision 2.3 2002/06/14 14:35:38 skulawik
*** empty log message ***
Revision 2.2 2002/04/19 09:00:05 skulawik
Fehler mit der Authentifizierung behoben, die Kontrollfunktion gab keinen Fehler zurück, wenn das nicht-eigene-Web überschrieben werden sollte
Revision 2.1 2002/04/12 12:48:33 skulawik
Versionsinfos eingetragen
###############################################################################
classes/class.soap_server.php
SOAPx4, a SOAP Toolkit for PHP: Server Class
Copyright (C) 2001 Dietrich Ayala <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
This project was inspired by the projects below, many thanks.
XML-RPC for PHP, by Edd Dumbill
SOAP for PHP, by Victor Zou
for example usage of the server, see the test_server.php file that
is included with the distribution.
#############################################################################*/
/*
changelog:
2001-07-05
- detection of character encoding in Content-Type header. server
will now call the soap_parser object with the specified encoding
- server will now return the Content-Type header with the sender's encoding type specified
must still learn more bout encoding, and figure out what i have to do to be able to
make sure that my response is *actually* encoded correctly
2001-07-21
- force proper error reporting for windows compatibility
2001-07-27
- get_all_headers() check for windows compatibility
*/
// make errors handle properly in windows
error_reporting(2039);
class soap_server {
function soap_server() {
// create empty dispatch map
$this->dispatch_map = array();
$this->debug_flag = false;
$this->debug_str = "";
$this->headers = "";
$this->request = "";
$this->xml_encoding = "UTF-8";
$this->fault = false;
$this->fault_code = "";
$this->fault_str = "";
$this->fault_actor = "";
// for logging interop results to db
$this->result = "successful";
}
// parses request and posts response
function service($data){
// $response is a soap_msg object
$response = $this->parse_request($data);
$this->debug("parsed request and got an object of this class '".get_class($response)."'");
$this->debug("server sending...");
// pass along the debug string
if($this->debug_flag){
$response->debug($this->debug_str);
}
$payload = $response->serialize();
// print headers
if($this->fault){
$header[] = "HTTP/1.0 500 Internal Server Error\r\n";
} else {
$header[] = "HTTP/1.0 200 OK\r\n";
$header[] = "Status: 200\r\n";
}
$header[] = "Server: SOAPx4 Server v0.5\r\n";
$header[] = "Connection: Close\r\n";
$header[] = "Content-Type: text/xml; charset=$this->xml_encoding\r\n";
$header[] = "Content-Length: ".strlen($payload)."\r\n\r\n";
reset($header);
foreach($header as $hdr){
header($hdr);
}
print $payload;
}
function parse_request($data="") {
$this->debug("entering parseRequest() on ".date("H:i Y-m-d"));
$this->debug("$request uri: ".$HTTP_SERVER_VARS["REQUEST_URI"]);
// get headers
if(function_exists("getallheaders")){
$this->headers = getallheaders();
foreach($this->headers as $k=>$v){
$dump .= "$k: $v\r\n";
}
// get SOAPAction header
if($headers_array["SOAPAction"]){
$this->SOAPAction = str_replace('"','',$headers_array["SOAPAction"]);
$this->service = $this->SOAPAction;
}
// get the character encoding of the incoming request
if(ereg("=",$headers_array["Content-Type"])){
$enc = str_replace("\"","",substr(strstr($headers_array["Content-Type"],"="),1));
if(eregi("^(ISO-8859-1|US-ASCII|UTF-8)$",$enc)){
$this->xml_encoding = $enc;
} else {
$this->xml_encoding = "us-ascii";
}
}
$this->debug("got encoding: $this->xml_encoding");
}
$this->request = $dump."\r\n\r\n".$data;
// parse response, get soap parser obj
$parser = new soap_parser($data,$this->xml_encoding);
// get/set methodname
$this->methodname = $parser->root_struct_name;
$this->debug("method name: $this->methodname");
// does method exist?
if(function_exists($this->methodname)){
$this->debug("method '$this->methodname' exists");
} else {
// "method not found" fault here
$this->debug("method '$this->methodname' not found!");
$this->result = "fault: method not found";
$this->make_fault("Server","method '$this->methodname' not defined in service '$this->service'");
return $this->fault();
}
// if fault occurred during message parsing
if($parser->fault()){
// parser debug
$this->debug($parser->debug_str);
$this->result = "fault: error in msg parsing or eval";
$this->make_fault("Server","error in msg parsing or eval:\n".$parser->get_response());
// return soapresp
return $this->fault();
// else successfully parsed request into soapval object
} else {
// get eval_str
$this->debug("calling parser->get_response()");
// evaluate it, getting back a soapval object
if(!$request_val = $parser->get_response()){
return $this->fault();
}
// parser debug
$this->debug($parser->debug_str);
if($parser->namespaces["xsd"] != ""){
//print "got ".$parser->namespaces["xsd"];
global $XMLSchemaVersion,$namespaces;
$XMLSchemaVersion = $parser->namespaces["xsd"];
$tmpNS = array_flip($namespaces);
$tmpNS["xsd"] = $XMLSchemaVersion;
$tmpNS["xsi"] = $XMLSchemaVersion."-instance";
$namespaces = array_flip($tmpNS);
}
if(get_class($request_val) == "soapval"){
// verify that soapval objects in request match the methods signature
if($this->verify_method($request_val)){
$this->debug("request data - name: $request_val->name, type: $request_val->type, value: $request_val->value");
if($this->input_value){// decode the soapval object, and pass resulting values to the requested method
if(!$request_data = $request_val->decode()){
$this->make_fault("Server","Unable to decode response from soapval object into native php type.");
return $this->fault();
}
$this->debug("request data: $request_data");
}
// if there are return values
if($this->return_type = $this->get_return_type()){
$this->debug("got return type: '$this->return_type'");
// if there are parameters to pass
if($request_data){
// call method with parameters
$this->debug("about to call method '$this->methodname'");
if(!$method_response = call_user_func_array("$this->methodname",$request_data)){
$this->make_fault("Server","Method call failed for '$this->methodname' with params: ".join(",",$request_data));
return $this->fault();
}
} else {
// call method w/ no parameters
$this->debug("about to call method '$this->methodname'");
if(!$method_response = call_user_func("$this->methodname")){
$this->make_fault("Server","Method call failed for '$this->methodname' with no params");
return $this->fault();
}
}
// no return values
} else {
if($request_data){
// call method with parameters
$this->debug("about to call method '$this->methodname'");
call_user_func_array("$this->methodname",$request_data);
} else {
// call method w/ no parameters
$this->debug("about to call method '$this->methodname'");
call_user_func("$this->methodname",$request_data);
}
}
// return fault
if(get_class($method_response) == "soapmsg"){
if(eregi("fault",$method_response->value->name)){
$this->fault = true;
}
$return_msg = $method_response;
} else {
// return soapval object
if(get_class($method_response) == "soapval"){
$return_val = $method_response;
// create soap_val object w/ return values from method, use method signature to determine type
} else {
$return_val = new soapval($this->methodname,$this->return_type,$method_response);
}
$this->debug($return_val->debug_str);
// response object is a soap_msg object
$return_msg = new soapmsg($this->methodname."Response",array($return_val),"$this->service");
}
if($this->debug_flag){
$return_msg->debug_flag = true;
}
$this->result = "successful";
return $return_msg;
} else {
// debug
$this->debug("ERROR: request not verified against method signature");
$this->result = "fault: request failed validation against method signature";
// return soapresp
return $this->fault();
}
} else {
// debug
$this->debug("ERROR: parser did not return soapval object: $request_val ".get_class($request_val));
$this->result = "fault: parser did not return soapval object: $request_val";
// return fault
$this->make_fault("Server","parser did not return soapval object: $request_val");
return $this->fault();
}
}
}
function verify_method($request){
//return true;
$this->debug("entered verify_method() w/ request name: ".$request->name);
$params = $request->value;
// if there are input parameters required...
if($sig = $this->dispatch_map[$this->methodname]["in"]){
$this->input_value = count($sig);
if(is_array($params)){
$this->debug("entered verify_method() with ".count($params)." parameters");
foreach($params as $v){
$this->debug("param '$v->name' of type '$v->type'");
}
// validate the number of parameters
if(count($params) == count($sig)){
$this->debug("got correct number of parameters: ".count($sig));
// make array of param types
foreach($params as $param){
$p[] = strtolower($param->type);
}
// validate each param's type
for($i=0; $i < count($p); $i++){
// type not match
if(strtolower($sig[$i]) != strtolower($p[$i])){
$this->debug("mismatched parameter types: $sig[$i] != $p[$i]");
$this->make_fault("Client","soap request contained mismatching parameters of name $v->name had type $p[$i], which did not match signature's type: $sig[$i]");
return false;
}
$this->debug("parameter type match: $sig[$i] = $p[$i]");
}
return true;
// oops, wrong number of paramss
} else {
$this->debug("oops, wrong number of parameter!");
$this->make_fault("Client","soap request contained incorrect number of parameters. method '$this->methodname' required ".count($sig)." and request provided ".count($params));
return false;
}
// oops, no params...
} else {
$this->debug("oops, no parameters sent! Method '$this->methodname' requires ".count($sig)." input parameters!");
$this->make_fault("Client","soap request contained incorrect number of parameters. method '$this->methodname' requires ".count($sig)." parameters, and request provided none");
return false;
}
// no params
} elseif( (count($params)==0) && (count($sig) <= 1) ){
$this->input_values = 0;
return true;
} else {
//$this->debug("well, request passed parameters to a method that requires none?");
//$this->make_fault("Client","method '$this->methodname' requires no parameters. The request passed in ".count($params).": ".@implode(" param: ",$params) );
return true;
}
}
// get string return type from dispatch map
function get_return_type(){
if(count($this->dispatch_map[$this->methodname]["out"]) >= 1){
$type = array_shift($this->dispatch_map[$this->methodname]["out"]);
$this->debug("got return type from dispatch map: '$type'");
return $type;
}
return false;
}
// dbg
function debug($string){
if($this->debug_flag){
$this->debug_str .= "$string\n";
}
}
// add a method to the dispatch map
function add_to_map($methodname,$in,$out){
$this->dispatch_map[$methodname]["in"] = $in;
$this->dispatch_map[$methodname]["out"] = $out;
}
// set up a fault
function fault(){
return new soapmsg("Fault",
array(
"faultcode" => $this->fault_code,
"faultstring" => $this->fault_str,
"faultactor" => $this->fault_actor,
"faultdetail" => $this->fault_detail.$this->debug_str
),
"http://schemas.xmlsoap.org/soap/envelope/"
);
}
function make_fault($fault_code,$fault_string){
$this->fault_code = $fault_code;
$this->fault_str = $fault_string;
$this->fault();
$this->fault = true;
}
}
/* #####################################################################################################
classes/class.soap_client.php
##################################################################################################### */
// make errors handle properly in windows (thx, [email protected])
error_reporting(2039);
// set default encoding
$soap_defencoding = "UTF-8";
// set schema version
$XMLSchemaVersion = "http://www.w3.org/2001/XMLSchema";
// load types into typemap array
$typemap["http://www.w3.org/2001/XMLSchema"] = array(
"string","boolean","float","double","decimal","duration","dateTime","time",
"date","gYearMonth","gYear","gMonthDay","gDay","gMonth","hexBinary","base64Binary",
// derived datatypes
"normalizedString","token","language","NMTOKEN","NMTOKENS","Name","NCName","ID",
"IDREF","IDREFS","ENTITY","ENTITIES","integer","nonPositiveInteger",
"negativeInteger","long","int","short","byte","nonNegativeInteger",
"unsignedLong","unsignedInt","unsignedShort","unsignedByte","positiveInteger");
$typemap["http://www.w3.org/1999/XMLSchema"] = array(
"i4","int","boolean","string","double","float","dateTime",
"timeInstant","base64Binary","base64","ur-type");
$typemap["http://soapinterop.org/xsd"] = array("SOAPStruct");
$typemap["http://schemas.xmlsoap.org/soap/encoding/"] = array("base64","array","Array");
// load namespace uris into an array of uri => prefix
$namespaces = array(
"http://schemas.xmlsoap.org/soap/envelope/" => "SOAP-ENV",
$XMLSchemaVersion => "xsd",
$XMLSchemaVersion."-instance" => "xsi",
"http://schemas.xmlsoap.org/soap/encoding/" => "SOAP-ENC",
"http://soapinterop.org/xsd"=>"si");
$xmlEntities = array("quot" => '"',"amp" => "&",
"lt" => "<","gt" => ">","apos" => "'");
// $path can be a complete endpoint url, with the other parameters left blank:
// $soap_client = new soap_client("http://path/to/soap/server");
class soap_client {
function soap_client($path, $server=false, $port=false){
$this->port = 80;
$this->path = $path;
$this->server = $server;
$this->errno;
$this->errstring;
$this->debug_flag = false;
$this->debug_str = "";
$this->username = "";
$this->password = "";
$this->action = "";
$this->incoming_payload = "";
$this->outgoing_payload = "";
$this->response = "";
$this->action = "";
// endpoint mangling
if(ereg("^http://",$path)){
$path = str_replace("http://","",$path);
$this->path = strstr($path,"/");
$this->debug("path = $this->path");
if(ereg(":",$path)){
$this->server = substr($path,0,strpos($path,":"));
$this->port = substr(strstr($path,":"),1);
$this->port = substr($this->port,0,strpos($this->port,"/"));
} else {
$this->server = substr($path,0,strpos($path,"/"));
}
}
if($port){
$this->port = $port;
}
}
function setCredentials($username, $pword) {
$this->username = $username;
$this->password = $pword;
}
function send($msg, $action, $timeout=0) {
// where msg is an soapmsg
if($this->debug_flag){
$msg->debug_flag = true;
}
$this->action = $action;
return $this->sendPayloadHTTP10(
$msg,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password
);
}
function sendPayloadHTTP10($msg, $server, $port, $timeout=0, $username="", $password="") {
if($timeout > 0){
$fp = fsockopen($server, $port,&$this->errno, &$this->errstr, $timeout);
} else {
$fp = fsockopen($server, $port,&$this->errno, &$this->errstr);
}
if (!$fp) {
$this->debug("Couldn't open socket connection to server!");
$this->debug("Server: $this->server");
return 0;
}
$credentials = "";
if($username != "") {
$credentials = "Authorization: Basic ".base64_encode("$username:$password")."\r\n";
}
$soap_data = $msg->serialize();
$this->outgoing_payload =
"POST ".$this->path." HTTP/1.0\r\n".
"User-Agent: SOAPx4 v0.5\r\n".
"Host: ".$this->server."\r\n".
$credentials.
"Content-Type: text/xml\r\nContent-Length: ".strlen($soap_data)."\r\n".
"SOAPAction: \"$this->action\""."\r\n\r\n".
$soap_data;
// send
if(!fputs($fp, $this->outgoing_payload, strlen($this->outgoing_payload))) {
$this->debug("Write error");
}
// get reponse
while($data = fread($fp, 32768)) {
$incoming_payload .= $data;
}
fclose($fp);
$this->incoming_payload = $incoming_payload;
// $response is a soapmsg object
$this->response = $msg->parseResponse($incoming_payload);
$this->debug($msg->debug_str);
return $this->response;
}
function debug($string){
if($this->debug_flag){
$this->debug_str .= "$string\n";
}
}
} // end class soap_client
// soap message class
class soapmsg {
// params is an array of soapval objects
function soapmsg($method,$params,$method_namespace="http://testuri.org",$new_namespaces=false){
// globalize method namespace
global $methodNamespace;
$methodNamespace = $method_namespace;
// make method struct
$this->value = new soapval($method,"struct",$params,$method_namespace);
if(is_array($new_namespaces)){
global $namespaces;
$i = count($namespaces);
foreach($new_namespaces as $v){
$namespaces[$v] = "ns".$i++;
}
$this->namespaces = $namespaces;
}
$this->payload = "";
$this->debug_flag = false;
$this->debug_str = "entering soapmsg() with soapval ".$this->value->name."\n";
}
function make_envelope($payload) {
global $namespaces;
foreach($namespaces as $k => $v){
$ns_string .= "xmlns:$v=\"$k\" ";
}
return "<SOAP-ENV:Envelope $ns_string SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n".
$payload.
"</SOAP-ENV:Envelope>\n";
}
function make_body($payload) {
return "<SOAP-ENV:Body>\n".
$payload.
"</SOAP-ENV:Body>\n";
}
function createPayload() {
$value = $this->value;
$payload = $this->make_envelope($this->make_body($value->serialize()));
$this->debug($value->debug_str);
$payload = "<?xml version=\"1.0\"?>\n".$payload;
if($this->debug_flag){
$payload .= $this->serializeDebug();
}
$this->payload = str_replace("\n","\r\n", $payload);
}
function serialize(){
if($this->payload == ""){
$this->createPayload();
return $this->payload;
} else {
return $this->payload;
}
}
// returns a soapval object
function parseResponse($data) {
$this->debug("Entering parseResponse()");
//$this->debug(" w/ data $data");
// strip headers here
//$clean_data = ereg_replace("\r\n","\n", $data);
if(ereg("^.*\r\n\r\n<",$data)) {
$this->debug("found proper separation of headers and document");
$this->debug("getting rid of headers, stringlen: ".strlen($data));
$clean_data = ereg_replace("^.*\r\n\r\n<","<", $data);
$this->debug("cleaned data, stringlen: ".strlen($clean_data));
} else {
// return fault
return new soapval("fault","SOAPStruct",array(new soapval("faultcode","string","SOAP-MSG"),new soapval("faultstring","string","HTTP error"),new soapval("faultdetail","string","HTTP headers were not immediately followed by '\r\n\r\n'")));
}
/* if response is a proper http response, and is not a 200
if(ereg("^HTTP",$clean_data) && !ereg("200$", $clean_data)){
// get error data
$errstr = substr($clean_data, 0, strpos($clean_data, "\n")-1);
// return fault
return new soapval("fault","SOAPStruct",array(new soapval("faultcode","string","SOAP-MSG"),new soapval("faultstring","string","HTTP error")));
}*/
$this->debug("about to create parser instance w/ data: $clean_data");
// parse response
$response = new soap_parser($clean_data);
// return array of parameters
$ret = $response->get_response();
$this->debug($response->debug_str);
return $ret;
}
// dbg
function debug($string){
if($this->debug_flag){
$this->debug_str .= "$string\n";
}
}
// preps debug data for encoding into soapmsg
function serializeDebug() {
if($this->debug_flag){
return "<!-- DEBUG INFO:\n".$this->debug_str."-->\n";
} else {
return "";
}
}
}
// soap value object
class soapval {
function soapval($name="",$type=false,$value=-1,$namespace=false,$type_namespace=false) {
global $soapTypes, $typemap, $namespaces, $methodNamespace, $XMLSchemaVersion;
// detect type if not passed
if(!$type){
if(is_array($value) && count($value) >= 1){
if(ereg("[a-zA-Z0-9\-]*",key($v))){
$type = "struct";
} else {
$type = "array";
}
} elseif(is_int($v)){
$type = "int";
} elseif(is_float($v) || $v == "NaN" || $v == "INF"){
$type = "float";
} else {
$type = gettype($value);
}
}
// php type name mangle
if($type == "integer"){
$type = "int";
}
$this->soapTypes = $soapTypes;
$this->name = $name;
$this->value = "";
$this->type = $type;
$this->type_code = 0;
$this->type_prefix = false;
$this->array_type = "";
$this->debug_flag = false;
$this->debug_str = "";
$this->debug("Entering soapval - name: '$name' type: '$type'");
if($namespace){
$this->namespace = $namespace;
if(!isset($namespaces[$namespace])){
$namespaces[$namespace] = "ns".(count($namespaces)+1);
}
$this->prefix = $namespaces[$namespace];
}
// get type prefix
if(ereg(":",$type)){
$this->type = substr(strrchr($type,":"),1,strlen(strrchr($type,":")));
$this->type_prefix = substr($type,0,strpos($type,":"));
} elseif($type_namespace){
if(!isset($namespaces[$type_namespace])){
$namespaces[$type_namespace] = "ns".(count($namespaces)+1);
}
$this->type_prefix = $namespaces[$type_namespace];
// if type namespace was not explicitly passed, and we're not in a method struct:
} elseif(!$this->type_prefix && !isset($this->namespace)){
// try to get type prefix from typeMap
if(!$ns = $this->verify_type($type)){
// else default to method namespace
$this->type_prefix = $namespaces[$methodNamespace];
} else {
$this->type_prefix = $namespaces[$ns];
}
}
// if scalar
if(in_array($this->type,$typemap[$XMLSchemaVersion])) {
$this->type_code = 1;
$this->addScalar($value,$this->type,$name);
// if array
} elseif(eregi("^(array|ur-type)$",$this->type)){
$this->type_code = 2;
$this->addArray($value);
// if struct
} elseif(eregi("struct",$this->type)){
$this->type_code = 3;
$this->addStruct($value);
} else {
$this->type_code = 3;
$this->addStruct($value);
}
}
function addScalar($value, $type, $name=""){
$this->debug("adding scalar '$name' of type '$type'");
// if boolean, change value to 1 or 0
if ($type == "boolean") {
if((strcasecmp($value,"true") == 0) || ($value == 1)) {
$value = 1;
} else {
$value = 0;
}
}
$this->value = $value;
return true;
}
function addArray($vals){
$this->debug("adding array '$this->name' with ".count($vals)." vals");
$this->value = array();
if(is_array($vals) && count($vals) >= 1){
foreach($vals as $k => $v){
$this->debug("checking value $k : $v");
// if soapval, add..
if(get_class($v) == "soapval"){
$this->value[] = $v;
$this->debug($v->debug_str);
// else make obj and serialize
} else {
if(is_array($v)){
if(ereg("[a-zA-Z\-]*",key($v))){
$type = "struct";
} else {
$type = "array";
}
} elseif(!ereg("^[0-9]*$",$k) && $this->verify_type($k)){
$type = $k;
} elseif(is_int($v)){
$type = "int";
} elseif(is_float($v) || $v == "NaN" || $v == "INF"){
$type = "float";
} else {
$type = gettype($v);
}
$new_val = new soapval("item",$type,$v);
$this->debug($new_val->debug_str);
$this->value[] = $new_val;
}
}
}
return true;
}
function addStruct($vals){
$this->debug("adding struct '$this->name' with ".count($vals)." vals");
if(is_array($vals) && count($vals) >= 1){
foreach($vals as $k => $v){
// if serialize, if soapval
if(get_class($v) == "soapval"){
$this->value[] = $v;
$this->debug($v->debug_str);
// else make obj and serialize
} else {
if(is_array($v)){
foreach($v as $a => $b){
if($a == "0"){
$type = "array";
} else {
$type = "struct";
}
break;
}
} elseif($this->verify_type($k)){
$this->debug("got type '$type' for value '$v' from typemap!");
$type = $k;
} elseif(is_int($v)){
$type = "int";
} elseif(is_float($v) || $v == "NaN" || $v == "INF"){
$type = "float";
} else {
$type = gettype($v);
$this->debug("got type '$type' for value '$v' from php gettype()!");
}
$new_val = new soapval($k,$type,$v);
$this->debug($new_val->debug_str);
$this->value[] = $new_val;
}
}
} else {
$this->value = array();
}
return true;
}
// turn soapvals into xml, woohoo!
function serializeval($soapval=false) {
if(!$soapval){
$soapval = $this;
}
$this->debug("serializing '$soapval->name' of type '$soapval->type'");
if($soapval->name == ""){
$soapval->name = "return";
}
switch($soapval->type_code) {
case 3:
// struct
$this->debug("got a struct");
if($soapval->prefix && $soapval->type_prefix){
$xml .= "<$soapval->prefix:$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">\n";
} elseif($soapval->type_prefix){
$xml .= "<$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">\n";
} elseif($soapval->prefix){
$xml .= "<$soapval->prefix:$soapval->name>\n";
} else {
$xml .= "<$soapval->name>\n";
}
if(is_array($soapval->value)){
foreach($soapval->value as $k => $v){
$xml .= $this->serializeval($v);
}
}
if($soapval->prefix){
$xml .= "</$soapval->prefix:$soapval->name>\n";
} else {
$xml .= "</$soapval->name>\n";
}
break;
case 2:
// array
foreach($soapval->value as $array_val){
$array_types[$array_val->type] = 1;
$xml .= $this->serializeval($array_val);
}
if(count($array_types) > 1){
$array_type = "xsd:ur-type";
} elseif(count($array_types) >= 1){
$array_type = $array_val->type_prefix.":".$array_val->type;
}
$xml = "<$soapval->name xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"".$array_type."[".sizeof($soapval->value)."]\">\n".$xml."</$soapval->name>\n";
break;
case 1:
$xml .= "<$soapval->name xsi:type=\"$soapval->type_prefix:$soapval->type\">$soapval->value</$soapval->name>\n";
break;
default:
break;
}
return $xml;
}
// serialize
function serialize() {
return $this->serializeval($this);
}
function decode($soapval=false){
if(!$soapval){
$soapval = $this;
}
// scalar decode
if($soapval->type_code == 1){
return $soapval->value;
// array decode
} elseif($soapval->type_code == 2){
if(is_array($soapval->value)){
foreach($soapval->value as $item){
$return[] = $this->decode($item);
}
return $return;
} else {
return array();
}
// struct decode
} elseif($soapval->type_code == 3){
if(is_array($soapval->value)){
foreach($soapval->value as $item){
$return[$item->name] = $this->decode($item);
}
return $return;
} else {
return array();
}
}
}
// pass it a type, and it attempts to return a namespace uri
function verify_type($type){
global $typemap,$namespaces;
/*foreach($typemap as $namespace => $types){
if(is_array($types) && in_array($type,$types)){
return $namespace;
}
}*/
foreach($namespaces as $uri => $prefix){
if(is_array($typemap[$uri]) && in_array($type,$typemap[$uri])){
return $uri;
}
}
return false;
}
// alias for verify_type() - pass it a type, and it returns it's prefix
function get_prefix($type){
if($prefix = $this->verify_type($type)){
return $prefix;
}
return false;
}
function debug($string){
if($this->debug_flag){
$this->debug_str .= "$string\n";
}
}
}
class soap_parser {
function soap_parser($xml,$encoding="UTF-8"){
//global $soapTypes;
//$this->soapTypes = $soapTypes;
$this->xml = $xml;
$this->xml_encoding = $encoding;
$this->root_struct = "";
// determines where in the message we are (envelope,header,body,method)
$this->status = "";
$this->position = 0;
$this->pos_stat = 0;
$this->depth = 0;
$this->default_namespace = "";
$this->namespaces = array();
$this->message = array();
$this->fault = false;
$this->fault_code = "";
$this->fault_str = "";
$this->fault_detail = "";
$this->eval_str = "";
$this->depth_array = array();
$this->debug_flag = true;
$this->debug_str = "";
$this->previous_element = "";
$this->entities = array ( "&" => "&", "<" => "<", ">" => ">",
"'" => "'", '"' => """ );
// Check whether content has been read.
if(!empty($xml)){
$this->debug("Entering soap_parser()");
//$this->debug("DATA DUMP:\n\n$xml");
// Create an XML parser.
$this->parser = xml_parser_create($this->xml_encoding);
// Set the options for parsing the XML data.
//xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
// Set the object for the parser.
xml_set_object($this->parser, &$this);
// Set the element handlers for the parser.
xml_set_element_handler($this->parser, "start_element","end_element");
xml_set_character_data_handler($this->parser,"character_data");
xml_set_default_handler($this->parser, "default_handler");
// Parse the XML file.
if(!xml_parse($this->parser,$xml,true)){
// Display an error message.
$this->debug(sprintf("XML error on line %d: %s",
xml_get_current_line_number($this->parser),
xml_error_string(xml_get_error_code($this->parser))));
$this->fault = true;
} else {
// get final eval string
$this->eval_str = "\$response = ".trim($this->build_eval($this->root_struct)).";";
}
xml_parser_free($this->parser);
} else {
$this->debug("xml was empty, didn't parse!");
}
}
// loop through msg, building eval_str
function build_eval($pos){
$this->debug("inside build_eval() for $pos: ".$this->message[$pos]["name"]);
$eval_str = $this->message[$pos]["eval_str"];
// loop through children, building...
if($this->message[$pos]["children"] != ""){
$this->debug("children string = ".$this->message[$pos]["children"]);
$children = explode("|",$this->message[$pos]["children"]);
$this->debug("it has ".count($children)." children");
foreach($children as $c => $child_pos){
//$this->debug("child pos $child_pos: ".$this->message[$child_pos]["name"]);
if($this->message[$child_pos]["eval_str"] != ""){
$this->debug("entering build_eval() for ".$this->message[$child_pos]["name"].", array pos $c, pos: $child_pos");
$eval_str .= $this->build_eval($child_pos).", ";
}
}
$eval_str = substr($eval_str,0,strlen($eval_str)-2);
}
// add current node's eval_str
$eval_str .= $this->message[$pos]["end_eval_str"];