magento-hg-mirror /lib/PEAR/SOAP/WSDL.php

Language PHP Lines 2295
MD5 Hash 67394dd9431b6970eac9a32df6171f2a Estimated Cost $42,776 (why?)
Repository https://bitbucket.org/claudiu_marginean/magento-hg-mirror View Raw File View Project SPDX
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
<?php
/**
 * This file contains the code for dealing with WSDL access and services.
 *
 * PHP versions 4 and 5
 *
 * LICENSE: This source file is subject to version 2.02 of the PHP license,
 * that is bundled with this package in the file LICENSE, and is available at
 * through the world-wide-web at http://www.php.net/license/2_02.txt.  If you
 * did not receive a copy of the PHP license and are unable to obtain it
 * through the world-wide-web, please send a note to license@php.net so we can
 * mail you a copy immediately.
 *
 * @category   Web Services
 * @package    SOAP
 * @author     Dietrich Ayala <dietrich@ganx4.com> Original Author
 * @author     Shane Caraveo <Shane@Caraveo.com>   Port to PEAR and more
 * @author     Chuck Hagenbuch <chuck@horde.org>   Maintenance
 * @author     Jan Schneider <jan@horde.org>       Maintenance
 * @copyright  2003-2005 The PHP Group
 * @license    http://www.php.net/license/2_02.txt  PHP License 2.02
 * @link       http://pear.php.net/package/SOAP
 */

require_once 'SOAP/Base.php';
require_once 'SOAP/Fault.php';
require_once 'HTTP/Request.php';

define('WSDL_CACHE_MAX_AGE', 43200);

/**
 * This class parses WSDL files, and can be used by SOAP::Client to properly
 * register soap values for services.
 *
 * Originally based on SOAPx4 by Dietrich Ayala
 * http://dietrich.ganx4.com/soapx4
 *
 * @todo
 * - refactor namespace handling ($namespace/$ns)
 * - implement IDL type syntax declaration so we can generate WSDL
 *
 * @access public
 * @package SOAP
 * @author Shane Caraveo <shane@php.net> Conversion to PEAR and updates
 * @author Dietrich Ayala <dietrich@ganx4.com> Original Author
 */
class SOAP_WSDL extends SOAP_Base
{
    var $tns = null;
    var $definition = array();
    var $namespaces = array();
    var $ns = array();
    var $xsd = SOAP_XML_SCHEMA_VERSION;
    var $complexTypes = array();
    var $elements = array();
    var $messages = array();
    var $portTypes = array();
    var $bindings = array();
    var $imports = array();
    var $services = array();
    var $service = '';

    /**
     * URL to WSDL file.
     *
     * @var string
     */
    var $uri;

    /**
     * Parse documentation in the WSDL?
     *
     * @var boolean
     */
    var $docs;

    /**
     * Proxy parameters.
     *
     * @var array
     */
    var $proxy;

    /**
     * Enable tracing in the generated proxy class?
     *
     * @var boolean
     */
    var $trace = false;

    /**
     * Use WSDL cache?
     *
     * @var boolean
     */
    var $cacheUse;

    /**
     * WSDL cache directory.
     *
     * @var string
     */
    var $cacheDir;

    /**
     * Cache maximum lifetime (in seconds).
     *
     * @var integer
     */
    var $cacheMaxAge;

    /**
     * Class to use for WSDL parsing. Can be overridden for special cases,
     * subclasses, etc.
     *
     * @var string
     */
    var $wsdlParserClass = 'SOAP_WSDL_Parser';

    /**
     * Reserved PHP keywords.
     *
     * @link http://www.php.net/manual/en/reserved.php
     *
     * @var array
     */
    var $_reserved = array('abstract', 'and', 'array', 'as', 'break', 'case',
                           'catch', 'cfunction', 'class', 'clone', 'const',
                           'continue', 'declare', 'default', 'die', 'do',
                           'echo', 'else', 'elseif', 'empty', 'enddeclare',
                           'endfor', 'endforeach', 'endif', 'endswitch',
                           'endwhile', 'eval', 'exception', 'exit', 'extends',
                           'final', 'for', 'foreach', 'function', 'global',
                           'if', 'implements', 'include', 'include_once',
                           'interface', 'isset', 'list', 'new', 'old_function',
                           'or', 'php_user_filter', 'print', 'private',
                           'protected', 'public', 'require', 'require_once',
                           'return', 'static', 'switch', 'this', 'throw',
                           'try', 'unset', 'use', 'var', 'while', 'xor');

    /**
     * Regular expressions for invalid PHP labels.
     *
     * @link http://www.php.net/manual/en/language.variables.php.
     *
     * @var string
     */
    var $_invalid = array('/^[^a-zA-Z_\x7f-\xff]/', '/[^a-zA-Z0-9_\x7f-\xff]/');

    /**
     * SOAP_WSDL constructor.
     *
     * @param string $wsdl_uri          URL to WSDL file.
     * @param array $proxy              Options for HTTP_Request class
     *                                  @see HTTP_Request.
     * @param boolean|string $cacheUse  Use WSDL caching? The cache directory
     *                                  if a string.
     * @param integer $cacheMaxAge      Cache maximum lifetime (in seconds).
     * @param boolean $docs             Parse documentation in the WSDL?
     *
     * @access public
     */
    function SOAP_WSDL($wsdl_uri    = false,
                       $proxy       = array(),
                       $cacheUse    = false,
                       $cacheMaxAge = WSDL_CACHE_MAX_AGE,
                       $docs        = false)
    {
        parent::SOAP_Base('WSDL');
        $this->uri         = $wsdl_uri;
        $this->proxy       = $proxy;
        $this->cacheUse    = !empty($cacheUse);
        $this->cacheMaxAge = $cacheMaxAge;
        $this->docs        = $docs;
        if (is_string($cacheUse)) {
            $this->cacheDir = $cacheUse;
        }

        if ($wsdl_uri) {
            if (!PEAR::isError($this->parseURL($wsdl_uri))) {
                reset($this->services);
                $this->service = key($this->services);
            }
        }
    }

    /**
     * @deprecated  Use setService().
     */
    function set_service($service)
    {
        $this->setService($service);
    }

    /**
     * Sets the service currently to be used.
     *
     * @param string $service  An (existing) service name.
     */
    function setService($service)
    {
        if (array_key_exists($service, $this->services)) {
            $this->service = $service;
        }
    }

    /**
     * Fills the WSDL array tree with data from a WSDL file.
     *
     * @param string $wsdl_uri  URL to WSDL file.
     */
    function parseURL($wsdl_uri)
    {
        $parser = new $this->wsdlParserClass($wsdl_uri, $this, $this->docs);

        if ($parser->fault) {
            $this->_raiseSoapFault($parser->fault);
        }
    }

    /**
     * Fills the WSDL array tree with data from one or more PHP class objects.
     *
     * @param mixed $wsdl_obj          An object or array of objects to add to
     *                                 the internal WSDL tree.
     * @param string $targetNamespace  The target namespace of schema types
     *                                 etc.
     * @param string $service_name     Name of the WSDL service.
     * @param string $service_desc     Optional description of the WSDL
     *                                 service.
     */
    function parseObject($wsdl_obj, $targetNamespace, $service_name,
                         $service_desc = '')
    {
        $parser = new SOAP_WSDL_ObjectParser($wsdl_obj, $this,
                                             $targetNamespace, $service_name,
                                             $service_desc);

        if ($parser->fault) {
            $this->_raiseSoapFault($parser->fault);
        }
    }

    function getEndpoint($portName)
    {
        if ($this->_isfault()) {
            return $this->_getfault();
        }

        return (isset($this->services[$this->service]['ports'][$portName]['address']['location']))
                ? $this->services[$this->service]['ports'][$portName]['address']['location']
                : $this->_raiseSoapFault("No endpoint for port for $portName", $this->uri);
    }

    function _getPortName($operation, $service)
    {
        if (isset($this->services[$service]['ports'])) {
            $ports = $this->services[$service]['ports'];
            foreach ($ports as $port => $portAttrs) {
                $type = $ports[$port]['type'];
                if ($type == 'soap' &&
                    isset($this->bindings[$portAttrs['binding']]['operations'][$operation])) {
                    return $port;
                }
            }
        }
        return null;
    }

    /**
     * Finds the name of the first port that contains an operation of name
     * $operation. Always returns a SOAP portName.
     */
    function getPortName($operation, $service = null)
    {
        if ($this->_isfault()) {
            return $this->_getfault();
        }

        if (!$service) {
            $service = $this->service;
        }
        if (isset($this->services[$service]['ports'])) {
            if ($portName = $this->_getPortName($operation, $service)) {
                return $portName;
            }
        }
        // Try any service in the WSDL.
        foreach ($this->services as $serviceName => $service) {
            if (isset($this->services[$serviceName]['ports'])) {
                if ($portName = $this->_getPortName($operation, $serviceName)) {
                    $this->service = $serviceName;
                    return $portName;
                }
            }
        }
        return $this->_raiseSoapFault("No operation $operation in WSDL.", $this->uri);
    }

    function getOperationData($portName, $operation)
    {
        if ($this->_isfault()) {
            return $this->_getfault();
        }

        if (!isset($this->services[$this->service]['ports'][$portName]['binding']) ||
            !($binding = $this->services[$this->service]['ports'][$portName]['binding'])) {
            return $this->_raiseSoapFault("No binding for port $portName in WSDL.", $this->uri);
        }

        // Get operation data from binding.
        if (is_array($this->bindings[$binding]['operations'][$operation])) {
            $opData = $this->bindings[$binding]['operations'][$operation];
        }
        // Get operation data from porttype.
        $portType = $this->bindings[$binding]['type'];
        if (!$portType) {
            return $this->_raiseSoapFault("No port type for binding $binding in WSDL.", $this->uri);
        }
        if (is_array($type = $this->portTypes[$portType][$operation])) {
            if (isset($type['parameterOrder'])) {
                $opData['parameterOrder'] = $type['parameterOrder'];
            }
            $opData['input'] = array_merge($opData['input'], $type['input']);
            $opData['output'] = array_merge($opData['output'], $type['output']);
        }
        if (!$opData)
            return $this->_raiseSoapFault("No operation $operation for port $portName in WSDL.", $this->uri);
        $opData['parameters'] = false;
        if (isset($this->bindings[$binding]['operations'][$operation]['input']['namespace']))
            $opData['namespace'] = $this->bindings[$binding]['operations'][$operation]['input']['namespace'];
        // Message data from messages.
        $inputMsg = $opData['input']['message'];
        if (is_array($this->messages[$inputMsg])) {
            foreach ($this->messages[$inputMsg] as $pname => $pattrs) {
                if ($opData['style'] == 'document' &&
                    $opData['input']['use'] == 'literal' &&
                    $pname == 'parameters') {
                    $opData['parameters'] = true;
                    $opData['namespace'] = $this->namespaces[$pattrs['namespace']];
                    $el = $this->elements[$pattrs['namespace']][$pattrs['type']];
                    if (isset($el['elements'])) {
                        foreach ($el['elements'] as $elname => $elattrs) {
                            $opData['input']['parts'][$elname] = $elattrs;
                        }
                    }
                } else {
                    $opData['input']['parts'][$pname] = $pattrs;
                }
            }
        }
        $outputMsg = $opData['output']['message'];
        if (is_array($this->messages[$outputMsg])) {
            foreach ($this->messages[$outputMsg] as $pname => $pattrs) {
                if ($opData['style'] == 'document' &&
                    $opData['output']['use'] == 'literal' &&
                    $pname == 'parameters') {

                    $el = $this->elements[$pattrs['namespace']][$pattrs['type']];
                    if (isset($el['elements'])) {
                        foreach ($el['elements'] as $elname => $elattrs) {
                            $opData['output']['parts'][$elname] = $elattrs;
                        }
                    }
                } else {
                    $opData['output']['parts'][$pname] = $pattrs;
                }
            }
        }
        return $opData;
    }

    function matchMethod(&$operation)
    {
        if ($this->_isfault()) {
            return $this->_getfault();
        }

        // Overloading lowercases function names :(
        foreach ($this->services[$this->service]['ports'] as $portAttrs) {
            foreach (array_keys($this->bindings[$portAttrs['binding']]['operations']) as $op) {
                if (strcasecmp($op, $operation) == 0) {
                    $operation = $op;
                }
            }
        }
    }

    /**
     * Given a datatype, what function handles the processing?
     *
     * This is used for doc/literal requests where we receive a datatype, and
     * we need to pass it to a method in out server class.
     *
     * @param string $datatype
     * @param string $namespace
     * @return string
     * @access public
     */
    function getDataHandler($datatype, $namespace)
    {
        // See if we have an element by this name.
        if (isset($this->namespaces[$namespace])) {
            $namespace = $this->namespaces[$namespace];
        }

        if (!isset($this->ns[$namespace])) {
            return null;
        }

        $nsp = $this->ns[$namespace];
        //if (!isset($this->elements[$nsp]))
        //    $nsp = $this->namespaces[$nsp];
        if (!isset($this->elements[$nsp][$datatype])) {
            return null;
        }

        $checkmessages = array();
        // Find what messages use this datatype.
        foreach ($this->messages as $messagename => $message) {
            foreach ($message as $part) {
                if ($part['type'] == $datatype) {
                    $checkmessages[] = $messagename;
                    break;
                }
            }
        }
        // Find the operation that uses this message.
        foreach($this->portTypes as $porttype) {
            foreach ($porttype as $opname => $opinfo) {
                foreach ($checkmessages as $messagename) {
                    if ($opinfo['input']['message'] == $messagename) {
                        return $opname;
                    }
                }
            }
        }

        return null;
    }

    function getSoapAction($portName, $operation)
    {
        if ($this->_isfault()) {
            return $this->_getfault();
        }

        if (!empty($this->bindings[$this->services[$this->service]['ports'][$portName]['binding']]['operations'][$operation]['soapAction'])) {
            return $this->bindings[$this->services[$this->service]['ports'][$portName]['binding']]['operations'][$operation]['soapAction'];
        }

        return false;
    }

    function getNamespace($portName, $operation)
    {
        if ($this->_isfault()) {
            return $this->_getfault();
        }

        if (!empty($this->bindings[$this->services[$this->service]['ports'][$portName]['binding']]['operations'][$operation]['input']['namespace'])) {
            return $this->bindings[$this->services[$this->service]['ports'][$portName]['binding']]['operations'][$operation]['input']['namespace'];
        }

        return false;
    }

    function getNamespaceAttributeName($namespace)
    {
        /* If it doesn't exist at first, flip the array and check again. */
        if (empty($this->ns[$namespace])) {
            $this->ns = array_flip($this->namespaces);
        }

        /* If it doesn't exist now, add it. */
        if (empty($this->ns[$namespace])) {
            return $this->addNamespace($namespace);
        }

        return $this->ns[$namespace];
    }

    function addNamespace($namespace)
    {
        if (!empty($this->ns[$namespace])) {
            return $this->ns[$namespace];
        }

        $n = count($this->ns);
        $attr = 'ns' . $n;
        $this->namespaces['ns' . $n] = $namespace;
        $this->ns[$namespace] = $attr;

        return $attr;
    }

    function _validateString($string)
    {
        return preg_match('/^[\w_:#\/]+$/', $string);
    }

    function _addArg(&$args, &$argarray, $argname)
    {
        if ($args) {
            $args .= ', ';
        }
        $args .= '$' . $argname;
        if (!$this->_validateString($argname)) {
            return;
        }
        if ($argarray) {
            $argarray .= ', ';
        }
        $argarray .= "'$argname' => $" . $argname;
    }

    function _elementArg(&$args, &$argarray, &$_argtype, $_argname)
    {
        $comments = '';
        $el = $this->elements[$_argtype['namespace']][$_argtype['type']];
        $tns = isset($this->ns[$el['namespace']])
            ? $this->ns[$el['namespace']]
            : $_argtype['namespace'];

        if (!empty($el['complex']) ||
            (isset($el['type']) &&
             isset($this->complexTypes[$tns][$el['type']]))) {
            // The element is a complex type.
            $comments .= "        // {$_argtype['type']} is a ComplexType, refer to the WSDL for more info.\n";
            $attrname = "{$_argtype['type']}_attr";
            if (isset($el['type']) &&
                isset($this->complexTypes[$tns][$el['type']]['attribute'])) {
                $comments .= "        // {$_argtype['type']} may require attributes, refer to the WSDL for more info.\n";
            }
            $comments .= "        \${$attrname}['xmlns'] = '{$this->namespaces[$_argtype['namespace']]}';\n";
            $comments .= "        \${$_argtype['type']} = new SOAP_Value('{$_argtype['type']}', false, \${$_argtype['type']}, \$$attrname);\n";
            $this->_addArg($args, $argarray, $_argtype['type']);
            if (isset($el['type']) &&
                isset($this->complexTypes[$tns][$el['type']]['attribute'])) {
                if ($args) {
                    $args .= ', ';
                }
                $args .= '$' . $attrname;
            }
        } elseif (isset($el['elements'])) {
            foreach ($el['elements'] as $ename => $element) {
                $comments .= "        \$$ename = new SOAP_Value('{{$this->namespaces[$element['namespace']]}}$ename', '" .
                    (isset($element['type']) ? $element['type'] : false) .
                    "', \$$ename);\n";
                $this->_addArg($args, $argarray, $ename);
            }
        } else {
            $comments .= "        \$$_argname = new SOAP_Value('{{$this->namespaces[$tns]}}$_argname', '{$el['type']}', \$$_argname);\n";
            $this->_addArg($args, $argarray, $_argname);
        }

        return $comments;
    }

    function _complexTypeArg(&$args, &$argarray, &$_argtype, $_argname)
    {
        $comments = '';
        if (isset($this->complexTypes[$_argtype['namespace']][$_argtype['type']])) {
            $comments  = "        // $_argname is a ComplexType {$_argtype['type']},\n" .
                "        // refer to wsdl for more info\n";
            if (isset($this->complexTypes[$_argtype['namespace']][$_argtype['type']]['attribute'])) {
                $comments .= "        // $_argname may require attributes, refer to wsdl for more info\n";
            }
            $wrapname = '{' . $this->namespaces[$_argtype['namespace']].'}' . $_argtype['type'];
            $comments .= "        \$$_argname = new SOAP_Value('$_argname', '$wrapname', \$$_argname);\n";
        }

        $this->_addArg($args, $argarray, $_argname);

        return $comments;
    }

    /**
     * Generates stub code from the WSDL that can be saved to a file or eval'd
     * into existence.
     */
    function generateProxyCode($port = '', $classname = '')
    {
        if ($this->_isfault()) {
            return $this->_getfault();
        }

        $multiport = count($this->services[$this->service]['ports']) > 1;
        if (!$port) {
            reset($this->services[$this->service]['ports']);
            $port = current($this->services[$this->service]['ports']);
        }
        // XXX currently do not support HTTP ports
        if ($port['type'] != 'soap') {
            return null;
        }

        // XXX currentPort is BAD
        $clienturl = $port['address']['location'];
        if (!$classname) {
            if ($multiport || $port) {
                $classname = 'WebService_' . $this->service . '_' . $port['name'];
            } else {
                $classname = 'WebService_' . $this->service;
            }
            $classname = $this->_sanitize($classname);
        }

        if (!$this->_validateString($classname)) {
            return null;
        }

        if (is_array($this->proxy) && count($this->proxy)) {
            $class = "class $classname extends SOAP_Client\n{\n" .
            "    function $classname(\$path = '$clienturl')\n    {\n" .
            "        \$this->SOAP_Client(\$path, 0, 0,\n" .
            '                           array(';

            foreach ($this->proxy as $key => $val) {
                if (is_array($val)) {
                    $class .= "'$key' => array(";
                    foreach ($val as $key2 => $val2) {
                        $class .= "'$key2' => '$val2', ";
                    }
                    $class .= ')';
                } else {
                    $class .= "'$key' => '$val', ";
                }
            }
            $class .= "));\n    }\n";
            $class = str_replace(', ))', '))', $class);
        } else {
            $class = "class $classname extends SOAP_Client\n{\n" .
            "    function $classname(\$path = '$clienturl')\n    {\n" .
            "        \$this->SOAP_Client(\$path, 0);\n" .
            "    }\n";
        }

        // Get the binding, from that get the port type.
        $primaryBinding = $port['binding'];
        $primaryBinding = preg_replace("/^(.*:)/", '', $primaryBinding);
        $portType = $this->bindings[$primaryBinding]['type'];
        $portType = preg_replace("/^(.*:)/", '', $portType);
        $style = $this->bindings[$primaryBinding]['style'];

        // XXX currentPortType is BAD
        foreach ($this->portTypes[$portType] as $opname => $operation) {
            $binding = $this->bindings[$primaryBinding]['operations'][$opname];
            if (isset($binding['soapAction'])) {
                $soapaction = $binding['soapAction'];
            } else {
                $soapaction = null;
            }
            if (isset($binding['style'])) {
                $opstyle = $binding['style'];
            } else {
                $opstyle = $style;
            }
            $use = $binding['input']['use'];
            if ($use == 'encoded') {
                $namespace = $binding['input']['namespace'];
            } else {
                $bindingType = $this->bindings[$primaryBinding]['type'];
                $ns = $this->portTypes[$bindingType][$opname]['input']['namespace'];
                $namespace = $this->namespaces[$ns];
            }

            $args = '';
            $argarray = '';
            $comments = '';
            $wrappers = '';
            foreach ($operation['input'] as $argname => $argtype) {
                if ($argname == 'message') {
                    foreach ($this->messages[$argtype] as $_argname => $_argtype) {
                        $_argname = $this->_sanitize($_argname);
                        if ($opstyle == 'document' && $use == 'literal' &&
                            $_argtype['name'] == 'parameters') {
                            // The type or element refered to is used for
                            // parameters.
                            $elattrs = null;
                            $el = $this->elements[$_argtype['namespace']][$_argtype['type']];

                            if ($el['complex']) {
                                $namespace = $this->namespaces[$_argtype['namespace']];
                                // XXX need to wrap the parameters in a
                                // SOAP_Value.
                            }
                            if (isset($el['elements'])) {
                                foreach ($el['elements'] as $elname => $elattrs) {
                                    $elname = $this->_sanitize($elname);
                                    // Is the element a complex type?
                                    if (isset($this->complexTypes[$elattrs['namespace']][$elname])) {
                                        $comments .= $this->_complexTypeArg($args, $argarray, $_argtype, $_argname);
                                    } else {
                                        $this->_addArg($args, $argarray, $elname);
                                    }
                                }
                            }
                            if ($el['complex'] && $argarray) {
                                $wrapname = '{' . $this->namespaces[$_argtype['namespace']].'}' . $el['name'];
                                $comments .= "        \${$el['name']} = new SOAP_Value('$wrapname', false, \$v = array($argarray));\n";
                                $argarray = "'{$el['name']}' => \${$el['name']}";
                            }
                        } else {
                            if (isset($_argtype['element'])) {
                                // Element argument.
                                $comments .= $this->_elementArg($args, $argarray, $_argtype, $_argtype['type']);
                            } else {
                                // Complex type argument.
                                $comments .= $this->_complexTypeArg($args, $argarray, $_argtype, $_argname);
                            }
                        }
                    }
                }
            }

            // Validate entries.

            // Operation names are function names, so try to make sure it's
            // legal. This could potentially cause collisions, but let's try
            // to make everything callable and see how many problems that
            // causes.
            $opname_php = $this->_sanitize($opname);
            if (!$this->_validateString($opname_php)) {
                return null;
            }

            if ($argarray) {
                $argarray = "array($argarray)";
            } else {
                $argarray = 'null';
            }

            $class .= "    function &$opname_php($args)\n    {\n$comments$wrappers" .
                "        \$result = \$this->call('$opname',\n" .
                "                              \$v = $argarray,\n" .
                "                              array('namespace' => '$namespace',\n" .
                "                                    'soapaction' => '$soapaction',\n" .
                "                                    'style' => '$opstyle',\n" .
                "                                    'use' => '$use'" .
                ($this->trace ? ",\n                                    'trace' => true" : '') . "));\n" .
                "        return \$result;\n" .
                "    }\n";
        }

        $class .= "}\n";

        return $class;
    }

    function generateAllProxies()
    {
        $proxycode = '';
        foreach (array_keys($this->services[$this->service]['ports']) as $key) {
            $port =& $this->services[$this->service]['ports'][$key];
            $proxycode .= $this->generateProxyCode($port);
        }
        return $proxycode;
    }

    function &getProxy($port = '', $name = '')
    {
        if ($this->_isfault()) {
            $fault =& $this->_getfault();
            return $fault;
        }

        $multiport = count($this->services[$this->service]['ports']) > 1;

        if (!$port) {
            reset($this->services[$this->service]['ports']);
            $port = current($this->services[$this->service]['ports']);
        }

        if ($multiport || $port) {
            $classname = 'WebService_' . $this->service . '_' . $port['name'];
        } else {
            $classname = 'WebService_' . $this->service;
        }

        if ($name) {
            $classname = $name . '_' . $classname;
        }

        $classname = $this->_sanitize($classname);
        if (!class_exists($classname)) {
            $proxy = $this->generateProxyCode($port, $classname);
            require_once 'SOAP/Client.php';
            eval($proxy);
        }
        $proxy = new $classname;

        return $proxy;
    }

    /**
     * Sanitizes a SOAP value, method or class name so that it can be used as
     * a valid PHP identifier. Invalid characters are converted into
     * underscores and reserved words are prefixed with an underscore.
     *
     * @param string $name  The identifier to sanitize.
     *
     * @return string  The sanitized identifier.
     */
    function _sanitize($name)
    {
        $name = preg_replace($this->_invalid, '_', $name);
        if (in_array($name, $this->_reserved)) {
            $name = '_' . $name;
        }
        return $name;
    }

    function &_getComplexTypeForElement($name, $namespace)
    {
        $t = null;
        if (isset($this->ns[$namespace]) &&
            isset($this->elements[$this->ns[$namespace]][$name]['type'])) {

            $type = $this->elements[$this->ns[$namespace]][$name]['type'];
            $ns = $this->elements[$this->ns[$namespace]][$name]['namespace'];

            if (isset($this->complexTypes[$ns][$type])) {
                $t = $this->complexTypes[$ns][$type];
            }
        }
        return $t;
    }

    function getComplexTypeNameForElement($name, $namespace)
    {
        $t = $this->_getComplexTypeForElement($name, $namespace);
        if ($t) {
            return $t['name'];
        }
        return null;
    }

    function getComplexTypeChildType($ns, $name, $child_ns, $child_name)
    {
        // Is the type an element?
        $t = $this->_getComplexTypeForElement($name, $ns);
        if ($t) {
            // No, get it from complex types directly.
            if (isset($t['elements'][$child_name]['type']))
                return $t['elements'][$child_name]['type'];
        } elseif (isset($this->ns[$ns]) &&
                  isset($this->elements[$this->ns[$ns]][$name]['complex']) &&
                  $this->elements[$this->ns[$ns]][$name]['complex']) {
            // Type is not an element but complex.
            return $this->elements[$this->ns[$ns]][$name]['elements'][$child_name]['type'];
        }
        return null;
    }

    /**
     * @param QName $name  A parameter name.
     * @param QName $type  A parameter type.
     *
     * @return array  A list of [type, array element type, array element
     *                namespace, array length].
     */
    function getSchemaType($type, $name)
    {
        // see if it's a complex type so we can deal properly with
        // SOAPENC:arrayType.
        if ($name && $type) {
            // XXX TODO:
            // look up the name in the wsdl and validate the type.
            foreach ($this->complexTypes as $types) {
                if (isset($types[$type->name])) {
                    if (isset($types[$type->name]['type'])) {
                        list($arraytype_ns, $arraytype, $array_depth) = isset($types[$type->name]['arrayType'])
                            ? $this->_getDeepestArrayType($types[$type->name]['namespace'], $types[$type->name]['arrayType'])
                            : array($this->namespaces[$types[$type->name]['namespace']], null, 0);
                        return array($types[$type->name]['type'], $arraytype, $arraytype_ns, $array_depth);
                    }
                    if (isset($types[$type->name]['arrayType'])) {
                        list($arraytype_ns, $arraytype, $array_depth) =
                            $this->_getDeepestArrayType($types[$type->name]['namespace'], $types[$type->name]['arrayType']);
                        return array('Array', $arraytype, $arraytype_ns, $array_depth);
                    }
                    if (!empty($types[$type->name]['elements'][$name->name])) {
                        $type->name = $types[$type->name]['elements']['type'];
                        return array($type->name, null, $this->namespaces[$types[$type->name]['namespace']], null);
                    }
                    break;
                }
            }
        }
        if ($type && $type->namespace) {
            $arrayType = null;
            // XXX TODO:
            // this code currently handles only one way of encoding array
            // types in wsdl need to do a generalized function to figure out
            // complex types
            $p = $this->ns[$type->namespace];
            if ($p && !empty($this->complexTypes[$p][$type->name])) {
                if ($arrayType = $this->complexTypes[$p][$type->name]['arrayType']) {
                    $type->name = 'Array';
                } elseif ($this->complexTypes[$p][$type->name]['order'] == 'sequence' &&
                          array_key_exists('elements', $this->complexTypes[$p][$type->name])) {
                    reset($this->complexTypes[$p][$type->name]['elements']);
                    // assume an array
                    if (count($this->complexTypes[$p][$type->name]['elements']) == 1) {
                        $arg = current($this->complexTypes[$p][$type->name]['elements']);
                        $arrayType = $arg['type'];
                        $type->name = 'Array';
                    } else {
                        foreach ($this->complexTypes[$p][$type->name]['elements'] as $element) {
                            if ($element['name'] == $type->name) {
                                $arrayType = $element['type'];
                                $type->name = $element['type'];
                            }
                        }
                    }
                } else {
                    $type->name = 'Struct';
                }
                return array($type->name, $arrayType, $type->namespace, null);
            }
        }
        return array(null, null, null, null);
    }

    /**
     * Recurse through the WSDL structure looking for the innermost array type
     * of multi-dimensional arrays.
     *
     * Takes a namespace prefix and a type, which can be in the form 'type' or
     * 'type[]', and returns the full namespace URI, the type of the most
     * deeply nested array type found, and the number of levels of nesting.
     *
     * @access private
     * @return mixed array or nothing
     */
    function _getDeepestArrayType($nsPrefix, $arrayType)
    {
        static $trail = array();

        $arrayType = ereg_replace('\[\]$', '', $arrayType);

        // Protect against circular references XXX We really need to remove
        // trail from this altogether (it's very inefficient and in the wrong
        // place!) and put circular reference checking in when the WSDL info
        // is generated in the first place
        if (array_search($nsPrefix . ':' . $arrayType, $trail)) {
            return array(null, null, -count($trail));
        }

        if (array_key_exists($nsPrefix, $this->complexTypes) &&
            array_key_exists($arrayType, $this->complexTypes[$nsPrefix]) &&
            array_key_exists('arrayType', $this->complexTypes[$nsPrefix][$arrayType])) {
            $trail[] = $nsPrefix . ':' . $arrayType;
            $result = $this->_getDeepestArrayType($this->complexTypes[$nsPrefix][$arrayType]['namespace'],
                                                  $this->complexTypes[$nsPrefix][$arrayType]['arrayType']);
            return array($result[0], $result[1], $result[2] + 1);
        }
        return array($this->namespaces[$nsPrefix], $arrayType, 0);
    }

}

class SOAP_WSDL_Cache extends SOAP_Base
{
    /**
     * Use WSDL cache?
     *
     * @var boolean
     */
    var $_cacheUse;

    /**
     * WSDL cache directory.
     *
     * @var string
     */
    var $_cacheDir;

    /**
     * Cache maximum lifetime (in seconds)
     *
     * @var integer
     */
    var $_cacheMaxAge;

    /**
     * Constructor.
     *
     * @param boolean $cashUse      Use caching?
     * @param integer $cacheMaxAge  Cache maximum lifetime (in seconds)
     */
    function SOAP_WSDL_Cache($cacheUse = false,
                             $cacheMaxAge = WSDL_CACHE_MAX_AGE,
                             $cacheDir = null)
    {
        parent::SOAP_Base('WSDLCACHE');
        $this->_cacheUse = $cacheUse;
        $this->_cacheDir = $cacheDir;
        $this->_cacheMaxAge = $cacheMaxAge;
    }

    /**
     * Returns the path to the cache and creates it, if it doesn't exist.
     *
     * @private
     *
     * @return string  The directory to use for the cache.
     */
    function _cacheDir()
    {
        if (!empty($this->_cacheDir)) {
            $dir = $this->_cacheDir;
        } else {
            $dir = getenv('WSDLCACHE');
            if (empty($dir)) {
                $dir = './wsdlcache';
            }
        }
        @mkdir($dir, 0700);
        return $dir;
    }

    /**
     * Retrieves a file from cache if it exists, otherwise retreive from net,
     * add to cache, and return from cache.
     *
     * @param  string   URL to WSDL
     * @param  array    proxy parameters
     * @param  int      expected MD5 of WSDL URL
     * @access public
     * @return string  data
     */
    function get($wsdl_fname, $proxy_params = array(), $cache = 0)
    {
        $cachename = $md5_wsdl = $file_data = '';
        if ($this->_cacheUse) {
            // Try to retrieve WSDL from cache
            $cachename = $this->_cacheDir() . '/' . md5($wsdl_fname). ' .wsdl';
            if (file_exists($cachename) &&
                $file_data = file_get_contents($cachename)) {
                $md5_wsdl = md5($file_data);
                if ($cache) {
                    if ($cache != $md5_wsdl) {
                        return $this->_raiseSoapFault('WSDL Checksum error!', $wsdl_fname);
                    }
                } else {
                    $fi = stat($cachename);
                    $cache_mtime = $fi[8];
                    if ($cache_mtime + $this->_cacheMaxAge < time()) {
                        // Expired, refetch.
                        $md5_wsdl = '';
                    }
                }
            }
        }

        // Not cached or not using cache. Retrieve WSDL from URL
        if (!$md5_wsdl) {
            // Is it a local file?
            if (strpos($wsdl_fname, 'file://') === 0) {
                $wsdl_fname = substr($wsdl_fname, 7);
                if (!file_exists($wsdl_fname)) {
                    return $this->_raiseSoapFault('Unable to read local WSDL file', $wsdl_fname);
                }
                $file_data = file_get_contents($wsdl_fname);
            } elseif (!preg_match('|^https?://|', $wsdl_fname)) {
                return $this->_raiseSoapFault('Unknown schema of WSDL URL', $wsdl_fname);
            } else {
                $uri = explode('?', $wsdl_fname);
                $rq = new HTTP_Request($uri[0], $proxy_params);
                // the user agent HTTP_Request uses fouls things up
                if (isset($uri[1])) {
                    $rq->addRawQueryString($uri[1]);
                }

                if (isset($proxy_params['proxy_host']) &&
                    isset($proxy_params['proxy_port']) &&
                    isset($proxy_params['proxy_user']) &&
                    isset($proxy_params['proxy_pass'])) {
                    $rq->setProxy($proxy_params['proxy_host'],
                                  $proxy_params['proxy_port'],
                                  $proxy_params['proxy_user'],
                                  $proxy_params['proxy_pass']);
                } elseif (isset($proxy_params['proxy_host']) &&
                          isset($proxy_params['proxy_port'])) {
                    $rq->setProxy($proxy_params['proxy_host'],
                                  $proxy_params['proxy_port']);
                }

                $result = $rq->sendRequest();
                if (PEAR::isError($result)) {
                    return $this->_raiseSoapFault("Unable to retrieve WSDL $wsdl_fname," . $rq->getResponseCode(), $wsdl_fname);
                }
                $file_data = $rq->getResponseBody();
                if (!$file_data) {
                    return $this->_raiseSoapFault("Unable to retrieve WSDL $wsdl_fname, no http body", $wsdl_fname);
                }
            }

            $md5_wsdl = md5($file_data);

            if ($this->_cacheUse) {
                $fp = fopen($cachename, "wb");
                fwrite($fp, $file_data);
                fclose($fp);
            }
        }

        if ($this->_cacheUse && $cache && $cache != $md5_wsdl) {
            return $this->_raiseSoapFault('WSDL Checksum error!', $wsdl_fname);
        }

        return $file_data;
    }

}

class SOAP_WSDL_Parser extends SOAP_Base
{

    /**
     * Define internal arrays of bindings, ports, operations,
     * messages, etc.
     */
    var $currentMessage;
    var $currentOperation;
    var $currentPortType;
    var $currentBinding;
    var $currentPort;

    /**
     * Parser vars.
     */
    var $cache;

    var $tns = null;
    var $soapns = array('soap');
    var $uri = '';
    var $wsdl = null;

    var $status = '';
    var $element_stack = array();
    var $parentElement = '';

    var $schema = '';
    var $schemaStatus = '';
    var $schema_stack = array();
    var $currentComplexType;
    var $schema_element_stack = array();
    var $currentElement;

    /**
     * Constructor.
     */
    function SOAP_WSDL_Parser($uri, &$wsdl, $docs = false)
    {
        parent::SOAP_Base('WSDLPARSER');
        $this->cache = new SOAP_WSDL_Cache($wsdl->cacheUse,
                                            $wsdl->cacheMaxAge,
                                            $wsdl->cacheDir);
        $this->uri = $uri;
        $this->wsdl = &$wsdl;
        $this->docs = $docs;
        $this->parse($uri);
    }

    function parse($uri)
    {
        // Check whether content has been read.
        $fd = $this->cache->get($uri, $this->wsdl->proxy);
        if (PEAR::isError($fd)) {
            return $this->_raiseSoapFault($fd);
        }

        // Create an XML parser.
        $parser = xml_parser_create();
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_set_object($parser, $this);
        xml_set_element_handler($parser, 'startElement', 'endElement');
        if ($this->docs) {
            xml_set_character_data_handler($parser, 'characterData');
        }

        if (!xml_parse($parser, $fd, true)) {
            $detail = sprintf('XML error on line %d: %s',
                              xml_get_current_line_number($parser),
                              xml_error_string(xml_get_error_code($parser)));
            return $this->_raiseSoapFault("Unable to parse WSDL file $uri\n$detail");
        }
        xml_parser_free($parser);
        return true;
    }

    /**
     * start-element handler
     */
    function startElement($parser, $name, $attrs)
    {
        // Get element prefix.
        $qname = new QName($name);
        if ($qname->ns) {
            $ns = $qname->ns;
            if ($ns && ((!$this->tns && strcasecmp($qname->name, 'definitions') == 0) || $ns == $this->tns)) {
                $name = $qname->name;
            }
        }
        $this->currentTag = $qname->name;
        $this->parentElement = '';
        $stack_size = count($this->element_stack);
        if ($stack_size) {
            $this->parentElement = $this->element_stack[$stack_size - 1];
        }
        $this->element_stack[] = $this->currentTag;

        // Find status, register data.
        switch ($this->status) {
        case 'types':
            // sect 2.2 wsdl:types
            // children: xsd:schema
            $parent_tag = '';
            $stack_size = count($this->schema_stack);
            if ($stack_size) {
                $parent_tag = $this->schema_stack[$stack_size - 1];
            }

            switch ($qname->name) {
            case 'schema':
                // No parent should be in the stack.
                if (!$parent_tag || $parent_tag == 'types') {
                    if (array_key_exists('targetNamespace', $attrs)) {
                        $this->schema = $this->wsdl->getNamespaceAttributeName($attrs['targetNamespace']);
                    } else {
                        $this->schema = $this->wsdl->getNamespaceAttributeName($this->wsdl->tns);
                    }
                    $this->wsdl->complexTypes[$this->schema] = array();
                    $this->wsdl->elements[$this->schema] = array();
                }
                break;

            case 'complexType':
                if ($parent_tag == 'schema') {
                    $this->currentComplexType = $attrs['name'];
                    if (!isset($attrs['namespace'])) {
                        $attrs['namespace'] = $this->schema;
                    }
                    $this->wsdl->complexTypes[$this->schema][$this->currentComplexType] = $attrs;
                    if (array_key_exists('base', $attrs)) {
                        $qn = new QName($attrs['base']);
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = $qn->name;
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['namespace'] = $qn->ns;
                    } else {
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = 'Struct';
                    }
                    $this->schemaStatus = 'complexType';
                } else {
                    $this->wsdl->elements[$this->schema][$this->currentElement]['complex'] = true;
                }
                break;

            case 'element':
                if (isset($attrs['type'])) {
                    $qn = new QName($attrs['type']);
                    $attrs['type'] = $qn->name;
                    if ($qn->ns && array_key_exists($qn->ns, $this->wsdl->namespaces)) {
                        $attrs['namespace'] = $qn->ns;
                    }
                }

                $parentElement = '';
                $stack_size = count($this->schema_element_stack);
                if ($stack_size > 0) {
                    $parentElement = $this->schema_element_stack[$stack_size - 1];
                }

                if (isset($attrs['ref'])) {
                    $qn = new QName($attrs['ref']);
                    $this->currentElement = $qn->name;
                } else {
                    $this->currentElement = $attrs['name'];
                }
                $this->schema_element_stack[] = $this->currentElement;
                if (!isset($attrs['namespace'])) {
                    $attrs['namespace'] = $this->schema;
                }

                if ($parent_tag == 'schema') {
                    $this->wsdl->elements[$this->schema][$this->currentElement] = $attrs;
                    $this->wsdl->elements[$this->schema][$this->currentElement]['complex'] = false;
                    $this->schemaStatus = 'element';
                } elseif ($this->currentComplexType) {
                    // we're inside a complexType
                    if ((isset($this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['order']) &&
                         $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['order'] == 'sequence')
                        && $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] == 'Array') {
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['arrayType'] = isset($attrs['type']) ? $attrs['type'] : null;
                    }
                    $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['elements'][$this->currentElement] = $attrs;
                } else {
                    $this->wsdl->elements[$this->schema][$parentElement]['elements'][$this->currentElement] = $attrs;
                }
                break;

            case 'complexContent':
            case 'simpleContent':
                break;

            case 'extension':
            case 'restriction':
                if ($this->schemaStatus == 'complexType') {
                    if (!empty($attrs['base'])) {
                        $qn = new QName($attrs['base']);
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = $qn->name;

                        // Types that extend from other types aren't
                        // *of* those types. Reflect this by denoting
                        // which type they extend. I'm leaving the
                        // 'type' setting here since I'm not sure what
                        // removing it might break at the moment.
                        if ($qname->name == 'extension') {
                            $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['extends'] = $qn->name;
                        }
                    } else {
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = 'Struct';
                    }
                }
                break;

            case 'sequence':
                if ($this->schemaStatus == 'complexType') {
                    $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['order'] = $qname->name;
                    if (!isset($this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'])) {
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = 'Array';
                    }
                }
                break;

            case 'all':
                $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['order'] = $qname->name;
                if (!isset($this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'])) {
                    $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = 'Struct';
                }
                break;

            case 'choice':
                $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['order'] = $qname->name;
                if (!isset($this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'])) {
                    $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = 'Array';
                }

            case 'attribute':
                if ($this->schemaStatus == 'complexType') {
                    if (isset($attrs['name'])) {
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['attribute'][$attrs['name']] = $attrs;
                    } else {
                        if (isset($attrs['ref'])) {
                            $q = new QName($attrs['ref']);
                            foreach ($attrs as $k => $v) {
                                if ($k != 'ref' && strstr($k, $q->name)) {
                                    $vq = new QName($v);
                                    if ($q->name == 'arrayType') {
                                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType][$q->name] = $vq->name. $vq->arrayInfo;
                                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = 'Array';
                                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['namespace'] = $vq->ns;
                                    } else {
                                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType][$q->name] = $vq->name;
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            }

            $this->schema_stack[] = $qname->name;
            break;

        case 'message':
            // sect 2.3 wsdl:message child wsdl:part
            switch ($qname->name) {
            case 'part':
                $qn = null;
                if (isset($attrs['type'])) {
                    $qn = new QName($attrs['type']);
                } elseif (isset($attrs['element'])) {
                    $qn = new QName($attrs['element']);
                }
                if ($qn) {
                    $attrs['type'] = $qn->name;
                    $attrs['namespace'] = $qn->ns;
                }
                $this->wsdl->messages[$this->currentMessage][$attrs['name']] = $attrs;
                // error in wsdl

            case 'documentation':
                break;

            default:
                break;
            }
            break;

        case 'portType':
            // sect 2.4
            switch ($qname->name) {
            case 'operation':
                // attributes: name
                // children: wsdl:input wsdl:output wsdl:fault
                $this->currentOperation = $attrs['name'];
                $this->wsdl->portTypes[$this->currentPortType][$this->currentOperation] = $attrs;
                break;

            case 'input':
            case 'output':
            case 'fault':
                // wsdl:input wsdl:output wsdl:fault
                // attributes: name message parameterOrder(optional)
                if ($this->currentOperation) {
                    if (isset($this->wsdl->portTypes[$this->currentPortType][$this->currentOperation][$name])) {
                        $this->wsdl->portTypes[$this->currentPortType][$this->currentOperation][$name] = array_merge($this->wsdl->portTypes[$this->currentPortType][$this->currentOperation][$name], $attrs);
                    } else {
                        $this->wsdl->portTypes[$this->currentPortType][$this->currentOperation][$name] = $attrs;
                    }
                    if (array_key_exists('message', $attrs)) {
                        $qn = new QName($attrs['message']);
                        $this->wsdl->portTypes[$this->currentPortType][$this->currentOperation][$name]['message'] = $qn->name;
                        $this->wsdl->portTypes[$this->currentPortType][$this->currentOperation][$name]['namespace'] = $qn->ns;
                    }
                }
                break;

            case 'documentation':
                break;

            default:
                break;
            }
            break;

        case 'binding':
            $ns = $qname->ns ? $this->wsdl->namespaces[$qname->ns] : SCHEMA_WSDL;
            switch ($ns) {
            case SCHEMA_SOAP:
            case SCHEMA_SOAP12:
                // this deals with wsdl section 3 soap binding
                switch ($qname->name) {
                case 'binding':
                    // sect 3.3
                    // soap:binding, attributes: transport(required), style(optional, default = document)
                    // if style is missing, it is assumed to be 'document'
                    if (!isset($attrs['style'])) {
                        $attrs['style'] = 'document';
                    }
                    $this->wsdl->bindings[$this->currentBinding] = array_merge($this->wsdl->bindings[$this->currentBinding], $attrs);
                    break;

                case 'operation':
                    // sect 3.4
                    // soap:operation, attributes: soapAction(required), style(optional, default = soap:binding:style)
                    if (!isset($attrs['style'])) {
                        $attrs['style'] = $this->wsdl->bindings[$this->currentBinding]['style'];
                    }
                    if (isset($this->wsdl->bindings[$this->currentBinding]['operations'][$this->currentOperation])) {
                        $this->wsdl->bindings[$this->currentBinding]['operations'][$this->currentOperation] = array_merge($this->wsdl->bindings[$this->currentBinding]['operations'][$this->currentOperation], $attrs);
                    } else {
                        $this->wsdl->bindings[$this->currentBinding]['operations'][$this->currentOperation] = $attrs;
                    }
                    break;

                case 'body':
                    // sect 3.5
                    // soap:body attributes:
                    // part - optional.  listed parts must appear in body, missing means all parts appear in body
                    // use - required. encoded|literal
                    // encodingStyle - optional.  space seperated list of encodings (uri's)
                    $this->wsdl->bindings[$this->currentBinding]
                                    ['operations'][$this->currentOperation][$this->opStatus] = $attrs;
                    break;

                case 'fault':
                    // sect 3.6
                    // soap:fault attributes: name use  encodingStyle namespace
                    $this->wsdl->bindings[$this->currentBinding]
                                    ['operations'][$this->currentOperation][$this->opStatus] = $attrs;
                    break;

                case 'header':
                    // sect 3.7
                    // soap:header attributes: message part use encodingStyle namespace
                    $this->wsdl->bindings[$this->currentBinding]
                                    ['operations'][$this->currentOperation][$this->opStatus]['headers'][] = $attrs;
                    break;

                case 'headerfault':
                    // sect 3.7
                    // soap:header attributes: message part use encodingStyle namespace
                    $header = count($this->wsdl->bindings[$this->currentBinding]
                                    ['operations'][$this->currentOperation][$this->opStatus]['headers'])-1;
                    $this->wsdl->bindings[$this->currentBinding]
                                    ['operations'][$this->currentOperation][$this->opStatus]['headers'][$header]['fault'] = $attrs;
                    break;

                case 'documentation':
                    break;

                default:
                    // error!  not a valid element inside binding
                    break;
                }
                break;

            case SCHEMA_WSDL:
                // XXX verify correct namespace
                // for now, default is the 'wsdl' namespace
                // other possible namespaces include smtp, http, etc. for alternate bindings
                switch ($qname->name) {
                case 'operation':
                    // sect 2.5
                    // wsdl:operation attributes: name
                    $this->currentOperation = $attrs['name'];
                    break;

                case 'output':
                case 'input':
                case 'fault':
                    // sect 2.5
                    // wsdl:input attributes: name
                    $this->opStatus = $qname->name;
                    break;

                case 'documentation':
                    break;

                default:
                    break;
                }
                break;

            case SCHEMA_WSDL_HTTP:
                switch ($qname->name) {
                case 'binding':
                    // sect 4.4
                    // http:binding attributes: verb
                    // parent: wsdl:binding
                    $this->wsdl->bindings[$this->currentBinding] = array_merge($this->wsdl->bindings[$this->currentBinding], $attrs);
                    break;

                case 'operation':
                    // sect 4.5
                    // http:operation attributes: location
                    // parent: wsdl:operation
                    $this->wsdl->bindings[$this->currentBinding]['operations']
                                                        [$this->currentOperation] = $attrs;
                    break;

                case 'urlEncoded':
                    // sect 4.6
                    // http:urlEncoded attributes: location
                    // parent: wsdl:input wsdl:output etc.
                    $this->wsdl->bindings[$this->currentBinding]['operations'][$this->opStatus]
                                                        [$this->currentOperation]['uri'] = 'urlEncoded';
                    break;

                case 'urlReplacement':
                    // sect 4.7
                    // http:urlReplacement attributes: location
                    // parent: wsdl:input wsdl:output etc.
                    $this->wsdl->bindings[$this->currentBinding]['operations'][$this->opStatus]
                                                        [$this->currentOperation]['uri'] = 'urlReplacement';
                    break;

                case 'documentation':
                    break;

                default:
                    // error
                    break;
                }

            case SCHEMA_MIME:
                // sect 5
                // all mime parts are children of wsdl:input, wsdl:output, etc.
                // unsuported as of yet
                switch ($qname->name) {
                case 'content':
                    // sect 5.3 mime:content
                    // <mime:content part="nmtoken"? type="string"?/>
                    // part attribute only required if content is child of multipart related,
                    //        it contains the name of the part
                    // type attribute contains the mime type
                case 'multipartRelated':
                    // sect 5.4 mime:multipartRelated
                case 'part':
                case 'mimeXml':
                    // sect 5.6 mime:mimeXml
                    // <mime:mimeXml part="nmtoken"?/>
                    //
                case 'documentation':
                    break;

                default:
                    // error
                    break;
                }

            case SCHEMA_DIME:
                // DIME is defined in:
                // http://gotdotnet.com/team/xml_wsspecs/dime/WSDL-Extension-for-DIME.htm
                // all DIME parts are children of wsdl:input, wsdl:output, etc.
                // unsuported as of yet
                switch ($qname->name) {
                case 'message':
                    // sect 4.1 dime:message
                    // appears in binding section
                    $this->wsdl->bindings[$this->currentBinding]['dime'] = $attrs;
                    break;

                default:
                    break;
                }

            default:
                break;
            }
            break;

        case 'service':
            $ns = $qname->ns ? $this->wsdl->namespaces[$qname->ns] : SCHEMA_WSDL;

            switch ($qname->name) {
            case 'port':
                // sect 2.6 wsdl:port attributes: name binding
                $this->currentPort = $attrs['name'];
                $this->wsdl->services[$this->currentService]['ports'][$this->currentPort] = $attrs;
                // XXX hack to deal with binding namespaces
                $qn = new QName($attrs['binding']);
                $this->wsdl->services[$this->currentService]['ports'][$this->currentPort]['binding'] = $qn->name;
                $this->wsdl->services[$this->currentService]['ports'][$this->currentPort]['namespace'] = $qn->ns;
                break;

            case 'address':
                $this->wsdl->services[$this->currentService]['ports'][$this->currentPort]['address'] = $attrs;
                // what TYPE of port is it?  SOAP or HTTP?
                $ns = $qname->ns ? $this->wsdl->namespaces[$qname->ns] : SCHEMA_WSDL;
                switch ($ns) {
                case SCHEMA_WSDL_HTTP:
                    $this->wsdl->services[$this->currentService]['ports'][$this->currentPort]['type']='http';
                    break;

                case SCHEMA_SOAP:
                    $this->wsdl->services[$this->currentService]['ports'][$this->currentPort]['type']='soap';
                    break;

                default:
                    // Shouldn't happen, we'll assume SOAP.
                    $this->wsdl->services[$this->currentService]['ports'][$this->currentPort]['type']='soap';
                }

                break;

            case 'documentation':
                break;

            default:
                break;
            }
        }

        // Top level elements found under wsdl:definitions.
        switch ($qname->name) {
        case 'import':
            // sect 2.1.1 wsdl:import attributes: namespace location
            if ((isset($attrs['location']) || isset($attrs['schemaLocation'])) &&
                !isset($this->wsdl->imports[$attrs['namespace']])) {
                $uri = isset($attrs['location']) ? $attrs['location'] : $attrs['schemaLocation'];
                $location = @parse_url($uri);
                if (!isset($location['scheme'])) {
                    $base = @parse_url($this->uri);
                    $uri = $this->mergeUrl($base, $uri);
                }

                $this->wsdl->imports[$attrs['namespace']] = $attrs;
                $import_parser_class = get_class($this);
                $import_parser = new $import_parser_class($uri, $this->wsdl, $this->docs);
                if ($import_parser->fault) {
                    unset($this->wsdl->imports[$attrs['namespace']]);
                    return false;
                }
                $this->currentImport = $attrs['namespace'];
            }
            // Continue on to the 'types' case - lack of break; is
            // intentional.

        case 'types':
            // sect 2.2 wsdl:types
            $this->status = 'types';
            break;

        case 'schema':
            // We can hit this at the top level if we've been asked to
            // import an XSD file.
            if (!empty($attrs['targetNamespace'])) {
                $this->schema = $this->wsdl->getNamespaceAttributeName($attrs['targetNamespace']);
            } else {
                $this->schema = $this->wsdl->getNamespaceAttributeName($this->wsdl->tns);
            }
            $this->wsdl->complexTypes[$this->schema] = array();
            $this->wsdl->elements[$this->schema] = array();
            $this->schema_stack[] = $qname->name;
            $this->status = 'types';
            break;

        case 'message':
            // sect 2.3 wsdl:message attributes: name children:wsdl:part
            $this->status = 'message';
            if (isset($attrs['name'])) {
                $this->currentMessage = $attrs['name'];
                $this->wsdl->messages[$this->currentMessage] = array();
            }
            break;

        case 'portType':
            // sect 2.4 wsdl:portType
            // attributes: name
            // children: wsdl:operation
            $this->status = 'portType';
            $this->currentPortType = $attrs['name'];
            $this->wsdl->portTypes[$this->currentPortType] = array();
            break;

        case 'binding':
            // sect 2.5 wsdl:binding attributes: name type
            // children: wsdl:operation soap:binding http:binding
            if ($qname->ns && $qname->ns != $this->tns) {
                break;
            }
            $this->status = 'binding';
            $this->currentBinding = $attrs['name'];
            $qn = new QName($attrs['type']);
            $this->wsdl->bindings[$this->currentBinding]['type'] = $qn->name;
            $this->wsdl->bindings[$this->currentBinding]['namespace'] = $qn->ns;
            break;

        case 'service':
            // sect 2.7 wsdl:service attributes: name children: ports
            $this->currentService = $attrs['name'];
            $this->wsdl->services[$this->currentService]['ports'] = array();
            $this->status = 'service';
            break;

        case 'definitions':
            // sec 2.1 wsdl:definitions
            // attributes: name targetNamespace xmlns:*
            // children: wsdl:import wsdl:types wsdl:message wsdl:portType wsdl:binding wsdl:service
            $this->wsdl->definition = $attrs;
            foreach ($attrs as $key => $value) {
                if (strstr($key, 'xmlns:') !== false) {
                    $qn = new QName($key);
                    // XXX need to refactor ns handling.
                    $this->wsdl->namespaces[$qn->name] = $value;
                    $this->wsdl->ns[$value] = $qn->name;
                    if ($key == 'targetNamespace' ||
                        strcasecmp($value,SOAP_SCHEMA) == 0) {
                        $this->soapns[] = $qn->name;
                    } else {
                        if (in_array($value, $this->_XMLSchema)) {
                            $this->wsdl->xsd = $value;
                        }
                    }
                }
            }
            if (isset($ns) && $ns) {
                $namespace = 'xmlns:' . $ns;
                if (!$this->wsdl->definition[$namespace]) {
                    return $this->_raiseSoapFault("parse error, no namespace for $namespace", $this->uri);
                }
                $this->tns = $ns;
            }
            break;
        }
    }

    /**
     * end-element handler.
     */
    function endElement($parser, $name)
    {
        $stacksize = count($this->element_stack);
        if ($stacksize) {
            if ($this->element_stack[$stacksize - 1] == 'definitions') {
                $this->status = '';
            }
            array_pop($this->element_stack);
        }

        if (stristr($name, 'schema')) {
            array_pop($this->schema_stack);
            $this->schema = '';
        }

        if ($this->schema) {
            array_pop($this->schema_stack);
            if (count($this->schema_stack) <= 1) {
                /* Correct the type for sequences with multiple
                 * elements. */
                if (isset($this->currentComplexType) && isset($this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'])
                    && $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] == 'Array'
                    && array_key_exists('elements', $this->wsdl->complexTypes[$this->schema][$this->currentComplexType])
                    && count($this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['elements']) > 1) {
                        $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['type'] = 'Struct';
                }
            }
            if (stristr($name, 'complexType')) {
                $this->currentComplexType = '';
                if (count($this->schema_element_stack)) {
                    $this->currentElement = array_pop($this->schema_element_stack);
                } else {
                    $this->currentElement = '';
                }
            } elseif (stristr($name, 'element')) {
                if (count($this->schema_element_stack)) {
                    $this->currentElement = array_pop($this->schema_element_stack);
                } else {
                    $this->currentElement = '';
                }
            }
        }
    }

    /**
     * Element content handler.
     */
    function characterData($parser, $data)
    {
        // Store the documentation in the WSDL file.
        if ($this->currentTag == 'documentation') {
            $data = trim(preg_replace('/\s+/', ' ', $data));
            if (!strlen($data)) {
                return;
            }

            switch ($this->status) {
            case 'service':
                $ptr =& $this->wsdl->services[$this->currentService];
                break;

            case 'portType':
                $ptr =& $this->wsdl->portTypes[$this->currentPortType][$this->currentOperation];
                break;

            case 'binding':
                $ptr =& $this->wsdl->bindings[$this->currentBinding];
                break;

            case 'message':
                $ptr =& $this->wsdl->messages[$this->currentMessage];
                break;

            case 'operation':
                break;

            case 'types':
                if (isset($this->currentComplexType) &&
                    isset($this->wsdl->complexTypes[$this->schema][$this->currentComplexType])) {
                    if ($this->currentElement) {
                        $ptr =& $this->wsdl->complexTypes[$this->schema][$this->currentComplexType]['elements'][$this->currentElement];
                    } else {
                        $ptr =& $this->wsdl->complexTypes[$this->schema][$this->currentComplexType];
                    }
                }
                break;
            }

            if (isset($ptr)) {
                if (!isset($ptr['documentation'])) {
                    $ptr['documentation'] = '';
                } else {
                    $ptr['documentation'] .= ' ';
                }
                $ptr['documentation'] .= $data;
            }
        }
    }

    /**
     * $parsed is an array returned by parse_url().
     *
     * @access private
     */
    function mergeUrl($parsed, $path)
    {
        if (!is_array($parsed)) {
            return false;
        }

        $uri = '';
        if (!empty($parsed['scheme'])) {
            $sep = (strtolower($parsed['scheme']) == 'mailto' ? ':' : '://');
            $uri = $parsed['scheme'] . $sep;
        }

        if (isset($parsed['pass'])) {
            $uri .= "$parsed[user]:$parsed[pass]@";
        } elseif (isset($parsed['user'])) {
            $uri .= "$parsed[user]@";
        }

        if (isset($parsed['host'])) {
            $uri .= $parsed['host'];
        }
        if (isset($parsed['port'])) {
            $uri .= ":$parsed[port]";
        }
        if ($path[0] != '/' && isset($parsed['path'])) {
            if ($parsed['path'][strlen($parsed['path']) - 1] != '/') {
                $path = dirname($parsed['path']) . '/' . $path;
            } else {
                $path = $parsed['path'] . $path;
            }
            $path = $this->_normalize($path);
        }
        $sep = $path[0] == '/' ? '' : '/';
        $uri .= $sep . $path;

        return $uri;
    }

    function _normalize($path_str)
    {
        $pwd = '';
        $strArr = preg_split('/(\/)/', $path_str, -1, PREG_SPLIT_NO_EMPTY);
        $pwdArr = '';
        $j = 0;
        for ($i = 0; $i < count($strArr); $i++) {
            if ($strArr[$i] != ' ..') {
                if ($strArr[$i] != ' .') {
                    $pwdArr[$j] = $strArr[$i];
                    $j++;
                }
            } else {
                array_pop($pwdArr);
                $j--;
            }
        }
        $pStr = implode('/', $pwdArr);
        $pwd = (strlen($pStr) > 0) ? ('/' . $pStr) : '/';
        return $pwd;
    }

}

/**
 * Parses the types and methods used in web service objects into the internal
 * data structures used by SOAP_WSDL.
 *
 * Assumes the SOAP_WSDL class is unpopulated to start with.
 *
 * @author Chris Coe <info@intelligentstreaming.com>
 */
class SOAP_WSDL_ObjectParser extends SOAP_Base
{
    /**
     * Target namespace for the WSDL document will have the following
     * prefix.
     */
    var $tnsPrefix = 'tns';

    /**
     * Reference to the SOAP_WSDL object to populate.
     */
    var $wsdl = null;

    /**
     * Constructor.
     *
     * @param object|array $objects    Reference to the object or array of
     *                                 objects to parse.
     * @param SOAP_WSDL $wsdl          Reference to the SOAP_WSDL object to
     *                                 populate.
     * @param string $targetNamespace  The target namespace of schema types
     *                                 etc.
     * @param string $service_name     Name of the WSDL <service>.
     * @param string $service_desc     Optional description of the WSDL
     *                                 <service>.
     */
    function SOAP_WSDL_ObjectParser($objects, &$wsdl, $targetNamespace,
                                    $service_name, $service_desc = '')
    {
        parent::SOAP_Base('WSDLOBJECTPARSER');

        $this->wsdl = &$wsdl;

        // Set up the SOAP_WSDL object
        $this->_initialise($service_name);

        // Parse each web service object
        $wsdl_ref = is_array($objects) ? $objects : array($objects);

        foreach ($wsdl_ref as $ref_item) {
            if (!is_object($ref_item)) {
                $this->_raiseSoapFault('Invalid web service object passed to object parser');
                continue;
            }

            if (!$this->_parse($ref_item, $targetNamespace, $service_name)) {
                break;
            }
        }

        // Build bindings from abstract data.
        if ($this->fault == null) {
            $this->_generateBindingsAndServices($targetNamespace, $service_name, $service_desc);
        }
    }

    /**
     * Initialise the SOAP_WSDL tree (destructive).
     *
     * If the object has already been initialised, the only effect
     * will be to change the tns namespace to the new service name.
     *
     * @param  $service_name Name of the WSDL <service>
     * @access private
     */
    function _initialise($service_name)
    {
        // Set up the basic namespaces that all WSDL definitions use.
        $this->wsdl->namespaces['wsdl'] = SCHEMA_WSDL;                                      // WSDL language
        $this->wsdl->namespaces['soap'] = SCHEMA_SOAP;                                      // WSDL SOAP bindings
        $this->wsdl->namespaces[$this->tnsPrefix] = 'urn:' . $service_name;                 // Target namespace
        $this->wsdl->namespaces['xsd'] = array_search('xsd', $this->_namespaces);           // XML Schema
        $this->wsdl->namespaces[SOAP_BASE::SOAPENCPrefix()] = array_search(SOAP_BASE::SOAPENCPrefix(), $this->_namespaces); // SOAP types

        // XXX Refactor $namespace/$ns for Shane :-)
        unset($this->wsdl->ns['urn:' . $service_name]);
        $this->wsdl->ns += array_flip($this->wsdl->namespaces);

        // Imports are not implemented in WSDL generation from classes.
        // *** <wsdl:import> ***
    }

    /**
     * Parser - takes a single object to add to tree (non-destructive).
     *
     * @access private
     *
     * @param object $object           Reference to the object to parse.
     * @param string $schemaNamespace
     * @param string $service_name     Name of the WSDL <service>.
     */
    function _parse($object, $schemaNamespace, $service_name)
    {
        // Create namespace prefix for the schema
        list($schPrefix,) = $this->_getTypeNs('{' . $schemaNamespace . '}');

        // Parse all the types defined by the object in whatever
        // schema language we are using (currently __typedef arrays)
        // *** <wsdl:types> ***
        foreach ($object->__typedef as $typeName => $typeValue) {
            // Get/create namespace definition
            list($nsPrefix, $typeName) = $this->_getTypeNs($typeName);

            // Create type definition
            $this->wsdl->complexTypes[$schPrefix][$typeName] = array('name' => $typeName);
            $thisType =& $this->wsdl->complexTypes[$schPrefix][$typeName];

            // According to Dmitri's documentation, __typedef comes in two
            // flavors:
            // Array = array(array("item" => "value"))
            // Struct = array("item1" => "value1", "item2" => "value2", ...)
            if (is_array($typeValue)) {
                if (is_array(current($typeValue)) && count($typeValue) == 1
                    && count(current($typeValue)) == 1) {
                    // It's an array
                    $thisType['type'] = 'Array';
                    $nsType = current(current($typeValue));
                    list($nsPrefix, $typeName) = $this->_getTypeNs($nsType);
                    $thisType['namespace'] = $nsPrefix;
                    $thisType['arrayType'] = $typeName . '[]';
                } elseif (!is_array(current($typeValue))) {
                    // It's a struct
                    $thisType['type'] = 'Struct';
                    $thisType['order'] = 'all';
                    $thisType['namespace'] = $nsPrefix;
                    $thisType['elements'] = array();

                    foreach ($typeValue as $elementName => $elementType) {
                        list($nsPrefix, $typeName) = $this->_getTypeNs($elementType);
                        $thisType['elements'][$elementName]['name'] = $elementName;
                        $thisType['elements'][$elementName]['type'] = $typeName;
                        $thisType['elements'][$elementName]['namespace'] = $nsPrefix;
                    }
                } else {
                    // It's erroneous
                    return $this->_raiseSoapFault("The type definition for $nsPrefix:$typeName is invalid.", 'urn:' . get_class($object));
                }
            } else {
                // It's erroneous
                return $this->_raiseSoapFault("The type definition for $nsPrefix:$typeName is invalid.", 'urn:' . get_class($object));
            }
        }

        // Create an empty element array with the target namespace
        // prefix, to match the results of WSDL parsing.
        $this->wsdl->elements[$schPrefix] = array();

        // Populate tree with message information
        // *** <wsdl:message> ***
        foreach ($object->__dispatch_map as $operationName => $messages) {
            // We need at least 'in' and 'out' parameters.
            if (!isset($messages['in']) || !isset($messages['out'])) {
                return $this->_raiseSoapFault('The dispatch map for the method "' . $operationName . '" is missing an "in" or "out" definition.', 'urn:' . get_class($object));
            }
            foreach ($messages as $messageType => $messageParts) {
                unset($thisMessage);

                switch ($messageType) {
                case 'in':
                    $this->wsdl->messages[$operationName . 'Request'] = array();
                    $thisMessage =& $this->wsdl->messages[$operationName . 'Request'];
                    break;

                case 'out':
                    $this->wsdl->messages[$operationName . 'Response'] = array();
                    $thisMessage =& $this->wsdl->messages[$operationName . 'Response'];
                    break;

                case 'alias':
                    // Do nothing
                    break;

                default:
                    // Error condition
                    break;
                }

                if (isset($thisMessage)) {
                    foreach ($messageParts as $partName => $partType) {
                        list ($nsPrefix, $typeName) = $this->_getTypeNs($partType);

                        $thisMessage[$partName] = array(
                            'name' => $partName,
                            'type' => $typeName,
                            'namespace' => $nsPrefix
                            );
                    }
                }
            }
        }

        // Populate tree with portType information
        // XXX Current implementation only supports one portType that
        // encompasses all of the operations available.
        // *** <wsdl:portType> ***
        if (!isset($this->wsdl->portTypes[$service_name . 'Port'])) {
            $this->wsdl->portTypes[$service_name . 'Port'] = array();
        }
        $thisPortType =& $this->wsdl->portTypes[$service_name . 'Port'];

        foreach ($object->__dispatch_map as $operationName => $messages) {
            $thisPortType[$operationName] = array('name' => $operationName);

            foreach ($messages as $messageType => $messageParts) {
                switch ($messageType) {
                case 'in':
                    $thisPortType[$operationName]['input'] = array(
                        'message' => $operationName . 'Request',
                        'namespace' => $this->tnsPrefix);
                    break;

                case 'out':
                    $thisPortType[$operationName]['output'] = array(
                        'message' => $operationName . 'Response',
                        'namespace' => $this->tnsPrefix);
                    break;
                }
            }
        }

        return true;
    }

    /**
     * Takes all the abstract WSDL data and builds concrete bindings and
     * services (destructive).
     *
     * @access private
     * @todo Current implementation discards $service_desc.
     *
     * @param string $schemaNamespace  Namespace for types etc.
     * @param string $service_name     Name of the WSDL <service>.
     * @param string $service_desc     Optional description of the WSDL
     *                                 <service>.
     */
    function _generateBindingsAndServices($schemaNamespace, $service_name,
                                          $service_desc = '')
    {
        // Populate tree with bindings information
        // XXX Current implementation only supports one binding that
        // matches the single portType and all of its operations.
        // XXX Is this the correct use of $schemaNamespace here?
        // *** <wsdl:binding> ***
        $this->wsdl->bindings[$service_name . 'Binding'] = array(
                'type' => $service_name . 'Port',
                'namespace' => $this->tnsPrefix,
                'style' => 'rpc',
                'transport' => SCHEMA_SOAP_HTTP,
                'operations' => array());
        $thisBinding =& $this->wsdl->bindings[$service_name . 'Binding'];

        foreach ($this->wsdl->portTypes[$service_name . 'Port'] as $operationName => $operationData) {
            $thisBinding['operations'][$operationName] = array(
                'soapAction' => $schemaNamespace . '#' . $operationName,
                'style' => $thisBinding['style']);

            foreach (array('input', 'output') as $messageType)
                if (isset($operationData[$messageType])) {
                    $thisBinding['operations'][$operationName][$messageType] = array(
                            'use' => 'encoded',
                            'namespace' => $schemaNamespace,
                            'encodingStyle' => SOAP_SCHEMA_ENCODING);
                }
        }

        // Populate tree with service information
        // XXX Current implementation supports one service which groups
        // all of the ports together, one port per binding
        // *** <wsdl:service> ***

        $this->wsdl->services[$service_name . 'Service'] = array('ports' => array());
        $thisService =& $this->wsdl->services[$service_name . 'Service']['ports'];
        $https = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) ||
            getenv('SSL_PROTOCOL_VERSION');

        foreach ($this->wsdl->bindings as $bindingName => $bindingData) {
            $thisService[$bindingData['type']] = array(
                    'name' => $bindingData['type'],
                    'binding' => $bindingName,
                    'namespace' => $this->tnsPrefix,
                    'address' => array('location' =>
                        ($https ? 'https://' : 'http://') .
                        $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] .
                        (isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '')),
                    'type' => 'soap');
        }

        // Set service
        $this->wsdl->set_service($service_name . 'Service');
        $this->wsdl->uri = $this->wsdl->namespaces[$this->tnsPrefix];

        // Create WSDL definition
        // *** <wsdl:definitions> ***

        $this->wsdl->definition = array(
                'name' => $service_name,
                'targetNamespace' => $this->wsdl->namespaces[$this->tnsPrefix],
                'xmlns' => SCHEMA_WSDL);

        foreach ($this->wsdl->namespaces as $nsPrefix => $namespace) {
            $this->wsdl->definition['xmlns:' . $nsPrefix] = $namespace;
        }
    }

    /**
     * This function is adapted from Dmitri V's implementation of
     * DISCO/WSDL generation. It separates namespace from type name in
     * a __typedef key and creates a new namespace entry in the WSDL
     * structure if the namespace has not been used before. The
     * namespace prefix and type name are returned. If no namespace is
     * specified, xsd is assumed.
     *
     * We will not need this function anymore once __typedef is
     * eliminated.
     */
    function _getTypeNs($type)
    {
        preg_match_all('/\{(.*)\}/sm', $type, $m);
        if (!empty($m[1][0])) {
            if (!isset($this->wsdl->ns[$m[1][0]])) {
                $ns_pref = 'ns' . count($this->wsdl->namespaces);
                $this->wsdl->ns[$m[1][0]] = $ns_pref;
                $this->wsdl->namespaces[$ns_pref] = $m[1][0];
            }
            $typens = $this->wsdl->ns[$m[1][0]];
            $type = str_replace($m[0][0], '', $type);
        } else {
            $typens = 'xsd';
        }

        return array($typens, $type);
    }

}
Back to Top