PageRenderTime 109ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/includes/nusoap/lib/nusoap.php

https://bitbucket.org/speedealing/speedealing
PHP | 8143 lines | 6631 code | 205 blank | 1307 comment | 790 complexity | eeae0107c2945b49de51916cb87adf49 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /*
  3. NuSOAP - Web Services Toolkit for PHP
  4. Copyright (c) 2002 NuSphere Corporation
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. The NuSOAP project home is:
  16. http://sourceforge.net/projects/nusoap/
  17. The primary support for NuSOAP is the Help forum on the project home page.
  18. If you have any questions or comments, please email:
  19. Dietrich Ayala
  20. dietrich@ganx4.com
  21. http://dietrich.ganx4.com/nusoap
  22. NuSphere Corporation
  23. http://www.nusphere.com
  24. */
  25. /*
  26. * Some of the standards implmented in whole or part by NuSOAP:
  27. *
  28. * SOAP 1.1 (http://www.w3.org/TR/2000/NOTE-SOAP-20000508/)
  29. * WSDL 1.1 (http://www.w3.org/TR/2001/NOTE-wsdl-20010315)
  30. * SOAP Messages With Attachments (http://www.w3.org/TR/SOAP-attachments)
  31. * XML 1.0 (http://www.w3.org/TR/2006/REC-xml-20060816/)
  32. * Namespaces in XML 1.0 (http://www.w3.org/TR/2006/REC-xml-names-20060816/)
  33. * XML Schema 1.0 (http://www.w3.org/TR/xmlschema-0/)
  34. * RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies
  35. * RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1
  36. * RFC 2617 HTTP Authentication: Basic and Digest Access Authentication
  37. */
  38. /* load classes
  39. // necessary classes
  40. require_once('class.soapclient.php');
  41. require_once('class.soap_val.php');
  42. require_once('class.soap_parser.php');
  43. require_once('class.soap_fault.php');
  44. // transport classes
  45. require_once('class.soap_transport_http.php');
  46. // optional add-on classes
  47. require_once('class.xmlschema.php');
  48. require_once('class.wsdl.php');
  49. // server class
  50. require_once('class.soap_server.php');*/
  51. // class variable emulation
  52. // cf. http://www.webkreator.com/php/techniques/php-static-class-variables.html
  53. $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'] = 9;
  54. /**
  55. *
  56. * nusoap_base
  57. *
  58. * @author Dietrich Ayala <dietrich@ganx4.com>
  59. * @author Scott Nichol <snichol@users.sourceforge.net>
  60. * @access public
  61. */
  62. class nusoap_base {
  63. /**
  64. * Identification for HTTP headers.
  65. *
  66. * @var string
  67. * @access private
  68. */
  69. var $title = 'NuSOAP';
  70. /**
  71. * Version for HTTP headers.
  72. *
  73. * @var string
  74. * @access private
  75. */
  76. var $version = '0.9.5';
  77. /**
  78. * CVS revision for HTTP headers.
  79. *
  80. * @var string
  81. * @access private
  82. */
  83. var $revision = '1.15';
  84. /**
  85. * Current error string (manipulated by getError/setError)
  86. *
  87. * @var string
  88. * @access private
  89. */
  90. var $error_str = '';
  91. /**
  92. * Current debug string (manipulated by debug/appendDebug/clearDebug/getDebug/getDebugAsXMLComment)
  93. *
  94. * @var string
  95. * @access private
  96. */
  97. var $debug_str = '';
  98. /**
  99. * toggles automatic encoding of special characters as entities
  100. * (should always be true, I think)
  101. *
  102. * @var boolean
  103. * @access private
  104. */
  105. var $charencoding = true;
  106. /**
  107. * the debug level for this instance
  108. *
  109. * @var integer
  110. * @access private
  111. */
  112. var $debugLevel;
  113. /**
  114. * set schema version
  115. *
  116. * @var string
  117. * @access public
  118. */
  119. var $XMLSchemaVersion = 'http://www.w3.org/2001/XMLSchema';
  120. /**
  121. * charset encoding for outgoing messages
  122. *
  123. * @var string
  124. * @access public
  125. */
  126. var $soap_defencoding = 'ISO-8859-1';
  127. //var $soap_defencoding = 'UTF-8';
  128. /**
  129. * namespaces in an array of prefix => uri
  130. *
  131. * this is "seeded" by a set of constants, but it may be altered by code
  132. *
  133. * @var array
  134. * @access public
  135. */
  136. var $namespaces = array(
  137. 'SOAP-ENV' => 'http://schemas.xmlsoap.org/soap/envelope/',
  138. 'xsd' => 'http://www.w3.org/2001/XMLSchema',
  139. 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  140. 'SOAP-ENC' => 'http://schemas.xmlsoap.org/soap/encoding/'
  141. );
  142. /**
  143. * namespaces used in the current context, e.g. during serialization
  144. *
  145. * @var array
  146. * @access private
  147. */
  148. var $usedNamespaces = array();
  149. /**
  150. * XML Schema types in an array of uri => (array of xml type => php type)
  151. * is this legacy yet?
  152. * no, this is used by the nusoap_xmlschema class to verify type => namespace mappings.
  153. * @var array
  154. * @access public
  155. */
  156. var $typemap = array(
  157. 'http://www.w3.org/2001/XMLSchema' => array(
  158. 'string'=>'string','boolean'=>'boolean','float'=>'double','double'=>'double','decimal'=>'double',
  159. 'duration'=>'','dateTime'=>'string','time'=>'string','date'=>'string','gYearMonth'=>'',
  160. 'gYear'=>'','gMonthDay'=>'','gDay'=>'','gMonth'=>'','hexBinary'=>'string','base64Binary'=>'string',
  161. // abstract "any" types
  162. 'anyType'=>'string','anySimpleType'=>'string',
  163. // derived datatypes
  164. 'normalizedString'=>'string','token'=>'string','language'=>'','NMTOKEN'=>'','NMTOKENS'=>'','Name'=>'','NCName'=>'','ID'=>'',
  165. 'IDREF'=>'','IDREFS'=>'','ENTITY'=>'','ENTITIES'=>'','integer'=>'integer','nonPositiveInteger'=>'integer',
  166. 'negativeInteger'=>'integer','long'=>'integer','int'=>'integer','short'=>'integer','byte'=>'integer','nonNegativeInteger'=>'integer',
  167. 'unsignedLong'=>'','unsignedInt'=>'','unsignedShort'=>'','unsignedByte'=>'','positiveInteger'=>''),
  168. 'http://www.w3.org/2000/10/XMLSchema' => array(
  169. 'i4'=>'','int'=>'integer','boolean'=>'boolean','string'=>'string','double'=>'double',
  170. 'float'=>'double','dateTime'=>'string',
  171. 'timeInstant'=>'string','base64Binary'=>'string','base64'=>'string','ur-type'=>'array'),
  172. 'http://www.w3.org/1999/XMLSchema' => array(
  173. 'i4'=>'','int'=>'integer','boolean'=>'boolean','string'=>'string','double'=>'double',
  174. 'float'=>'double','dateTime'=>'string',
  175. 'timeInstant'=>'string','base64Binary'=>'string','base64'=>'string','ur-type'=>'array'),
  176. 'http://soapinterop.org/xsd' => array('SOAPStruct'=>'struct'),
  177. 'http://schemas.xmlsoap.org/soap/encoding/' => array('base64'=>'string','array'=>'array','Array'=>'array'),
  178. 'http://xml.apache.org/xml-soap' => array('Map')
  179. );
  180. /**
  181. * XML entities to convert
  182. *
  183. * @var array
  184. * @access public
  185. * @deprecated
  186. * @see expandEntities
  187. */
  188. var $xmlEntities = array('quot' => '"','amp' => '&',
  189. 'lt' => '<','gt' => '>','apos' => "'");
  190. /**
  191. * constructor
  192. *
  193. * @access public
  194. */
  195. function nusoap_base() {
  196. $this->debugLevel = $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'];
  197. }
  198. /**
  199. * gets the global debug level, which applies to future instances
  200. *
  201. * @return integer Debug level 0-9, where 0 turns off
  202. * @access public
  203. */
  204. function getGlobalDebugLevel() {
  205. return $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'];
  206. }
  207. /**
  208. * sets the global debug level, which applies to future instances
  209. *
  210. * @param int $level Debug level 0-9, where 0 turns off
  211. * @access public
  212. */
  213. function setGlobalDebugLevel($level) {
  214. $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'] = $level;
  215. }
  216. /**
  217. * gets the debug level for this instance
  218. *
  219. * @return int Debug level 0-9, where 0 turns off
  220. * @access public
  221. */
  222. function getDebugLevel() {
  223. return $this->debugLevel;
  224. }
  225. /**
  226. * sets the debug level for this instance
  227. *
  228. * @param int $level Debug level 0-9, where 0 turns off
  229. * @access public
  230. */
  231. function setDebugLevel($level) {
  232. $this->debugLevel = $level;
  233. }
  234. /**
  235. * adds debug data to the instance debug string with formatting
  236. *
  237. * @param string $string debug data
  238. * @access private
  239. */
  240. function debug($string){
  241. if ($this->debugLevel > 0) {
  242. $this->appendDebug($this->getmicrotime().' '.get_class($this).": $string\n");
  243. }
  244. }
  245. /**
  246. * adds debug data to the instance debug string without formatting
  247. *
  248. * @param string $string debug data
  249. * @access public
  250. */
  251. function appendDebug($string){
  252. if ($this->debugLevel > 0) {
  253. // it would be nice to use a memory stream here to use
  254. // memory more efficiently
  255. $this->debug_str .= $string;
  256. }
  257. }
  258. /**
  259. * clears the current debug data for this instance
  260. *
  261. * @access public
  262. */
  263. function clearDebug() {
  264. // it would be nice to use a memory stream here to use
  265. // memory more efficiently
  266. $this->debug_str = '';
  267. }
  268. /**
  269. * gets the current debug data for this instance
  270. *
  271. * @return debug data
  272. * @access public
  273. */
  274. function &getDebug() {
  275. // it would be nice to use a memory stream here to use
  276. // memory more efficiently
  277. return $this->debug_str;
  278. }
  279. /**
  280. * gets the current debug data for this instance as an XML comment
  281. * this may change the contents of the debug data
  282. *
  283. * @return debug data as an XML comment
  284. * @access public
  285. */
  286. function &getDebugAsXMLComment() {
  287. // it would be nice to use a memory stream here to use
  288. // memory more efficiently
  289. while (strpos($this->debug_str, '--')) {
  290. $this->debug_str = str_replace('--', '- -', $this->debug_str);
  291. }
  292. $ret = "<!--\n" . $this->debug_str . "\n-->";
  293. return $ret;
  294. }
  295. /**
  296. * expands entities, e.g. changes '<' to '&lt;'.
  297. *
  298. * @param string $val The string in which to expand entities.
  299. * @access private
  300. */
  301. function expandEntities($val) {
  302. if ($this->charencoding) {
  303. $val = str_replace('&', '&amp;', $val);
  304. $val = str_replace("'", '&apos;', $val);
  305. $val = str_replace('"', '&quot;', $val);
  306. $val = str_replace('<', '&lt;', $val);
  307. $val = str_replace('>', '&gt;', $val);
  308. }
  309. return $val;
  310. }
  311. /**
  312. * returns error string if present
  313. *
  314. * @return mixed error string or false
  315. * @access public
  316. */
  317. function getError(){
  318. if($this->error_str != ''){
  319. return $this->error_str;
  320. }
  321. return false;
  322. }
  323. /**
  324. * sets error string
  325. *
  326. * @return boolean $string error string
  327. * @access private
  328. */
  329. function setError($str){
  330. $this->error_str = $str;
  331. }
  332. /**
  333. * detect if array is a simple array or a struct (associative array)
  334. *
  335. * @param mixed $val The PHP array
  336. * @return string (arraySimple|arrayStruct)
  337. * @access private
  338. */
  339. function isArraySimpleOrStruct($val) {
  340. $keyList = array_keys($val);
  341. foreach ($keyList as $keyListValue) {
  342. if (!is_int($keyListValue)) {
  343. return 'arrayStruct';
  344. }
  345. }
  346. return 'arraySimple';
  347. }
  348. /**
  349. * serializes PHP values in accordance w/ section 5. Type information is
  350. * not serialized if $use == 'literal'.
  351. *
  352. * @param mixed $val The value to serialize
  353. * @param string $name The name (local part) of the XML element
  354. * @param string $type The XML schema type (local part) for the element
  355. * @param string $name_ns The namespace for the name of the XML element
  356. * @param string $type_ns The namespace for the type of the element
  357. * @param array $attributes The attributes to serialize as name=>value pairs
  358. * @param string $use The WSDL "use" (encoded|literal)
  359. * @param boolean $soapval Whether this is called from soapval.
  360. * @return string The serialized element, possibly with child elements
  361. * @access public
  362. */
  363. function serialize_val($val,$name=false,$type=false,$name_ns=false,$type_ns=false,$attributes=false,$use='encoded',$soapval=false) {
  364. $this->debug("in serialize_val: name=$name, type=$type, name_ns=$name_ns, type_ns=$type_ns, use=$use, soapval=$soapval");
  365. $this->appendDebug('value=' . $this->varDump($val));
  366. $this->appendDebug('attributes=' . $this->varDump($attributes));
  367. if (is_object($val) && get_class($val) == 'soapval' && (! $soapval)) {
  368. $this->debug("serialize_val: serialize soapval");
  369. $xml = $val->serialize($use);
  370. $this->appendDebug($val->getDebug());
  371. $val->clearDebug();
  372. $this->debug("serialize_val of soapval returning $xml");
  373. return $xml;
  374. }
  375. // force valid name if necessary
  376. if (is_numeric($name)) {
  377. $name = '__numeric_' . $name;
  378. } elseif (! $name) {
  379. $name = 'noname';
  380. }
  381. // if name has ns, add ns prefix to name
  382. $xmlns = '';
  383. if($name_ns){
  384. $prefix = 'nu'.rand(1000,9999);
  385. $name = $prefix.':'.$name;
  386. $xmlns .= " xmlns:$prefix=\"$name_ns\"";
  387. }
  388. // if type is prefixed, create type prefix
  389. if($type_ns != '' && $type_ns == $this->namespaces['xsd']){
  390. // need to fix this. shouldn't default to xsd if no ns specified
  391. // w/o checking against typemap
  392. $type_prefix = 'xsd';
  393. } elseif($type_ns){
  394. $type_prefix = 'ns'.rand(1000,9999);
  395. $xmlns .= " xmlns:$type_prefix=\"$type_ns\"";
  396. }
  397. // serialize attributes if present
  398. $atts = '';
  399. if($attributes){
  400. foreach($attributes as $k => $v){
  401. $atts .= " $k=\"".$this->expandEntities($v).'"';
  402. }
  403. }
  404. // serialize null value
  405. if (is_null($val)) {
  406. $this->debug("serialize_val: serialize null");
  407. if ($use == 'literal') {
  408. // TODO: depends on minOccurs
  409. $xml = "<$name$xmlns$atts/>";
  410. $this->debug("serialize_val returning $xml");
  411. return $xml;
  412. } else {
  413. if (isset($type) && isset($type_prefix)) {
  414. $type_str = " xsi:type=\"$type_prefix:$type\"";
  415. } else {
  416. $type_str = '';
  417. }
  418. $xml = "<$name$xmlns$type_str$atts xsi:nil=\"true\"/>";
  419. $this->debug("serialize_val returning $xml");
  420. return $xml;
  421. }
  422. }
  423. // serialize if an xsd built-in primitive type
  424. if($type != '' && isset($this->typemap[$this->XMLSchemaVersion][$type])){
  425. $this->debug("serialize_val: serialize xsd built-in primitive type");
  426. if (is_bool($val)) {
  427. if ($type == 'boolean') {
  428. $val = $val ? 'true' : 'false';
  429. } elseif (! $val) {
  430. $val = 0;
  431. }
  432. } else if (is_string($val)) {
  433. $val = $this->expandEntities($val);
  434. }
  435. if ($use == 'literal') {
  436. $xml = "<$name$xmlns$atts>$val</$name>";
  437. $this->debug("serialize_val returning $xml");
  438. return $xml;
  439. } else {
  440. $xml = "<$name$xmlns xsi:type=\"xsd:$type\"$atts>$val</$name>";
  441. $this->debug("serialize_val returning $xml");
  442. return $xml;
  443. }
  444. }
  445. // detect type and serialize
  446. $xml = '';
  447. switch(true) {
  448. case (is_bool($val) || $type == 'boolean'):
  449. $this->debug("serialize_val: serialize boolean");
  450. if ($type == 'boolean') {
  451. $val = $val ? 'true' : 'false';
  452. } elseif (! $val) {
  453. $val = 0;
  454. }
  455. if ($use == 'literal') {
  456. $xml .= "<$name$xmlns$atts>$val</$name>";
  457. } else {
  458. $xml .= "<$name$xmlns xsi:type=\"xsd:boolean\"$atts>$val</$name>";
  459. }
  460. break;
  461. case (is_int($val) || is_long($val) || $type == 'int'):
  462. $this->debug("serialize_val: serialize int");
  463. if ($use == 'literal') {
  464. $xml .= "<$name$xmlns$atts>$val</$name>";
  465. } else {
  466. $xml .= "<$name$xmlns xsi:type=\"xsd:int\"$atts>$val</$name>";
  467. }
  468. break;
  469. case (is_float($val)|| is_double($val) || $type == 'float'):
  470. $this->debug("serialize_val: serialize float");
  471. if ($use == 'literal') {
  472. $xml .= "<$name$xmlns$atts>$val</$name>";
  473. } else {
  474. $xml .= "<$name$xmlns xsi:type=\"xsd:float\"$atts>$val</$name>";
  475. }
  476. break;
  477. case (is_string($val) || $type == 'string'):
  478. $this->debug("serialize_val: serialize string");
  479. $val = $this->expandEntities($val);
  480. if ($use == 'literal') {
  481. $xml .= "<$name$xmlns$atts>$val</$name>";
  482. } else {
  483. $xml .= "<$name$xmlns xsi:type=\"xsd:string\"$atts>$val</$name>";
  484. }
  485. break;
  486. case is_object($val):
  487. $this->debug("serialize_val: serialize object");
  488. if (get_class($val) == 'soapval') {
  489. $this->debug("serialize_val: serialize soapval object");
  490. $pXml = $val->serialize($use);
  491. $this->appendDebug($val->getDebug());
  492. $val->clearDebug();
  493. } else {
  494. if (! $name) {
  495. $name = get_class($val);
  496. $this->debug("In serialize_val, used class name $name as element name");
  497. } else {
  498. $this->debug("In serialize_val, do not override name $name for element name for class " . get_class($val));
  499. }
  500. foreach(get_object_vars($val) as $k => $v){
  501. $pXml = isset($pXml) ? $pXml.$this->serialize_val($v,$k,false,false,false,false,$use) : $this->serialize_val($v,$k,false,false,false,false,$use);
  502. }
  503. }
  504. if(isset($type) && isset($type_prefix)){
  505. $type_str = " xsi:type=\"$type_prefix:$type\"";
  506. } else {
  507. $type_str = '';
  508. }
  509. if ($use == 'literal') {
  510. $xml .= "<$name$xmlns$atts>$pXml</$name>";
  511. } else {
  512. $xml .= "<$name$xmlns$type_str$atts>$pXml</$name>";
  513. }
  514. break;
  515. break;
  516. case (is_array($val) || $type):
  517. // detect if struct or array
  518. $valueType = $this->isArraySimpleOrStruct($val);
  519. if($valueType=='arraySimple' || preg_match('/^ArrayOf/',$type)){
  520. $this->debug("serialize_val: serialize array");
  521. $i = 0;
  522. if(is_array($val) && count($val)> 0){
  523. foreach($val as $v){
  524. if(is_object($v) && get_class($v) == 'soapval'){
  525. $tt_ns = $v->type_ns;
  526. $tt = $v->type;
  527. } elseif (is_array($v)) {
  528. $tt = $this->isArraySimpleOrStruct($v);
  529. } else {
  530. $tt = gettype($v);
  531. }
  532. $array_types[$tt] = 1;
  533. // TODO: for literal, the name should be $name
  534. //DOL_CHANGE This is to have tag name with name reported by wsdl and not "item"
  535. //$xml .= $this->serialize_val($v,'item',false,false,false,false,$use);
  536. $tmp=preg_replace('/s$/i','',$name);
  537. $xml .= $this->serialize_val($v,$tmp?$tmp:'item',false,false,false,false,$use);
  538. ++$i;
  539. }
  540. if(count($array_types) > 1){
  541. $array_typename = 'xsd:anyType';
  542. } elseif(isset($tt) && isset($this->typemap[$this->XMLSchemaVersion][$tt])) {
  543. if ($tt == 'integer') {
  544. $tt = 'int';
  545. }
  546. $array_typename = 'xsd:'.$tt;
  547. } elseif(isset($tt) && $tt == 'arraySimple'){
  548. $array_typename = 'SOAP-ENC:Array';
  549. } elseif(isset($tt) && $tt == 'arrayStruct'){
  550. $array_typename = 'unnamed_struct_use_soapval';
  551. } else {
  552. // if type is prefixed, create type prefix
  553. if ($tt_ns != '' && $tt_ns == $this->namespaces['xsd']){
  554. $array_typename = 'xsd:' . $tt;
  555. } elseif ($tt_ns) {
  556. $tt_prefix = 'ns' . rand(1000, 9999);
  557. $array_typename = "$tt_prefix:$tt";
  558. $xmlns .= " xmlns:$tt_prefix=\"$tt_ns\"";
  559. } else {
  560. $array_typename = $tt;
  561. }
  562. }
  563. $array_type = $i;
  564. if ($use == 'literal') {
  565. $type_str = '';
  566. } else if (isset($type) && isset($type_prefix)) {
  567. $type_str = " xsi:type=\"$type_prefix:$type\"";
  568. } else {
  569. $type_str = " xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"".$array_typename."[$array_type]\"";
  570. }
  571. // empty array
  572. } else {
  573. if ($use == 'literal') {
  574. $type_str = '';
  575. } else if (isset($type) && isset($type_prefix)) {
  576. $type_str = " xsi:type=\"$type_prefix:$type\"";
  577. } else {
  578. $type_str = " xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"xsd:anyType[0]\"";
  579. }
  580. }
  581. // TODO: for array in literal, there is no wrapper here
  582. $xml = "<$name$xmlns$type_str$atts>".$xml."</$name>";
  583. } else {
  584. // got a struct
  585. $this->debug("serialize_val: serialize struct");
  586. if(isset($type) && isset($type_prefix)){
  587. $type_str = " xsi:type=\"$type_prefix:$type\"";
  588. } else {
  589. $type_str = '';
  590. }
  591. if ($use == 'literal') {
  592. $xml .= "<$name$xmlns$atts>";
  593. } else {
  594. $xml .= "<$name$xmlns$type_str$atts>";
  595. }
  596. foreach($val as $k => $v){
  597. // Apache Map
  598. if ($type == 'Map' && $type_ns == 'http://xml.apache.org/xml-soap') {
  599. $xml .= '<item>';
  600. $xml .= $this->serialize_val($k,'key',false,false,false,false,$use);
  601. $xml .= $this->serialize_val($v,'value',false,false,false,false,$use);
  602. $xml .= '</item>';
  603. } else {
  604. $xml .= $this->serialize_val($v,$k,false,false,false,false,$use);
  605. }
  606. }
  607. $xml .= "</$name>";
  608. }
  609. break;
  610. default:
  611. $this->debug("serialize_val: serialize unknown");
  612. $xml .= 'not detected, got '.gettype($val).' for '.$val;
  613. break;
  614. }
  615. $this->debug("serialize_val returning $xml");
  616. return $xml;
  617. }
  618. /**
  619. * serializes a message
  620. *
  621. * @param string $body the XML of the SOAP body
  622. * @param mixed $headers optional string of XML with SOAP header content, or array of soapval objects for SOAP headers, or associative array
  623. * @param array $namespaces optional the namespaces used in generating the body and headers
  624. * @param string $style optional (rpc|document)
  625. * @param string $use optional (encoded|literal)
  626. * @param string $encodingStyle optional (usually 'http://schemas.xmlsoap.org/soap/encoding/' for encoded)
  627. * @return string the message
  628. * @access public
  629. */
  630. function serializeEnvelope($body,$headers=false,$namespaces=array(),$style='rpc',$use='encoded',$encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'){
  631. // TODO: add an option to automatically run utf8_encode on $body and $headers
  632. // if $this->soap_defencoding is UTF-8. Not doing this automatically allows
  633. // one to send arbitrary UTF-8 characters, not just characters that map to ISO-8859-1
  634. $this->debug("In serializeEnvelope length=" . strlen($body) . " body (max 1000 characters)=" . substr($body, 0, 1000) . " style=$style use=$use encodingStyle=$encodingStyle");
  635. $this->debug("headers:");
  636. $this->appendDebug($this->varDump($headers));
  637. $this->debug("namespaces:");
  638. $this->appendDebug($this->varDump($namespaces));
  639. // serialize namespaces
  640. $ns_string = '';
  641. foreach(array_merge($this->namespaces,$namespaces) as $k => $v){
  642. $ns_string .= " xmlns:$k=\"$v\"";
  643. }
  644. if($encodingStyle) {
  645. $ns_string = " SOAP-ENV:encodingStyle=\"$encodingStyle\"$ns_string";
  646. }
  647. // serialize headers
  648. if($headers){
  649. if (is_array($headers)) {
  650. $xml = '';
  651. foreach ($headers as $k => $v) {
  652. if (is_object($v) && get_class($v) == 'soapval') {
  653. $xml .= $this->serialize_val($v, false, false, false, false, false, $use);
  654. } else {
  655. $xml .= $this->serialize_val($v, $k, false, false, false, false, $use);
  656. }
  657. }
  658. $headers = $xml;
  659. $this->debug("In serializeEnvelope, serialized array of headers to $headers");
  660. }
  661. $headers = "<SOAP-ENV:Header>".$headers."</SOAP-ENV:Header>";
  662. }
  663. // serialize envelope
  664. return
  665. '<?xml version="1.0" encoding="'.$this->soap_defencoding .'"?'.">".
  666. '<SOAP-ENV:Envelope'.$ns_string.">".
  667. $headers.
  668. "<SOAP-ENV:Body>".
  669. $body.
  670. "</SOAP-ENV:Body>".
  671. "</SOAP-ENV:Envelope>";
  672. }
  673. /**
  674. * formats a string to be inserted into an HTML stream
  675. *
  676. * @param string $str The string to format
  677. * @return string The formatted string
  678. * @access public
  679. * @deprecated
  680. */
  681. function formatDump($str){
  682. $str = htmlspecialchars($str);
  683. return nl2br($str);
  684. }
  685. /**
  686. * contracts (changes namespace to prefix) a qualified name
  687. *
  688. * @param string $qname qname
  689. * @return string contracted qname
  690. * @access private
  691. */
  692. function contractQname($qname){
  693. // get element namespace
  694. //$this->xdebug("Contract $qname");
  695. if (strrpos($qname, ':')) {
  696. // get unqualified name
  697. $name = substr($qname, strrpos($qname, ':') + 1);
  698. // get ns
  699. $ns = substr($qname, 0, strrpos($qname, ':'));
  700. $p = $this->getPrefixFromNamespace($ns);
  701. if ($p) {
  702. return $p . ':' . $name;
  703. }
  704. return $qname;
  705. } else {
  706. return $qname;
  707. }
  708. }
  709. /**
  710. * expands (changes prefix to namespace) a qualified name
  711. *
  712. * @param string $qname qname
  713. * @return string expanded qname
  714. * @access private
  715. */
  716. function expandQname($qname){
  717. // get element prefix
  718. if(strpos($qname,':') && !preg_match('/^http:\/\//',$qname)){
  719. // get unqualified name
  720. $name = substr(strstr($qname,':'),1);
  721. // get ns prefix
  722. $prefix = substr($qname,0,strpos($qname,':'));
  723. if(isset($this->namespaces[$prefix])){
  724. return $this->namespaces[$prefix].':'.$name;
  725. } else {
  726. return $qname;
  727. }
  728. } else {
  729. return $qname;
  730. }
  731. }
  732. /**
  733. * returns the local part of a prefixed string
  734. * returns the original string, if not prefixed
  735. *
  736. * @param string $str The prefixed string
  737. * @return string The local part
  738. * @access public
  739. */
  740. function getLocalPart($str){
  741. if($sstr = strrchr($str,':')){
  742. // get unqualified name
  743. return substr( $sstr, 1 );
  744. } else {
  745. return $str;
  746. }
  747. }
  748. /**
  749. * returns the prefix part of a prefixed string
  750. * returns false, if not prefixed
  751. *
  752. * @param string $str The prefixed string
  753. * @return mixed The prefix or false if there is no prefix
  754. * @access public
  755. */
  756. function getPrefix($str){
  757. if($pos = strrpos($str,':')){
  758. // get prefix
  759. return substr($str,0,$pos);
  760. }
  761. return false;
  762. }
  763. /**
  764. * pass it a prefix, it returns a namespace
  765. *
  766. * @param string $prefix The prefix
  767. * @return mixed The namespace, false if no namespace has the specified prefix
  768. * @access public
  769. */
  770. function getNamespaceFromPrefix($prefix){
  771. if (isset($this->namespaces[$prefix])) {
  772. return $this->namespaces[$prefix];
  773. }
  774. //$this->setError("No namespace registered for prefix '$prefix'");
  775. return false;
  776. }
  777. /**
  778. * returns the prefix for a given namespace (or prefix)
  779. * or false if no prefixes registered for the given namespace
  780. *
  781. * @param string $ns The namespace
  782. * @return mixed The prefix, false if the namespace has no prefixes
  783. * @access public
  784. */
  785. function getPrefixFromNamespace($ns) {
  786. foreach ($this->namespaces as $p => $n) {
  787. if ($ns == $n || $ns == $p) {
  788. $this->usedNamespaces[$p] = $n;
  789. return $p;
  790. }
  791. }
  792. return false;
  793. }
  794. /**
  795. * returns the time in ODBC canonical form with microseconds
  796. *
  797. * @return string The time in ODBC canonical form with microseconds
  798. * @access public
  799. */
  800. function getmicrotime() {
  801. if (function_exists('gettimeofday')) {
  802. $tod = gettimeofday();
  803. $sec = $tod['sec'];
  804. $usec = $tod['usec'];
  805. } else {
  806. $sec = time();
  807. $usec = 0;
  808. }
  809. return strftime('%Y-%m-%d %H:%M:%S', $sec) . '.' . sprintf('%06d', $usec);
  810. }
  811. /**
  812. * Returns a string with the output of var_dump
  813. *
  814. * @param mixed $data The variable to var_dump
  815. * @return string The output of var_dump
  816. * @access public
  817. */
  818. function varDump($data) {
  819. ob_start();
  820. var_dump($data);
  821. $ret_val = ob_get_contents();
  822. ob_end_clean();
  823. return $ret_val;
  824. }
  825. /**
  826. * represents the object as a string
  827. *
  828. * @return string
  829. * @access public
  830. */
  831. function __toString() {
  832. return $this->varDump($this);
  833. }
  834. }
  835. // XML Schema Datatype Helper Functions
  836. //xsd:dateTime helpers
  837. /**
  838. * convert unix timestamp to ISO 8601 compliant date string
  839. *
  840. * @param int $timestamp Unix time stamp
  841. * @param boolean $utc Whether the time stamp is UTC or local
  842. * @return mixed ISO 8601 date string or false
  843. * @access public
  844. */
  845. function timestamp_to_iso8601($timestamp,$utc=true){
  846. $datestr = date('Y-m-d\TH:i:sO',$timestamp);
  847. $pos = strrpos($datestr, "+");
  848. if ($pos === FALSE) {
  849. $pos = strrpos($datestr, "-");
  850. }
  851. if ($pos !== FALSE) {
  852. if (strlen($datestr) == $pos + 5) {
  853. $datestr = substr($datestr, 0, $pos + 3) . ':' . substr($datestr, -2);
  854. }
  855. }
  856. if($utc){
  857. $pattern = '/'.
  858. '([0-9]{4})-'. // centuries & years CCYY-
  859. '([0-9]{2})-'. // months MM-
  860. '([0-9]{2})'. // days DD
  861. 'T'. // separator T
  862. '([0-9]{2}):'. // hours hh:
  863. '([0-9]{2}):'. // minutes mm:
  864. '([0-9]{2})(\.[0-9]*)?'. // seconds ss.ss...
  865. '(Z|[+\-][0-9]{2}:?[0-9]{2})?'. // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
  866. '/';
  867. if(preg_match($pattern,$datestr,$regs)){
  868. return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',$regs[1],$regs[2],$regs[3],$regs[4],$regs[5],$regs[6]);
  869. }
  870. return false;
  871. } else {
  872. return $datestr;
  873. }
  874. }
  875. /**
  876. * convert ISO 8601 compliant date string to unix timestamp
  877. *
  878. * @param string $datestr ISO 8601 compliant date string
  879. * @return mixed Unix timestamp (int) or false
  880. * @access public
  881. */
  882. function iso8601_to_timestamp($datestr){
  883. $pattern = '/'.
  884. '([0-9]{4})-'. // centuries & years CCYY-
  885. '([0-9]{2})-'. // months MM-
  886. '([0-9]{2})'. // days DD
  887. 'T'. // separator T
  888. '([0-9]{2}):'. // hours hh:
  889. '([0-9]{2}):'. // minutes mm:
  890. '([0-9]{2})(\.[0-9]+)?'. // seconds ss.ss...
  891. '(Z|[+\-][0-9]{2}:?[0-9]{2})?'. // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
  892. '/';
  893. if(preg_match($pattern,$datestr,$regs)){
  894. // not utc
  895. if($regs[8] != 'Z'){
  896. $op = substr($regs[8],0,1);
  897. $h = substr($regs[8],1,2);
  898. $m = substr($regs[8],strlen($regs[8])-2,2);
  899. if($op == '-'){
  900. $regs[4] = $regs[4] + $h;
  901. $regs[5] = $regs[5] + $m;
  902. } elseif($op == '+'){
  903. $regs[4] = $regs[4] - $h;
  904. $regs[5] = $regs[5] - $m;
  905. }
  906. }
  907. return gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  908. // return strtotime("$regs[1]-$regs[2]-$regs[3] $regs[4]:$regs[5]:$regs[6]Z");
  909. } else {
  910. return false;
  911. }
  912. }
  913. /**
  914. * sleeps some number of microseconds
  915. *
  916. * @param string $usec the number of microseconds to sleep
  917. * @access public
  918. * @deprecated
  919. */
  920. function usleepWindows($usec)
  921. {
  922. $start = gettimeofday();
  923. do
  924. {
  925. $stop = gettimeofday();
  926. $timePassed = 1000000 * ($stop['sec'] - $start['sec'])
  927. + $stop['usec'] - $start['usec'];
  928. }
  929. while ($timePassed < $usec);
  930. }
  931. ?><?php
  932. /**
  933. * Contains information for a SOAP fault.
  934. * Mainly used for returning faults from deployed functions
  935. * in a server instance.
  936. * @author Dietrich Ayala <dietrich@ganx4.com>
  937. * @access public
  938. */
  939. class nusoap_fault extends nusoap_base {
  940. /**
  941. * The fault code (client|server)
  942. * @var string
  943. * @access private
  944. */
  945. var $faultcode;
  946. /**
  947. * The fault actor
  948. * @var string
  949. * @access private
  950. */
  951. var $faultactor;
  952. /**
  953. * The fault string, a description of the fault
  954. * @var string
  955. * @access private
  956. */
  957. var $faultstring;
  958. /**
  959. * The fault detail, typically a string or array of string
  960. * @var mixed
  961. * @access private
  962. */
  963. var $faultdetail;
  964. /**
  965. * constructor
  966. *
  967. * @param string $faultcode (SOAP-ENV:Client | SOAP-ENV:Server)
  968. * @param string $faultactor only used when msg routed between multiple actors
  969. * @param string $faultstring human readable error message
  970. * @param mixed $faultdetail detail, typically a string or array of string
  971. */
  972. function nusoap_fault($faultcode,$faultactor='',$faultstring='',$faultdetail=''){
  973. parent::nusoap_base();
  974. $this->faultcode = $faultcode;
  975. $this->faultactor = $faultactor;
  976. $this->faultstring = $faultstring;
  977. $this->faultdetail = $faultdetail;
  978. }
  979. /**
  980. * serialize a fault
  981. *
  982. * @return string The serialization of the fault instance.
  983. * @access public
  984. */
  985. function serialize(){
  986. $ns_string = '';
  987. foreach($this->namespaces as $k => $v){
  988. $ns_string .= "\n xmlns:$k=\"$v\"";
  989. }
  990. $return_msg =
  991. '<?xml version="1.0" encoding="'.$this->soap_defencoding.'"?>'.
  992. '<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"'.$ns_string.">\n".
  993. '<SOAP-ENV:Body>'.
  994. '<SOAP-ENV:Fault>'.
  995. $this->serialize_val($this->faultcode, 'faultcode').
  996. $this->serialize_val($this->faultactor, 'faultactor').
  997. $this->serialize_val($this->faultstring, 'faultstring').
  998. $this->serialize_val($this->faultdetail, 'detail').
  999. '</SOAP-ENV:Fault>'.
  1000. '</SOAP-ENV:Body>'.
  1001. '</SOAP-ENV:Envelope>';
  1002. return $return_msg;
  1003. }
  1004. }
  1005. /**
  1006. * Backward compatibility
  1007. */
  1008. class soap_fault extends nusoap_fault {
  1009. }
  1010. ?><?php
  1011. /**
  1012. * parses an XML Schema, allows access to it's data, other utility methods.
  1013. * imperfect, no validation... yet, but quite functional.
  1014. *
  1015. * @author Dietrich Ayala <dietrich@ganx4.com>
  1016. * @author Scott Nichol <snichol@users.sourceforge.net>
  1017. * @access public
  1018. */
  1019. class nusoap_xmlschema extends nusoap_base {
  1020. // files
  1021. var $schema = '';
  1022. var $xml = '';
  1023. // namespaces
  1024. var $enclosingNamespaces;
  1025. // schema info
  1026. var $schemaInfo = array();
  1027. var $schemaTargetNamespace = '';
  1028. // types, elements, attributes defined by the schema
  1029. var $attributes = array();
  1030. var $complexTypes = array();
  1031. var $complexTypeStack = array();
  1032. var $currentComplexType = null;
  1033. var $elements = array();
  1034. var $elementStack = array();
  1035. var $currentElement = null;
  1036. var $simpleTypes = array();
  1037. var $simpleTypeStack = array();
  1038. var $currentSimpleType = null;
  1039. // imports
  1040. var $imports = array();
  1041. // parser vars
  1042. var $parser;
  1043. var $position = 0;
  1044. var $depth = 0;
  1045. var $depth_array = array();
  1046. var $message = array();
  1047. var $defaultNamespace = array();
  1048. /**
  1049. * constructor
  1050. *
  1051. * @param string $schema schema document URI
  1052. * @param string $xml xml document URI
  1053. * @param string $namespaces namespaces defined in enclosing XML
  1054. * @access public
  1055. */
  1056. function nusoap_xmlschema($schema='',$xml='',$namespaces=array()){
  1057. parent::nusoap_base();
  1058. $this->debug('nusoap_xmlschema class instantiated, inside constructor');
  1059. // files
  1060. $this->schema = $schema;
  1061. $this->xml = $xml;
  1062. // namespaces
  1063. $this->enclosingNamespaces = $namespaces;
  1064. $this->namespaces = array_merge($this->namespaces, $namespaces);
  1065. // parse schema file
  1066. if($schema != ''){
  1067. $this->debug('initial schema file: '.$schema);
  1068. $this->parseFile($schema, 'schema');
  1069. }
  1070. // parse xml file
  1071. if($xml != ''){
  1072. $this->debug('initial xml file: '.$xml);
  1073. $this->parseFile($xml, 'xml');
  1074. }
  1075. }
  1076. /**
  1077. * parse an XML file
  1078. *
  1079. * @param string $xml path/URL to XML file
  1080. * @param string $type (schema | xml)
  1081. * @return boolean
  1082. * @access public
  1083. */
  1084. function parseFile($xml,$type){
  1085. // parse xml file
  1086. if($xml != ""){
  1087. $xmlStr = @join("",@file($xml));
  1088. if($xmlStr == ""){
  1089. $msg = 'Error reading XML from '.$xml;
  1090. $this->setError($msg);
  1091. $this->debug($msg);
  1092. return false;
  1093. } else {
  1094. $this->debug("parsing $xml");
  1095. $this->parseString($xmlStr,$type);
  1096. $this->debug("done parsing $xml");
  1097. return true;
  1098. }
  1099. }
  1100. return false;
  1101. }
  1102. /**
  1103. * parse an XML string
  1104. *
  1105. * @param string $xml path or URL
  1106. * @param string $type (schema|xml)
  1107. * @access private
  1108. */
  1109. function parseString($xml,$type){
  1110. // parse xml string
  1111. if($xml != ""){
  1112. // Create an XML parser.
  1113. $this->parser = xml_parser_create();
  1114. // Set the options for parsing the XML data.
  1115. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
  1116. // Set the object for the parser.
  1117. xml_set_object($this->parser, $this);
  1118. // Set the element handlers for the parser.
  1119. if($type == "schema"){
  1120. xml_set_element_handler($this->parser, 'schemaStartElement','schemaEndElement');
  1121. xml_set_character_data_handler($this->parser,'schemaCharacterData');
  1122. } elseif($type == "xml"){
  1123. xml_set_element_handler($this->parser, 'xmlStartElement','xmlEndElement');
  1124. xml_set_character_data_handler($this->parser,'xmlCharacterData');
  1125. }
  1126. // Parse the XML file.
  1127. if(!xml_parse($this->parser,$xml,true)){
  1128. // Display an error message.
  1129. $errstr = sprintf('XML error parsing XML schema on line %d: %s',
  1130. xml_get_current_line_number($this->parser),
  1131. xml_error_string(xml_get_error_code($this->parser))
  1132. );
  1133. $this->debug($errstr);
  1134. $this->debug("XML payload:\n" . $xml);
  1135. $this->setError($errstr);
  1136. }
  1137. xml_parser_free($this->parser);
  1138. } else{
  1139. $this->debug('no xml passed to parseString()!!');
  1140. $this->setError('no xml passed to parseString()!!');
  1141. }
  1142. }
  1143. /**
  1144. * gets a type name for an unnamed type
  1145. *
  1146. * @param string Element name
  1147. * @return string A type name for an unnamed type
  1148. * @access private
  1149. */
  1150. function CreateTypeName($ename) {
  1151. $scope = '';
  1152. for ($i = 0; $i < count($this->complexTypeStack); $i++) {
  1153. $scope .= $this->complexTypeStack[$i] . '_';
  1154. }
  1155. return $scope . $ename . '_ContainedType';
  1156. }
  1157. /**
  1158. * start-element handler
  1159. *
  1160. * @param string $parser XML parser object
  1161. * @param string $name element name
  1162. * @param string $attrs associative array of attributes
  1163. * @access private
  1164. */
  1165. function schemaStartElement($parser, $name, $attrs) {
  1166. // position in the total number of elements, starting from 0
  1167. $pos = $this->position++;
  1168. $depth = $this->depth++;
  1169. // set self as current value for this depth
  1170. $this->depth_array[$depth] = $pos;
  1171. $this->message[$pos] = array('cdata' => '');
  1172. if ($depth > 0) {
  1173. $this->defaultNamespace[$pos] = $this->defaultNamespace[$this->depth_array[$depth - 1]];
  1174. } else {
  1175. $this->defaultNamespace[$pos] = false;
  1176. }
  1177. // get element prefix
  1178. if($prefix = $this->getPrefix($name)){
  1179. // get unqualified name
  1180. $name = $this->getLocalPart($name);
  1181. } else {
  1182. $prefix = '';
  1183. }
  1184. // loop thru attributes, expanding, and registering namespace declarations
  1185. if(count($attrs) > 0){
  1186. foreach($attrs as $k => $v){
  1187. // if ns declarations, add to class level array of valid namespaces
  1188. if(preg_match('/^xmlns/',$k)){
  1189. //$this->xdebug("$k: $v");
  1190. //$this->xdebug('ns_prefix: '.$this->getPrefix($k));
  1191. if($ns_prefix = substr(strrchr($k,':'),1)){
  1192. //$this->xdebug("Add namespace[$ns_prefix] = $v");
  1193. $this->namespaces[$ns_prefix] = $v;
  1194. } else {
  1195. $this->defaultNamespace[$pos] = $v;
  1196. if (! $this->getPrefixFromNamespace($v)) {
  1197. $this->namespaces['ns'.(count($this->namespaces)+1)] = $v;
  1198. }
  1199. }
  1200. if($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema'){
  1201. $this->XMLSchemaVersion = $v;
  1202. $this->namespaces['xsi'] = $v.'-instance';
  1203. }
  1204. }
  1205. }
  1206. foreach($attrs as $k => $v){
  1207. // expand each attribute
  1208. $k = strpos($k,':') ? $this->expandQname($k) : $k;
  1209. $v = strpos($v,':') ? $this->expandQname($v) : $v;
  1210. $eAttrs[$k] = $v;
  1211. }
  1212. $attrs = $eAttrs;
  1213. } else {
  1214. $attrs = array();
  1215. }
  1216. // find status, register data
  1217. switch($name){
  1218. case 'all': // (optional) compositor content for a complexType
  1219. case 'choice':
  1220. case 'group':
  1221. case 'sequence':
  1222. //$this->xdebug("compositor $name for currentComplexType: $this->currentComplexType and currentElement: $this->currentElement");
  1223. $this->complexTypes[$this->currentComplexType]['compositor'] = $name;
  1224. //if($name == 'all' || $name == 'sequence'){
  1225. // $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
  1226. //}
  1227. break;
  1228. case 'attribute': // complexType attribute
  1229. //$this->xdebug("parsing attribute $attrs[name] $attrs[ref] of value: ".$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']);
  1230. $this->xdebug("parsing attribute:");
  1231. $this->appendDebug($this->varDump($attrs));
  1232. if (!isset($attrs['form'])) {
  1233. // TODO: handle globals
  1234. $attrs['form'] = $this->schemaInfo['attributeFormDefault'];
  1235. }
  1236. if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
  1237. $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
  1238. if (!strpos($v, ':')) {
  1239. // no namespace in arrayType attribute value...
  1240. if ($this->defaultNamespace[$pos]) {
  1241. // ...so use the default
  1242. $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'] = $this->defaultNamespace[$pos] . ':' . $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
  1243. }
  1244. }
  1245. }
  1246. if(isset($attrs['name'])){
  1247. $this->attributes[$attrs['name']] = $attrs;
  1248. $aname = $attrs['name'];
  1249. } elseif(isset($attrs['ref']) && $attrs['ref'] == 'http://schemas.xmlsoap.org/soap/encoding/:arrayType'){
  1250. if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
  1251. $aname = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
  1252. } else {
  1253. $aname = '';
  1254. }
  1255. } elseif(isset($attrs['ref'])){
  1256. $aname = $attrs['ref'];
  1257. $this->attributes[$attrs['ref']] = $attrs;
  1258. }
  1259. if($this->currentComplexType){ // This should *always* be
  1260. $this->complexTypes[$this->currentComplexType]['attrs'][$aname] = $attrs;
  1261. }
  1262. // arrayType attribute
  1263. if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']) || $this->getLocalPart($aname) == 'arrayType'){
  1264. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  1265. $prefix = $this->getPrefix($aname);
  1266. if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])){
  1267. $v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
  1268. } else {
  1269. $v = '';
  1270. }
  1271. if(strpos($v,'[,]')){
  1272. $this->complexTypes[$this->currentComplexType]['multidimensional'] = true;
  1273. }
  1274. $v = substr($v,0,strpos($v,'[')); // clip the []
  1275. if(!strpos($v,':') && isset($this->typemap[$this->XMLSchemaVersion][$v])){
  1276. $v = $this->XMLSchemaVersion.':'.$v;
  1277. }
  1278. $this->complexTypes[$this->currentComplexType]['arrayType'] = $v;
  1279. }
  1280. break;
  1281. case 'complexContent': // (optional) content for a complexType
  1282. $this->xdebug("do nothing for element $name");
  1283. break;
  1284. case 'complexType':
  1285. array_push($this->complexTypeStack, $this->currentComplexType);
  1286. if(isset($attrs['name'])){
  1287. // TODO: what is the scope of named complexTypes that appear
  1288. // nested within other c complexTypes?
  1289. $this->xdebug('processing named complexType '.$attrs['name']);
  1290. //$this->currentElement = false;
  1291. $this->currentComplexType = $attrs['name'];
  1292. $this->complexTypes[$this->currentComplexType] = $attrs;
  1293. $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
  1294. // This is for constructs like
  1295. // <complexType name="ListOfString" base="soap:Array">
  1296. // <sequence>
  1297. // <element name="string" type="xsd:string"
  1298. // minOccurs="0" maxOccurs="unbounded" />
  1299. // </sequence>
  1300. // </complexType>
  1301. if(isset($attrs['base']) && preg_match('/:Array$/',$attrs['base'])){
  1302. $this->xdebug('complexType is unusual array');
  1303. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  1304. } else {
  1305. $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
  1306. }
  1307. } else {
  1308. $name = $this->CreateTypeName($this->currentElement);
  1309. $this->xdebug('processing unnamed complexType for element ' . $this->currentElement . ' named ' . $name);
  1310. $this->currentComplexType = $name;
  1311. //$this->currentElement = false;
  1312. $this->complexTypes[$this->currentComplexType] = $attrs;
  1313. $this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
  1314. // This is for constructs like
  1315. // <complexType name="ListOfString" base="soap:Array">
  1316. // <sequence>
  1317. // <element name="string" type="xsd:string"
  1318. // minOccurs="0" maxOccurs="unbounded" />
  1319. // </sequence>
  1320. // </complexType>
  1321. if(isset($attrs['base']) && preg_match('/:Array$/',$attrs['base'])){
  1322. $this->xdebug('complexType is unusual array');
  1323. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  1324. } else {
  1325. $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
  1326. }
  1327. }
  1328. $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'false';
  1329. break;
  1330. case 'element':
  1331. array_push($this->elementStack, $this->currentElement);
  1332. if (!isset($attrs['form'])) {
  1333. if ($this->currentComplexType) {
  1334. $attrs['form'] = $this->schemaInfo['elementFormDefault'];
  1335. } else {
  1336. // global
  1337. $attrs['form'] = 'qualified';
  1338. }
  1339. }
  1340. if(isset($attrs['type'])){
  1341. $this->xdebug("processing typed element ".$attrs['name']." of type ".$attrs['type']);
  1342. if (! $this->getPrefix($attrs['type'])) {
  1343. if ($this->defaultNamespace[$pos]) {
  1344. $attrs['type'] = $this->defaultNamespace[$pos] . ':' . $attrs['type'];
  1345. $this->xdebug('used default namespace to make type ' . $attrs['type']);
  1346. }
  1347. }
  1348. // This is for constructs like
  1349. // <complexType name="ListOfString" base="soap:Array">
  1350. // <sequence>
  1351. // <element name="string" type="xsd:string"
  1352. // minOccurs="0" maxOccurs="unbounded" />
  1353. // </sequence>
  1354. // </complexType>
  1355. if ($this->currentComplexType && $this->complexTypes[$this->currentComplexType]['phpType'] == 'array') {
  1356. $this->xdebug('arrayType for unusual array is ' . $attrs['type']);
  1357. $this->complexTypes[$this->currentComplexType]['arrayType'] = $attrs['type'];
  1358. }
  1359. $this->currentElement = $attrs['name'];
  1360. $ename = $attrs['name'];
  1361. } elseif(isset($attrs['ref'])){
  1362. $this->xdebug("processing element as ref to ".$attrs['ref']);
  1363. $this->currentElement = "ref to ".$attrs['ref'];
  1364. $ename = $this->getLocalPart($attrs['ref']);
  1365. } else {
  1366. $type = $this->CreateTypeName($this->currentComplexType . '_' . $attrs['name']);
  1367. $this->xdebug("processing untyped element " . $attrs['name'] . ' type ' . $type);
  1368. $this->currentElement = $attrs['name'];
  1369. $attrs['type'] = $this->schemaTargetNamespace . ':' . $type;
  1370. $ename = $attrs['name'];
  1371. }
  1372. if (isset($ename) && $this->currentComplexType) {
  1373. $this->xdebug("add element $ename to complexType $this->currentComplexType");
  1374. $this->complexTypes[$this->currentComplexType]['elements'][$ename] = $attrs;
  1375. } elseif (!isset($attrs['ref'])) {
  1376. $this->xdebug("add element $ename to elements array");
  1377. $this->elements[ $attrs['name'] ] = $attrs;
  1378. $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
  1379. }
  1380. break;
  1381. case 'enumeration': // restriction value list member
  1382. $this->xdebug('enumeration ' . $attrs['value']);
  1383. if ($this->currentSimpleType) {
  1384. $this->simpleTypes[$this->currentSimpleType]['enumeration'][] = $attrs['value'];
  1385. } elseif ($this->currentComplexType) {
  1386. $this->complexTypes[$this->currentComplexType]['enumeration'][] = $attrs['value'];
  1387. }
  1388. break;
  1389. case 'extension': // simpleContent or complexContent type extension
  1390. $this->xdebug('extension ' . $attrs['base']);
  1391. if ($this->currentComplexType) {
  1392. $ns = $this->getPrefix($attrs['base']);
  1393. if ($ns == '') {
  1394. $this->complexTypes[$this->currentComplexType]['extensionBase'] = $this->schemaTargetNamespace . ':' . $attrs['base'];
  1395. } else {
  1396. $this->complexTypes[$this->currentComplexType]['extensionBase'] = $attrs['base'];
  1397. }
  1398. } else {
  1399. $this->xdebug('no current complexType to set extensionBase');
  1400. }
  1401. break;
  1402. case 'import':
  1403. if (isset($attrs['schemaLocation'])) {
  1404. $this->xdebug('import namespace ' . $attrs['namespace'] . ' from ' . $attrs['schemaLocation']);
  1405. $this->imports[$attrs['namespace']][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
  1406. } else {
  1407. $this->xdebug('import namespace ' . $attrs['namespace']);
  1408. $this->imports[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
  1409. if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
  1410. $this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
  1411. }
  1412. }
  1413. break;
  1414. case 'include':
  1415. if (isset($attrs['schemaLocation'])) {
  1416. $this->xdebug('include into namespace ' . $this->schemaTargetNamespace . ' from ' . $attrs['schemaLocation']);
  1417. $this->imports[$this->schemaTargetNamespace][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
  1418. } else {
  1419. $this->xdebug('ignoring invalid XML Schema construct: include without schemaLocation attribute');
  1420. }
  1421. break;
  1422. case 'list': // simpleType value list
  1423. $this->xdebug("do nothing for element $name");
  1424. break;
  1425. case 'restriction': // simpleType, simpleContent or complexContent value restriction
  1426. $this->xdebug('restriction ' . $attrs['base']);
  1427. if($this->currentSimpleType){
  1428. $this->simpleTypes[$this->currentSimpleType]['type'] = $attrs['base'];
  1429. } elseif($this->currentComplexType){
  1430. $this->complexTypes[$this->currentComplexType]['restrictionBase'] = $attrs['base'];
  1431. if(strstr($attrs['base'],':') == ':Array'){
  1432. $this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
  1433. }
  1434. }
  1435. break;
  1436. case 'schema':
  1437. $this->schemaInfo = $attrs;
  1438. $this->schemaInfo['schemaVersion'] = $this->getNamespaceFromPrefix($prefix);
  1439. if (isset($attrs['targetNamespace'])) {
  1440. $this->schemaTargetNamespace = $attrs['targetNamespace'];
  1441. }
  1442. if (!isset($attrs['elementFormDefault'])) {
  1443. $this->schemaInfo['elementFormDefault'] = 'unqualified';
  1444. }
  1445. if (!isset($attrs['attributeFormDefault'])) {
  1446. $this->schemaInfo['attributeFormDefault'] = 'unqualified';
  1447. }
  1448. break;
  1449. case 'simpleContent': // (optional) content for a complexType
  1450. if ($this->currentComplexType) { // This should *always* be
  1451. $this->complexTypes[$this->currentComplexType]['simpleContent'] = 'true';
  1452. } else {
  1453. $this->xdebug("do nothing for element $name because there is no current complexType");
  1454. }
  1455. break;
  1456. case 'simpleType':
  1457. array_push($this->simpleTypeStack, $this->currentSimpleType);
  1458. if(isset($attrs['name'])){
  1459. $this->xdebug("processing simpleType for name " . $attrs['name']);
  1460. $this->currentSimpleType = $attrs['name'];
  1461. $this->simpleTypes[ $attrs['name'] ] = $attrs;
  1462. $this->simpleTypes[ $attrs['name'] ]['typeClass'] = 'simpleType';
  1463. $this->simpleTypes[ $attrs['name'] ]['phpType'] = 'scalar';
  1464. } else {
  1465. $name = $this->CreateTypeName($this->currentComplexType . '_' . $this->currentElement);
  1466. $this->xdebug('processing unnamed simpleType for element ' . $this->currentElement . ' named ' . $name);
  1467. $this->currentSimpleType = $name;
  1468. //$this->currentElement = false;
  1469. $this->simpleTypes[$this->currentSimpleType] = $attrs;
  1470. $this->simpleTypes[$this->currentSimpleType]['phpType'] = 'scalar';
  1471. }
  1472. break;
  1473. case 'union': // simpleType type list
  1474. $this->xdebug("do nothing for element $name");
  1475. break;
  1476. default:
  1477. $this->xdebug("do not have any logic to process element $name");
  1478. }
  1479. }
  1480. /**
  1481. * end-element handler
  1482. *
  1483. * @param string $parser XML parser object
  1484. * @param string $name element name
  1485. * @access private
  1486. */
  1487. function schemaEndElement($parser, $name) {
  1488. // bring depth down a notch
  1489. $this->depth--;
  1490. // position of current element is equal to the last value left in depth_array for my depth
  1491. if(isset($this->depth_array[$this->depth])){
  1492. $pos = $this->depth_array[$this->depth];
  1493. }
  1494. // get element prefix
  1495. if ($prefix = $this->getPrefix($name)){
  1496. // get unqualified name
  1497. $name = $this->getLocalPart($name);
  1498. } else {
  1499. $prefix = '';
  1500. }
  1501. // move on...
  1502. if($name == 'complexType'){
  1503. $this->xdebug('done processing complexType ' . ($this->currentComplexType ? $this->currentComplexType : '(unknown)'));
  1504. $this->xdebug($this->varDump($this->complexTypes[$this->currentComplexType]));
  1505. $this->currentComplexType = array_pop($this->complexTypeStack);
  1506. //$this->currentElement = false;
  1507. }
  1508. if($name == 'element'){
  1509. $this->xdebug('done processing element ' . ($this->currentElement ? $this->currentElement : '(unknown)'));
  1510. $this->currentElement = array_pop($this->elementStack);
  1511. }
  1512. if($name == 'simpleType'){
  1513. $this->xdebug('done processing simpleType ' . ($this->currentSimpleType ? $this->currentSimpleType : '(unknown)'));
  1514. $this->xdebug($this->varDump($this->simpleTypes[$this->currentSimpleType]));
  1515. $this->currentSimpleType = array_pop($this->simpleTypeStack);
  1516. }
  1517. }
  1518. /**
  1519. * element content handler
  1520. *
  1521. * @param string $parser XML parser object
  1522. * @param string $data element content
  1523. * @access private
  1524. */
  1525. function schemaCharacterData($parser, $data){
  1526. $pos = $this->depth_array[$this->depth - 1];
  1527. $this->message[$pos]['cdata'] .= $data;
  1528. }
  1529. /**
  1530. * serialize the schema
  1531. *
  1532. * @access public
  1533. */
  1534. function serializeSchema(){
  1535. $schemaPrefix = $this->getPrefixFromNamespace($this->XMLSchemaVersion);
  1536. $xml = '';
  1537. // imports
  1538. if (sizeof($this->imports) > 0) {
  1539. foreach($this->imports as $ns => $list) {
  1540. foreach ($list as $ii) {
  1541. if ($ii['location'] != '') {
  1542. $xml .= " <$schemaPrefix:import location=\"" . $ii['location'] . '" namespace="' . $ns . "\" />\n";
  1543. } else {
  1544. $xml .= " <$schemaPrefix:import namespace=\"" . $ns . "\" />\n";
  1545. }
  1546. }
  1547. }
  1548. }
  1549. // complex types
  1550. foreach($this->complexTypes as $typeName => $attrs){
  1551. $contentStr = '';
  1552. // serialize child elements
  1553. if(isset($attrs['elements']) && (count($attrs['elements']) > 0)){
  1554. foreach($attrs['elements'] as $element => $eParts){
  1555. if(isset($eParts['ref'])){
  1556. $contentStr .= " <$schemaPrefix:element ref=\"$element\"/>\n";
  1557. } else {
  1558. $contentStr .= " <$schemaPrefix:element name=\"$element\" type=\"" . $this->contractQName($eParts['type']) . "\"";
  1559. foreach ($eParts as $aName => $aValue) {
  1560. // handle, e.g., abstract, default, form, minOccurs, maxOccurs, nillable
  1561. if ($aName != 'name' && $aName != 'type') {
  1562. $contentStr .= " $aName=\"$aValue\"";
  1563. }
  1564. }
  1565. $contentStr .= "/>\n";
  1566. }
  1567. }
  1568. // compositor wraps elements
  1569. if (isset($attrs['compositor']) && ($attrs['compositor'] != '')) {
  1570. $contentStr = " <$schemaPrefix:$attrs[compositor]>\n".$contentStr." </$schemaPrefix:$attrs[compositor]>\n";
  1571. }
  1572. }
  1573. // attributes
  1574. if(isset($attrs['attrs']) && (count($attrs['attrs']) >= 1)){
  1575. foreach($attrs['attrs'] as $attr => $aParts){
  1576. $contentStr .= " <$schemaPrefix:attribute";
  1577. foreach ($aParts as $a => $v) {
  1578. if ($a == 'ref' || $a == 'type') {
  1579. $contentStr .= " $a=\"".$this->contractQName($v).'"';
  1580. } elseif ($a == 'http://schemas.xmlsoap.org/wsdl/:arrayType') {
  1581. $this->usedNamespaces['wsdl'] = $this->namespaces['wsdl'];
  1582. $contentStr .= ' wsdl:arrayType="'.$this->contractQName($v).'"';
  1583. } else {
  1584. $contentStr .= " $a=\"$v\"";
  1585. }
  1586. }
  1587. $contentStr .= "/>\n";
  1588. }
  1589. }
  1590. // if restriction
  1591. if (isset($attrs['restrictionBase']) && $attrs['restrictionBase'] != ''){
  1592. $contentStr = " <$schemaPrefix:restriction base=\"".$this->contractQName($attrs['restrictionBase'])."\">\n".$contentStr." </$schemaPrefix:restriction>\n";
  1593. // complex or simple content
  1594. if ((isset($attrs['elements']) && count($attrs['elements']) > 0) || (isset($attrs['attrs']) && count($attrs['attrs']) > 0)){
  1595. $contentStr = " <$schemaPrefix:complexContent>\n".$contentStr." </$schemaPrefix:complexContent>\n";
  1596. }
  1597. }
  1598. // finalize complex type
  1599. if($contentStr != ''){
  1600. $contentStr = " <$schemaPrefix:complexType name=\"$typeName\">\n".$contentStr." </$schemaPrefix:complexType>\n";
  1601. } else {
  1602. $contentStr = " <$schemaPrefix:complexType name=\"$typeName\"/>\n";
  1603. }
  1604. $xml .= $contentStr;
  1605. }
  1606. // simple types
  1607. if(isset($this->simpleTypes) && count($this->simpleTypes) > 0){
  1608. foreach($this->simpleTypes as $typeName => $eParts){
  1609. $xml .= " <$schemaPrefix:simpleType name=\"$typeName\">\n <$schemaPrefix:restriction base=\"".$this->contractQName($eParts['type'])."\">\n";
  1610. if (isset($eParts['enumeration'])) {
  1611. foreach ($eParts['enumeration'] as $e) {
  1612. $xml .= " <$schemaPrefix:enumeration value=\"$e\"/>\n";
  1613. }
  1614. }
  1615. $xml .= " </$schemaPrefix:restriction>\n </$schemaPrefix:simpleType>";
  1616. }
  1617. }
  1618. // elements
  1619. if(isset($this->elements) && count($this->elements) > 0){
  1620. foreach($this->elements as $element => $eParts){
  1621. $xml .= " <$schemaPrefix:element name=\"$element\" type=\"".$this->contractQName($eParts['type'])."\"/>\n";
  1622. }
  1623. }
  1624. // attributes
  1625. if(isset($this->attributes) && count($this->attributes) > 0){
  1626. foreach($this->attributes as $attr => $aParts){
  1627. $xml .= " <$schemaPrefix:attribute name=\"$attr\" type=\"".$this->contractQName($aParts['type'])."\"\n/>";
  1628. }
  1629. }
  1630. // finish 'er up
  1631. $attr = '';
  1632. foreach ($this->schemaInfo as $k => $v) {
  1633. if ($k == 'elementFormDefault' || $k == 'attributeFormDefault') {
  1634. $attr .= " $k=\"$v\"";
  1635. }
  1636. }
  1637. $el = "<$schemaPrefix:schema$attr targetNamespace=\"$this->schemaTargetNamespace\"\n";
  1638. foreach (array_diff($this->usedNamespaces, $this->enclosingNamespaces) as $nsp => $ns) {
  1639. $el .= " xmlns:$nsp=\"$ns\"";
  1640. }
  1641. $xml = $el . ">\n".$xml."</$schemaPrefix:schema>\n";
  1642. return $xml;
  1643. }
  1644. /**
  1645. * adds debug data to the clas level debug string
  1646. *
  1647. * @param string $string debug data
  1648. * @access private
  1649. */
  1650. function xdebug($string){
  1651. $this->debug('<' . $this->schemaTargetNamespace . '> '.$string);
  1652. }
  1653. /**
  1654. * get the PHP type of a user defined type in the schema
  1655. * PHP type is kind of a misnomer since it actually returns 'struct' for assoc. arrays
  1656. * returns false if no type exists, or not w/ the given namespace
  1657. * else returns a string that is either a native php type, or 'struct'
  1658. *
  1659. * @param string $type name of defined type
  1660. * @param string $ns namespace of type
  1661. * @return mixed
  1662. * @access public
  1663. * @deprecated
  1664. */
  1665. function getPHPType($type,$ns){
  1666. if(isset($this->typemap[$ns][$type])){
  1667. //print "found type '$type' and ns $ns in typemap<br>";
  1668. return $this->typemap[$ns][$type];
  1669. } elseif(isset($this->complexTypes[$type])){
  1670. //print "getting type '$type' and ns $ns from complexTypes array<br>";
  1671. return $this->complexTypes[$type]['phpType'];
  1672. }
  1673. return false;
  1674. }
  1675. /**
  1676. * returns an associative array of information about a given type
  1677. * returns false if no type exists by the given name
  1678. *
  1679. * For a complexType typeDef = array(
  1680. * 'restrictionBase' => '',
  1681. * 'phpType' => '',
  1682. * 'compositor' => '(sequence|all)',
  1683. * 'elements' => array(), // refs to elements array
  1684. * 'attrs' => array() // refs to attributes array
  1685. * ... and so on (see addComplexType)
  1686. * )
  1687. *
  1688. * For simpleType or element, the array has different keys.
  1689. *
  1690. * @param string $type
  1691. * @return mixed
  1692. * @access public
  1693. * @see addComplexType
  1694. * @see addSimpleType
  1695. * @see addElement
  1696. */
  1697. function getTypeDef($type){
  1698. //$this->debug("in getTypeDef for type $type");
  1699. if (substr($type, -1) == '^') {
  1700. $is_element = 1;
  1701. $type = substr($type, 0, -1);
  1702. } else {
  1703. $is_element = 0;
  1704. }
  1705. if((! $is_element) && isset($this->complexTypes[$type])){
  1706. $this->xdebug("in getTypeDef, found complexType $type");
  1707. return $this->complexTypes[$type];
  1708. } elseif((! $is_element) && isset($this->simpleTypes[$type])){
  1709. $this->xdebug("in getTypeDef, found simpleType $type");
  1710. if (!isset($this->simpleTypes[$type]['phpType'])) {
  1711. // get info for type to tack onto the simple type
  1712. // TODO: can this ever really apply (i.e. what is a simpleType really?)
  1713. $uqType = substr($this->simpleTypes[$type]['type'], strrpos($this->simpleTypes[$type]['type'], ':') + 1);
  1714. $ns = substr($this->simpleTypes[$type]['type'], 0, strrpos($this->simpleTypes[$type]['type'], ':'));
  1715. $etype = $this->getTypeDef($uqType);
  1716. if ($etype) {
  1717. $this->xdebug("in getTypeDef, found type for simpleType $type:");
  1718. $this->xdebug($this->varDump($etype));
  1719. if (isset($etype['phpType'])) {
  1720. $this->simpleTypes[$type]['phpType'] = $etype['phpType'];
  1721. }
  1722. if (isset($etype['elements'])) {
  1723. $this->simpleTypes[$type]['elements'] = $etype['elements'];
  1724. }
  1725. }
  1726. }
  1727. return $this->simpleTypes[$type];
  1728. } elseif(isset($this->elements[$type])){
  1729. $this->xdebug("in getTypeDef, found element $type");
  1730. if (!isset($this->elements[$type]['phpType'])) {
  1731. // get info for type to tack onto the element
  1732. $uqType = substr($this->elements[$type]['type'], strrpos($this->elements[$type]['type'], ':') + 1);
  1733. $ns = substr($this->elements[$type]['type'], 0, strrpos($this->elements[$type]['type'], ':'));
  1734. $etype = $this->getTypeDef($uqType);
  1735. if ($etype) {
  1736. $this->xdebug("in getTypeDef, found type for element $type:");
  1737. $this->xdebug($this->varDump($etype));
  1738. if (isset($etype['phpType'])) {
  1739. $this->elements[$type]['phpType'] = $etype['phpType'];
  1740. }
  1741. if (isset($etype['elements'])) {
  1742. $this->elements[$type]['elements'] = $etype['elements'];
  1743. }
  1744. if (isset($etype['extensionBase'])) {
  1745. $this->elements[$type]['extensionBase'] = $etype['extensionBase'];
  1746. }
  1747. } elseif ($ns == 'http://www.w3.org/2001/XMLSchema') {
  1748. $this->xdebug("in getTypeDef, element $type is an XSD type");
  1749. $this->elements[$type]['phpType'] = 'scalar';
  1750. }
  1751. }
  1752. return $this->elements[$type];
  1753. } elseif(isset($this->attributes[$type])){
  1754. $this->xdebug("in getTypeDef, found attribute $type");
  1755. return $this->attributes[$type];
  1756. } elseif (preg_match('/_ContainedType$/', $type)) {
  1757. $this->xdebug("in getTypeDef, have an untyped element $type");
  1758. $typeDef['typeClass'] = 'simpleType';
  1759. $typeDef['phpType'] = 'scalar';
  1760. $typeDef['type'] = 'http://www.w3.org/2001/XMLSchema:string';
  1761. return $typeDef;
  1762. }
  1763. $this->xdebug("in getTypeDef, did not find $type");
  1764. return false;
  1765. }
  1766. /**
  1767. * returns a sample serialization of a given type, or false if no type by the given name
  1768. *
  1769. * @param string $type name of type
  1770. * @return mixed
  1771. * @access public
  1772. * @deprecated
  1773. */
  1774. function serializeTypeDef($type){
  1775. //print "in sTD() for type $type<br>";
  1776. if($typeDef = $this->getTypeDef($type)){
  1777. $str .= '<'.$type;
  1778. if(is_array($typeDef['attrs'])){
  1779. foreach($typeDef['attrs'] as $attName => $data){
  1780. $str .= " $attName=\"{type = ".$data['type']."}\"";
  1781. }
  1782. }
  1783. $str .= " xmlns=\"".$this->schema['targetNamespace']."\"";
  1784. if(count($typeDef['elements']) > 0){
  1785. $str .= ">";
  1786. foreach($typeDef['elements'] as $element => $eData){
  1787. $str .= $this->serializeTypeDef($element);
  1788. }
  1789. $str .= "</$type>";
  1790. } elseif($typeDef['typeClass'] == 'element') {
  1791. $str .= "></$type>";
  1792. } else {
  1793. $str .= "/>";
  1794. }
  1795. return $str;
  1796. }
  1797. return false;
  1798. }
  1799. /**
  1800. * returns HTML form elements that allow a user
  1801. * to enter values for creating an instance of the given type.
  1802. *
  1803. * @param string $name name for type instance
  1804. * @param string $type name of type
  1805. * @return string
  1806. * @access public
  1807. * @deprecated
  1808. */
  1809. function typeToForm($name,$type){
  1810. // get typedef
  1811. if($typeDef = $this->getTypeDef($type)){
  1812. // if struct
  1813. if($typeDef['phpType'] == 'struct'){
  1814. $buffer .= '<table>';
  1815. foreach($typeDef['elements'] as $child => $childDef){
  1816. $buffer .= "
  1817. <tr><td align='right'>$childDef[name] (type: ".$this->getLocalPart($childDef['type'])."):</td>
  1818. <td><input type='text' name='parameters[".$name."][$childDef[name]]'></td></tr>";
  1819. }
  1820. $buffer .= '</table>';
  1821. // if array
  1822. } elseif($typeDef['phpType'] == 'array'){
  1823. $buffer .= '<table>';
  1824. for($i=0;$i < 3; $i++){
  1825. $buffer .= "
  1826. <tr><td align='right'>array item (type: $typeDef[arrayType]):</td>
  1827. <td><input type='text' name='parameters[".$name."][]'></td></tr>";
  1828. }
  1829. $buffer .= '</table>';
  1830. // if scalar
  1831. } else {
  1832. $buffer .= "<input type='text' name='parameters[$name]'>";
  1833. }
  1834. } else {
  1835. $buffer .= "<input type='text' name='parameters[$name]'>";
  1836. }
  1837. return $buffer;
  1838. }
  1839. /**
  1840. * adds a complex type to the schema
  1841. *
  1842. * example: array
  1843. *
  1844. * addType(
  1845. * 'ArrayOfstring',
  1846. * 'complexType',
  1847. * 'array',
  1848. * '',
  1849. * 'SOAP-ENC:Array',
  1850. * array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]'),
  1851. * 'xsd:string'
  1852. * );
  1853. *
  1854. * example: PHP associative array ( SOAP Struct )
  1855. *
  1856. * addType(
  1857. * 'SOAPStruct',
  1858. * 'complexType',
  1859. * 'struct',
  1860. * 'all',
  1861. * array('myVar'=> array('name'=>'myVar','type'=>'string')
  1862. * );
  1863. *
  1864. * @param name
  1865. * @param typeClass (complexType|simpleType|attribute)
  1866. * @param phpType: currently supported are array and struct (php assoc array)
  1867. * @param compositor (all|sequence|choice)
  1868. * @param restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  1869. * @param elements = array ( name = array(name=>'',type=>'') )
  1870. * @param attrs = array(
  1871. * array(
  1872. * 'ref' => "http://schemas.xmlsoap.org/soap/encoding/:arrayType",
  1873. * "http://schemas.xmlsoap.org/wsdl/:arrayType" => "string[]"
  1874. * )
  1875. * )
  1876. * @param arrayType: namespace:name (http://www.w3.org/2001/XMLSchema:string)
  1877. * @access public
  1878. * @see getTypeDef
  1879. */
  1880. function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType=''){
  1881. $this->complexTypes[$name] = array(
  1882. 'name' => $name,
  1883. 'typeClass' => $typeClass,
  1884. 'phpType' => $phpType,
  1885. 'compositor'=> $compositor,
  1886. 'restrictionBase' => $restrictionBase,
  1887. 'elements' => $elements,
  1888. 'attrs' => $attrs,
  1889. 'arrayType' => $arrayType
  1890. );
  1891. $this->xdebug("addComplexType $name:");
  1892. $this->appendDebug($this->varDump($this->complexTypes[$name]));
  1893. }
  1894. /**
  1895. * adds a simple type to the schema
  1896. *
  1897. * @param string $name
  1898. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  1899. * @param string $typeClass (should always be simpleType)
  1900. * @param string $phpType (should always be scalar)
  1901. * @param array $enumeration array of values
  1902. * @access public
  1903. * @see nusoap_xmlschema
  1904. * @see getTypeDef
  1905. */
  1906. function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
  1907. $this->simpleTypes[$name] = array(
  1908. 'name' => $name,
  1909. 'typeClass' => $typeClass,
  1910. 'phpType' => $phpType,
  1911. 'type' => $restrictionBase,
  1912. 'enumeration' => $enumeration
  1913. );
  1914. $this->xdebug("addSimpleType $name:");
  1915. $this->appendDebug($this->varDump($this->simpleTypes[$name]));
  1916. }
  1917. /**
  1918. * adds an element to the schema
  1919. *
  1920. * @param array $attrs attributes that must include name and type
  1921. * @see nusoap_xmlschema
  1922. * @access public
  1923. */
  1924. function addElement($attrs) {
  1925. if (! $this->getPrefix($attrs['type'])) {
  1926. $attrs['type'] = $this->schemaTargetNamespace . ':' . $attrs['type'];
  1927. }
  1928. $this->elements[ $attrs['name'] ] = $attrs;
  1929. $this->elements[ $attrs['name'] ]['typeClass'] = 'element';
  1930. $this->xdebug("addElement " . $attrs['name']);
  1931. $this->appendDebug($this->varDump($this->elements[ $attrs['name'] ]));
  1932. }
  1933. }
  1934. /**
  1935. * Backward compatibility
  1936. */
  1937. class XMLSchema extends nusoap_xmlschema {
  1938. }
  1939. ?><?php
  1940. /**
  1941. * For creating serializable abstractions of native PHP types. This class
  1942. * allows element name/namespace, XSD type, and XML attributes to be
  1943. * associated with a value. This is extremely useful when WSDL is not
  1944. * used, but is also useful when WSDL is used with polymorphic types, including
  1945. * xsd:anyType and user-defined types.
  1946. *
  1947. * @author Dietrich Ayala <dietrich@ganx4.com>
  1948. * @access public
  1949. */
  1950. class soapval extends nusoap_base {
  1951. /**
  1952. * The XML element name
  1953. *
  1954. * @var string
  1955. * @access private
  1956. */
  1957. var $name;
  1958. /**
  1959. * The XML type name (string or false)
  1960. *
  1961. * @var mixed
  1962. * @access private
  1963. */
  1964. var $type;
  1965. /**
  1966. * The PHP value
  1967. *
  1968. * @var mixed
  1969. * @access private
  1970. */
  1971. var $value;
  1972. /**
  1973. * The XML element namespace (string or false)
  1974. *
  1975. * @var mixed
  1976. * @access private
  1977. */
  1978. var $element_ns;
  1979. /**
  1980. * The XML type namespace (string or false)
  1981. *
  1982. * @var mixed
  1983. * @access private
  1984. */
  1985. var $type_ns;
  1986. /**
  1987. * The XML element attributes (array or false)
  1988. *
  1989. * @var mixed
  1990. * @access private
  1991. */
  1992. var $attributes;
  1993. /**
  1994. * constructor
  1995. *
  1996. * @param string $name optional name
  1997. * @param mixed $type optional type name
  1998. * @param mixed $value optional value
  1999. * @param mixed $element_ns optional namespace of value
  2000. * @param mixed $type_ns optional namespace of type
  2001. * @param mixed $attributes associative array of attributes to add to element serialization
  2002. * @access public
  2003. */
  2004. function soapval($name='soapval',$type=false,$value=-1,$element_ns=false,$type_ns=false,$attributes=false) {
  2005. parent::nusoap_base();
  2006. $this->name = $name;
  2007. $this->type = $type;
  2008. $this->value = $value;
  2009. $this->element_ns = $element_ns;
  2010. $this->type_ns = $type_ns;
  2011. $this->attributes = $attributes;
  2012. }
  2013. /**
  2014. * return serialized value
  2015. *
  2016. * @param string $use The WSDL use value (encoded|literal)
  2017. * @return string XML data
  2018. * @access public
  2019. */
  2020. function serialize($use='encoded') {
  2021. return $this->serialize_val($this->value, $this->name, $this->type, $this->element_ns, $this->type_ns, $this->attributes, $use, true);
  2022. }
  2023. /**
  2024. * decodes a soapval object into a PHP native type
  2025. *
  2026. * @return mixed
  2027. * @access public
  2028. */
  2029. function decode(){
  2030. return $this->value;
  2031. }
  2032. }
  2033. ?><?php
  2034. /**
  2035. * transport class for sending/receiving data via HTTP and HTTPS
  2036. * NOTE: PHP must be compiled with the CURL extension for HTTPS support
  2037. *
  2038. * @author Dietrich Ayala <dietrich@ganx4.com>
  2039. * @author Scott Nichol <snichol@users.sourceforge.net>
  2040. * @access public
  2041. */
  2042. class soap_transport_http extends nusoap_base {
  2043. var $url = '';
  2044. var $uri = '';
  2045. var $digest_uri = '';
  2046. var $scheme = '';
  2047. var $host = '';
  2048. var $port = '';
  2049. var $path = '';
  2050. var $request_method = 'POST';
  2051. var $protocol_version = '1.0';
  2052. var $encoding = '';
  2053. var $outgoing_headers = array();
  2054. var $incoming_headers = array();
  2055. var $incoming_cookies = array();
  2056. var $outgoing_payload = '';
  2057. var $incoming_payload = '';
  2058. var $response_status_line; // HTTP response status line
  2059. var $useSOAPAction = true;
  2060. var $persistentConnection = false;
  2061. var $ch = false; // cURL handle
  2062. var $ch_options = array(); // cURL custom options
  2063. var $use_curl = false; // force cURL use
  2064. var $proxy = null; // proxy information (associative array)
  2065. var $username = '';
  2066. var $password = '';
  2067. var $authtype = '';
  2068. var $digestRequest = array();
  2069. var $certRequest = array(); // keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, certpassword (optional), verifypeer (optional), verifyhost (optional)
  2070. // cainfofile: certificate authority file, e.g. '$pathToPemFiles/rootca.pem'
  2071. // sslcertfile: SSL certificate file, e.g. '$pathToPemFiles/mycert.pem'
  2072. // sslkeyfile: SSL key file, e.g. '$pathToPemFiles/mykey.pem'
  2073. // passphrase: SSL key password/passphrase
  2074. // certpassword: SSL certificate password
  2075. // verifypeer: default is 1
  2076. // verifyhost: default is 1
  2077. /**
  2078. * constructor
  2079. *
  2080. * @param string $url The URL to which to connect
  2081. * @param array $curl_options User-specified cURL options
  2082. * @param boolean $use_curl Whether to try to force cURL use
  2083. * @access public
  2084. */
  2085. function soap_transport_http($url, $curl_options = NULL, $use_curl = false){
  2086. parent::nusoap_base();
  2087. $this->debug("ctor url=$url use_curl=$use_curl curl_options:");
  2088. $this->appendDebug($this->varDump($curl_options));
  2089. $this->setURL($url);
  2090. if (is_array($curl_options)) {
  2091. $this->ch_options = $curl_options;
  2092. }
  2093. $this->use_curl = $use_curl;
  2094. preg_match('/\$Revisio' . 'n: ([^ ]+)/', $this->revision, $rev);
  2095. $this->setHeader('User-Agent', $this->title.'/'.$this->version.' ('.$rev[1].')');
  2096. }
  2097. /**
  2098. * sets a cURL option
  2099. *
  2100. * @param mixed $option The cURL option (always integer?)
  2101. * @param mixed $value The cURL option value
  2102. * @access private
  2103. */
  2104. function setCurlOption($option, $value) {
  2105. $this->debug("setCurlOption option=$option, value=");
  2106. $this->appendDebug($this->varDump($value));
  2107. curl_setopt($this->ch, $option, $value);
  2108. }
  2109. /**
  2110. * sets an HTTP header
  2111. *
  2112. * @param string $name The name of the header
  2113. * @param string $value The value of the header
  2114. * @access private
  2115. */
  2116. function setHeader($name, $value) {
  2117. $this->outgoing_headers[$name] = $value;
  2118. $this->debug("set header $name: $value");
  2119. }
  2120. /**
  2121. * unsets an HTTP header
  2122. *
  2123. * @param string $name The name of the header
  2124. * @access private
  2125. */
  2126. function unsetHeader($name) {
  2127. if (isset($this->outgoing_headers[$name])) {
  2128. $this->debug("unset header $name");
  2129. unset($this->outgoing_headers[$name]);
  2130. }
  2131. }
  2132. /**
  2133. * sets the URL to which to connect
  2134. *
  2135. * @param string $url The URL to which to connect
  2136. * @access private
  2137. */
  2138. function setURL($url) {
  2139. $this->url = $url;
  2140. $u = parse_url($url);
  2141. foreach($u as $k => $v){
  2142. $this->debug("parsed URL $k = $v");
  2143. $this->$k = $v;
  2144. }
  2145. // add any GET params to path
  2146. if(isset($u['query']) && $u['query'] != ''){
  2147. $this->path .= '?' . $u['query'];
  2148. }
  2149. // set default port
  2150. if(!isset($u['port'])){
  2151. if($u['scheme'] == 'https'){
  2152. $this->port = 443;
  2153. } else {
  2154. $this->port = 80;
  2155. }
  2156. }
  2157. $this->uri = $this->path;
  2158. $this->digest_uri = $this->uri;
  2159. // build headers
  2160. if (!isset($u['port'])) {
  2161. $this->setHeader('Host', $this->host);
  2162. } else {
  2163. $this->setHeader('Host', $this->host.':'.$this->port);
  2164. }
  2165. if (isset($u['user']) && $u['user'] != '') {
  2166. $this->setCredentials(urldecode($u['user']), isset($u['pass']) ? urldecode($u['pass']) : '');
  2167. }
  2168. }
  2169. /**
  2170. * gets the I/O method to use
  2171. *
  2172. * @return string I/O method to use (socket|curl|unknown)
  2173. * @access private
  2174. */
  2175. function io_method() {
  2176. if ($this->use_curl || ($this->scheme == 'https') || ($this->scheme == 'http' && $this->authtype == 'ntlm') || ($this->scheme == 'http' && is_array($this->proxy) && $this->proxy['authtype'] == 'ntlm'))
  2177. return 'curl';
  2178. if (($this->scheme == 'http' || $this->scheme == 'ssl') && $this->authtype != 'ntlm' && (!is_array($this->proxy) || $this->proxy['authtype'] != 'ntlm'))
  2179. return 'socket';
  2180. return 'unknown';
  2181. }
  2182. /**
  2183. * establish an HTTP connection
  2184. *
  2185. * @param integer $timeout set connection timeout in seconds
  2186. * @param integer $response_timeout set response timeout in seconds
  2187. * @return boolean true if connected, false if not
  2188. * @access private
  2189. */
  2190. function connect($connection_timeout=0,$response_timeout=30){
  2191. // For PHP 4.3 with OpenSSL, change https scheme to ssl, then treat like
  2192. // "regular" socket.
  2193. // TODO: disabled for now because OpenSSL must be *compiled* in (not just
  2194. // loaded), and until PHP5 stream_get_wrappers is not available.
  2195. // if ($this->scheme == 'https') {
  2196. // if (version_compare(phpversion(), '4.3.0') >= 0) {
  2197. // if (extension_loaded('openssl')) {
  2198. // $this->scheme = 'ssl';
  2199. // $this->debug('Using SSL over OpenSSL');
  2200. // }
  2201. // }
  2202. // }
  2203. $this->debug("connect connection_timeout $connection_timeout, response_timeout $response_timeout, scheme $this->scheme, host $this->host, port $this->port");
  2204. if ($this->io_method() == 'socket') {
  2205. if (!is_array($this->proxy)) {
  2206. $host = $this->host;
  2207. $port = $this->port;
  2208. } else {
  2209. $host = $this->proxy['host'];
  2210. $port = $this->proxy['port'];
  2211. }
  2212. // use persistent connection
  2213. if($this->persistentConnection && isset($this->fp) && is_resource($this->fp)){
  2214. if (!feof($this->fp)) {
  2215. $this->debug('Re-use persistent connection');
  2216. return true;
  2217. }
  2218. fclose($this->fp);
  2219. $this->debug('Closed persistent connection at EOF');
  2220. }
  2221. // munge host if using OpenSSL
  2222. if ($this->scheme == 'ssl') {
  2223. $host = 'ssl://' . $host;
  2224. }
  2225. $this->debug('calling fsockopen with host ' . $host . ' connection_timeout ' . $connection_timeout);
  2226. // open socket
  2227. if($connection_timeout > 0){
  2228. $this->fp = @fsockopen( $host, $this->port, $this->errno, $this->error_str, $connection_timeout);
  2229. } else {
  2230. $this->fp = @fsockopen( $host, $this->port, $this->errno, $this->error_str);
  2231. }
  2232. // test pointer
  2233. if(!$this->fp) {
  2234. $msg = 'Couldn\'t open socket connection to server ' . $this->url;
  2235. if ($this->errno) {
  2236. $msg .= ', Error ('.$this->errno.'): '.$this->error_str;
  2237. } else {
  2238. $msg .= ' prior to connect(). This is often a problem looking up the host name.';
  2239. }
  2240. $this->debug($msg);
  2241. $this->setError($msg);
  2242. return false;
  2243. }
  2244. // set response timeout
  2245. $this->debug('set response timeout to ' . $response_timeout);
  2246. socket_set_timeout( $this->fp, $response_timeout);
  2247. $this->debug('socket connected');
  2248. return true;
  2249. } else if ($this->io_method() == 'curl') {
  2250. if (!extension_loaded('curl')) {
  2251. // $this->setError('cURL Extension, or OpenSSL extension w/ PHP version >= 4.3 is required for HTTPS');
  2252. $this->setError('The PHP cURL Extension is required for HTTPS or NLTM. You will need to re-build or update your PHP to include cURL or change php.ini to load the PHP cURL extension.');
  2253. return false;
  2254. }
  2255. // Avoid warnings when PHP does not have these options
  2256. if (defined('CURLOPT_CONNECTIONTIMEOUT'))
  2257. $CURLOPT_CONNECTIONTIMEOUT = CURLOPT_CONNECTIONTIMEOUT;
  2258. else
  2259. $CURLOPT_CONNECTIONTIMEOUT = 78;
  2260. if (defined('CURLOPT_HTTPAUTH'))
  2261. $CURLOPT_HTTPAUTH = CURLOPT_HTTPAUTH;
  2262. else
  2263. $CURLOPT_HTTPAUTH = 107;
  2264. if (defined('CURLOPT_PROXYAUTH'))
  2265. $CURLOPT_PROXYAUTH = CURLOPT_PROXYAUTH;
  2266. else
  2267. $CURLOPT_PROXYAUTH = 111;
  2268. if (defined('CURLAUTH_BASIC'))
  2269. $CURLAUTH_BASIC = CURLAUTH_BASIC;
  2270. else
  2271. $CURLAUTH_BASIC = 1;
  2272. if (defined('CURLAUTH_DIGEST'))
  2273. $CURLAUTH_DIGEST = CURLAUTH_DIGEST;
  2274. else
  2275. $CURLAUTH_DIGEST = 2;
  2276. if (defined('CURLAUTH_NTLM'))
  2277. $CURLAUTH_NTLM = CURLAUTH_NTLM;
  2278. else
  2279. $CURLAUTH_NTLM = 8;
  2280. $this->debug('connect using cURL');
  2281. // init CURL
  2282. $this->ch = curl_init();
  2283. // set url
  2284. $hostURL = ($this->port != '') ? "$this->scheme://$this->host:$this->port" : "$this->scheme://$this->host";
  2285. // add path
  2286. $hostURL .= $this->path;
  2287. $this->setCurlOption(CURLOPT_URL, $hostURL);
  2288. // follow location headers (re-directs)
  2289. if (ini_get('safe_mode') || ini_get('open_basedir')) {
  2290. $this->debug('safe_mode or open_basedir set, so do not set CURLOPT_FOLLOWLOCATION');
  2291. $this->debug('safe_mode = ');
  2292. $this->appendDebug($this->varDump(ini_get('safe_mode')));
  2293. $this->debug('open_basedir = ');
  2294. $this->appendDebug($this->varDump(ini_get('open_basedir')));
  2295. } else {
  2296. $this->setCurlOption(CURLOPT_FOLLOWLOCATION, 1);
  2297. }
  2298. // ask for headers in the response output
  2299. $this->setCurlOption(CURLOPT_HEADER, 1);
  2300. // ask for the response output as the return value
  2301. $this->setCurlOption(CURLOPT_RETURNTRANSFER, 1);
  2302. // encode
  2303. // We manage this ourselves through headers and encoding
  2304. // if(function_exists('gzuncompress')){
  2305. // $this->setCurlOption(CURLOPT_ENCODING, 'deflate');
  2306. // }
  2307. // persistent connection
  2308. if ($this->persistentConnection) {
  2309. // I believe the following comment is now bogus, having applied to
  2310. // the code when it used CURLOPT_CUSTOMREQUEST to send the request.
  2311. // The way we send data, we cannot use persistent connections, since
  2312. // there will be some "junk" at the end of our request.
  2313. //$this->setCurlOption(CURL_HTTP_VERSION_1_1, true);
  2314. $this->persistentConnection = false;
  2315. $this->setHeader('Connection', 'close');
  2316. }
  2317. // set timeouts
  2318. if ($connection_timeout != 0) {
  2319. $this->setCurlOption($CURLOPT_CONNECTIONTIMEOUT, $connection_timeout);
  2320. }
  2321. if ($response_timeout != 0) {
  2322. $this->setCurlOption(CURLOPT_TIMEOUT, $response_timeout);
  2323. }
  2324. if ($this->scheme == 'https') {
  2325. $this->debug('set cURL SSL verify options');
  2326. // recent versions of cURL turn on peer/host checking by default,
  2327. // while PHP binaries are not compiled with a default location for the
  2328. // CA cert bundle, so disable peer/host checking.
  2329. //$this->setCurlOption(CURLOPT_CAINFO, 'f:\php-4.3.2-win32\extensions\curl-ca-bundle.crt');
  2330. $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, 0);
  2331. $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, 0);
  2332. // support client certificates (thanks Tobias Boes, Doug Anarino, Eryan Ariobowo)
  2333. if ($this->authtype == 'certificate') {
  2334. $this->debug('set cURL certificate options');
  2335. if (isset($this->certRequest['cainfofile'])) {
  2336. $this->setCurlOption(CURLOPT_CAINFO, $this->certRequest['cainfofile']);
  2337. }
  2338. if (isset($this->certRequest['verifypeer'])) {
  2339. $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, $this->certRequest['verifypeer']);
  2340. } else {
  2341. $this->setCurlOption(CURLOPT_SSL_VERIFYPEER, 1);
  2342. }
  2343. if (isset($this->certRequest['verifyhost'])) {
  2344. $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, $this->certRequest['verifyhost']);
  2345. } else {
  2346. $this->setCurlOption(CURLOPT_SSL_VERIFYHOST, 1);
  2347. }
  2348. if (isset($this->certRequest['sslcertfile'])) {
  2349. $this->setCurlOption(CURLOPT_SSLCERT, $this->certRequest['sslcertfile']);
  2350. }
  2351. if (isset($this->certRequest['sslkeyfile'])) {
  2352. $this->setCurlOption(CURLOPT_SSLKEY, $this->certRequest['sslkeyfile']);
  2353. }
  2354. if (isset($this->certRequest['passphrase'])) {
  2355. $this->setCurlOption(CURLOPT_SSLKEYPASSWD, $this->certRequest['passphrase']);
  2356. }
  2357. if (isset($this->certRequest['certpassword'])) {
  2358. $this->setCurlOption(CURLOPT_SSLCERTPASSWD, $this->certRequest['certpassword']);
  2359. }
  2360. }
  2361. }
  2362. if ($this->authtype && ($this->authtype != 'certificate')) {
  2363. if ($this->username) {
  2364. $this->debug('set cURL username/password');
  2365. $this->setCurlOption(CURLOPT_USERPWD, "$this->username:$this->password");
  2366. }
  2367. if ($this->authtype == 'basic') {
  2368. $this->debug('set cURL for Basic authentication');
  2369. $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_BASIC);
  2370. }
  2371. if ($this->authtype == 'digest') {
  2372. $this->debug('set cURL for digest authentication');
  2373. $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_DIGEST);
  2374. }
  2375. if ($this->authtype == 'ntlm') {
  2376. $this->debug('set cURL for NTLM authentication');
  2377. $this->setCurlOption($CURLOPT_HTTPAUTH, $CURLAUTH_NTLM);
  2378. }
  2379. }
  2380. if (is_array($this->proxy)) {
  2381. $this->debug('set cURL proxy options');
  2382. if ($this->proxy['port'] != '') {
  2383. $this->setCurlOption(CURLOPT_PROXY, $this->proxy['host'].':'.$this->proxy['port']);
  2384. } else {
  2385. $this->setCurlOption(CURLOPT_PROXY, $this->proxy['host']);
  2386. }
  2387. if ($this->proxy['username'] || $this->proxy['password']) {
  2388. $this->debug('set cURL proxy authentication options');
  2389. $this->setCurlOption(CURLOPT_PROXYUSERPWD, $this->proxy['username'].':'.$this->proxy['password']);
  2390. if ($this->proxy['authtype'] == 'basic') {
  2391. $this->setCurlOption($CURLOPT_PROXYAUTH, $CURLAUTH_BASIC);
  2392. }
  2393. if ($this->proxy['authtype'] == 'ntlm') {
  2394. $this->setCurlOption($CURLOPT_PROXYAUTH, $CURLAUTH_NTLM);
  2395. }
  2396. }
  2397. }
  2398. $this->debug('cURL connection set up');
  2399. return true;
  2400. } else {
  2401. $this->setError('Unknown scheme ' . $this->scheme);
  2402. $this->debug('Unknown scheme ' . $this->scheme);
  2403. return false;
  2404. }
  2405. }
  2406. /**
  2407. * sends the SOAP request and gets the SOAP response via HTTP[S]
  2408. *
  2409. * @param string $data message data
  2410. * @param integer $timeout set connection timeout in seconds
  2411. * @param integer $response_timeout set response timeout in seconds
  2412. * @param array $cookies cookies to send
  2413. * @return string data
  2414. * @access public
  2415. */
  2416. function send($data, $timeout=0, $response_timeout=30, $cookies=NULL) {
  2417. $this->debug('entered send() with data of length: '.strlen($data));
  2418. $this->tryagain = true;
  2419. $tries = 0;
  2420. while ($this->tryagain) {
  2421. $this->tryagain = false;
  2422. if ($tries++ < 2) {
  2423. // make connnection
  2424. if (!$this->connect($timeout, $response_timeout)){
  2425. return false;
  2426. }
  2427. // send request
  2428. if (!$this->sendRequest($data, $cookies)){
  2429. return false;
  2430. }
  2431. // get response
  2432. $respdata = $this->getResponse();
  2433. } else {
  2434. $this->setError("Too many tries to get an OK response ($this->response_status_line)");
  2435. }
  2436. }
  2437. $this->debug('end of send()');
  2438. return $respdata;
  2439. }
  2440. /**
  2441. * sends the SOAP request and gets the SOAP response via HTTPS using CURL
  2442. *
  2443. * @param string $data message data
  2444. * @param integer $timeout set connection timeout in seconds
  2445. * @param integer $response_timeout set response timeout in seconds
  2446. * @param array $cookies cookies to send
  2447. * @return string data
  2448. * @access public
  2449. * @deprecated
  2450. */
  2451. function sendHTTPS($data, $timeout=0, $response_timeout=30, $cookies) {
  2452. return $this->send($data, $timeout, $response_timeout, $cookies);
  2453. }
  2454. /**
  2455. * if authenticating, set user credentials here
  2456. *
  2457. * @param string $username
  2458. * @param string $password
  2459. * @param string $authtype (basic|digest|certificate|ntlm)
  2460. * @param array $digestRequest (keys must be nonce, nc, realm, qop)
  2461. * @param array $certRequest (keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, certpassword (optional), verifypeer (optional), verifyhost (optional): see corresponding options in cURL docs)
  2462. * @access public
  2463. */
  2464. function setCredentials($username, $password, $authtype = 'basic', $digestRequest = array(), $certRequest = array()) {
  2465. $this->debug("setCredentials username=$username authtype=$authtype digestRequest=");
  2466. $this->appendDebug($this->varDump($digestRequest));
  2467. $this->debug("certRequest=");
  2468. $this->appendDebug($this->varDump($certRequest));
  2469. // cf. RFC 2617
  2470. if ($authtype == 'basic') {
  2471. $this->setHeader('Authorization', 'Basic '.base64_encode(str_replace(':','',$username).':'.$password));
  2472. } elseif ($authtype == 'digest') {
  2473. if (isset($digestRequest['nonce'])) {
  2474. $digestRequest['nc'] = isset($digestRequest['nc']) ? $digestRequest['nc']++ : 1;
  2475. // calculate the Digest hashes (calculate code based on digest implementation found at: http://www.rassoc.com/gregr/weblog/stories/2002/07/09/webServicesSecurityHttpDigestAuthenticationWithoutActiveDirectory.html)
  2476. // A1 = unq(username-value) ":" unq(realm-value) ":" passwd
  2477. $A1 = $username. ':' . (isset($digestRequest['realm']) ? $digestRequest['realm'] : '') . ':' . $password;
  2478. // H(A1) = MD5(A1)
  2479. $HA1 = md5($A1);
  2480. // A2 = Method ":" digest-uri-value
  2481. $A2 = $this->request_method . ':' . $this->digest_uri;
  2482. // H(A2)
  2483. $HA2 = md5($A2);
  2484. // KD(secret, data) = H(concat(secret, ":", data))
  2485. // if qop == auth:
  2486. // request-digest = <"> < KD ( H(A1), unq(nonce-value)
  2487. // ":" nc-value
  2488. // ":" unq(cnonce-value)
  2489. // ":" unq(qop-value)
  2490. // ":" H(A2)
  2491. // ) <">
  2492. // if qop is missing,
  2493. // request-digest = <"> < KD ( H(A1), unq(nonce-value) ":" H(A2) ) > <">
  2494. $unhashedDigest = '';
  2495. $nonce = isset($digestRequest['nonce']) ? $digestRequest['nonce'] : '';
  2496. $cnonce = $nonce;
  2497. if ($digestRequest['qop'] != '') {
  2498. $unhashedDigest = $HA1 . ':' . $nonce . ':' . sprintf("%08d", $digestRequest['nc']) . ':' . $cnonce . ':' . $digestRequest['qop'] . ':' . $HA2;
  2499. } else {
  2500. $unhashedDigest = $HA1 . ':' . $nonce . ':' . $HA2;
  2501. }
  2502. $hashedDigest = md5($unhashedDigest);
  2503. $opaque = '';
  2504. if (isset($digestRequest['opaque'])) {
  2505. $opaque = ', opaque="' . $digestRequest['opaque'] . '"';
  2506. }
  2507. $this->setHeader('Authorization', 'Digest username="' . $username . '", realm="' . $digestRequest['realm'] . '", nonce="' . $nonce . '", uri="' . $this->digest_uri . $opaque . '", cnonce="' . $cnonce . '", nc=' . sprintf("%08x", $digestRequest['nc']) . ', qop="' . $digestRequest['qop'] . '", response="' . $hashedDigest . '"');
  2508. }
  2509. } elseif ($authtype == 'certificate') {
  2510. $this->certRequest = $certRequest;
  2511. $this->debug('Authorization header not set for certificate');
  2512. } elseif ($authtype == 'ntlm') {
  2513. // do nothing
  2514. $this->debug('Authorization header not set for ntlm');
  2515. }
  2516. $this->username = $username;
  2517. $this->password = $password;
  2518. $this->authtype = $authtype;
  2519. $this->digestRequest = $digestRequest;
  2520. }
  2521. /**
  2522. * set the soapaction value
  2523. *
  2524. * @param string $soapaction
  2525. * @access public
  2526. */
  2527. function setSOAPAction($soapaction) {
  2528. $this->setHeader('SOAPAction', '"' . $soapaction . '"');
  2529. }
  2530. /**
  2531. * use http encoding
  2532. *
  2533. * @param string $enc encoding style. supported values: gzip, deflate, or both
  2534. * @access public
  2535. */
  2536. function setEncoding($enc='gzip, deflate') {
  2537. if (function_exists('gzdeflate')) {
  2538. $this->protocol_version = '1.1';
  2539. $this->setHeader('Accept-Encoding', $enc);
  2540. if (!isset($this->outgoing_headers['Connection'])) {
  2541. $this->setHeader('Connection', 'close');
  2542. $this->persistentConnection = false;
  2543. }
  2544. // deprecated as of PHP 5.3.0
  2545. //set_magic_quotes_runtime(0);
  2546. $this->encoding = $enc;
  2547. }
  2548. }
  2549. /**
  2550. * set proxy info here
  2551. *
  2552. * @param string $proxyhost use an empty string to remove proxy
  2553. * @param string $proxyport
  2554. * @param string $proxyusername
  2555. * @param string $proxypassword
  2556. * @param string $proxyauthtype (basic|ntlm)
  2557. * @access public
  2558. */
  2559. function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 'basic') {
  2560. if ($proxyhost) {
  2561. $this->proxy = array(
  2562. 'host' => $proxyhost,
  2563. 'port' => $proxyport,
  2564. 'username' => $proxyusername,
  2565. 'password' => $proxypassword,
  2566. 'authtype' => $proxyauthtype
  2567. );
  2568. if ($proxyusername != '' && $proxypassword != '' && $proxyauthtype = 'basic') {
  2569. $this->setHeader('Proxy-Authorization', ' Basic '.base64_encode($proxyusername.':'.$proxypassword));
  2570. }
  2571. } else {
  2572. $this->debug('remove proxy');
  2573. $proxy = null;
  2574. unsetHeader('Proxy-Authorization');
  2575. }
  2576. }
  2577. /**
  2578. * Test if the given string starts with a header that is to be skipped.
  2579. * Skippable headers result from chunked transfer and proxy requests.
  2580. *
  2581. * @param string $data The string to check.
  2582. * @returns boolean Whether a skippable header was found.
  2583. * @access private
  2584. */
  2585. function isSkippableCurlHeader(&$data) {
  2586. $skipHeaders = array( 'HTTP/1.1 100',
  2587. 'HTTP/1.0 301',
  2588. 'HTTP/1.1 301',
  2589. 'HTTP/1.0 302',
  2590. 'HTTP/1.1 302',
  2591. 'HTTP/1.0 401',
  2592. 'HTTP/1.1 401',
  2593. 'HTTP/1.0 200 Connection established');
  2594. foreach ($skipHeaders as $hd) {
  2595. $prefix = substr($data, 0, strlen($hd));
  2596. if ($prefix == $hd) return true;
  2597. }
  2598. return false;
  2599. }
  2600. /**
  2601. * decode a string that is encoded w/ "chunked' transfer encoding
  2602. * as defined in RFC2068 19.4.6
  2603. *
  2604. * @param string $buffer
  2605. * @param string $lb
  2606. * @returns string
  2607. * @access public
  2608. * @deprecated
  2609. */
  2610. function decodeChunked($buffer, $lb){
  2611. // length := 0
  2612. $length = 0;
  2613. $new = '';
  2614. // read chunk-size, chunk-extension (if any) and CRLF
  2615. // get the position of the linebreak
  2616. $chunkend = strpos($buffer, $lb);
  2617. if ($chunkend == FALSE) {
  2618. $this->debug('no linebreak found in decodeChunked');
  2619. return $new;
  2620. }
  2621. $temp = substr($buffer,0,$chunkend);
  2622. $chunk_size = hexdec( trim($temp) );
  2623. $chunkstart = $chunkend + strlen($lb);
  2624. // while (chunk-size > 0) {
  2625. while ($chunk_size > 0) {
  2626. $this->debug("chunkstart: $chunkstart chunk_size: $chunk_size");
  2627. $chunkend = strpos( $buffer, $lb, $chunkstart + $chunk_size);
  2628. // Just in case we got a broken connection
  2629. if ($chunkend == FALSE) {
  2630. $chunk = substr($buffer,$chunkstart);
  2631. // append chunk-data to entity-body
  2632. $new .= $chunk;
  2633. $length += strlen($chunk);
  2634. break;
  2635. }
  2636. // read chunk-data and CRLF
  2637. $chunk = substr($buffer,$chunkstart,$chunkend-$chunkstart);
  2638. // append chunk-data to entity-body
  2639. $new .= $chunk;
  2640. // length := length + chunk-size
  2641. $length += strlen($chunk);
  2642. // read chunk-size and CRLF
  2643. $chunkstart = $chunkend + strlen($lb);
  2644. $chunkend = strpos($buffer, $lb, $chunkstart) + strlen($lb);
  2645. if ($chunkend == FALSE) {
  2646. break; //Just in case we got a broken connection
  2647. }
  2648. $temp = substr($buffer,$chunkstart,$chunkend-$chunkstart);
  2649. $chunk_size = hexdec( trim($temp) );
  2650. $chunkstart = $chunkend;
  2651. }
  2652. return $new;
  2653. }
  2654. /**
  2655. * Writes the payload, including HTTP headers, to $this->outgoing_payload.
  2656. *
  2657. * @param string $data HTTP body
  2658. * @param string $cookie_str data for HTTP Cookie header
  2659. * @return void
  2660. * @access private
  2661. */
  2662. function buildPayload($data, $cookie_str = '') {
  2663. // Note: for cURL connections, $this->outgoing_payload is ignored,
  2664. // as is the Content-Length header, but these are still created as
  2665. // debugging guides.
  2666. // add content-length header
  2667. if ($this->request_method != 'GET') {
  2668. $this->setHeader('Content-Length', strlen($data));
  2669. }
  2670. // start building outgoing payload:
  2671. if ($this->proxy) {
  2672. $uri = $this->url;
  2673. } else {
  2674. $uri = $this->uri;
  2675. }
  2676. $req = "$this->request_method $uri HTTP/$this->protocol_version";
  2677. $this->debug("HTTP request: $req");
  2678. $this->outgoing_payload = "$req\r\n";
  2679. // loop thru headers, serializing
  2680. foreach($this->outgoing_headers as $k => $v){
  2681. $hdr = $k.': '.$v;
  2682. $this->debug("HTTP header: $hdr");
  2683. $this->outgoing_payload .= "$hdr\r\n";
  2684. }
  2685. // add any cookies
  2686. if ($cookie_str != '') {
  2687. $hdr = 'Cookie: '.$cookie_str;
  2688. $this->debug("HTTP header: $hdr");
  2689. $this->outgoing_payload .= "$hdr\r\n";
  2690. }
  2691. // header/body separator
  2692. $this->outgoing_payload .= "\r\n";
  2693. // add data
  2694. $this->outgoing_payload .= $data;
  2695. }
  2696. /**
  2697. * sends the SOAP request via HTTP[S]
  2698. *
  2699. * @param string $data message data
  2700. * @param array $cookies cookies to send
  2701. * @return boolean true if OK, false if problem
  2702. * @access private
  2703. */
  2704. function sendRequest($data, $cookies = NULL) {
  2705. // build cookie string
  2706. $cookie_str = $this->getCookiesForRequest($cookies, (($this->scheme == 'ssl') || ($this->scheme == 'https')));
  2707. // build payload
  2708. $this->buildPayload($data, $cookie_str);
  2709. if ($this->io_method() == 'socket') {
  2710. // send payload
  2711. if(!fputs($this->fp, $this->outgoing_payload, strlen($this->outgoing_payload))) {
  2712. $this->setError('couldn\'t write message data to socket');
  2713. $this->debug('couldn\'t write message data to socket');
  2714. return false;
  2715. }
  2716. $this->debug('wrote data to socket, length = ' . strlen($this->outgoing_payload));
  2717. return true;
  2718. } else if ($this->io_method() == 'curl') {
  2719. // set payload
  2720. // cURL does say this should only be the verb, and in fact it
  2721. // turns out that the URI and HTTP version are appended to this, which
  2722. // some servers refuse to work with (so we no longer use this method!)
  2723. //$this->setCurlOption(CURLOPT_CUSTOMREQUEST, $this->outgoing_payload);
  2724. $curl_headers = array();
  2725. foreach($this->outgoing_headers as $k => $v){
  2726. if ($k == 'Connection' || $k == 'Content-Length' || $k == 'Host' || $k == 'Authorization' || $k == 'Proxy-Authorization') {
  2727. $this->debug("Skip cURL header $k: $v");
  2728. } else {
  2729. $curl_headers[] = "$k: $v";
  2730. }
  2731. }
  2732. if ($cookie_str != '') {
  2733. $curl_headers[] = 'Cookie: ' . $cookie_str;
  2734. }
  2735. $this->setCurlOption(CURLOPT_HTTPHEADER, $curl_headers);
  2736. $this->debug('set cURL HTTP headers');
  2737. if ($this->request_method == "POST") {
  2738. $this->setCurlOption(CURLOPT_POST, 1);
  2739. $this->setCurlOption(CURLOPT_POSTFIELDS, $data);
  2740. $this->debug('set cURL POST data');
  2741. } else {
  2742. }
  2743. // insert custom user-set cURL options
  2744. foreach ($this->ch_options as $key => $val) {
  2745. $this->setCurlOption($key, $val);
  2746. }
  2747. $this->debug('set cURL payload');
  2748. return true;
  2749. }
  2750. }
  2751. /**
  2752. * gets the SOAP response via HTTP[S]
  2753. *
  2754. * @return string the response (also sets member variables like incoming_payload)
  2755. * @access private
  2756. */
  2757. function getResponse(){
  2758. $this->incoming_payload = '';
  2759. if ($this->io_method() == 'socket') {
  2760. // loop until headers have been retrieved
  2761. $data = '';
  2762. while (!isset($lb)){
  2763. // We might EOF during header read.
  2764. if(feof($this->fp)) {
  2765. $this->incoming_payload = $data;
  2766. $this->debug('found no headers before EOF after length ' . strlen($data));
  2767. $this->debug("received before EOF:\n" . $data);
  2768. $this->setError('server failed to send headers');
  2769. return false;
  2770. }
  2771. $tmp = fgets($this->fp, 256);
  2772. $tmplen = strlen($tmp);
  2773. $this->debug("read line of $tmplen bytes: " . trim($tmp));
  2774. if ($tmplen == 0) {
  2775. $this->incoming_payload = $data;
  2776. $this->debug('socket read of headers timed out after length ' . strlen($data));
  2777. $this->debug("read before timeout: " . $data);
  2778. $this->setError('socket read of headers timed out');
  2779. return false;
  2780. }
  2781. $data .= $tmp;
  2782. $pos = strpos($data,"\r\n\r\n");
  2783. if($pos > 1){
  2784. $lb = "\r\n";
  2785. } else {
  2786. $pos = strpos($data,"\n\n");
  2787. if($pos > 1){
  2788. $lb = "\n";
  2789. }
  2790. }
  2791. // remove 100 headers
  2792. if (isset($lb) && preg_match('/^HTTP\/1.1 100/',$data)) {
  2793. unset($lb);
  2794. $data = '';
  2795. }//
  2796. }
  2797. // store header data
  2798. $this->incoming_payload .= $data;
  2799. $this->debug('found end of headers after length ' . strlen($data));
  2800. // process headers
  2801. $header_data = trim(substr($data,0,$pos));
  2802. $header_array = explode($lb,$header_data);
  2803. $this->incoming_headers = array();
  2804. $this->incoming_cookies = array();
  2805. foreach($header_array as $header_line){
  2806. $arr = explode(':',$header_line, 2);
  2807. if(count($arr) > 1){
  2808. $header_name = strtolower(trim($arr[0]));
  2809. $this->incoming_headers[$header_name] = trim($arr[1]);
  2810. if ($header_name == 'set-cookie') {
  2811. // TODO: allow multiple cookies from parseCookie
  2812. $cookie = $this->parseCookie(trim($arr[1]));
  2813. if ($cookie) {
  2814. $this->incoming_cookies[] = $cookie;
  2815. $this->debug('found cookie: ' . $cookie['name'] . ' = ' . $cookie['value']);
  2816. } else {
  2817. $this->debug('did not find cookie in ' . trim($arr[1]));
  2818. }
  2819. }
  2820. } else if (isset($header_name)) {
  2821. // append continuation line to previous header
  2822. $this->incoming_headers[$header_name] .= $lb . ' ' . $header_line;
  2823. }
  2824. }
  2825. // loop until msg has been received
  2826. if (isset($this->incoming_headers['transfer-encoding']) && strtolower($this->incoming_headers['transfer-encoding']) == 'chunked') {
  2827. $content_length = 2147483647; // ignore any content-length header
  2828. $chunked = true;
  2829. $this->debug("want to read chunked content");
  2830. } elseif (isset($this->incoming_headers['content-length'])) {
  2831. $content_length = $this->incoming_headers['content-length'];
  2832. $chunked = false;
  2833. $this->debug("want to read content of length $content_length");
  2834. } else {
  2835. $content_length = 2147483647;
  2836. $chunked = false;
  2837. $this->debug("want to read content to EOF");
  2838. }
  2839. $data = '';
  2840. do {
  2841. if ($chunked) {
  2842. $tmp = fgets($this->fp, 256);
  2843. $tmplen = strlen($tmp);
  2844. $this->debug("read chunk line of $tmplen bytes");
  2845. if ($tmplen == 0) {
  2846. $this->incoming_payload = $data;
  2847. $this->debug('socket read of chunk length timed out after length ' . strlen($data));
  2848. $this->debug("read before timeout:\n" . $data);
  2849. $this->setError('socket read of chunk length timed out');
  2850. return false;
  2851. }
  2852. $content_length = hexdec(trim($tmp));
  2853. $this->debug("chunk length $content_length");
  2854. }
  2855. $strlen = 0;
  2856. while (($strlen < $content_length) && (!feof($this->fp))) {
  2857. $readlen = min(8192, $content_length - $strlen);
  2858. $tmp = fread($this->fp, $readlen);
  2859. $tmplen = strlen($tmp);
  2860. $this->debug("read buffer of $tmplen bytes");
  2861. if (($tmplen == 0) && (!feof($this->fp))) {
  2862. $this->incoming_payload = $data;
  2863. $this->debug('socket read of body timed out after length ' . strlen($data));
  2864. $this->debug("read before timeout:\n" . $data);
  2865. $this->setError('socket read of body timed out');
  2866. return false;
  2867. }
  2868. $strlen += $tmplen;
  2869. $data .= $tmp;
  2870. }
  2871. if ($chunked && ($content_length > 0)) {
  2872. $tmp = fgets($this->fp, 256);
  2873. $tmplen = strlen($tmp);
  2874. $this->debug("read chunk terminator of $tmplen bytes");
  2875. if ($tmplen == 0) {
  2876. $this->incoming_payload = $data;
  2877. $this->debug('socket read of chunk terminator timed out after length ' . strlen($data));
  2878. $this->debug("read before timeout:\n" . $data);
  2879. $this->setError('socket read of chunk terminator timed out');
  2880. return false;
  2881. }
  2882. }
  2883. } while ($chunked && ($content_length > 0) && (!feof($this->fp)));
  2884. if (feof($this->fp)) {
  2885. $this->debug('read to EOF');
  2886. }
  2887. $this->debug('read body of length ' . strlen($data));
  2888. $this->incoming_payload .= $data;
  2889. $this->debug('received a total of '.strlen($this->incoming_payload).' bytes of data from server');
  2890. // close filepointer
  2891. if(
  2892. (isset($this->incoming_headers['connection']) && strtolower($this->incoming_headers['connection']) == 'close') ||
  2893. (! $this->persistentConnection) || feof($this->fp)){
  2894. fclose($this->fp);
  2895. $this->fp = false;
  2896. $this->debug('closed socket');
  2897. }
  2898. // connection was closed unexpectedly
  2899. if($this->incoming_payload == ''){
  2900. $this->setError('no response from server');
  2901. return false;
  2902. }
  2903. // decode transfer-encoding
  2904. // if(isset($this->incoming_headers['transfer-encoding']) && strtolower($this->incoming_headers['transfer-encoding']) == 'chunked'){
  2905. // if(!$data = $this->decodeChunked($data, $lb)){
  2906. // $this->setError('Decoding of chunked data failed');
  2907. // return false;
  2908. // }
  2909. //print "<pre>\nde-chunked:\n---------------\n$data\n\n---------------\n</pre>";
  2910. // set decoded payload
  2911. // $this->incoming_payload = $header_data.$lb.$lb.$data;
  2912. // }
  2913. } else if ($this->io_method() == 'curl') {
  2914. // send and receive
  2915. $this->debug('send and receive with cURL');
  2916. $this->incoming_payload = curl_exec($this->ch);
  2917. $data = $this->incoming_payload;
  2918. $cErr = curl_error($this->ch);
  2919. if ($cErr != '') {
  2920. $err = 'cURL ERROR: '.curl_errno($this->ch).': '.$cErr.'<br>';
  2921. // TODO: there is a PHP bug that can cause this to SEGV for CURLINFO_CONTENT_TYPE
  2922. foreach(curl_getinfo($this->ch) as $k => $v){
  2923. $err .= "$k: $v<br>";
  2924. }
  2925. $this->debug($err);
  2926. $this->setError($err);
  2927. curl_close($this->ch);
  2928. return false;
  2929. } else {
  2930. //echo '<pre>';
  2931. //var_dump(curl_getinfo($this->ch));
  2932. //echo '</pre>';
  2933. }
  2934. // close curl
  2935. $this->debug('No cURL error, closing cURL');
  2936. curl_close($this->ch);
  2937. // try removing skippable headers
  2938. $savedata = $data;
  2939. while ($this->isSkippableCurlHeader($data)) {
  2940. $this->debug("Found HTTP header to skip");
  2941. if ($pos = strpos($data,"\r\n\r\n")) {
  2942. $data = ltrim(substr($data,$pos));
  2943. } elseif($pos = strpos($data,"\n\n") ) {
  2944. $data = ltrim(substr($data,$pos));
  2945. }
  2946. }
  2947. if ($data == '') {
  2948. // have nothing left; just remove 100 header(s)
  2949. $data = $savedata;
  2950. while (preg_match('/^HTTP\/1.1 100/',$data)) {
  2951. if ($pos = strpos($data,"\r\n\r\n")) {
  2952. $data = ltrim(substr($data,$pos));
  2953. } elseif($pos = strpos($data,"\n\n") ) {
  2954. $data = ltrim(substr($data,$pos));
  2955. }
  2956. }
  2957. }
  2958. // separate content from HTTP headers
  2959. if ($pos = strpos($data,"\r\n\r\n")) {
  2960. $lb = "\r\n";
  2961. } elseif( $pos = strpos($data,"\n\n")) {
  2962. $lb = "\n";
  2963. } else {
  2964. $this->debug('no proper separation of headers and document');
  2965. $this->setError('no proper separation of headers and document');
  2966. return false;
  2967. }
  2968. $header_data = trim(substr($data,0,$pos));
  2969. $header_array = explode($lb,$header_data);
  2970. $data = ltrim(substr($data,$pos));
  2971. $this->debug('found proper separation of headers and document');
  2972. $this->debug('cleaned data, stringlen: '.strlen($data));
  2973. // clean headers
  2974. foreach ($header_array as $header_line) {
  2975. $arr = explode(':',$header_line,2);
  2976. if(count($arr) > 1){
  2977. $header_name = strtolower(trim($arr[0]));
  2978. $this->incoming_headers[$header_name] = trim($arr[1]);
  2979. if ($header_name == 'set-cookie') {
  2980. // TODO: allow multiple cookies from parseCookie
  2981. $cookie = $this->parseCookie(trim($arr[1]));
  2982. if ($cookie) {
  2983. $this->incoming_cookies[] = $cookie;
  2984. $this->debug('found cookie: ' . $cookie['name'] . ' = ' . $cookie['value']);
  2985. } else {
  2986. $this->debug('did not find cookie in ' . trim($arr[1]));
  2987. }
  2988. }
  2989. } else if (isset($header_name)) {
  2990. // append continuation line to previous header
  2991. $this->incoming_headers[$header_name] .= $lb . ' ' . $header_line;
  2992. }
  2993. }
  2994. }
  2995. $this->response_status_line = $header_array[0];
  2996. $arr = explode(' ', $this->response_status_line, 3);
  2997. $http_version = $arr[0];
  2998. $http_status = intval($arr[1]);
  2999. $http_reason = count($arr) > 2 ? $arr[2] : '';
  3000. // see if we need to resend the request with http digest authentication
  3001. if (isset($this->incoming_headers['location']) && ($http_status == 301 || $http_status == 302)) {
  3002. $this->debug("Got $http_status $http_reason with Location: " . $this->incoming_headers['location']);
  3003. $this->setURL($this->incoming_headers['location']);
  3004. $this->tryagain = true;
  3005. return false;
  3006. }
  3007. // see if we need to resend the request with http digest authentication
  3008. if (isset($this->incoming_headers['www-authenticate']) && $http_status == 401) {
  3009. $this->debug("Got 401 $http_reason with WWW-Authenticate: " . $this->incoming_headers['www-authenticate']);
  3010. if (strstr($this->incoming_headers['www-authenticate'], "Digest ")) {
  3011. $this->debug('Server wants digest authentication');
  3012. // remove "Digest " from our elements
  3013. $digestString = str_replace('Digest ', '', $this->incoming_headers['www-authenticate']);
  3014. // parse elements into array
  3015. $digestElements = explode(',', $digestString);
  3016. foreach ($digestElements as $val) {
  3017. $tempElement = explode('=', trim($val), 2);
  3018. $digestRequest[$tempElement[0]] = str_replace("\"", '', $tempElement[1]);
  3019. }
  3020. // should have (at least) qop, realm, nonce
  3021. if (isset($digestRequest['nonce'])) {
  3022. $this->setCredentials($this->username, $this->password, 'digest', $digestRequest);
  3023. $this->tryagain = true;
  3024. return false;
  3025. }
  3026. }
  3027. $this->debug('HTTP authentication failed');
  3028. $this->setError('HTTP authentication failed');
  3029. return false;
  3030. }
  3031. if (
  3032. ($http_status >= 300 && $http_status <= 307) ||
  3033. ($http_status >= 400 && $http_status <= 417) ||
  3034. ($http_status >= 501 && $http_status <= 505)
  3035. ) {
  3036. $this->setError("Unsupported HTTP response status $http_status $http_reason (soapclient->response has contents of the response)");
  3037. return false;
  3038. }
  3039. // decode content-encoding
  3040. if(isset($this->incoming_headers['content-encoding']) && $this->incoming_headers['content-encoding'] != ''){
  3041. if(strtolower($this->incoming_headers['content-encoding']) == 'deflate' || strtolower($this->incoming_headers['content-encoding']) == 'gzip'){
  3042. // if decoding works, use it. else assume data wasn't gzencoded
  3043. if(function_exists('gzinflate')){
  3044. //$timer->setMarker('starting decoding of gzip/deflated content');
  3045. // IIS 5 requires gzinflate instead of gzuncompress (similar to IE 5 and gzdeflate v. gzcompress)
  3046. // this means there are no Zlib headers, although there should be
  3047. $this->debug('The gzinflate function exists');
  3048. $datalen = strlen($data);
  3049. if ($this->incoming_headers['content-encoding'] == 'deflate') {
  3050. if ($degzdata = @gzinflate($data)) {
  3051. $data = $degzdata;
  3052. $this->debug('The payload has been inflated to ' . strlen($data) . ' bytes');
  3053. if (strlen($data) < $datalen) {
  3054. // test for the case that the payload has been compressed twice
  3055. $this->debug('The inflated payload is smaller than the gzipped one; try again');
  3056. if ($degzdata = @gzinflate($data)) {
  3057. $data = $degzdata;
  3058. $this->debug('The payload has been inflated again to ' . strlen($data) . ' bytes');
  3059. }
  3060. }
  3061. } else {
  3062. $this->debug('Error using gzinflate to inflate the payload');
  3063. $this->setError('Error using gzinflate to inflate the payload');
  3064. }
  3065. } elseif ($this->incoming_headers['content-encoding'] == 'gzip') {
  3066. if ($degzdata = @gzinflate(substr($data, 10))) { // do our best
  3067. $data = $degzdata;
  3068. $this->debug('The payload has been un-gzipped to ' . strlen($data) . ' bytes');
  3069. if (strlen($data) < $datalen) {
  3070. // test for the case that the payload has been compressed twice
  3071. $this->debug('The un-gzipped payload is smaller than the gzipped one; try again');
  3072. if ($degzdata = @gzinflate(substr($data, 10))) {
  3073. $data = $degzdata;
  3074. $this->debug('The payload has been un-gzipped again to ' . strlen($data) . ' bytes');
  3075. }
  3076. }
  3077. } else {
  3078. $this->debug('Error using gzinflate to un-gzip the payload');
  3079. $this->setError('Error using gzinflate to un-gzip the payload');
  3080. }
  3081. }
  3082. //$timer->setMarker('finished decoding of gzip/deflated content');
  3083. //print "<xmp>\nde-inflated:\n---------------\n$data\n-------------\n</xmp>";
  3084. // set decoded payload
  3085. $this->incoming_payload = $header_data.$lb.$lb.$data;
  3086. } else {
  3087. $this->debug('The server sent compressed data. Your php install must have the Zlib extension compiled in to support this.');
  3088. $this->setError('The server sent compressed data. Your php install must have the Zlib extension compiled in to support this.');
  3089. }
  3090. } else {
  3091. $this->debug('Unsupported Content-Encoding ' . $this->incoming_headers['content-encoding']);
  3092. $this->setError('Unsupported Content-Encoding ' . $this->incoming_headers['content-encoding']);
  3093. }
  3094. } else {
  3095. $this->debug('No Content-Encoding header');
  3096. }
  3097. if(strlen($data) == 0){
  3098. $this->debug('no data after headers!');
  3099. $this->setError('no data present after HTTP headers');
  3100. return false;
  3101. }
  3102. return $data;
  3103. }
  3104. /**
  3105. * sets the content-type for the SOAP message to be sent
  3106. *
  3107. * @param string $type the content type, MIME style
  3108. * @param mixed $charset character set used for encoding (or false)
  3109. * @access public
  3110. */
  3111. function setContentType($type, $charset = false) {
  3112. $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
  3113. }
  3114. /**
  3115. * specifies that an HTTP persistent connection should be used
  3116. *
  3117. * @return boolean whether the request was honored by this method.
  3118. * @access public
  3119. */
  3120. function usePersistentConnection(){
  3121. if (isset($this->outgoing_headers['Accept-Encoding'])) {
  3122. return false;
  3123. }
  3124. $this->protocol_version = '1.1';
  3125. $this->persistentConnection = true;
  3126. $this->setHeader('Connection', 'Keep-Alive');
  3127. return true;
  3128. }
  3129. /**
  3130. * parse an incoming Cookie into it's parts
  3131. *
  3132. * @param string $cookie_str content of cookie
  3133. * @return array with data of that cookie
  3134. * @access private
  3135. */
  3136. /*
  3137. * TODO: allow a Set-Cookie string to be parsed into multiple cookies
  3138. */
  3139. function parseCookie($cookie_str) {
  3140. $cookie_str = str_replace('; ', ';', $cookie_str) . ';';
  3141. $data = preg_split('/;/', $cookie_str);
  3142. $value_str = $data[0];
  3143. $cookie_param = 'domain=';
  3144. $start = strpos($cookie_str, $cookie_param);
  3145. if ($start > 0) {
  3146. $domain = substr($cookie_str, $start + strlen($cookie_param));
  3147. $domain = substr($domain, 0, strpos($domain, ';'));
  3148. } else {
  3149. $domain = '';
  3150. }
  3151. $cookie_param = 'expires=';
  3152. $start = strpos($cookie_str, $cookie_param);
  3153. if ($start > 0) {
  3154. $expires = substr($cookie_str, $start + strlen($cookie_param));
  3155. $expires = substr($expires, 0, strpos($expires, ';'));
  3156. } else {
  3157. $expires = '';
  3158. }
  3159. $cookie_param = 'path=';
  3160. $start = strpos($cookie_str, $cookie_param);
  3161. if ( $start > 0 ) {
  3162. $path = substr($cookie_str, $start + strlen($cookie_param));
  3163. $path = substr($path, 0, strpos($path, ';'));
  3164. } else {
  3165. $path = '/';
  3166. }
  3167. $cookie_param = ';secure;';
  3168. if (strpos($cookie_str, $cookie_param) !== FALSE) {
  3169. $secure = true;
  3170. } else {
  3171. $secure = false;
  3172. }
  3173. $sep_pos = strpos($value_str, '=');
  3174. if ($sep_pos) {
  3175. $name = substr($value_str, 0, $sep_pos);
  3176. $value = substr($value_str, $sep_pos + 1);
  3177. $cookie= array( 'name' => $name,
  3178. 'value' => $value,
  3179. 'domain' => $domain,
  3180. 'path' => $path,
  3181. 'expires' => $expires,
  3182. 'secure' => $secure
  3183. );
  3184. return $cookie;
  3185. }
  3186. return false;
  3187. }
  3188. /**
  3189. * sort out cookies for the current request
  3190. *
  3191. * @param array $cookies array with all cookies
  3192. * @param boolean $secure is the send-content secure or not?
  3193. * @return string for Cookie-HTTP-Header
  3194. * @access private
  3195. */
  3196. function getCookiesForRequest($cookies, $secure=false) {
  3197. $cookie_str = '';
  3198. if ((! is_null($cookies)) && (is_array($cookies))) {
  3199. foreach ($cookies as $cookie) {
  3200. if (! is_array($cookie)) {
  3201. continue;
  3202. }
  3203. $this->debug("check cookie for validity: ".$cookie['name'].'='.$cookie['value']);
  3204. if ((isset($cookie['expires'])) && (! empty($cookie['expires']))) {
  3205. if (strtotime($cookie['expires']) <= time()) {
  3206. $this->debug('cookie has expired');
  3207. continue;
  3208. }
  3209. }
  3210. if ((isset($cookie['domain'])) && (! empty($cookie['domain']))) {
  3211. $domain = preg_quote($cookie['domain']);
  3212. if (! preg_match("'.*$domain$'i", $this->host)) {
  3213. $this->debug('cookie has different domain');
  3214. continue;
  3215. }
  3216. }
  3217. if ((isset($cookie['path'])) && (! empty($cookie['path']))) {
  3218. $path = preg_quote($cookie['path']);
  3219. if (! preg_match("'^$path.*'i", $this->path)) {
  3220. $this->debug('cookie is for a different path');
  3221. continue;
  3222. }
  3223. }
  3224. if ((! $secure) && (isset($cookie['secure'])) && ($cookie['secure'])) {
  3225. $this->debug('cookie is secure, transport is not');
  3226. continue;
  3227. }
  3228. $cookie_str .= $cookie['name'] . '=' . $cookie['value'] . '; ';
  3229. $this->debug('add cookie to Cookie-String: ' . $cookie['name'] . '=' . $cookie['value']);
  3230. }
  3231. }
  3232. return $cookie_str;
  3233. }
  3234. }
  3235. ?><?php
  3236. /**
  3237. *
  3238. * nusoap_server allows the user to create a SOAP server
  3239. * that is capable of receiving messages and returning responses
  3240. *
  3241. * @author Dietrich Ayala <dietrich@ganx4.com>
  3242. * @author Scott Nichol <snichol@users.sourceforge.net>
  3243. * @access public
  3244. */
  3245. class nusoap_server extends nusoap_base {
  3246. /**
  3247. * HTTP headers of request
  3248. * @var array
  3249. * @access private
  3250. */
  3251. var $headers = array();
  3252. /**
  3253. * HTTP request
  3254. * @var string
  3255. * @access private
  3256. */
  3257. var $request = '';
  3258. /**
  3259. * SOAP headers from request (incomplete namespace resolution; special characters not escaped) (text)
  3260. * @var string
  3261. * @access public
  3262. */
  3263. var $requestHeaders = '';
  3264. /**
  3265. * SOAP Headers from request (parsed)
  3266. * @var mixed
  3267. * @access public
  3268. */
  3269. var $requestHeader = NULL;
  3270. /**
  3271. * SOAP body request portion (incomplete namespace resolution; special characters not escaped) (text)
  3272. * @var string
  3273. * @access public
  3274. */
  3275. var $document = '';
  3276. /**
  3277. * SOAP payload for request (text)
  3278. * @var string
  3279. * @access public
  3280. */
  3281. var $requestSOAP = '';
  3282. /**
  3283. * requested method namespace URI
  3284. * @var string
  3285. * @access private
  3286. */
  3287. var $methodURI = '';
  3288. /**
  3289. * name of method requested
  3290. * @var string
  3291. * @access private
  3292. */
  3293. var $methodname = '';
  3294. /**
  3295. * method parameters from request
  3296. * @var array
  3297. * @access private
  3298. */
  3299. var $methodparams = array();
  3300. /**
  3301. * SOAP Action from request
  3302. * @var string
  3303. * @access private
  3304. */
  3305. var $SOAPAction = '';
  3306. /**
  3307. * character set encoding of incoming (request) messages
  3308. * @var string
  3309. * @access public
  3310. */
  3311. var $xml_encoding = '';
  3312. /**
  3313. * toggles whether the parser decodes element content w/ utf8_decode()
  3314. * @var boolean
  3315. * @access public
  3316. */
  3317. var $decode_utf8 = true;
  3318. /**
  3319. * HTTP headers of response
  3320. * @var array
  3321. * @access public
  3322. */
  3323. var $outgoing_headers = array();
  3324. /**
  3325. * HTTP response
  3326. * @var string
  3327. * @access private
  3328. */
  3329. var $response = '';
  3330. /**
  3331. * SOAP headers for response (text or array of soapval or associative array)
  3332. * @var mixed
  3333. * @access public
  3334. */
  3335. var $responseHeaders = '';
  3336. /**
  3337. * SOAP payload for response (text)
  3338. * @var string
  3339. * @access private
  3340. */
  3341. var $responseSOAP = '';
  3342. /**
  3343. * method return value to place in response
  3344. * @var mixed
  3345. * @access private
  3346. */
  3347. var $methodreturn = false;
  3348. /**
  3349. * whether $methodreturn is a string of literal XML
  3350. * @var boolean
  3351. * @access public
  3352. */
  3353. var $methodreturnisliteralxml = false;
  3354. /**
  3355. * SOAP fault for response (or false)
  3356. * @var mixed
  3357. * @access private
  3358. */
  3359. var $fault = false;
  3360. /**
  3361. * text indication of result (for debugging)
  3362. * @var string
  3363. * @access private
  3364. */
  3365. var $result = 'successful';
  3366. /**
  3367. * assoc array of operations => opData; operations are added by the register()
  3368. * method or by parsing an external WSDL definition
  3369. * @var array
  3370. * @access private
  3371. */
  3372. var $operations = array();
  3373. /**
  3374. * wsdl instance (if one)
  3375. * @var mixed
  3376. * @access private
  3377. */
  3378. var $wsdl = false;
  3379. /**
  3380. * URL for WSDL (if one)
  3381. * @var mixed
  3382. * @access private
  3383. */
  3384. var $externalWSDLURL = false;
  3385. /**
  3386. * whether to append debug to response as XML comment
  3387. * @var boolean
  3388. * @access public
  3389. */
  3390. var $debug_flag = false;
  3391. /**
  3392. * constructor
  3393. * the optional parameter is a path to a WSDL file that you'd like to bind the server instance to.
  3394. *
  3395. * @param mixed $wsdl file path or URL (string), or wsdl instance (object)
  3396. * @access public
  3397. */
  3398. function nusoap_server($wsdl=false){
  3399. parent::nusoap_base();
  3400. // turn on debugging?
  3401. global $debug;
  3402. global $HTTP_SERVER_VARS;
  3403. if (isset($_SERVER)) {
  3404. $this->debug("_SERVER is defined:");
  3405. $this->appendDebug($this->varDump($_SERVER));
  3406. } elseif (isset($HTTP_SERVER_VARS)) {
  3407. $this->debug("HTTP_SERVER_VARS is defined:");
  3408. $this->appendDebug($this->varDump($HTTP_SERVER_VARS));
  3409. } else {
  3410. $this->debug("Neither _SERVER nor HTTP_SERVER_VARS is defined.");
  3411. }
  3412. if (isset($debug)) {
  3413. $this->debug("In nusoap_server, set debug_flag=$debug based on global flag");
  3414. $this->debug_flag = $debug;
  3415. } elseif (isset($_SERVER['QUERY_STRING'])) {
  3416. $qs = explode('&', $_SERVER['QUERY_STRING']);
  3417. foreach ($qs as $v) {
  3418. if (substr($v, 0, 6) == 'debug=') {
  3419. $this->debug("In nusoap_server, set debug_flag=" . substr($v, 6) . " based on query string #1");
  3420. $this->debug_flag = substr($v, 6);
  3421. }
  3422. }
  3423. } elseif (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  3424. $qs = explode('&', $HTTP_SERVER_VARS['QUERY_STRING']);
  3425. foreach ($qs as $v) {
  3426. if (substr($v, 0, 6) == 'debug=') {
  3427. $this->debug("In nusoap_server, set debug_flag=" . substr($v, 6) . " based on query string #2");
  3428. $this->debug_flag = substr($v, 6);
  3429. }
  3430. }
  3431. }
  3432. // wsdl
  3433. if($wsdl){
  3434. $this->debug("In nusoap_server, WSDL is specified");
  3435. if (is_object($wsdl) && (get_class($wsdl) == 'wsdl')) {
  3436. $this->wsdl = $wsdl;
  3437. $this->externalWSDLURL = $this->wsdl->wsdl;
  3438. $this->debug('Use existing wsdl instance from ' . $this->externalWSDLURL);
  3439. } else {
  3440. $this->debug('Create wsdl from ' . $wsdl);
  3441. $this->wsdl = new wsdl($wsdl);
  3442. $this->externalWSDLURL = $wsdl;
  3443. }
  3444. $this->appendDebug($this->wsdl->getDebug());
  3445. $this->wsdl->clearDebug();
  3446. if($err = $this->wsdl->getError()){
  3447. die('WSDL ERROR: '.$err);
  3448. }
  3449. }
  3450. }
  3451. /**
  3452. * processes request and returns response
  3453. *
  3454. * @param string $data usually is the value of $HTTP_RAW_POST_DATA
  3455. * @access public
  3456. */
  3457. function service($data){
  3458. global $HTTP_SERVER_VARS;
  3459. if (isset($_SERVER['REQUEST_METHOD'])) {
  3460. $rm = $_SERVER['REQUEST_METHOD'];
  3461. } elseif (isset($HTTP_SERVER_VARS['REQUEST_METHOD'])) {
  3462. $rm = $HTTP_SERVER_VARS['REQUEST_METHOD'];
  3463. } else {
  3464. $rm = '';
  3465. }
  3466. if (isset($_SERVER['QUERY_STRING'])) {
  3467. $qs = $_SERVER['QUERY_STRING'];
  3468. } elseif (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
  3469. $qs = $HTTP_SERVER_VARS['QUERY_STRING'];
  3470. } else {
  3471. $qs = '';
  3472. }
  3473. $this->debug("In service, request method=$rm query string=$qs strlen(\$data)=" . strlen($data));
  3474. if ($rm == 'POST') {
  3475. $this->debug("In service, invoke the request");
  3476. $this->parse_request($data);
  3477. if (! $this->fault) {
  3478. $this->invoke_method();
  3479. }
  3480. if (! $this->fault) {
  3481. $this->serialize_return();
  3482. }
  3483. $this->send_response();
  3484. } elseif (preg_match('/wsdl/', $qs) ){
  3485. $this->debug("In service, this is a request for WSDL");
  3486. if ($this->externalWSDLURL){
  3487. if (strpos($this->externalWSDLURL, "http://") !== false) { // assume URL
  3488. $this->debug("In service, re-direct for WSDL");
  3489. header('Location: '.$this->externalWSDLURL);
  3490. } else { // assume file
  3491. $this->debug("In service, use file passthru for WSDL");
  3492. header("Content-Type: text/xml\r\n");
  3493. $pos = strpos($this->externalWSDLURL, "file://");
  3494. if ($pos === false) {
  3495. $filename = $this->externalWSDLURL;
  3496. } else {
  3497. $filename = substr($this->externalWSDLURL, $pos + 7);
  3498. }
  3499. $fp = fopen($this->externalWSDLURL, 'r');
  3500. fpassthru($fp);
  3501. }
  3502. } elseif ($this->wsdl) {
  3503. $this->debug("In service, serialize WSDL");
  3504. header("Content-Type: text/xml; charset=ISO-8859-1\r\n");
  3505. print $this->wsdl->serialize($this->debug_flag);
  3506. if ($this->debug_flag) {
  3507. $this->debug('wsdl:');
  3508. $this->appendDebug($this->varDump($this->wsdl));
  3509. print $this->getDebugAsXMLComment();
  3510. }
  3511. } else {
  3512. $this->debug("In service, there is no WSDL");
  3513. header("Content-Type: text/html; charset=ISO-8859-1\r\n");
  3514. print "This service does not provide WSDL";
  3515. }
  3516. } elseif ($this->wsdl) {
  3517. $this->debug("In service, return Web description");
  3518. print $this->wsdl->webDescription();
  3519. } else {
  3520. $this->debug("In service, no Web description");
  3521. header("Content-Type: text/html; charset=ISO-8859-1\r\n");
  3522. print "This service does not provide a Web description";
  3523. }
  3524. }
  3525. /**
  3526. * parses HTTP request headers.
  3527. *
  3528. * The following fields are set by this function (when successful)
  3529. *
  3530. * headers
  3531. * request
  3532. * xml_encoding
  3533. * SOAPAction
  3534. *
  3535. * @access private
  3536. */
  3537. function parse_http_headers() {
  3538. global $HTTP_SERVER_VARS;
  3539. $this->request = '';
  3540. $this->SOAPAction = '';
  3541. if(function_exists('getallheaders')){
  3542. $this->debug("In parse_http_headers, use getallheaders");
  3543. $headers = getallheaders();
  3544. foreach($headers as $k=>$v){
  3545. $k = strtolower($k);
  3546. $this->headers[$k] = $v;
  3547. $this->request .= "$k: $v\r\n";
  3548. $this->debug("$k: $v");
  3549. }
  3550. // get SOAPAction header
  3551. if(isset($this->headers['soapaction'])){
  3552. $this->SOAPAction = str_replace('"','',$this->headers['soapaction']);
  3553. }
  3554. // get the character encoding of the incoming request
  3555. if(isset($this->headers['content-type']) && strpos($this->headers['content-type'],'=')){
  3556. $enc = str_replace('"','',substr(strstr($this->headers["content-type"],'='),1));
  3557. if(preg_match('/^(ISO-8859-1|US-ASCII|UTF-8)$/i',$enc)){
  3558. $this->xml_encoding = strtoupper($enc);
  3559. } else {
  3560. $this->xml_encoding = 'US-ASCII';
  3561. }
  3562. } else {
  3563. // should be US-ASCII for HTTP 1.0 or ISO-8859-1 for HTTP 1.1
  3564. $this->xml_encoding = 'ISO-8859-1';
  3565. }
  3566. } elseif(isset($_SERVER) && is_array($_SERVER)){
  3567. $this->debug("In parse_http_headers, use _SERVER");
  3568. foreach ($_SERVER as $k => $v) {
  3569. if (substr($k, 0, 5) == 'HTTP_') {
  3570. $k = str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($k, 5))));
  3571. } else {
  3572. $k = str_replace(' ', '-', strtolower(str_replace('_', ' ', $k)));
  3573. }
  3574. if ($k == 'soapaction') {
  3575. // get SOAPAction header
  3576. $k = 'SOAPAction';
  3577. $v = str_replace('"', '', $v);
  3578. $v = str_replace('\\', '', $v);
  3579. $this->SOAPAction = $v;
  3580. } else if ($k == 'content-type') {
  3581. // get the character encoding of the incoming request
  3582. if (strpos($v, '=')) {
  3583. $enc = substr(strstr($v, '='), 1);
  3584. $enc = str_replace('"', '', $enc);
  3585. $enc = str_replace('\\', '', $enc);
  3586. if (preg_match('/^(ISO-8859-1|US-ASCII|UTF-8)$/i',$enc)) {
  3587. $this->xml_encoding = strtoupper($enc);
  3588. } else {
  3589. $this->xml_encoding = 'US-ASCII';
  3590. }
  3591. } else {
  3592. // should be US-ASCII for HTTP 1.0 or ISO-8859-1 for HTTP 1.1
  3593. $this->xml_encoding = 'ISO-8859-1';
  3594. }
  3595. }
  3596. $this->headers[$k] = $v;
  3597. $this->request .= "$k: $v\r\n";
  3598. $this->debug("$k: $v");
  3599. }
  3600. } elseif (is_array($HTTP_SERVER_VARS)) {
  3601. $this->debug("In parse_http_headers, use HTTP_SERVER_VARS");
  3602. foreach ($HTTP_SERVER_VARS as $k => $v) {
  3603. if (substr($k, 0, 5) == 'HTTP_') {
  3604. $k = str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($k, 5)))); $k = strtolower(substr($k, 5));
  3605. } else {
  3606. $k = str_replace(' ', '-', strtolower(str_replace('_', ' ', $k))); $k = strtolower($k);
  3607. }
  3608. if ($k == 'soapaction') {
  3609. // get SOAPAction header
  3610. $k = 'SOAPAction';
  3611. $v = str_replace('"', '', $v);
  3612. $v = str_replace('\\', '', $v);
  3613. $this->SOAPAction = $v;
  3614. } else if ($k == 'content-type') {
  3615. // get the character encoding of the incoming request
  3616. if (strpos($v, '=')) {
  3617. $enc = substr(strstr($v, '='), 1);
  3618. $enc = str_replace('"', '', $enc);
  3619. $enc = str_replace('\\', '', $enc);
  3620. if (preg_match('/^(ISO-8859-1|US-ASCII|UTF-8)$/i',$enc)) {
  3621. $this->xml_encoding = strtoupper($enc);
  3622. } else {
  3623. $this->xml_encoding = 'US-ASCII';
  3624. }
  3625. } else {
  3626. // should be US-ASCII for HTTP 1.0 or ISO-8859-1 for HTTP 1.1
  3627. $this->xml_encoding = 'ISO-8859-1';
  3628. }
  3629. }
  3630. $this->headers[$k] = $v;
  3631. $this->request .= "$k: $v\r\n";
  3632. $this->debug("$k: $v");
  3633. }
  3634. } else {
  3635. $this->debug("In parse_http_headers, HTTP headers not accessible");
  3636. $this->setError("HTTP headers not accessible");
  3637. }
  3638. }
  3639. /**
  3640. * parses a request
  3641. *
  3642. * The following fields are set by this function (when successful)
  3643. *
  3644. * headers
  3645. * request
  3646. * xml_encoding
  3647. * SOAPAction
  3648. * request
  3649. * requestSOAP
  3650. * methodURI
  3651. * methodname
  3652. * methodparams
  3653. * requestHeaders
  3654. * document
  3655. *
  3656. * This sets the fault field on error
  3657. *
  3658. * @param string $data XML string
  3659. * @access private
  3660. */
  3661. function parse_request($data='') {
  3662. $this->debug('entering parse_request()');
  3663. $this->parse_http_headers();
  3664. $this->debug('got character encoding: '.$this->xml_encoding);
  3665. // uncompress if necessary
  3666. if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] != '') {
  3667. $this->debug('got content encoding: ' . $this->headers['content-encoding']);
  3668. if ($this->headers['content-encoding'] == 'deflate' || $this->headers['content-encoding'] == 'gzip') {
  3669. // if decoding works, use it. else assume data wasn't gzencoded
  3670. if (function_exists('gzuncompress')) {
  3671. if ($this->headers['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) {
  3672. $data = $degzdata;
  3673. } elseif ($this->headers['content-encoding'] == 'gzip' && $degzdata = gzinflate(substr($data, 10))) {
  3674. $data = $degzdata;
  3675. } else {
  3676. $this->fault('SOAP-ENV:Client', 'Errors occurred when trying to decode the data');
  3677. return;
  3678. }
  3679. } else {
  3680. $this->fault('SOAP-ENV:Client', 'This Server does not support compressed data');
  3681. return;
  3682. }
  3683. }
  3684. }
  3685. $this->request .= "\r\n".$data;
  3686. $data = $this->parseRequest($this->headers, $data);
  3687. $this->requestSOAP = $data;
  3688. $this->debug('leaving parse_request');
  3689. }
  3690. /**
  3691. * invokes a PHP function for the requested SOAP method
  3692. *
  3693. * The following fields are set by this function (when successful)
  3694. *
  3695. * methodreturn
  3696. *
  3697. * Note that the PHP function that is called may also set the following
  3698. * fields to affect the response sent to the client
  3699. *
  3700. * responseHeaders
  3701. * outgoing_headers
  3702. *
  3703. * This sets the fault field on error
  3704. *
  3705. * @access private
  3706. */
  3707. function invoke_method() {
  3708. $this->debug('in invoke_method, methodname=' . $this->methodname . ' methodURI=' . $this->methodURI . ' SOAPAction=' . $this->SOAPAction);
  3709. //
  3710. // if you are debugging in this area of the code, your service uses a class to implement methods,
  3711. // you use SOAP RPC, and the client is .NET, please be aware of the following...
  3712. // when the .NET wsdl.exe utility generates a proxy, it will remove the '.' or '..' from the
  3713. // method name. that is fine for naming the .NET methods. it is not fine for properly constructing
  3714. // the XML request and reading the XML response. you need to add the RequestElementName and
  3715. // ResponseElementName to the System.Web.Services.Protocols.SoapRpcMethodAttribute that wsdl.exe
  3716. // generates for the method. these parameters are used to specify the correct XML element names
  3717. // for .NET to use, i.e. the names with the '.' in them.
  3718. //
  3719. $orig_methodname = $this->methodname;
  3720. if ($this->wsdl) {
  3721. if ($this->opData = $this->wsdl->getOperationData($this->methodname)) {
  3722. $this->debug('in invoke_method, found WSDL operation=' . $this->methodname);
  3723. $this->appendDebug('opData=' . $this->varDump($this->opData));
  3724. } elseif ($this->opData = $this->wsdl->getOperationDataForSoapAction($this->SOAPAction)) {
  3725. // Note: hopefully this case will only be used for doc/lit, since rpc services should have wrapper element
  3726. $this->debug('in invoke_method, found WSDL soapAction=' . $this->SOAPAction . ' for operation=' . $this->opData['name']);
  3727. $this->appendDebug('opData=' . $this->varDump($this->opData));
  3728. $this->methodname = $this->opData['name'];
  3729. } else {
  3730. $this->debug('in invoke_method, no WSDL for operation=' . $this->methodname);
  3731. $this->fault('SOAP-ENV:Client', "Operation '" . $this->methodname . "' is not defined in the WSDL for this service");
  3732. return;
  3733. }
  3734. } else {
  3735. $this->debug('in invoke_method, no WSDL to validate method');
  3736. }
  3737. // if a . is present in $this->methodname, we see if there is a class in scope,
  3738. // which could be referred to. We will also distinguish between two deliminators,
  3739. // to allow methods to be called a the class or an instance
  3740. if (strpos($this->methodname, '..') > 0) {
  3741. $delim = '..';
  3742. } else if (strpos($this->methodname, '.') > 0) {
  3743. $delim = '.';
  3744. } else {
  3745. $delim = '';
  3746. }
  3747. $this->debug("in invoke_method, delim=$delim");
  3748. $class = '';
  3749. $method = '';
  3750. if (strlen($delim) > 0 && substr_count($this->methodname, $delim) == 1) {
  3751. $try_class = substr($this->methodname, 0, strpos($this->methodname, $delim));
  3752. if (class_exists($try_class)) {
  3753. // get the class and method name
  3754. $class = $try_class;
  3755. $method = substr($this->methodname, strpos($this->methodname, $delim) + strlen($delim));
  3756. $this->debug("in invoke_method, class=$class method=$method delim=$delim");
  3757. } else {
  3758. $this->debug("in invoke_method, class=$try_class not found");
  3759. }
  3760. } else {
  3761. $try_class = '';
  3762. $this->debug("in invoke_method, no class to try");
  3763. }
  3764. // does method exist?
  3765. if ($class == '') {
  3766. if (!function_exists($this->methodname)) {
  3767. $this->debug("in invoke_method, function '$this->methodname' not found!");
  3768. $this->result = 'fault: method not found';
  3769. $this->fault('SOAP-ENV:Client',"method '$this->methodname'('$orig_methodname') not defined in service('$try_class' '$delim')");
  3770. return;
  3771. }
  3772. } else {
  3773. $method_to_compare = (substr(phpversion(), 0, 2) == '4.') ? strtolower($method) : $method;
  3774. if (!in_array($method_to_compare, get_class_methods($class))) {
  3775. $this->debug("in invoke_method, method '$this->methodname' not found in class '$class'!");
  3776. $this->result = 'fault: method not found';
  3777. $this->fault('SOAP-ENV:Client',"method '$this->methodname'/'$method_to_compare'('$orig_methodname') not defined in service/'$class'('$try_class' '$delim')");
  3778. return;
  3779. }
  3780. }
  3781. // evaluate message, getting back parameters
  3782. // verify that request parameters match the method's signature
  3783. if(! $this->verify_method($this->methodname,$this->methodparams)){
  3784. // debug
  3785. $this->debug('ERROR: request not verified against method signature');
  3786. $this->result = 'fault: request failed validation against method signature';
  3787. // return fault
  3788. $this->fault('SOAP-ENV:Client',"Operation '$this->methodname' not defined in service.");
  3789. return;
  3790. }
  3791. // if there are parameters to pass
  3792. $this->debug('in invoke_method, params:');
  3793. $this->appendDebug($this->varDump($this->methodparams));
  3794. $this->debug("in invoke_method, calling '$this->methodname'");
  3795. if (!function_exists('call_user_func_array')) {
  3796. if ($class == '') {
  3797. $this->debug('in invoke_method, calling function using eval()');
  3798. $funcCall = "\$this->methodreturn = $this->methodname(";
  3799. } else {
  3800. if ($delim == '..') {
  3801. $this->debug('in invoke_method, calling class method using eval()');
  3802. $funcCall = "\$this->methodreturn = ".$class."::".$method."(";
  3803. } else {
  3804. $this->debug('in invoke_method, calling instance method using eval()');
  3805. // generate unique instance name
  3806. $instname = "\$inst_".time();
  3807. $funcCall = $instname." = new ".$class."(); ";
  3808. $funcCall .= "\$this->methodreturn = ".$instname."->".$method."(";
  3809. }
  3810. }
  3811. if ($this->methodparams) {
  3812. foreach ($this->methodparams as $param) {
  3813. if (is_array($param) || is_object($param)) {
  3814. $this->fault('SOAP-ENV:Client', 'NuSOAP does not handle complexType parameters correctly when using eval; call_user_func_array must be available');
  3815. return;
  3816. }
  3817. $funcCall .= "\"$param\",";
  3818. }
  3819. $funcCall = substr($funcCall, 0, -1);
  3820. }
  3821. $funcCall .= ');';
  3822. $this->debug('in invoke_method, function call: '.$funcCall);
  3823. @eval($funcCall);
  3824. } else {
  3825. if ($class == '') {
  3826. $this->debug('in invoke_method, calling function using call_user_func_array()');
  3827. $call_arg = "$this->methodname"; // straight assignment changes $this->methodname to lower case after call_user_func_array()
  3828. } elseif ($delim == '..') {
  3829. $this->debug('in invoke_method, calling class method using call_user_func_array()');
  3830. $call_arg = array ($class, $method);
  3831. } else {
  3832. $this->debug('in invoke_method, calling instance method using call_user_func_array()');
  3833. $instance = new $class ();
  3834. $call_arg = array(&$instance, $method);
  3835. }
  3836. if (is_array($this->methodparams)) {
  3837. $this->methodreturn = call_user_func_array($call_arg, array_values($this->methodparams));
  3838. } else {
  3839. $this->methodreturn = call_user_func_array($call_arg, array());
  3840. }
  3841. }
  3842. $this->debug('in invoke_method, methodreturn:');
  3843. $this->appendDebug($this->varDump($this->methodreturn));
  3844. $this->debug("in invoke_method, called method $this->methodname, received data of type ".gettype($this->methodreturn));
  3845. }
  3846. /**
  3847. * serializes the return value from a PHP function into a full SOAP Envelope
  3848. *
  3849. * The following fields are set by this function (when successful)
  3850. *
  3851. * responseSOAP
  3852. *
  3853. * This sets the fault field on error
  3854. *
  3855. * @access private
  3856. */
  3857. function serialize_return() {
  3858. $this->debug('Entering serialize_return methodname: ' . $this->methodname . ' methodURI: ' . $this->methodURI);
  3859. // if fault
  3860. if (isset($this->methodreturn) && is_object($this->methodreturn) && ((get_class($this->methodreturn) == 'soap_fault') || (get_class($this->methodreturn) == 'nusoap_fault'))) {
  3861. $this->debug('got a fault object from method');
  3862. $this->fault = $this->methodreturn;
  3863. return;
  3864. } elseif ($this->methodreturnisliteralxml) {
  3865. $return_val = $this->methodreturn;
  3866. // returned value(s)
  3867. } else {
  3868. $this->debug('got a(n) '.gettype($this->methodreturn).' from method');
  3869. $this->debug('serializing return value');
  3870. if($this->wsdl){
  3871. if (sizeof($this->opData['output']['parts']) > 1) {
  3872. $this->debug('more than one output part, so use the method return unchanged');
  3873. $opParams = $this->methodreturn;
  3874. } elseif (sizeof($this->opData['output']['parts']) == 1) {
  3875. $this->debug('exactly one output part, so wrap the method return in a simple array');
  3876. // TODO: verify that it is not already wrapped!
  3877. //foreach ($this->opData['output']['parts'] as $name => $type) {
  3878. // $this->debug('wrap in element named ' . $name);
  3879. //}
  3880. $opParams = array($this->methodreturn);
  3881. }
  3882. $return_val = $this->wsdl->serializeRPCParameters($this->methodname,'output',$opParams);
  3883. $this->appendDebug($this->wsdl->getDebug());
  3884. $this->wsdl->clearDebug();
  3885. if($errstr = $this->wsdl->getError()){
  3886. $this->debug('got wsdl error: '.$errstr);
  3887. $this->fault('SOAP-ENV:Server', 'unable to serialize result');
  3888. return;
  3889. }
  3890. } else {
  3891. if (isset($this->methodreturn)) {
  3892. $return_val = $this->serialize_val($this->methodreturn, 'return');
  3893. } else {
  3894. $return_val = '';
  3895. $this->debug('in absence of WSDL, assume void return for backward compatibility');
  3896. }
  3897. }
  3898. }
  3899. $this->debug('return value:');
  3900. $this->appendDebug($this->varDump($return_val));
  3901. $this->debug('serializing response');
  3902. if ($this->wsdl) {
  3903. $this->debug('have WSDL for serialization: style is ' . $this->opData['style']);
  3904. if ($this->opData['style'] == 'rpc') {
  3905. $this->debug('style is rpc for serialization: use is ' . $this->opData['output']['use']);
  3906. if ($this->opData['output']['use'] == 'literal') {
  3907. // http://www.ws-i.org/Profiles/BasicProfile-1.1-2004-08-24.html R2735 says rpc/literal accessor elements should not be in a namespace
  3908. if ($this->methodURI) {
  3909. $payload = '<ns1:'.$this->methodname.'Response xmlns:ns1="'.$this->methodURI.'">'.$return_val.'</ns1:'.$this->methodname."Response>";
  3910. } else {
  3911. $payload = '<'.$this->methodname.'Response>'.$return_val.'</'.$this->methodname.'Response>';
  3912. }
  3913. } else {
  3914. if ($this->methodURI) {
  3915. $payload = '<ns1:'.$this->methodname.'Response xmlns:ns1="'.$this->methodURI.'">'.$return_val.'</ns1:'.$this->methodname."Response>";
  3916. } else {
  3917. $payload = '<'.$this->methodname.'Response>'.$return_val.'</'.$this->methodname.'Response>';
  3918. }
  3919. }
  3920. } else {
  3921. $this->debug('style is not rpc for serialization: assume document');
  3922. $payload = $return_val;
  3923. }
  3924. } else {
  3925. $this->debug('do not have WSDL for serialization: assume rpc/encoded');
  3926. $payload = '<ns1:'.$this->methodname.'Response xmlns:ns1="'.$this->methodURI.'">'.$return_val.'</ns1:'.$this->methodname."Response>";
  3927. }
  3928. $this->result = 'successful';
  3929. if($this->wsdl){
  3930. //if($this->debug_flag){
  3931. $this->appendDebug($this->wsdl->getDebug());
  3932. // }
  3933. if (isset($this->opData['output']['encodingStyle'])) {
  3934. $encodingStyle = $this->opData['output']['encodingStyle'];
  3935. } else {
  3936. $encodingStyle = '';
  3937. }
  3938. // Added: In case we use a WSDL, return a serialized env. WITH the usedNamespaces.
  3939. $this->responseSOAP = $this->serializeEnvelope($payload,$this->responseHeaders,$this->wsdl->usedNamespaces,$this->opData['style'],$this->opData['output']['use'],$encodingStyle);
  3940. } else {
  3941. $this->responseSOAP = $this->serializeEnvelope($payload,$this->responseHeaders);
  3942. }
  3943. $this->debug("Leaving serialize_return");
  3944. }
  3945. /**
  3946. * sends an HTTP response
  3947. *
  3948. * The following fields are set by this function (when successful)
  3949. *
  3950. * outgoing_headers
  3951. * response
  3952. *
  3953. * @access private
  3954. */
  3955. function send_response() {
  3956. $this->debug('Enter send_response');
  3957. if ($this->fault) {
  3958. $payload = $this->fault->serialize();
  3959. $this->outgoing_headers[] = "HTTP/1.0 500 Internal Server Error";
  3960. $this->outgoing_headers[] = "Status: 500 Internal Server Error";
  3961. } else {
  3962. $payload = $this->responseSOAP;
  3963. // Some combinations of PHP+Web server allow the Status
  3964. // to come through as a header. Since OK is the default
  3965. // just do nothing.
  3966. // $this->outgoing_headers[] = "HTTP/1.0 200 OK";
  3967. // $this->outgoing_headers[] = "Status: 200 OK";
  3968. }
  3969. // add debug data if in debug mode
  3970. if(isset($this->debug_flag) && $this->debug_flag){
  3971. $payload .= $this->getDebugAsXMLComment();
  3972. }
  3973. $this->outgoing_headers[] = "Server: $this->title Server v$this->version";
  3974. preg_match('/\$Revisio' . 'n: ([^ ]+)/', $this->revision, $rev);
  3975. $this->outgoing_headers[] = "X-SOAP-Server: $this->title/$this->version (".$rev[1].")";
  3976. // Let the Web server decide about this
  3977. //$this->outgoing_headers[] = "Connection: Close\r\n";
  3978. $payload = $this->getHTTPBody($payload);
  3979. $type = $this->getHTTPContentType();
  3980. $charset = $this->getHTTPContentTypeCharset();
  3981. $this->outgoing_headers[] = "Content-Type: $type" . ($charset ? '; charset=' . $charset : '');
  3982. //begin code to compress payload - by John
  3983. // NOTE: there is no way to know whether the Web server will also compress
  3984. // this data.
  3985. if (strlen($payload) > 1024 && isset($this->headers) && isset($this->headers['accept-encoding'])) {
  3986. if (strstr($this->headers['accept-encoding'], 'gzip')) {
  3987. if (function_exists('gzencode')) {
  3988. if (isset($this->debug_flag) && $this->debug_flag) {
  3989. $payload .= "<!-- Content being gzipped -->";
  3990. }
  3991. $this->outgoing_headers[] = "Content-Encoding: gzip";
  3992. $payload = gzencode($payload);
  3993. } else {
  3994. if (isset($this->debug_flag) && $this->debug_flag) {
  3995. $payload .= "<!-- Content will not be gzipped: no gzencode -->";
  3996. }
  3997. }
  3998. } elseif (strstr($this->headers['accept-encoding'], 'deflate')) {
  3999. // Note: MSIE requires gzdeflate output (no Zlib header and checksum),
  4000. // instead of gzcompress output,
  4001. // which conflicts with HTTP 1.1 spec (http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5)
  4002. if (function_exists('gzdeflate')) {
  4003. if (isset($this->debug_flag) && $this->debug_flag) {
  4004. $payload .= "<!-- Content being deflated -->";
  4005. }
  4006. $this->outgoing_headers[] = "Content-Encoding: deflate";
  4007. $payload = gzdeflate($payload);
  4008. } else {
  4009. if (isset($this->debug_flag) && $this->debug_flag) {
  4010. $payload .= "<!-- Content will not be deflated: no gzcompress -->";
  4011. }
  4012. }
  4013. }
  4014. }
  4015. //end code
  4016. $this->outgoing_headers[] = "Content-Length: ".strlen($payload);
  4017. reset($this->outgoing_headers);
  4018. foreach($this->outgoing_headers as $hdr){
  4019. header($hdr, false);
  4020. }
  4021. print $payload;
  4022. $this->response = join("\r\n",$this->outgoing_headers)."\r\n\r\n".$payload;
  4023. }
  4024. /**
  4025. * takes the value that was created by parsing the request
  4026. * and compares to the method's signature, if available.
  4027. *
  4028. * @param string $operation The operation to be invoked
  4029. * @param array $request The array of parameter values
  4030. * @return boolean Whether the operation was found
  4031. * @access private
  4032. */
  4033. function verify_method($operation,$request){
  4034. if(isset($this->wsdl) && is_object($this->wsdl)){
  4035. if($this->wsdl->getOperationData($operation)){
  4036. return true;
  4037. }
  4038. } elseif(isset($this->operations[$operation])){
  4039. return true;
  4040. }
  4041. return false;
  4042. }
  4043. /**
  4044. * processes SOAP message received from client
  4045. *
  4046. * @param array $headers The HTTP headers
  4047. * @param string $data unprocessed request data from client
  4048. * @return mixed value of the message, decoded into a PHP type
  4049. * @access private
  4050. */
  4051. function parseRequest($headers, $data) {
  4052. $this->debug('Entering parseRequest() for data of length ' . strlen($data) . ' headers:');
  4053. $this->appendDebug($this->varDump($headers));
  4054. if (!isset($headers['content-type'])) {
  4055. $this->setError('Request not of type text/xml (no content-type header)');
  4056. return false;
  4057. }
  4058. if (!strstr($headers['content-type'], 'text/xml')) {
  4059. $this->setError('Request not of type text/xml');
  4060. return false;
  4061. }
  4062. if (strpos($headers['content-type'], '=')) {
  4063. $enc = str_replace('"', '', substr(strstr($headers["content-type"], '='), 1));
  4064. $this->debug('Got response encoding: ' . $enc);
  4065. if(preg_match('/^(ISO-8859-1|US-ASCII|UTF-8)$/i',$enc)){
  4066. $this->xml_encoding = strtoupper($enc);
  4067. } else {
  4068. $this->xml_encoding = 'US-ASCII';
  4069. }
  4070. } else {
  4071. // should be US-ASCII for HTTP 1.0 or ISO-8859-1 for HTTP 1.1
  4072. $this->xml_encoding = 'ISO-8859-1';
  4073. }
  4074. $this->debug('Use encoding: ' . $this->xml_encoding . ' when creating nusoap_parser');
  4075. // parse response, get soap parser obj
  4076. $parser = new nusoap_parser($data,$this->xml_encoding,'',$this->decode_utf8);
  4077. // parser debug
  4078. $this->debug("parser debug: \n".$parser->getDebug());
  4079. // if fault occurred during message parsing
  4080. if($err = $parser->getError()){
  4081. $this->result = 'fault: error in msg parsing: '.$err;
  4082. $this->fault('SOAP-ENV:Client',"error in msg parsing:\n".$err);
  4083. // else successfully parsed request into soapval object
  4084. } else {
  4085. // get/set methodname
  4086. $this->methodURI = $parser->root_struct_namespace;
  4087. $this->methodname = $parser->root_struct_name;
  4088. $this->debug('methodname: '.$this->methodname.' methodURI: '.$this->methodURI);
  4089. $this->debug('calling parser->get_soapbody()');
  4090. $this->methodparams = $parser->get_soapbody();
  4091. // get SOAP headers
  4092. $this->requestHeaders = $parser->getHeaders();
  4093. // get SOAP Header
  4094. $this->requestHeader = $parser->get_soapheader();
  4095. // add document for doclit support
  4096. $this->document = $parser->document;
  4097. }
  4098. }
  4099. /**
  4100. * gets the HTTP body for the current response.
  4101. *
  4102. * @param string $soapmsg The SOAP payload
  4103. * @return string The HTTP body, which includes the SOAP payload
  4104. * @access private
  4105. */
  4106. function getHTTPBody($soapmsg) {
  4107. return $soapmsg;
  4108. }
  4109. /**
  4110. * gets the HTTP content type for the current response.
  4111. *
  4112. * Note: getHTTPBody must be called before this.
  4113. *
  4114. * @return string the HTTP content type for the current response.
  4115. * @access private
  4116. */
  4117. function getHTTPContentType() {
  4118. return 'text/xml';
  4119. }
  4120. /**
  4121. * gets the HTTP content type charset for the current response.
  4122. * returns false for non-text content types.
  4123. *
  4124. * Note: getHTTPBody must be called before this.
  4125. *
  4126. * @return string the HTTP content type charset for the current response.
  4127. * @access private
  4128. */
  4129. function getHTTPContentTypeCharset() {
  4130. return $this->soap_defencoding;
  4131. }
  4132. /**
  4133. * add a method to the dispatch map (this has been replaced by the register method)
  4134. *
  4135. * @param string $methodname
  4136. * @param string $in array of input values
  4137. * @param string $out array of output values
  4138. * @access public
  4139. * @deprecated
  4140. */
  4141. function add_to_map($methodname,$in,$out){
  4142. $this->operations[$methodname] = array('name' => $methodname,'in' => $in,'out' => $out);
  4143. }
  4144. /**
  4145. * register a service function with the server
  4146. *
  4147. * @param string $name the name of the PHP function, class.method or class..method
  4148. * @param array $in assoc array of input values: key = param name, value = param type
  4149. * @param array $out assoc array of output values: key = param name, value = param type
  4150. * @param mixed $namespace the element namespace for the method or false
  4151. * @param mixed $soapaction the soapaction for the method or false
  4152. * @param mixed $style optional (rpc|document) or false Note: when 'document' is specified, parameter and return wrappers are created for you automatically
  4153. * @param mixed $use optional (encoded|literal) or false
  4154. * @param string $documentation optional Description to include in WSDL
  4155. * @param string $encodingStyle optional (usually 'http://schemas.xmlsoap.org/soap/encoding/' for encoded)
  4156. * @access public
  4157. */
  4158. function register($name,$in=array(),$out=array(),$namespace=false,$soapaction=false,$style=false,$use=false,$documentation='',$encodingStyle=''){
  4159. global $HTTP_SERVER_VARS;
  4160. if($this->externalWSDLURL){
  4161. die('You cannot bind to an external WSDL file, and register methods outside of it! Please choose either WSDL or no WSDL.');
  4162. }
  4163. if (! $name) {
  4164. die('You must specify a name when you register an operation');
  4165. }
  4166. if (!is_array($in)) {
  4167. die('You must provide an array for operation inputs');
  4168. }
  4169. if (!is_array($out)) {
  4170. die('You must provide an array for operation outputs');
  4171. }
  4172. if(false == $namespace) {
  4173. }
  4174. if(false == $soapaction) {
  4175. if (isset($_SERVER)) {
  4176. $SERVER_NAME = $_SERVER['SERVER_NAME'];
  4177. $SCRIPT_NAME = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
  4178. $HTTPS = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : (isset($HTTP_SERVER_VARS['HTTPS']) ? $HTTP_SERVER_VARS['HTTPS'] : 'off');
  4179. } elseif (isset($HTTP_SERVER_VARS)) {
  4180. $SERVER_NAME = $HTTP_SERVER_VARS['SERVER_NAME'];
  4181. $SCRIPT_NAME = isset($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : $HTTP_SERVER_VARS['SCRIPT_NAME'];
  4182. $HTTPS = isset($HTTP_SERVER_VARS['HTTPS']) ? $HTTP_SERVER_VARS['HTTPS'] : 'off';
  4183. } else {
  4184. $this->setError("Neither _SERVER nor HTTP_SERVER_VARS is available");
  4185. }
  4186. if ($HTTPS == '1' || $HTTPS == 'on') {
  4187. $SCHEME = 'https';
  4188. } else {
  4189. $SCHEME = 'http';
  4190. }
  4191. $soapaction = "$SCHEME://$SERVER_NAME$SCRIPT_NAME/$name";
  4192. }
  4193. if(false == $style) {
  4194. $style = "rpc";
  4195. }
  4196. if(false == $use) {
  4197. $use = "encoded";
  4198. }
  4199. if ($use == 'encoded' && $encodingStyle == '') {
  4200. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  4201. }
  4202. $this->operations[$name] = array(
  4203. 'name' => $name,
  4204. 'in' => $in,
  4205. 'out' => $out,
  4206. 'namespace' => $namespace,
  4207. 'soapaction' => $soapaction,
  4208. 'style' => $style);
  4209. if($this->wsdl){
  4210. $this->wsdl->addOperation($name,$in,$out,$namespace,$soapaction,$style,$use,$documentation,$encodingStyle);
  4211. }
  4212. return true;
  4213. }
  4214. /**
  4215. * Specify a fault to be returned to the client.
  4216. * This also acts as a flag to the server that a fault has occured.
  4217. *
  4218. * @param string $faultcode
  4219. * @param string $faultstring
  4220. * @param string $faultactor
  4221. * @param string $faultdetail
  4222. * @access public
  4223. */
  4224. function fault($faultcode,$faultstring,$faultactor='',$faultdetail=''){
  4225. if ($faultdetail == '' && $this->debug_flag) {
  4226. $faultdetail = $this->getDebug();
  4227. }
  4228. $this->fault = new nusoap_fault($faultcode,$faultactor,$faultstring,$faultdetail);
  4229. $this->fault->soap_defencoding = $this->soap_defencoding;
  4230. }
  4231. /**
  4232. * Sets up wsdl object.
  4233. * Acts as a flag to enable internal WSDL generation
  4234. *
  4235. * @param string $serviceName, name of the service
  4236. * @param mixed $namespace optional 'tns' service namespace or false
  4237. * @param mixed $endpoint optional URL of service endpoint or false
  4238. * @param string $style optional (rpc|document) WSDL style (also specified by operation)
  4239. * @param string $transport optional SOAP transport
  4240. * @param mixed $schemaTargetNamespace optional 'types' targetNamespace for service schema or false
  4241. */
  4242. function configureWSDL($serviceName,$namespace = false,$endpoint = false,$style='rpc', $transport = 'http://schemas.xmlsoap.org/soap/http', $schemaTargetNamespace = false)
  4243. {
  4244. global $HTTP_SERVER_VARS;
  4245. if (isset($_SERVER)) {
  4246. $SERVER_NAME = $_SERVER['SERVER_NAME'];
  4247. $SERVER_PORT = $_SERVER['SERVER_PORT'];
  4248. $SCRIPT_NAME = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
  4249. $HTTPS = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : (isset($HTTP_SERVER_VARS['HTTPS']) ? $HTTP_SERVER_VARS['HTTPS'] : 'off');
  4250. } elseif (isset($HTTP_SERVER_VARS)) {
  4251. $SERVER_NAME = $HTTP_SERVER_VARS['SERVER_NAME'];
  4252. $SERVER_PORT = $HTTP_SERVER_VARS['SERVER_PORT'];
  4253. $SCRIPT_NAME = isset($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : $HTTP_SERVER_VARS['SCRIPT_NAME'];
  4254. $HTTPS = isset($HTTP_SERVER_VARS['HTTPS']) ? $HTTP_SERVER_VARS['HTTPS'] : 'off';
  4255. } else {
  4256. $this->setError("Neither _SERVER nor HTTP_SERVER_VARS is available");
  4257. }
  4258. // If server name has port number attached then strip it (else port number gets duplicated in WSDL output) (occurred using lighttpd and FastCGI)
  4259. $colon = strpos($SERVER_NAME,":");
  4260. if ($colon) {
  4261. $SERVER_NAME = substr($SERVER_NAME, 0, $colon);
  4262. }
  4263. if ($SERVER_PORT == 80) {
  4264. $SERVER_PORT = '';
  4265. } else {
  4266. $SERVER_PORT = ':' . $SERVER_PORT;
  4267. }
  4268. if(false == $namespace) {
  4269. $namespace = "http://$SERVER_NAME/soap/$serviceName";
  4270. }
  4271. if(false == $endpoint) {
  4272. if ($HTTPS == '1' || $HTTPS == 'on') {
  4273. $SCHEME = 'https';
  4274. } else {
  4275. $SCHEME = 'http';
  4276. }
  4277. $endpoint = "$SCHEME://$SERVER_NAME$SERVER_PORT$SCRIPT_NAME";
  4278. }
  4279. if(false == $schemaTargetNamespace) {
  4280. $schemaTargetNamespace = $namespace;
  4281. }
  4282. $this->wsdl = new wsdl;
  4283. $this->wsdl->serviceName = $serviceName;
  4284. $this->wsdl->endpoint = $endpoint;
  4285. $this->wsdl->namespaces['tns'] = $namespace;
  4286. $this->wsdl->namespaces['soap'] = 'http://schemas.xmlsoap.org/wsdl/soap/';
  4287. $this->wsdl->namespaces['wsdl'] = 'http://schemas.xmlsoap.org/wsdl/';
  4288. if ($schemaTargetNamespace != $namespace) {
  4289. $this->wsdl->namespaces['types'] = $schemaTargetNamespace;
  4290. }
  4291. $this->wsdl->schemas[$schemaTargetNamespace][0] = new nusoap_xmlschema('', '', $this->wsdl->namespaces);
  4292. if ($style == 'document') {
  4293. $this->wsdl->schemas[$schemaTargetNamespace][0]->schemaInfo['elementFormDefault'] = 'qualified';
  4294. }
  4295. $this->wsdl->schemas[$schemaTargetNamespace][0]->schemaTargetNamespace = $schemaTargetNamespace;
  4296. $this->wsdl->schemas[$schemaTargetNamespace][0]->imports['http://schemas.xmlsoap.org/soap/encoding/'][0] = array('location' => '', 'loaded' => true);
  4297. $this->wsdl->schemas[$schemaTargetNamespace][0]->imports['http://schemas.xmlsoap.org/wsdl/'][0] = array('location' => '', 'loaded' => true);
  4298. $this->wsdl->bindings[$serviceName.'Binding'] = array(
  4299. 'name'=>$serviceName.'Binding',
  4300. 'style'=>$style,
  4301. 'transport'=>$transport,
  4302. 'portType'=>$serviceName.'PortType');
  4303. $this->wsdl->ports[$serviceName.'Port'] = array(
  4304. 'binding'=>$serviceName.'Binding',
  4305. 'location'=>$endpoint,
  4306. 'bindingType'=>'http://schemas.xmlsoap.org/wsdl/soap/');
  4307. }
  4308. }
  4309. /**
  4310. * Backward compatibility
  4311. */
  4312. class soap_server extends nusoap_server {
  4313. }
  4314. ?><?php
  4315. /**
  4316. * parses a WSDL file, allows access to it's data, other utility methods.
  4317. * also builds WSDL structures programmatically.
  4318. *
  4319. * @author Dietrich Ayala <dietrich@ganx4.com>
  4320. * @author Scott Nichol <snichol@users.sourceforge.net>
  4321. * @access public
  4322. */
  4323. class wsdl extends nusoap_base {
  4324. // URL or filename of the root of this WSDL
  4325. var $wsdl;
  4326. // define internal arrays of bindings, ports, operations, messages, etc.
  4327. var $schemas = array();
  4328. var $currentSchema;
  4329. var $message = array();
  4330. var $complexTypes = array();
  4331. var $messages = array();
  4332. var $currentMessage;
  4333. var $currentOperation;
  4334. var $portTypes = array();
  4335. var $currentPortType;
  4336. var $bindings = array();
  4337. var $currentBinding;
  4338. var $ports = array();
  4339. var $currentPort;
  4340. var $opData = array();
  4341. var $status = '';
  4342. var $documentation = false;
  4343. var $endpoint = '';
  4344. // array of wsdl docs to import
  4345. var $import = array();
  4346. // parser vars
  4347. var $parser;
  4348. var $position = 0;
  4349. var $depth = 0;
  4350. var $depth_array = array();
  4351. // for getting wsdl
  4352. var $proxyhost = '';
  4353. var $proxyport = '';
  4354. var $proxyusername = '';
  4355. var $proxypassword = '';
  4356. var $timeout = 0;
  4357. var $response_timeout = 30;
  4358. var $curl_options = array(); // User-specified cURL options
  4359. var $use_curl = false; // whether to always try to use cURL
  4360. // for HTTP authentication
  4361. var $username = ''; // Username for HTTP authentication
  4362. var $password = ''; // Password for HTTP authentication
  4363. var $authtype = ''; // Type of HTTP authentication
  4364. var $certRequest = array(); // Certificate for HTTP SSL authentication
  4365. /**
  4366. * constructor
  4367. *
  4368. * @param string $wsdl WSDL document URL
  4369. * @param string $proxyhost
  4370. * @param string $proxyport
  4371. * @param string $proxyusername
  4372. * @param string $proxypassword
  4373. * @param integer $timeout set the connection timeout
  4374. * @param integer $response_timeout set the response timeout
  4375. * @param array $curl_options user-specified cURL options
  4376. * @param boolean $use_curl try to use cURL
  4377. * @access public
  4378. */
  4379. function wsdl($wsdl = '',$proxyhost=false,$proxyport=false,$proxyusername=false,$proxypassword=false,$timeout=0,$response_timeout=30,$curl_options=null,$use_curl=false){
  4380. parent::nusoap_base();
  4381. $this->debug("ctor wsdl=$wsdl timeout=$timeout response_timeout=$response_timeout");
  4382. $this->proxyhost = $proxyhost;
  4383. $this->proxyport = $proxyport;
  4384. $this->proxyusername = $proxyusername;
  4385. $this->proxypassword = $proxypassword;
  4386. $this->timeout = $timeout;
  4387. $this->response_timeout = $response_timeout;
  4388. if (is_array($curl_options))
  4389. $this->curl_options = $curl_options;
  4390. $this->use_curl = $use_curl;
  4391. $this->fetchWSDL($wsdl);
  4392. }
  4393. /**
  4394. * fetches the WSDL document and parses it
  4395. *
  4396. * @access public
  4397. */
  4398. function fetchWSDL($wsdl) {
  4399. $this->debug("parse and process WSDL path=$wsdl");
  4400. $this->wsdl = $wsdl;
  4401. // parse wsdl file
  4402. if ($this->wsdl != "") {
  4403. $this->parseWSDL($this->wsdl);
  4404. }
  4405. // imports
  4406. // TODO: handle imports more properly, grabbing them in-line and nesting them
  4407. $imported_urls = array();
  4408. $imported = 1;
  4409. while ($imported > 0) {
  4410. $imported = 0;
  4411. // Schema imports
  4412. foreach ($this->schemas as $ns => $list) {
  4413. foreach ($list as $xs) {
  4414. $wsdlparts = parse_url($this->wsdl); // this is bogusly simple!
  4415. foreach ($xs->imports as $ns2 => $list2) {
  4416. for ($ii = 0; $ii < count($list2); $ii++) {
  4417. if (! $list2[$ii]['loaded']) {
  4418. $this->schemas[$ns]->imports[$ns2][$ii]['loaded'] = true;
  4419. $url = $list2[$ii]['location'];
  4420. if ($url != '') {
  4421. $urlparts = parse_url($url);
  4422. if (!isset($urlparts['host'])) {
  4423. $url = $wsdlparts['scheme'] . '://' . $wsdlparts['host'] . (isset($wsdlparts['port']) ? ':' .$wsdlparts['port'] : '') .
  4424. substr($wsdlparts['path'],0,strrpos($wsdlparts['path'],'/') + 1) .$urlparts['path'];
  4425. }
  4426. if (! in_array($url, $imported_urls)) {
  4427. $this->parseWSDL($url);
  4428. $imported++;
  4429. $imported_urls[] = $url;
  4430. }
  4431. } else {
  4432. $this->debug("Unexpected scenario: empty URL for unloaded import");
  4433. }
  4434. }
  4435. }
  4436. }
  4437. }
  4438. }
  4439. // WSDL imports
  4440. $wsdlparts = parse_url($this->wsdl); // this is bogusly simple!
  4441. foreach ($this->import as $ns => $list) {
  4442. for ($ii = 0; $ii < count($list); $ii++) {
  4443. if (! $list[$ii]['loaded']) {
  4444. $this->import[$ns][$ii]['loaded'] = true;
  4445. $url = $list[$ii]['location'];
  4446. if ($url != '') {
  4447. $urlparts = parse_url($url);
  4448. if (!isset($urlparts['host'])) {
  4449. $url = $wsdlparts['scheme'] . '://' . $wsdlparts['host'] . (isset($wsdlparts['port']) ? ':' . $wsdlparts['port'] : '') .
  4450. substr($wsdlparts['path'],0,strrpos($wsdlparts['path'],'/') + 1) .$urlparts['path'];
  4451. }
  4452. if (! in_array($url, $imported_urls)) {
  4453. $this->parseWSDL($url);
  4454. $imported++;
  4455. $imported_urls[] = $url;
  4456. }
  4457. } else {
  4458. $this->debug("Unexpected scenario: empty URL for unloaded import");
  4459. }
  4460. }
  4461. }
  4462. }
  4463. }
  4464. // add new data to operation data
  4465. foreach($this->bindings as $binding => $bindingData) {
  4466. if (isset($bindingData['operations']) && is_array($bindingData['operations'])) {
  4467. foreach($bindingData['operations'] as $operation => $data) {
  4468. $this->debug('post-parse data gathering for ' . $operation);
  4469. $this->bindings[$binding]['operations'][$operation]['input'] =
  4470. isset($this->bindings[$binding]['operations'][$operation]['input']) ?
  4471. array_merge($this->bindings[$binding]['operations'][$operation]['input'], $this->portTypes[ $bindingData['portType'] ][$operation]['input']) :
  4472. $this->portTypes[ $bindingData['portType'] ][$operation]['input'];
  4473. $this->bindings[$binding]['operations'][$operation]['output'] =
  4474. isset($this->bindings[$binding]['operations'][$operation]['output']) ?
  4475. array_merge($this->bindings[$binding]['operations'][$operation]['output'], $this->portTypes[ $bindingData['portType'] ][$operation]['output']) :
  4476. $this->portTypes[ $bindingData['portType'] ][$operation]['output'];
  4477. if(isset($this->messages[ $this->bindings[$binding]['operations'][$operation]['input']['message'] ])){
  4478. $this->bindings[$binding]['operations'][$operation]['input']['parts'] = $this->messages[ $this->bindings[$binding]['operations'][$operation]['input']['message'] ];
  4479. }
  4480. if(isset($this->messages[ $this->bindings[$binding]['operations'][$operation]['output']['message'] ])){
  4481. $this->bindings[$binding]['operations'][$operation]['output']['parts'] = $this->messages[ $this->bindings[$binding]['operations'][$operation]['output']['message'] ];
  4482. }
  4483. // Set operation style if necessary, but do not override one already provided
  4484. if (isset($bindingData['style']) && !isset($this->bindings[$binding]['operations'][$operation]['style'])) {
  4485. $this->bindings[$binding]['operations'][$operation]['style'] = $bindingData['style'];
  4486. }
  4487. $this->bindings[$binding]['operations'][$operation]['transport'] = isset($bindingData['transport']) ? $bindingData['transport'] : '';
  4488. $this->bindings[$binding]['operations'][$operation]['documentation'] = isset($this->portTypes[ $bindingData['portType'] ][$operation]['documentation']) ? $this->portTypes[ $bindingData['portType'] ][$operation]['documentation'] : '';
  4489. $this->bindings[$binding]['operations'][$operation]['endpoint'] = isset($bindingData['endpoint']) ? $bindingData['endpoint'] : '';
  4490. }
  4491. }
  4492. }
  4493. }
  4494. /**
  4495. * parses the wsdl document
  4496. *
  4497. * @param string $wsdl path or URL
  4498. * @access private
  4499. */
  4500. function parseWSDL($wsdl = '') {
  4501. $this->debug("parse WSDL at path=$wsdl");
  4502. if ($wsdl == '') {
  4503. $this->debug('no wsdl passed to parseWSDL()!!');
  4504. $this->setError('no wsdl passed to parseWSDL()!!');
  4505. return false;
  4506. }
  4507. // parse $wsdl for url format
  4508. $wsdl_props = parse_url($wsdl);
  4509. if (isset($wsdl_props['scheme']) && ($wsdl_props['scheme'] == 'http' || $wsdl_props['scheme'] == 'https')) {
  4510. $this->debug('getting WSDL http(s) URL ' . $wsdl);
  4511. // get wsdl
  4512. $tr = new soap_transport_http($wsdl, $this->curl_options, $this->use_curl);
  4513. $tr->request_method = 'GET';
  4514. $tr->useSOAPAction = false;
  4515. if($this->proxyhost && $this->proxyport){
  4516. $tr->setProxy($this->proxyhost,$this->proxyport,$this->proxyusername,$this->proxypassword);
  4517. }
  4518. if ($this->authtype != '') {
  4519. $tr->setCredentials($this->username, $this->password, $this->authtype, array(), $this->certRequest);
  4520. }
  4521. $tr->setEncoding('gzip, deflate');
  4522. $wsdl_string = $tr->send('', $this->timeout, $this->response_timeout);
  4523. //$this->debug("WSDL request\n" . $tr->outgoing_payload);
  4524. //$this->debug("WSDL response\n" . $tr->incoming_payload);
  4525. $this->appendDebug($tr->getDebug());
  4526. // catch errors
  4527. if($err = $tr->getError() ){
  4528. $errstr = 'Getting ' . $wsdl . ' - HTTP ERROR: '.$err;
  4529. $this->debug($errstr);
  4530. $this->setError($errstr);
  4531. unset($tr);
  4532. return false;
  4533. }
  4534. unset($tr);
  4535. $this->debug("got WSDL URL");
  4536. } else {
  4537. // $wsdl is not http(s), so treat it as a file URL or plain file path
  4538. if (isset($wsdl_props['scheme']) && ($wsdl_props['scheme'] == 'file') && isset($wsdl_props['path'])) {
  4539. $path = isset($wsdl_props['host']) ? ($wsdl_props['host'] . ':' . $wsdl_props['path']) : $wsdl_props['path'];
  4540. } else {
  4541. $path = $wsdl;
  4542. }
  4543. $this->debug('getting WSDL file ' . $path);
  4544. if ($fp = @fopen($path, 'r')) {
  4545. $wsdl_string = '';
  4546. while ($data = fread($fp, 32768)) {
  4547. $wsdl_string .= $data;
  4548. }
  4549. fclose($fp);
  4550. } else {
  4551. $errstr = "Bad path to WSDL file $path";
  4552. $this->debug($errstr);
  4553. $this->setError($errstr);
  4554. return false;
  4555. }
  4556. }
  4557. $this->debug('Parse WSDL');
  4558. // end new code added
  4559. // Create an XML parser.
  4560. $this->parser = xml_parser_create();
  4561. // Set the options for parsing the XML data.
  4562. // xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  4563. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
  4564. // Set the object for the parser.
  4565. xml_set_object($this->parser, $this);
  4566. // Set the element handlers for the parser.
  4567. xml_set_element_handler($this->parser, 'start_element', 'end_element');
  4568. xml_set_character_data_handler($this->parser, 'character_data');
  4569. // Parse the XML file.
  4570. if (!xml_parse($this->parser, $wsdl_string, true)) {
  4571. // Display an error message.
  4572. $errstr = sprintf(
  4573. 'XML error parsing WSDL from %s on line %d: %s',
  4574. $wsdl,
  4575. xml_get_current_line_number($this->parser),
  4576. xml_error_string(xml_get_error_code($this->parser))
  4577. );
  4578. $this->debug($errstr);
  4579. $this->debug("XML payload:\n" . $wsdl_string);
  4580. $this->setError($errstr);
  4581. return false;
  4582. }
  4583. // free the parser
  4584. xml_parser_free($this->parser);
  4585. $this->debug('Parsing WSDL done');
  4586. // catch wsdl parse errors
  4587. if($this->getError()){
  4588. return false;
  4589. }
  4590. return true;
  4591. }
  4592. /**
  4593. * start-element handler
  4594. *
  4595. * @param string $parser XML parser object
  4596. * @param string $name element name
  4597. * @param string $attrs associative array of attributes
  4598. * @access private
  4599. */
  4600. function start_element($parser, $name, $attrs)
  4601. {
  4602. if ($this->status == 'schema') {
  4603. $this->currentSchema->schemaStartElement($parser, $name, $attrs);
  4604. $this->appendDebug($this->currentSchema->getDebug());
  4605. $this->currentSchema->clearDebug();
  4606. } elseif (preg_match('/schema$/', $name)) {
  4607. $this->debug('Parsing WSDL schema');
  4608. // $this->debug("startElement for $name ($attrs[name]). status = $this->status (".$this->getLocalPart($name).")");
  4609. $this->status = 'schema';
  4610. $this->currentSchema = new nusoap_xmlschema('', '', $this->namespaces);
  4611. $this->currentSchema->schemaStartElement($parser, $name, $attrs);
  4612. $this->appendDebug($this->currentSchema->getDebug());
  4613. $this->currentSchema->clearDebug();
  4614. } else {
  4615. // position in the total number of elements, starting from 0
  4616. $pos = $this->position++;
  4617. $depth = $this->depth++;
  4618. // set self as current value for this depth
  4619. $this->depth_array[$depth] = $pos;
  4620. $this->message[$pos] = array('cdata' => '');
  4621. // process attributes
  4622. if (count($attrs) > 0) {
  4623. // register namespace declarations
  4624. foreach($attrs as $k => $v) {
  4625. if (preg_match('/^xmlns/',$k)) {
  4626. if ($ns_prefix = substr(strrchr($k, ':'), 1)) {
  4627. $this->namespaces[$ns_prefix] = $v;
  4628. } else {
  4629. $this->namespaces['ns' . (count($this->namespaces) + 1)] = $v;
  4630. }
  4631. if ($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema') {
  4632. $this->XMLSchemaVersion = $v;
  4633. $this->namespaces['xsi'] = $v . '-instance';
  4634. }
  4635. }
  4636. }
  4637. // expand each attribute prefix to its namespace
  4638. foreach($attrs as $k => $v) {
  4639. $k = strpos($k, ':') ? $this->expandQname($k) : $k;
  4640. if ($k != 'location' && $k != 'soapAction' && $k != 'namespace') {
  4641. $v = strpos($v, ':') ? $this->expandQname($v) : $v;
  4642. }
  4643. $eAttrs[$k] = $v;
  4644. }
  4645. $attrs = $eAttrs;
  4646. } else {
  4647. $attrs = array();
  4648. }
  4649. // get element prefix, namespace and name
  4650. if (preg_match('/:/', $name)) {
  4651. // get ns prefix
  4652. $prefix = substr($name, 0, strpos($name, ':'));
  4653. // get ns
  4654. $namespace = isset($this->namespaces[$prefix]) ? $this->namespaces[$prefix] : '';
  4655. // get unqualified name
  4656. $name = substr(strstr($name, ':'), 1);
  4657. }
  4658. // process attributes, expanding any prefixes to namespaces
  4659. // find status, register data
  4660. switch ($this->status) {
  4661. case 'message':
  4662. if ($name == 'part') {
  4663. if (isset($attrs['type'])) {
  4664. $this->debug("msg " . $this->currentMessage . ": found part (with type) $attrs[name]: " . implode(',', $attrs));
  4665. $this->messages[$this->currentMessage][$attrs['name']] = $attrs['type'];
  4666. }
  4667. if (isset($attrs['element'])) {
  4668. $this->debug("msg " . $this->currentMessage . ": found part (with element) $attrs[name]: " . implode(',', $attrs));
  4669. $this->messages[$this->currentMessage][$attrs['name']] = $attrs['element'] . '^';
  4670. }
  4671. }
  4672. break;
  4673. case 'portType':
  4674. switch ($name) {
  4675. case 'operation':
  4676. $this->currentPortOperation = $attrs['name'];
  4677. $this->debug("portType $this->currentPortType operation: $this->currentPortOperation");
  4678. if (isset($attrs['parameterOrder'])) {
  4679. $this->portTypes[$this->currentPortType][$attrs['name']]['parameterOrder'] = $attrs['parameterOrder'];
  4680. }
  4681. break;
  4682. case 'documentation':
  4683. $this->documentation = true;
  4684. break;
  4685. // merge input/output data
  4686. default:
  4687. $m = isset($attrs['message']) ? $this->getLocalPart($attrs['message']) : '';
  4688. $this->portTypes[$this->currentPortType][$this->currentPortOperation][$name]['message'] = $m;
  4689. break;
  4690. }
  4691. break;
  4692. case 'binding':
  4693. switch ($name) {
  4694. case 'binding':
  4695. // get ns prefix
  4696. if (isset($attrs['style'])) {
  4697. $this->bindings[$this->currentBinding]['prefix'] = $prefix;
  4698. }
  4699. $this->bindings[$this->currentBinding] = array_merge($this->bindings[$this->currentBinding], $attrs);
  4700. break;
  4701. case 'header':
  4702. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus]['headers'][] = $attrs;
  4703. break;
  4704. case 'operation':
  4705. if (isset($attrs['soapAction'])) {
  4706. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['soapAction'] = $attrs['soapAction'];
  4707. }
  4708. if (isset($attrs['style'])) {
  4709. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['style'] = $attrs['style'];
  4710. }
  4711. if (isset($attrs['name'])) {
  4712. $this->currentOperation = $attrs['name'];
  4713. $this->debug("current binding operation: $this->currentOperation");
  4714. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['name'] = $attrs['name'];
  4715. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['binding'] = $this->currentBinding;
  4716. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['endpoint'] = isset($this->bindings[$this->currentBinding]['endpoint']) ? $this->bindings[$this->currentBinding]['endpoint'] : '';
  4717. }
  4718. break;
  4719. case 'input':
  4720. $this->opStatus = 'input';
  4721. break;
  4722. case 'output':
  4723. $this->opStatus = 'output';
  4724. break;
  4725. case 'body':
  4726. if (isset($this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus])) {
  4727. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus] = array_merge($this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus], $attrs);
  4728. } else {
  4729. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus] = $attrs;
  4730. }
  4731. break;
  4732. }
  4733. break;
  4734. case 'service':
  4735. switch ($name) {
  4736. case 'port':
  4737. $this->currentPort = $attrs['name'];
  4738. $this->debug('current port: ' . $this->currentPort);
  4739. $this->ports[$this->currentPort]['binding'] = $this->getLocalPart($attrs['binding']);
  4740. break;
  4741. case 'address':
  4742. $this->ports[$this->currentPort]['location'] = $attrs['location'];
  4743. $this->ports[$this->currentPort]['bindingType'] = $namespace;
  4744. $this->bindings[ $this->ports[$this->currentPort]['binding'] ]['bindingType'] = $namespace;
  4745. $this->bindings[ $this->ports[$this->currentPort]['binding'] ]['endpoint'] = $attrs['location'];
  4746. break;
  4747. }
  4748. break;
  4749. }
  4750. // set status
  4751. switch ($name) {
  4752. case 'import':
  4753. if (isset($attrs['location'])) {
  4754. $this->import[$attrs['namespace']][] = array('location' => $attrs['location'], 'loaded' => false);
  4755. $this->debug('parsing import ' . $attrs['namespace']. ' - ' . $attrs['location'] . ' (' . count($this->import[$attrs['namespace']]).')');
  4756. } else {
  4757. $this->import[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
  4758. if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
  4759. $this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
  4760. }
  4761. $this->debug('parsing import ' . $attrs['namespace']. ' - [no location] (' . count($this->import[$attrs['namespace']]).')');
  4762. }
  4763. break;
  4764. //wait for schema
  4765. //case 'types':
  4766. // $this->status = 'schema';
  4767. // break;
  4768. case 'message':
  4769. $this->status = 'message';
  4770. $this->messages[$attrs['name']] = array();
  4771. $this->currentMessage = $attrs['name'];
  4772. break;
  4773. case 'portType':
  4774. $this->status = 'portType';
  4775. $this->portTypes[$attrs['name']] = array();
  4776. $this->currentPortType = $attrs['name'];
  4777. break;
  4778. case "binding":
  4779. if (isset($attrs['name'])) {
  4780. // get binding name
  4781. if (strpos($attrs['name'], ':')) {
  4782. $this->currentBinding = $this->getLocalPart($attrs['name']);
  4783. } else {
  4784. $this->currentBinding = $attrs['name'];
  4785. }
  4786. $this->status = 'binding';
  4787. $this->bindings[$this->currentBinding]['portType'] = $this->getLocalPart($attrs['type']);
  4788. $this->debug("current binding: $this->currentBinding of portType: " . $attrs['type']);
  4789. }
  4790. break;
  4791. case 'service':
  4792. $this->serviceName = $attrs['name'];
  4793. $this->status = 'service';
  4794. $this->debug('current service: ' . $this->serviceName);
  4795. break;
  4796. case 'definitions':
  4797. foreach ($attrs as $name => $value) {
  4798. $this->wsdl_info[$name] = $value;
  4799. }
  4800. break;
  4801. }
  4802. }
  4803. }
  4804. /**
  4805. * end-element handler
  4806. *
  4807. * @param string $parser XML parser object
  4808. * @param string $name element name
  4809. * @access private
  4810. */
  4811. function end_element($parser, $name){
  4812. // unset schema status
  4813. if (/*preg_match('/types$/', $name) ||*/ preg_match('/schema$/', $name)) {
  4814. $this->status = "";
  4815. $this->appendDebug($this->currentSchema->getDebug());
  4816. $this->currentSchema->clearDebug();
  4817. $this->schemas[$this->currentSchema->schemaTargetNamespace][] = $this->currentSchema;
  4818. $this->debug('Parsing WSDL schema done');
  4819. }
  4820. if ($this->status == 'schema') {
  4821. $this->currentSchema->schemaEndElement($parser, $name);
  4822. } else {
  4823. // bring depth down a notch
  4824. $this->depth--;
  4825. }
  4826. // end documentation
  4827. if ($this->documentation) {
  4828. //TODO: track the node to which documentation should be assigned; it can be a part, message, etc.
  4829. //$this->portTypes[$this->currentPortType][$this->currentPortOperation]['documentation'] = $this->documentation;
  4830. $this->documentation = false;
  4831. }
  4832. }
  4833. /**
  4834. * element content handler
  4835. *
  4836. * @param string $parser XML parser object
  4837. * @param string $data element content
  4838. * @access private
  4839. */
  4840. function character_data($parser, $data)
  4841. {
  4842. $pos = isset($this->depth_array[$this->depth]) ? $this->depth_array[$this->depth] : 0;
  4843. if (isset($this->message[$pos]['cdata'])) {
  4844. $this->message[$pos]['cdata'] .= $data;
  4845. }
  4846. if ($this->documentation) {
  4847. $this->documentation .= $data;
  4848. }
  4849. }
  4850. /**
  4851. * if authenticating, set user credentials here
  4852. *
  4853. * @param string $username
  4854. * @param string $password
  4855. * @param string $authtype (basic|digest|certificate|ntlm)
  4856. * @param array $certRequest (keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, certpassword (optional), verifypeer (optional), verifyhost (optional): see corresponding options in cURL docs)
  4857. * @access public
  4858. */
  4859. function setCredentials($username, $password, $authtype = 'basic', $certRequest = array()) {
  4860. $this->debug("setCredentials username=$username authtype=$authtype certRequest=");
  4861. $this->appendDebug($this->varDump($certRequest));
  4862. $this->username = $username;
  4863. $this->password = $password;
  4864. $this->authtype = $authtype;
  4865. $this->certRequest = $certRequest;
  4866. }
  4867. function getBindingData($binding)
  4868. {
  4869. if (is_array($this->bindings[$binding])) {
  4870. return $this->bindings[$binding];
  4871. }
  4872. }
  4873. /**
  4874. * returns an assoc array of operation names => operation data
  4875. *
  4876. * @param string $portName WSDL port name
  4877. * @param string $bindingType eg: soap, smtp, dime (only soap and soap12 are currently supported)
  4878. * @return array
  4879. * @access public
  4880. */
  4881. function getOperations($portName = '', $bindingType = 'soap') {
  4882. $ops = array();
  4883. if ($bindingType == 'soap') {
  4884. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  4885. } elseif ($bindingType == 'soap12') {
  4886. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  4887. } else {
  4888. $this->debug("getOperations bindingType $bindingType may not be supported");
  4889. }
  4890. $this->debug("getOperations for port '$portName' bindingType $bindingType");
  4891. // loop thru ports
  4892. foreach($this->ports as $port => $portData) {
  4893. $this->debug("getOperations checking port $port bindingType " . $portData['bindingType']);
  4894. if ($portName == '' || $port == $portName) {
  4895. // binding type of port matches parameter
  4896. if ($portData['bindingType'] == $bindingType) {
  4897. $this->debug("getOperations found port $port bindingType $bindingType");
  4898. //$this->debug("port data: " . $this->varDump($portData));
  4899. //$this->debug("bindings: " . $this->varDump($this->bindings[ $portData['binding'] ]));
  4900. // merge bindings
  4901. if (isset($this->bindings[ $portData['binding'] ]['operations'])) {
  4902. $ops = array_merge ($ops, $this->bindings[ $portData['binding'] ]['operations']);
  4903. }
  4904. }
  4905. }
  4906. }
  4907. if (count($ops) == 0) {
  4908. $this->debug("getOperations found no operations for port '$portName' bindingType $bindingType");
  4909. }
  4910. return $ops;
  4911. }
  4912. /**
  4913. * returns an associative array of data necessary for calling an operation
  4914. *
  4915. * @param string $operation name of operation
  4916. * @param string $bindingType type of binding eg: soap, soap12
  4917. * @return array
  4918. * @access public
  4919. */
  4920. function getOperationData($operation, $bindingType = 'soap')
  4921. {
  4922. if ($bindingType == 'soap') {
  4923. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  4924. } elseif ($bindingType == 'soap12') {
  4925. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  4926. }
  4927. // loop thru ports
  4928. foreach($this->ports as $port => $portData) {
  4929. // binding type of port matches parameter
  4930. if ($portData['bindingType'] == $bindingType) {
  4931. // get binding
  4932. //foreach($this->bindings[ $portData['binding'] ]['operations'] as $bOperation => $opData) {
  4933. foreach(array_keys($this->bindings[ $portData['binding'] ]['operations']) as $bOperation) {
  4934. // note that we could/should also check the namespace here
  4935. if ($operation == $bOperation) {
  4936. $opData = $this->bindings[ $portData['binding'] ]['operations'][$operation];
  4937. return $opData;
  4938. }
  4939. }
  4940. }
  4941. }
  4942. }
  4943. /**
  4944. * returns an associative array of data necessary for calling an operation
  4945. *
  4946. * @param string $soapAction soapAction for operation
  4947. * @param string $bindingType type of binding eg: soap, soap12
  4948. * @return array
  4949. * @access public
  4950. */
  4951. function getOperationDataForSoapAction($soapAction, $bindingType = 'soap') {
  4952. if ($bindingType == 'soap') {
  4953. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  4954. } elseif ($bindingType == 'soap12') {
  4955. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  4956. }
  4957. // loop thru ports
  4958. foreach($this->ports as $port => $portData) {
  4959. // binding type of port matches parameter
  4960. if ($portData['bindingType'] == $bindingType) {
  4961. // loop through operations for the binding
  4962. foreach ($this->bindings[ $portData['binding'] ]['operations'] as $bOperation => $opData) {
  4963. if ($opData['soapAction'] == $soapAction) {
  4964. return $opData;
  4965. }
  4966. }
  4967. }
  4968. }
  4969. }
  4970. /**
  4971. * returns an array of information about a given type
  4972. * returns false if no type exists by the given name
  4973. *
  4974. * typeDef = array(
  4975. * 'elements' => array(), // refs to elements array
  4976. * 'restrictionBase' => '',
  4977. * 'phpType' => '',
  4978. * 'order' => '(sequence|all)',
  4979. * 'attrs' => array() // refs to attributes array
  4980. * )
  4981. *
  4982. * @param string $type the type
  4983. * @param string $ns namespace (not prefix) of the type
  4984. * @return mixed
  4985. * @access public
  4986. * @see nusoap_xmlschema
  4987. */
  4988. function getTypeDef($type, $ns) {
  4989. $this->debug("in getTypeDef: type=$type, ns=$ns");
  4990. if ((! $ns) && isset($this->namespaces['tns'])) {
  4991. $ns = $this->namespaces['tns'];
  4992. $this->debug("in getTypeDef: type namespace forced to $ns");
  4993. }
  4994. if (!isset($this->schemas[$ns])) {
  4995. foreach ($this->schemas as $ns0 => $schema0) {
  4996. if (strcasecmp($ns, $ns0) == 0) {
  4997. $this->debug("in getTypeDef: replacing schema namespace $ns with $ns0");
  4998. $ns = $ns0;
  4999. break;
  5000. }
  5001. }
  5002. }
  5003. if (isset($this->schemas[$ns])) {
  5004. $this->debug("in getTypeDef: have schema for namespace $ns");
  5005. for ($i = 0; $i < count($this->schemas[$ns]); $i++) {
  5006. $xs = &$this->schemas[$ns][$i];
  5007. $t = $xs->getTypeDef($type);
  5008. $this->appendDebug($xs->getDebug());
  5009. $xs->clearDebug();
  5010. if ($t) {
  5011. $this->debug("in getTypeDef: found type $type");
  5012. if (!isset($t['phpType'])) {
  5013. // get info for type to tack onto the element
  5014. $uqType = substr($t['type'], strrpos($t['type'], ':') + 1);
  5015. $ns = substr($t['type'], 0, strrpos($t['type'], ':'));
  5016. $etype = $this->getTypeDef($uqType, $ns);
  5017. if ($etype) {
  5018. $this->debug("found type for [element] $type:");
  5019. $this->debug($this->varDump($etype));
  5020. if (isset($etype['phpType'])) {
  5021. $t['phpType'] = $etype['phpType'];
  5022. }
  5023. if (isset($etype['elements'])) {
  5024. $t['elements'] = $etype['elements'];
  5025. }
  5026. if (isset($etype['attrs'])) {
  5027. $t['attrs'] = $etype['attrs'];
  5028. }
  5029. } else {
  5030. $this->debug("did not find type for [element] $type");
  5031. }
  5032. }
  5033. return $t;
  5034. }
  5035. }
  5036. $this->debug("in getTypeDef: did not find type $type");
  5037. } else {
  5038. $this->debug("in getTypeDef: do not have schema for namespace $ns");
  5039. }
  5040. return false;
  5041. }
  5042. /**
  5043. * prints html description of services
  5044. *
  5045. * @access private
  5046. */
  5047. function webDescription(){
  5048. global $HTTP_SERVER_VARS;
  5049. if (isset($_SERVER)) {
  5050. $PHP_SELF = $_SERVER['PHP_SELF'];
  5051. } elseif (isset($HTTP_SERVER_VARS)) {
  5052. $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
  5053. } else {
  5054. $this->setError("Neither _SERVER nor HTTP_SERVER_VARS is available");
  5055. }
  5056. $b = '
  5057. <html><head><title>NuSOAP: '.$this->serviceName.'</title>
  5058. <style type="text/css">
  5059. body { font-family: arial; color: #000000; background-color: #ffffff; margin: 0px 0px 0px 0px; }
  5060. p { font-family: arial; color: #000000; margin-top: 0px; margin-bottom: 12px; }
  5061. pre { background-color: silver; padding: 5px; font-family: Courier New; font-size: x-small; color: #000000;}
  5062. ul { margin-top: 10px; margin-left: 20px; }
  5063. li { list-style-type: none; margin-top: 10px; color: #000000; }
  5064. .content{
  5065. margin-left: 0px; padding-bottom: 2em; }
  5066. .nav {
  5067. padding-top: 10px; padding-bottom: 10px; padding-left: 15px; font-size: .70em;
  5068. margin-top: 10px; margin-left: 0px; color: #000000;
  5069. background-color: #ccccff; width: 20%; margin-left: 20px; margin-top: 20px; }
  5070. .title {
  5071. font-family: arial; font-size: 26px; color: #ffffff;
  5072. background-color: #999999; width: 100%;
  5073. margin-left: 0px; margin-right: 0px;
  5074. padding-top: 10px; padding-bottom: 10px;}
  5075. .hidden {
  5076. position: absolute; visibility: hidden; z-index: 200; left: 250px; top: 100px;
  5077. font-family: arial; overflow: hidden; width: 600;
  5078. padding: 20px; font-size: 10px; background-color: #999999;
  5079. layer-background-color:#FFFFFF; }
  5080. a,a:active { color: charcoal; font-weight: bold; }
  5081. a:visited { color: #666666; font-weight: bold; }
  5082. a:hover { color: cc3300; font-weight: bold; }
  5083. </style>
  5084. <script language="JavaScript" type="text/javascript">
  5085. <!--
  5086. // POP-UP CAPTIONS...
  5087. function lib_bwcheck(){ //Browsercheck (needed)
  5088. this.ver=navigator.appVersion
  5089. this.agent=navigator.userAgent
  5090. this.dom=document.getElementById?1:0
  5091. this.opera5=this.agent.indexOf("Opera 5")>-1
  5092. this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
  5093. this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  5094. this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  5095. this.ie=this.ie4||this.ie5||this.ie6
  5096. this.mac=this.agent.indexOf("Mac")>-1
  5097. this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
  5098. this.ns4=(document.layers && !this.dom)?1:0;
  5099. this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
  5100. return this
  5101. }
  5102. var bw = new lib_bwcheck()
  5103. //Makes crossbrowser object.
  5104. function makeObj(obj){
  5105. this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj]:0;
  5106. if(!this.evnt) return false
  5107. this.css=bw.dom||bw.ie4?this.evnt.style:bw.ns4?this.evnt:0;
  5108. this.wref=bw.dom||bw.ie4?this.evnt:bw.ns4?this.css.document:0;
  5109. this.writeIt=b_writeIt;
  5110. return this
  5111. }
  5112. // A unit of measure that will be added when setting the position of a layer.
  5113. //var px = bw.ns4||window.opera?"":"px";
  5114. function b_writeIt(text){
  5115. if (bw.ns4){this.wref.write(text);this.wref.close()}
  5116. else this.wref.innerHTML = text
  5117. }
  5118. //Shows the messages
  5119. var oDesc;
  5120. function popup(divid){
  5121. if(oDesc = new makeObj(divid)){
  5122. oDesc.css.visibility = "visible"
  5123. }
  5124. }
  5125. function popout(){ // Hides message
  5126. if(oDesc) oDesc.css.visibility = "hidden"
  5127. }
  5128. //-->
  5129. </script>
  5130. </head>
  5131. <body>
  5132. <div class=content>
  5133. <br><br>
  5134. <div class=title>'.$this->serviceName.'</div>
  5135. <div class=nav>
  5136. <p>View the <a href="'.$PHP_SELF.'?wsdl">WSDL</a> for the service.
  5137. Click on an operation name to view it&apos;s details.</p>
  5138. <ul>';
  5139. foreach($this->getOperations() as $op => $data){
  5140. $b .= "<li><a href='#' onclick=\"popout();popup('$op')\">$op</a></li>";
  5141. // create hidden div
  5142. $b .= "<div id='$op' class='hidden'>
  5143. <a href='#' onclick='popout()'><font color='#ffffff'>Close</font></a><br><br>";
  5144. foreach($data as $donnie => $marie){ // loop through opdata
  5145. if($donnie == 'input' || $donnie == 'output'){ // show input/output data
  5146. $b .= "<font color='white'>".ucfirst($donnie).':</font><br>';
  5147. foreach($marie as $captain => $tenille){ // loop through data
  5148. if($captain == 'parts'){ // loop thru parts
  5149. $b .= "&nbsp;&nbsp;$captain:<br>";
  5150. //if(is_array($tenille)){
  5151. foreach($tenille as $joanie => $chachi){
  5152. $b .= "&nbsp;&nbsp;&nbsp;&nbsp;$joanie: $chachi<br>";
  5153. }
  5154. //}
  5155. } else {
  5156. $b .= "&nbsp;&nbsp;$captain: $tenille<br>";
  5157. }
  5158. }
  5159. } else {
  5160. $b .= "<font color='white'>".ucfirst($donnie).":</font> $marie<br>";
  5161. }
  5162. }
  5163. $b .= '</div>';
  5164. }
  5165. $b .= '
  5166. <ul>
  5167. </div>
  5168. </div></body></html>';
  5169. return $b;
  5170. }
  5171. /**
  5172. * serialize the parsed wsdl
  5173. *
  5174. * @param mixed $debug whether to put debug=1 in endpoint URL
  5175. * @return string serialization of WSDL
  5176. * @access public
  5177. */
  5178. function serialize($debug = 0)
  5179. {
  5180. $xml = '<?xml version="1.0" encoding="ISO-8859-1"?>';
  5181. $xml .= "\n<definitions";
  5182. foreach($this->namespaces as $k => $v) {
  5183. $xml .= " xmlns:$k=\"$v\"";
  5184. }
  5185. // 10.9.02 - add poulter fix for wsdl and tns declarations
  5186. if (isset($this->namespaces['wsdl'])) {
  5187. $xml .= " xmlns=\"" . $this->namespaces['wsdl'] . "\"";
  5188. }
  5189. if (isset($this->namespaces['tns'])) {
  5190. $xml .= " targetNamespace=\"" . $this->namespaces['tns'] . "\"";
  5191. }
  5192. $xml .= '>';
  5193. // imports
  5194. if (sizeof($this->import) > 0) {
  5195. foreach($this->import as $ns => $list) {
  5196. foreach ($list as $ii) {
  5197. if ($ii['location'] != '') {
  5198. $xml .= '<import location="' . $ii['location'] . '" namespace="' . $ns . '" />';
  5199. } else {
  5200. $xml .= '<import namespace="' . $ns . '" />';
  5201. }
  5202. }
  5203. }
  5204. }
  5205. // types
  5206. if (count($this->schemas)>=1) {
  5207. $xml .= "\n<types>\n";
  5208. foreach ($this->schemas as $ns => $list) {
  5209. foreach ($list as $xs) {
  5210. $xml .= $xs->serializeSchema();
  5211. }
  5212. }
  5213. $xml .= '</types>';
  5214. }
  5215. // messages
  5216. if (count($this->messages) >= 1) {
  5217. foreach($this->messages as $msgName => $msgParts) {
  5218. $xml .= "\n<message name=\"" . $msgName . '">';
  5219. if(is_array($msgParts)){
  5220. foreach($msgParts as $partName => $partType) {
  5221. // print 'serializing '.$partType.', sv: '.$this->XMLSchemaVersion.'<br>';
  5222. if (strpos($partType, ':')) {
  5223. $typePrefix = $this->getPrefixFromNamespace($this->getPrefix($partType));
  5224. } elseif (isset($this->typemap[$this->namespaces['xsd']][$partType])) {
  5225. // print 'checking typemap: '.$this->XMLSchemaVersion.'<br>';
  5226. $typePrefix = 'xsd';
  5227. } else {
  5228. foreach($this->typemap as $ns => $types) {
  5229. if (isset($types[$partType])) {
  5230. $typePrefix = $this->getPrefixFromNamespace($ns);
  5231. }
  5232. }
  5233. if (!isset($typePrefix)) {
  5234. die("$partType has no namespace!");
  5235. }
  5236. }
  5237. $ns = $this->getNamespaceFromPrefix($typePrefix);
  5238. $localPart = $this->getLocalPart($partType);
  5239. $typeDef = $this->getTypeDef($localPart, $ns);
  5240. if ($typeDef['typeClass'] == 'element') {
  5241. $elementortype = 'element';
  5242. if (substr($localPart, -1) == '^') {
  5243. $localPart = substr($localPart, 0, -1);
  5244. }
  5245. } else {
  5246. $elementortype = 'type';
  5247. }
  5248. $xml .= "\n" . ' <part name="' . $partName . '" ' . $elementortype . '="' . $typePrefix . ':' . $localPart . '" />';
  5249. }
  5250. }
  5251. $xml .= '</message>';
  5252. }
  5253. }
  5254. // bindings & porttypes
  5255. if (count($this->bindings) >= 1) {
  5256. $binding_xml = '';
  5257. $portType_xml = '';
  5258. foreach($this->bindings as $bindingName => $attrs) {
  5259. $binding_xml .= "\n<binding name=\"" . $bindingName . '" type="tns:' . $attrs['portType'] . '">';
  5260. $binding_xml .= "\n" . ' <soap:binding style="' . $attrs['style'] . '" transport="' . $attrs['transport'] . '"/>';
  5261. $portType_xml .= "\n<portType name=\"" . $attrs['portType'] . '">';
  5262. foreach($attrs['operations'] as $opName => $opParts) {
  5263. $binding_xml .= "\n" . ' <operation name="' . $opName . '">';
  5264. $binding_xml .= "\n" . ' <soap:operation soapAction="' . $opParts['soapAction'] . '" style="'. $opParts['style'] . '"/>';
  5265. if (isset($opParts['input']['encodingStyle']) && $opParts['input']['encodingStyle'] != '') {
  5266. $enc_style = ' encodingStyle="' . $opParts['input']['encodingStyle'] . '"';
  5267. } else {
  5268. $enc_style = '';
  5269. }
  5270. $binding_xml .= "\n" . ' <input><soap:body use="' . $opParts['input']['use'] . '" namespace="' . $opParts['input']['namespace'] . '"' . $enc_style . '/></input>';
  5271. if (isset($opParts['output']['encodingStyle']) && $opParts['output']['encodingStyle'] != '') {
  5272. $enc_style = ' encodingStyle="' . $opParts['output']['encodingStyle'] . '"';
  5273. } else {
  5274. $enc_style = '';
  5275. }
  5276. $binding_xml .= "\n" . ' <output><soap:body use="' . $opParts['output']['use'] . '" namespace="' . $opParts['output']['namespace'] . '"' . $enc_style . '/></output>';
  5277. $binding_xml .= "\n" . ' </operation>';
  5278. $portType_xml .= "\n" . ' <operation name="' . $opParts['name'] . '"';
  5279. if (isset($opParts['parameterOrder'])) {
  5280. $portType_xml .= ' parameterOrder="' . $opParts['parameterOrder'] . '"';
  5281. }
  5282. $portType_xml .= '>';
  5283. if(isset($opParts['documentation']) && $opParts['documentation'] != '') {
  5284. $portType_xml .= "\n" . ' <documentation>' . htmlspecialchars($opParts['documentation']) . '</documentation>';
  5285. }
  5286. $portType_xml .= "\n" . ' <input message="tns:' . $opParts['input']['message'] . '"/>';
  5287. $portType_xml .= "\n" . ' <output message="tns:' . $opParts['output']['message'] . '"/>';
  5288. $portType_xml .= "\n" . ' </operation>';
  5289. }
  5290. $portType_xml .= "\n" . '</portType>';
  5291. $binding_xml .= "\n" . '</binding>';
  5292. }
  5293. $xml .= $portType_xml . $binding_xml;
  5294. }
  5295. // services
  5296. $xml .= "\n<service name=\"" . $this->serviceName . '">';
  5297. if (count($this->ports) >= 1) {
  5298. foreach($this->ports as $pName => $attrs) {
  5299. $xml .= "\n" . ' <port name="' . $pName . '" binding="tns:' . $attrs['binding'] . '">';
  5300. $xml .= "\n" . ' <soap:address location="' . $attrs['location'] . ($debug ? '?debug=1' : '') . '"/>';
  5301. $xml .= "\n" . ' </port>';
  5302. }
  5303. }
  5304. $xml .= "\n" . '</service>';
  5305. return $xml . "\n</definitions>";
  5306. }
  5307. /**
  5308. * determine whether a set of parameters are unwrapped
  5309. * when they are expect to be wrapped, Microsoft-style.
  5310. *
  5311. * @param string $type the type (element name) of the wrapper
  5312. * @param array $parameters the parameter values for the SOAP call
  5313. * @return boolean whether they parameters are unwrapped (and should be wrapped)
  5314. * @access private
  5315. */
  5316. function parametersMatchWrapped($type, &$parameters) {
  5317. $this->debug("in parametersMatchWrapped type=$type, parameters=");
  5318. $this->appendDebug($this->varDump($parameters));
  5319. // split type into namespace:unqualified-type
  5320. if (strpos($type, ':')) {
  5321. $uqType = substr($type, strrpos($type, ':') + 1);
  5322. $ns = substr($type, 0, strrpos($type, ':'));
  5323. $this->debug("in parametersMatchWrapped: got a prefixed type: $uqType, $ns");
  5324. if ($this->getNamespaceFromPrefix($ns)) {
  5325. $ns = $this->getNamespaceFromPrefix($ns);
  5326. $this->debug("in parametersMatchWrapped: expanded prefixed type: $uqType, $ns");
  5327. }
  5328. } else {
  5329. // TODO: should the type be compared to types in XSD, and the namespace
  5330. // set to XSD if the type matches?
  5331. $this->debug("in parametersMatchWrapped: No namespace for type $type");
  5332. $ns = '';
  5333. $uqType = $type;
  5334. }
  5335. // get the type information
  5336. if (!$typeDef = $this->getTypeDef($uqType, $ns)) {
  5337. $this->debug("in parametersMatchWrapped: $type ($uqType) is not a supported type.");
  5338. return false;
  5339. }
  5340. $this->debug("in parametersMatchWrapped: found typeDef=");
  5341. $this->appendDebug($this->varDump($typeDef));
  5342. if (substr($uqType, -1) == '^') {
  5343. $uqType = substr($uqType, 0, -1);
  5344. }
  5345. $phpType = $typeDef['phpType'];
  5346. $arrayType = (isset($typeDef['arrayType']) ? $typeDef['arrayType'] : '');
  5347. $this->debug("in parametersMatchWrapped: uqType: $uqType, ns: $ns, phptype: $phpType, arrayType: $arrayType");
  5348. // we expect a complexType or element of complexType
  5349. if ($phpType != 'struct') {
  5350. $this->debug("in parametersMatchWrapped: not a struct");
  5351. return false;
  5352. }
  5353. // see whether the parameter names match the elements
  5354. if (isset($typeDef['elements']) && is_array($typeDef['elements'])) {
  5355. $elements = 0;
  5356. $matches = 0;
  5357. foreach ($typeDef['elements'] as $name => $attrs) {
  5358. if (isset($parameters[$name])) {
  5359. $this->debug("in parametersMatchWrapped: have parameter named $name");
  5360. $matches++;
  5361. } else {
  5362. $this->debug("in parametersMatchWrapped: do not have parameter named $name");
  5363. }
  5364. $elements++;
  5365. }
  5366. $this->debug("in parametersMatchWrapped: $matches parameter names match $elements wrapped parameter names");
  5367. if ($matches == 0) {
  5368. return false;
  5369. }
  5370. return true;
  5371. }
  5372. // since there are no elements for the type, if the user passed no
  5373. // parameters, the parameters match wrapped.
  5374. $this->debug("in parametersMatchWrapped: no elements type $ns:$uqType");
  5375. return count($parameters) == 0;
  5376. }
  5377. /**
  5378. * serialize PHP values according to a WSDL message definition
  5379. * contrary to the method name, this is not limited to RPC
  5380. *
  5381. * TODO
  5382. * - multi-ref serialization
  5383. * - validate PHP values against type definitions, return errors if invalid
  5384. *
  5385. * @param string $operation operation name
  5386. * @param string $direction (input|output)
  5387. * @param mixed $parameters parameter value(s)
  5388. * @param string $bindingType (soap|soap12)
  5389. * @return mixed parameters serialized as XML or false on error (e.g. operation not found)
  5390. * @access public
  5391. */
  5392. function serializeRPCParameters($operation, $direction, $parameters, $bindingType = 'soap') {
  5393. $this->debug("in serializeRPCParameters: operation=$operation, direction=$direction, XMLSchemaVersion=$this->XMLSchemaVersion, bindingType=$bindingType");
  5394. $this->appendDebug('parameters=' . $this->varDump($parameters));
  5395. if ($direction != 'input' && $direction != 'output') {
  5396. $this->debug('The value of the \$direction argument needs to be either "input" or "output"');
  5397. $this->setError('The value of the \$direction argument needs to be either "input" or "output"');
  5398. return false;
  5399. }
  5400. if (!$opData = $this->getOperationData($operation, $bindingType)) {
  5401. $this->debug('Unable to retrieve WSDL data for operation: ' . $operation . ' bindingType: ' . $bindingType);
  5402. $this->setError('Unable to retrieve WSDL data for operation: ' . $operation . ' bindingType: ' . $bindingType);
  5403. return false;
  5404. }
  5405. $this->debug('in serializeRPCParameters: opData:');
  5406. $this->appendDebug($this->varDump($opData));
  5407. // Get encoding style for output and set to current
  5408. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  5409. if(($direction == 'input') && isset($opData['output']['encodingStyle']) && ($opData['output']['encodingStyle'] != $encodingStyle)) {
  5410. $encodingStyle = $opData['output']['encodingStyle'];
  5411. $enc_style = $encodingStyle;
  5412. }
  5413. // set input params
  5414. $xml = '';
  5415. if (isset($opData[$direction]['parts']) && sizeof($opData[$direction]['parts']) > 0) {
  5416. $parts = &$opData[$direction]['parts'];
  5417. $part_count = sizeof($parts);
  5418. $style = $opData['style'];
  5419. $use = $opData[$direction]['use'];
  5420. $this->debug("have $part_count part(s) to serialize using $style/$use");
  5421. if (is_array($parameters)) {
  5422. $parametersArrayType = $this->isArraySimpleOrStruct($parameters);
  5423. $parameter_count = count($parameters);
  5424. $this->debug("have $parameter_count parameter(s) provided as $parametersArrayType to serialize");
  5425. // check for Microsoft-style wrapped parameters
  5426. if ($style == 'document' && $use == 'literal' && $part_count == 1 && isset($parts['parameters'])) {
  5427. $this->debug('check whether the caller has wrapped the parameters');
  5428. if ($direction == 'output' && $parametersArrayType == 'arraySimple' && $parameter_count == 1) {
  5429. // TODO: consider checking here for double-wrapping, when
  5430. // service function wraps, then NuSOAP wraps again
  5431. $this->debug("change simple array to associative with 'parameters' element");
  5432. $parameters['parameters'] = $parameters[0];
  5433. unset($parameters[0]);
  5434. }
  5435. if (($parametersArrayType == 'arrayStruct' || $parameter_count == 0) && !isset($parameters['parameters'])) {
  5436. $this->debug('check whether caller\'s parameters match the wrapped ones');
  5437. if ($this->parametersMatchWrapped($parts['parameters'], $parameters)) {
  5438. $this->debug('wrap the parameters for the caller');
  5439. $parameters = array('parameters' => $parameters);
  5440. $parameter_count = 1;
  5441. }
  5442. }
  5443. }
  5444. foreach ($parts as $name => $type) {
  5445. $this->debug("serializing part $name of type $type");
  5446. // Track encoding style
  5447. if (isset($opData[$direction]['encodingStyle']) && $encodingStyle != $opData[$direction]['encodingStyle']) {
  5448. $encodingStyle = $opData[$direction]['encodingStyle'];
  5449. $enc_style = $encodingStyle;
  5450. } else {
  5451. $enc_style = false;
  5452. }
  5453. // NOTE: add error handling here
  5454. // if serializeType returns false, then catch global error and fault
  5455. if ($parametersArrayType == 'arraySimple') {
  5456. $p = array_shift($parameters);
  5457. $this->debug('calling serializeType w/indexed param');
  5458. $xml .= $this->serializeType($name, $type, $p, $use, $enc_style);
  5459. } elseif (isset($parameters[$name])) {
  5460. $this->debug('calling serializeType w/named param');
  5461. $xml .= $this->serializeType($name, $type, $parameters[$name], $use, $enc_style);
  5462. } else {
  5463. // TODO: only send nillable
  5464. $this->debug('calling serializeType w/null param');
  5465. $xml .= $this->serializeType($name, $type, null, $use, $enc_style);
  5466. }
  5467. }
  5468. } else {
  5469. $this->debug('no parameters passed.');
  5470. }
  5471. }
  5472. $this->debug("serializeRPCParameters returning: $xml");
  5473. return $xml;
  5474. }
  5475. /**
  5476. * serialize a PHP value according to a WSDL message definition
  5477. *
  5478. * TODO
  5479. * - multi-ref serialization
  5480. * - validate PHP values against type definitions, return errors if invalid
  5481. *
  5482. * @param string $operation operation name
  5483. * @param string $direction (input|output)
  5484. * @param mixed $parameters parameter value(s)
  5485. * @return mixed parameters serialized as XML or false on error (e.g. operation not found)
  5486. * @access public
  5487. * @deprecated
  5488. */
  5489. function serializeParameters($operation, $direction, $parameters)
  5490. {
  5491. $this->debug("in serializeParameters: operation=$operation, direction=$direction, XMLSchemaVersion=$this->XMLSchemaVersion");
  5492. $this->appendDebug('parameters=' . $this->varDump($parameters));
  5493. if ($direction != 'input' && $direction != 'output') {
  5494. $this->debug('The value of the \$direction argument needs to be either "input" or "output"');
  5495. $this->setError('The value of the \$direction argument needs to be either "input" or "output"');
  5496. return false;
  5497. }
  5498. if (!$opData = $this->getOperationData($operation)) {
  5499. $this->debug('Unable to retrieve WSDL data for operation: ' . $operation);
  5500. $this->setError('Unable to retrieve WSDL data for operation: ' . $operation);
  5501. return false;
  5502. }
  5503. $this->debug('opData:');
  5504. $this->appendDebug($this->varDump($opData));
  5505. // Get encoding style for output and set to current
  5506. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  5507. if(($direction == 'input') && isset($opData['output']['encodingStyle']) && ($opData['output']['encodingStyle'] != $encodingStyle)) {
  5508. $encodingStyle = $opData['output']['encodingStyle'];
  5509. $enc_style = $encodingStyle;
  5510. }
  5511. // set input params
  5512. $xml = '';
  5513. if (isset($opData[$direction]['parts']) && sizeof($opData[$direction]['parts']) > 0) {
  5514. $use = $opData[$direction]['use'];
  5515. $this->debug("use=$use");
  5516. $this->debug('got ' . count($opData[$direction]['parts']) . ' part(s)');
  5517. if (is_array($parameters)) {
  5518. $parametersArrayType = $this->isArraySimpleOrStruct($parameters);
  5519. $this->debug('have ' . $parametersArrayType . ' parameters');
  5520. foreach($opData[$direction]['parts'] as $name => $type) {
  5521. $this->debug('serializing part "'.$name.'" of type "'.$type.'"');
  5522. // Track encoding style
  5523. if(isset($opData[$direction]['encodingStyle']) && $encodingStyle != $opData[$direction]['encodingStyle']) {
  5524. $encodingStyle = $opData[$direction]['encodingStyle'];
  5525. $enc_style = $encodingStyle;
  5526. } else {
  5527. $enc_style = false;
  5528. }
  5529. // NOTE: add error handling here
  5530. // if serializeType returns false, then catch global error and fault
  5531. if ($parametersArrayType == 'arraySimple') {
  5532. $p = array_shift($parameters);
  5533. $this->debug('calling serializeType w/indexed param');
  5534. $xml .= $this->serializeType($name, $type, $p, $use, $enc_style);
  5535. } elseif (isset($parameters[$name])) {
  5536. $this->debug('calling serializeType w/named param');
  5537. $xml .= $this->serializeType($name, $type, $parameters[$name], $use, $enc_style);
  5538. } else {
  5539. // TODO: only send nillable
  5540. $this->debug('calling serializeType w/null param');
  5541. $xml .= $this->serializeType($name, $type, null, $use, $enc_style);
  5542. }
  5543. }
  5544. } else {
  5545. $this->debug('no parameters passed.');
  5546. }
  5547. }
  5548. $this->debug("serializeParameters returning: $xml");
  5549. return $xml;
  5550. }
  5551. /**
  5552. * serializes a PHP value according a given type definition
  5553. *
  5554. * @param string $name name of value (part or element)
  5555. * @param string $type XML schema type of value (type or element)
  5556. * @param mixed $value a native PHP value (parameter value)
  5557. * @param string $use use for part (encoded|literal)
  5558. * @param string $encodingStyle SOAP encoding style for the value (if different than the enclosing style)
  5559. * @param boolean $unqualified a kludge for what should be XML namespace form handling
  5560. * @return string value serialized as an XML string
  5561. * @access private
  5562. */
  5563. function serializeType($name, $type, $value, $use='encoded', $encodingStyle=false, $unqualified=false)
  5564. {
  5565. $this->debug("in serializeType: name=$name, type=$type, use=$use, encodingStyle=$encodingStyle, unqualified=" . ($unqualified ? "unqualified" : "qualified"));
  5566. $this->appendDebug("value=" . $this->varDump($value));
  5567. if($use == 'encoded' && $encodingStyle) {
  5568. $encodingStyle = ' SOAP-ENV:encodingStyle="' . $encodingStyle . '"';
  5569. }
  5570. // if a soapval has been supplied, let its type override the WSDL
  5571. if (is_object($value) && get_class($value) == 'soapval') {
  5572. if ($value->type_ns) {
  5573. $type = $value->type_ns . ':' . $value->type;
  5574. $forceType = true;
  5575. $this->debug("in serializeType: soapval overrides type to $type");
  5576. } elseif ($value->type) {
  5577. $type = $value->type;
  5578. $forceType = true;
  5579. $this->debug("in serializeType: soapval overrides type to $type");
  5580. } else {
  5581. $forceType = false;
  5582. $this->debug("in serializeType: soapval does not override type");
  5583. }
  5584. $attrs = $value->attributes;
  5585. $value = $value->value;
  5586. $this->debug("in serializeType: soapval overrides value to $value");
  5587. if ($attrs) {
  5588. if (!is_array($value)) {
  5589. $value['!'] = $value;
  5590. }
  5591. foreach ($attrs as $n => $v) {
  5592. $value['!' . $n] = $v;
  5593. }
  5594. $this->debug("in serializeType: soapval provides attributes");
  5595. }
  5596. } else {
  5597. $forceType = false;
  5598. }
  5599. $xml = '';
  5600. if (strpos($type, ':')) {
  5601. $uqType = substr($type, strrpos($type, ':') + 1);
  5602. $ns = substr($type, 0, strrpos($type, ':'));
  5603. $this->debug("in serializeType: got a prefixed type: $uqType, $ns");
  5604. if ($this->getNamespaceFromPrefix($ns)) {
  5605. $ns = $this->getNamespaceFromPrefix($ns);
  5606. $this->debug("in serializeType: expanded prefixed type: $uqType, $ns");
  5607. }
  5608. if($ns == $this->XMLSchemaVersion || $ns == 'http://schemas.xmlsoap.org/soap/encoding/'){
  5609. $this->debug('in serializeType: type namespace indicates XML Schema or SOAP Encoding type');
  5610. if ($unqualified && $use == 'literal') {
  5611. $elementNS = " xmlns=\"\"";
  5612. } else {
  5613. $elementNS = '';
  5614. }
  5615. if (is_null($value)) {
  5616. if ($use == 'literal') {
  5617. // TODO: depends on minOccurs
  5618. $xml = "<$name$elementNS/>";
  5619. } else {
  5620. // TODO: depends on nillable, which should be checked before calling this method
  5621. $xml = "<$name$elementNS xsi:nil=\"true\" xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"/>";
  5622. }
  5623. $this->debug("in serializeType: returning: $xml");
  5624. return $xml;
  5625. }
  5626. if ($uqType == 'Array') {
  5627. // JBoss/Axis does this sometimes
  5628. return $this->serialize_val($value, $name, false, false, false, false, $use);
  5629. }
  5630. if ($uqType == 'boolean') {
  5631. if ((is_string($value) && $value == 'false') || (! $value)) {
  5632. $value = 'false';
  5633. } else {
  5634. $value = 'true';
  5635. }
  5636. }
  5637. if ($uqType == 'string' && gettype($value) == 'string') {
  5638. $value = $this->expandEntities($value);
  5639. }
  5640. if (($uqType == 'long' || $uqType == 'unsignedLong') && gettype($value) == 'double') {
  5641. $value = sprintf("%.0lf", $value);
  5642. }
  5643. // it's a scalar
  5644. // TODO: what about null/nil values?
  5645. // check type isn't a custom type extending xmlschema namespace
  5646. if (!$this->getTypeDef($uqType, $ns)) {
  5647. if ($use == 'literal') {
  5648. if ($forceType) {
  5649. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">$value</$name>";
  5650. } else {
  5651. $xml = "<$name$elementNS>$value</$name>";
  5652. }
  5653. } else {
  5654. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>$value</$name>";
  5655. }
  5656. $this->debug("in serializeType: returning: $xml");
  5657. return $xml;
  5658. }
  5659. $this->debug('custom type extends XML Schema or SOAP Encoding namespace (yuck)');
  5660. } else if ($ns == 'http://xml.apache.org/xml-soap') {
  5661. $this->debug('in serializeType: appears to be Apache SOAP type');
  5662. if ($uqType == 'Map') {
  5663. $tt_prefix = $this->getPrefixFromNamespace('http://xml.apache.org/xml-soap');
  5664. if (! $tt_prefix) {
  5665. $this->debug('in serializeType: Add namespace for Apache SOAP type');
  5666. $tt_prefix = 'ns' . rand(1000, 9999);
  5667. $this->namespaces[$tt_prefix] = 'http://xml.apache.org/xml-soap';
  5668. // force this to be added to usedNamespaces
  5669. $tt_prefix = $this->getPrefixFromNamespace('http://xml.apache.org/xml-soap');
  5670. }
  5671. $contents = '';
  5672. foreach($value as $k => $v) {
  5673. $this->debug("serializing map element: key $k, value $v");
  5674. $contents .= '<item>';
  5675. $contents .= $this->serialize_val($k,'key',false,false,false,false,$use);
  5676. $contents .= $this->serialize_val($v,'value',false,false,false,false,$use);
  5677. $contents .= '</item>';
  5678. }
  5679. if ($use == 'literal') {
  5680. if ($forceType) {
  5681. $xml = "<$name xsi:type=\"" . $tt_prefix . ":$uqType\">$contents</$name>";
  5682. } else {
  5683. $xml = "<$name>$contents</$name>";
  5684. }
  5685. } else {
  5686. $xml = "<$name xsi:type=\"" . $tt_prefix . ":$uqType\"$encodingStyle>$contents</$name>";
  5687. }
  5688. $this->debug("in serializeType: returning: $xml");
  5689. return $xml;
  5690. }
  5691. $this->debug('in serializeType: Apache SOAP type, but only support Map');
  5692. }
  5693. } else {
  5694. // TODO: should the type be compared to types in XSD, and the namespace
  5695. // set to XSD if the type matches?
  5696. $this->debug("in serializeType: No namespace for type $type");
  5697. $ns = '';
  5698. $uqType = $type;
  5699. }
  5700. if(!$typeDef = $this->getTypeDef($uqType, $ns)){
  5701. $this->setError("$type ($uqType) is not a supported type.");
  5702. $this->debug("in serializeType: $type ($uqType) is not a supported type.");
  5703. return false;
  5704. } else {
  5705. $this->debug("in serializeType: found typeDef");
  5706. $this->appendDebug('typeDef=' . $this->varDump($typeDef));
  5707. if (substr($uqType, -1) == '^') {
  5708. $uqType = substr($uqType, 0, -1);
  5709. }
  5710. }
  5711. if (!isset($typeDef['phpType'])) {
  5712. $this->setError("$type ($uqType) has no phpType.");
  5713. $this->debug("in serializeType: $type ($uqType) has no phpType.");
  5714. return false;
  5715. }
  5716. $phpType = $typeDef['phpType'];
  5717. $this->debug("in serializeType: uqType: $uqType, ns: $ns, phptype: $phpType, arrayType: " . (isset($typeDef['arrayType']) ? $typeDef['arrayType'] : '') );
  5718. // if php type == struct, map value to the <all> element names
  5719. if ($phpType == 'struct') {
  5720. if (isset($typeDef['typeClass']) && $typeDef['typeClass'] == 'element') {
  5721. $elementName = $uqType;
  5722. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  5723. $elementNS = " xmlns=\"$ns\"";
  5724. } else {
  5725. $elementNS = " xmlns=\"\"";
  5726. }
  5727. } else {
  5728. $elementName = $name;
  5729. if ($unqualified) {
  5730. $elementNS = " xmlns=\"\"";
  5731. } else {
  5732. $elementNS = '';
  5733. }
  5734. }
  5735. if (is_null($value)) {
  5736. if ($use == 'literal') {
  5737. // TODO: depends on minOccurs and nillable
  5738. $xml = "<$elementName$elementNS/>";
  5739. } else {
  5740. $xml = "<$elementName$elementNS xsi:nil=\"true\" xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"/>";
  5741. }
  5742. $this->debug("in serializeType: returning: $xml");
  5743. return $xml;
  5744. }
  5745. if (is_object($value)) {
  5746. $value = get_object_vars($value);
  5747. }
  5748. if (is_array($value)) {
  5749. $elementAttrs = $this->serializeComplexTypeAttributes($typeDef, $value, $ns, $uqType);
  5750. if ($use == 'literal') {
  5751. if ($forceType) {
  5752. $xml = "<$elementName$elementNS$elementAttrs xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">";
  5753. } else {
  5754. $xml = "<$elementName$elementNS$elementAttrs>";
  5755. }
  5756. } else {
  5757. $xml = "<$elementName$elementNS$elementAttrs xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>";
  5758. }
  5759. if (isset($typeDef['simpleContent']) && $typeDef['simpleContent'] == 'true') {
  5760. if (isset($value['!'])) {
  5761. $xml .= $value['!'];
  5762. $this->debug("in serializeType: serialized simpleContent for type $type");
  5763. } else {
  5764. $this->debug("in serializeType: no simpleContent to serialize for type $type");
  5765. }
  5766. } else {
  5767. // complexContent
  5768. $xml .= $this->serializeComplexTypeElements($typeDef, $value, $ns, $uqType, $use, $encodingStyle);
  5769. }
  5770. $xml .= "</$elementName>";
  5771. } else {
  5772. $this->debug("in serializeType: phpType is struct, but value is not an array");
  5773. $this->setError("phpType is struct, but value is not an array: see debug output for details");
  5774. $xml = '';
  5775. }
  5776. } elseif ($phpType == 'array') {
  5777. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  5778. $elementNS = " xmlns=\"$ns\"";
  5779. } else {
  5780. if ($unqualified) {
  5781. $elementNS = " xmlns=\"\"";
  5782. } else {
  5783. $elementNS = '';
  5784. }
  5785. }
  5786. if (is_null($value)) {
  5787. if ($use == 'literal') {
  5788. // TODO: depends on minOccurs
  5789. $xml = "<$name$elementNS/>";
  5790. } else {
  5791. $xml = "<$name$elementNS xsi:nil=\"true\" xsi:type=\"" .
  5792. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/') .
  5793. ":Array\" " .
  5794. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/') .
  5795. ':arrayType="' .
  5796. $this->getPrefixFromNamespace($this->getPrefix($typeDef['arrayType'])) .
  5797. ':' .
  5798. $this->getLocalPart($typeDef['arrayType'])."[0]\"/>";
  5799. }
  5800. $this->debug("in serializeType: returning: $xml");
  5801. return $xml;
  5802. }
  5803. if (isset($typeDef['multidimensional'])) {
  5804. $nv = array();
  5805. foreach($value as $v) {
  5806. $cols = ',' . sizeof($v);
  5807. $nv = array_merge($nv, $v);
  5808. }
  5809. $value = $nv;
  5810. } else {
  5811. $cols = '';
  5812. }
  5813. if (is_array($value) && sizeof($value) >= 1) {
  5814. $rows = sizeof($value);
  5815. $contents = '';
  5816. foreach($value as $k => $v) {
  5817. $this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
  5818. //if (strpos($typeDef['arrayType'], ':') ) {
  5819. if (!in_array($typeDef['arrayType'],$this->typemap['http://www.w3.org/2001/XMLSchema'])) {
  5820. $contents .= $this->serializeType('item', $typeDef['arrayType'], $v, $use);
  5821. } else {
  5822. // DOL_CHANGE This is to have tag name with name reported by wsdl and not "item"
  5823. //$contents .= $this->serialize_val($v, 'itemzzzz', $typeDef['arrayType'], null, $this->XMLSchemaVersion, false, $use);
  5824. $tmp=array_keys($typeDef['elements']); //var_dump($tmp[0]);
  5825. $contents .= $this->serialize_val($v, ($tmp[0]?$tmp[0]:'item'), $typeDef['arrayType'], null, $this->XMLSchemaVersion, false, $use);
  5826. }
  5827. }
  5828. } else {
  5829. $rows = 0;
  5830. $contents = null;
  5831. }
  5832. // TODO: for now, an empty value will be serialized as a zero element
  5833. // array. Revisit this when coding the handling of null/nil values.
  5834. if ($use == 'literal') {
  5835. $xml = "<$name$elementNS>"
  5836. .$contents
  5837. ."</$name>";
  5838. } else {
  5839. $xml = "<$name$elementNS xsi:type=\"".$this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/').':Array" '.
  5840. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/')
  5841. .':arrayType="'
  5842. .$this->getPrefixFromNamespace($this->getPrefix($typeDef['arrayType']))
  5843. .":".$this->getLocalPart($typeDef['arrayType'])."[$rows$cols]\">"
  5844. .$contents
  5845. ."</$name>";
  5846. }
  5847. } elseif ($phpType == 'scalar') {
  5848. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  5849. $elementNS = " xmlns=\"$ns\"";
  5850. } else {
  5851. if ($unqualified) {
  5852. $elementNS = " xmlns=\"\"";
  5853. } else {
  5854. $elementNS = '';
  5855. }
  5856. }
  5857. if ($use == 'literal') {
  5858. if ($forceType) {
  5859. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">$value</$name>";
  5860. } else {
  5861. $xml = "<$name$elementNS>$value</$name>";
  5862. }
  5863. } else {
  5864. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>$value</$name>";
  5865. }
  5866. }
  5867. $this->debug("in serializeType: returning: $xml");
  5868. return $xml;
  5869. }
  5870. /**
  5871. * serializes the attributes for a complexType
  5872. *
  5873. * @param array $typeDef our internal representation of an XML schema type (or element)
  5874. * @param mixed $value a native PHP value (parameter value)
  5875. * @param string $ns the namespace of the type
  5876. * @param string $uqType the local part of the type
  5877. * @return string value serialized as an XML string
  5878. * @access private
  5879. */
  5880. function serializeComplexTypeAttributes($typeDef, $value, $ns, $uqType) {
  5881. $this->debug("serializeComplexTypeAttributes for XML Schema type $ns:$uqType");
  5882. $xml = '';
  5883. if (isset($typeDef['extensionBase'])) {
  5884. $nsx = $this->getPrefix($typeDef['extensionBase']);
  5885. $uqTypex = $this->getLocalPart($typeDef['extensionBase']);
  5886. if ($this->getNamespaceFromPrefix($nsx)) {
  5887. $nsx = $this->getNamespaceFromPrefix($nsx);
  5888. }
  5889. if ($typeDefx = $this->getTypeDef($uqTypex, $nsx)) {
  5890. $this->debug("serialize attributes for extension base $nsx:$uqTypex");
  5891. $xml .= $this->serializeComplexTypeAttributes($typeDefx, $value, $nsx, $uqTypex);
  5892. } else {
  5893. $this->debug("extension base $nsx:$uqTypex is not a supported type");
  5894. }
  5895. }
  5896. if (isset($typeDef['attrs']) && is_array($typeDef['attrs'])) {
  5897. $this->debug("serialize attributes for XML Schema type $ns:$uqType");
  5898. if (is_array($value)) {
  5899. $xvalue = $value;
  5900. } elseif (is_object($value)) {
  5901. $xvalue = get_object_vars($value);
  5902. } else {
  5903. $this->debug("value is neither an array nor an object for XML Schema type $ns:$uqType");
  5904. $xvalue = array();
  5905. }
  5906. foreach ($typeDef['attrs'] as $aName => $attrs) {
  5907. if (isset($xvalue['!' . $aName])) {
  5908. $xname = '!' . $aName;
  5909. $this->debug("value provided for attribute $aName with key $xname");
  5910. } elseif (isset($xvalue[$aName])) {
  5911. $xname = $aName;
  5912. $this->debug("value provided for attribute $aName with key $xname");
  5913. } elseif (isset($attrs['default'])) {
  5914. $xname = '!' . $aName;
  5915. $xvalue[$xname] = $attrs['default'];
  5916. $this->debug('use default value of ' . $xvalue[$aName] . ' for attribute ' . $aName);
  5917. } else {
  5918. $xname = '';
  5919. $this->debug("no value provided for attribute $aName");
  5920. }
  5921. if ($xname) {
  5922. $xml .= " $aName=\"" . $this->expandEntities($xvalue[$xname]) . "\"";
  5923. }
  5924. }
  5925. } else {
  5926. $this->debug("no attributes to serialize for XML Schema type $ns:$uqType");
  5927. }
  5928. return $xml;
  5929. }
  5930. /**
  5931. * serializes the elements for a complexType
  5932. *
  5933. * @param array $typeDef our internal representation of an XML schema type (or element)
  5934. * @param mixed $value a native PHP value (parameter value)
  5935. * @param string $ns the namespace of the type
  5936. * @param string $uqType the local part of the type
  5937. * @param string $use use for part (encoded|literal)
  5938. * @param string $encodingStyle SOAP encoding style for the value (if different than the enclosing style)
  5939. * @return string value serialized as an XML string
  5940. * @access private
  5941. */
  5942. function serializeComplexTypeElements($typeDef, $value, $ns, $uqType, $use='encoded', $encodingStyle=false) {
  5943. $this->debug("in serializeComplexTypeElements for XML Schema type $ns:$uqType");
  5944. $xml = '';
  5945. if (isset($typeDef['extensionBase'])) {
  5946. $nsx = $this->getPrefix($typeDef['extensionBase']);
  5947. $uqTypex = $this->getLocalPart($typeDef['extensionBase']);
  5948. if ($this->getNamespaceFromPrefix($nsx)) {
  5949. $nsx = $this->getNamespaceFromPrefix($nsx);
  5950. }
  5951. if ($typeDefx = $this->getTypeDef($uqTypex, $nsx)) {
  5952. $this->debug("serialize elements for extension base $nsx:$uqTypex");
  5953. $xml .= $this->serializeComplexTypeElements($typeDefx, $value, $nsx, $uqTypex, $use, $encodingStyle);
  5954. } else {
  5955. $this->debug("extension base $nsx:$uqTypex is not a supported type");
  5956. }
  5957. }
  5958. if (isset($typeDef['elements']) && is_array($typeDef['elements'])) {
  5959. $this->debug("in serializeComplexTypeElements, serialize elements for XML Schema type $ns:$uqType");
  5960. if (is_array($value)) {
  5961. $xvalue = $value;
  5962. } elseif (is_object($value)) {
  5963. $xvalue = get_object_vars($value);
  5964. } else {
  5965. $this->debug("value is neither an array nor an object for XML Schema type $ns:$uqType");
  5966. $xvalue = array();
  5967. }
  5968. // toggle whether all elements are present - ideally should validate against schema
  5969. if (count($typeDef['elements']) != count($xvalue)){
  5970. $optionals = true;
  5971. }
  5972. foreach ($typeDef['elements'] as $eName => $attrs) {
  5973. if (!isset($xvalue[$eName])) {
  5974. if (isset($attrs['default'])) {
  5975. $xvalue[$eName] = $attrs['default'];
  5976. $this->debug('use default value of ' . $xvalue[$eName] . ' for element ' . $eName);
  5977. }
  5978. }
  5979. // if user took advantage of a minOccurs=0, then only serialize named parameters
  5980. if (isset($optionals)
  5981. && (!isset($xvalue[$eName]))
  5982. && ( (!isset($attrs['nillable'])) || $attrs['nillable'] != 'true')
  5983. ){
  5984. if (isset($attrs['minOccurs']) && $attrs['minOccurs'] <> '0') {
  5985. $this->debug("apparent error: no value provided for element $eName with minOccurs=" . $attrs['minOccurs']);
  5986. }
  5987. // do nothing
  5988. $this->debug("no value provided for complexType element $eName and element is not nillable, so serialize nothing");
  5989. } else {
  5990. // get value
  5991. if (isset($xvalue[$eName])) {
  5992. $v = $xvalue[$eName];
  5993. } else {
  5994. $v = null;
  5995. }
  5996. if (isset($attrs['form'])) {
  5997. $unqualified = ($attrs['form'] == 'unqualified');
  5998. } else {
  5999. $unqualified = false;
  6000. }
  6001. if (isset($attrs['maxOccurs']) && ($attrs['maxOccurs'] == 'unbounded' || $attrs['maxOccurs'] > 1) && isset($v) && is_array($v) && $this->isArraySimpleOrStruct($v) == 'arraySimple') {
  6002. $vv = $v;
  6003. foreach ($vv as $k => $v) {
  6004. if (isset($attrs['type']) || isset($attrs['ref'])) {
  6005. // serialize schema-defined type
  6006. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  6007. } else {
  6008. // serialize generic type (can this ever really happen?)
  6009. $this->debug("calling serialize_val() for $v, $eName, false, false, false, false, $use");
  6010. $xml .= $this->serialize_val($v, $eName, false, false, false, false, $use);
  6011. }
  6012. }
  6013. } else {
  6014. if (is_null($v) && isset($attrs['minOccurs']) && $attrs['minOccurs'] == '0') {
  6015. // do nothing
  6016. } elseif (is_null($v) && isset($attrs['nillable']) && $attrs['nillable'] == 'true') {
  6017. // TODO: serialize a nil correctly, but for now serialize schema-defined type
  6018. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  6019. } elseif (isset($attrs['type']) || isset($attrs['ref'])) {
  6020. // serialize schema-defined type
  6021. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  6022. } else {
  6023. // serialize generic type (can this ever really happen?)
  6024. $this->debug("calling serialize_val() for $v, $eName, false, false, false, false, $use");
  6025. $xml .= $this->serialize_val($v, $eName, false, false, false, false, $use);
  6026. }
  6027. }
  6028. }
  6029. }
  6030. } else {
  6031. $this->debug("no elements to serialize for XML Schema type $ns:$uqType");
  6032. }
  6033. return $xml;
  6034. }
  6035. /**
  6036. * adds an XML Schema complex type to the WSDL types
  6037. *
  6038. * @param string $name
  6039. * @param string $typeClass (complexType|simpleType|attribute)
  6040. * @param string $phpType currently supported are array and struct (php assoc array)
  6041. * @param string $compositor (all|sequence|choice)
  6042. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  6043. * @param array $elements e.g. array ( name => array(name=>'',type=>'') )
  6044. * @param array $attrs e.g. array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:string[]'))
  6045. * @param string $arrayType as namespace:name (xsd:string)
  6046. * @see nusoap_xmlschema
  6047. * @access public
  6048. */
  6049. function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType='') {
  6050. if (count($elements) > 0) {
  6051. $eElements = array();
  6052. foreach($elements as $n => $e){
  6053. // expand each element
  6054. $ee = array();
  6055. foreach ($e as $k => $v) {
  6056. $k = strpos($k,':') ? $this->expandQname($k) : $k;
  6057. $v = strpos($v,':') ? $this->expandQname($v) : $v;
  6058. $ee[$k] = $v;
  6059. }
  6060. $eElements[$n] = $ee;
  6061. }
  6062. $elements = $eElements;
  6063. }
  6064. if (count($attrs) > 0) {
  6065. foreach($attrs as $n => $a){
  6066. // expand each attribute
  6067. foreach ($a as $k => $v) {
  6068. $k = strpos($k,':') ? $this->expandQname($k) : $k;
  6069. $v = strpos($v,':') ? $this->expandQname($v) : $v;
  6070. $aa[$k] = $v;
  6071. }
  6072. $eAttrs[$n] = $aa;
  6073. }
  6074. $attrs = $eAttrs;
  6075. }
  6076. $restrictionBase = strpos($restrictionBase,':') ? $this->expandQname($restrictionBase) : $restrictionBase;
  6077. $arrayType = strpos($arrayType,':') ? $this->expandQname($arrayType) : $arrayType;
  6078. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  6079. $this->schemas[$typens][0]->addComplexType($name,$typeClass,$phpType,$compositor,$restrictionBase,$elements,$attrs,$arrayType);
  6080. }
  6081. /**
  6082. * adds an XML Schema simple type to the WSDL types
  6083. *
  6084. * @param string $name
  6085. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  6086. * @param string $typeClass (should always be simpleType)
  6087. * @param string $phpType (should always be scalar)
  6088. * @param array $enumeration array of values
  6089. * @see nusoap_xmlschema
  6090. * @access public
  6091. */
  6092. function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
  6093. $restrictionBase = strpos($restrictionBase,':') ? $this->expandQname($restrictionBase) : $restrictionBase;
  6094. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  6095. $this->schemas[$typens][0]->addSimpleType($name, $restrictionBase, $typeClass, $phpType, $enumeration);
  6096. }
  6097. /**
  6098. * adds an element to the WSDL types
  6099. *
  6100. * @param array $attrs attributes that must include name and type
  6101. * @see nusoap_xmlschema
  6102. * @access public
  6103. */
  6104. function addElement($attrs) {
  6105. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  6106. $this->schemas[$typens][0]->addElement($attrs);
  6107. }
  6108. /**
  6109. * register an operation with the server
  6110. *
  6111. * @param string $name operation (method) name
  6112. * @param array $in assoc array of input values: key = param name, value = param type
  6113. * @param array $out assoc array of output values: key = param name, value = param type
  6114. * @param string $namespace optional The namespace for the operation
  6115. * @param string $soapaction optional The soapaction for the operation
  6116. * @param string $style (rpc|document) optional The style for the operation Note: when 'document' is specified, parameter and return wrappers are created for you automatically
  6117. * @param string $use (encoded|literal) optional The use for the parameters (cannot mix right now)
  6118. * @param string $documentation optional The description to include in the WSDL
  6119. * @param string $encodingStyle optional (usually 'http://schemas.xmlsoap.org/soap/encoding/' for encoded)
  6120. * @access public
  6121. */
  6122. function addOperation($name, $in = false, $out = false, $namespace = false, $soapaction = false, $style = 'rpc', $use = 'encoded', $documentation = '', $encodingStyle = ''){
  6123. if ($use == 'encoded' && $encodingStyle == '') {
  6124. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  6125. }
  6126. if ($style == 'document') {
  6127. $elements = array();
  6128. foreach ($in as $n => $t) {
  6129. $elements[$n] = array('name' => $n, 'type' => $t, 'form' => 'unqualified');
  6130. }
  6131. $this->addComplexType($name . 'RequestType', 'complexType', 'struct', 'all', '', $elements);
  6132. $this->addElement(array('name' => $name, 'type' => $name . 'RequestType'));
  6133. $in = array('parameters' => 'tns:' . $name . '^');
  6134. $elements = array();
  6135. foreach ($out as $n => $t) {
  6136. $elements[$n] = array('name' => $n, 'type' => $t, 'form' => 'unqualified');
  6137. }
  6138. $this->addComplexType($name . 'ResponseType', 'complexType', 'struct', 'all', '', $elements);
  6139. $this->addElement(array('name' => $name . 'Response', 'type' => $name . 'ResponseType', 'form' => 'qualified'));
  6140. $out = array('parameters' => 'tns:' . $name . 'Response' . '^');
  6141. }
  6142. // get binding
  6143. $this->bindings[ $this->serviceName . 'Binding' ]['operations'][$name] =
  6144. array(
  6145. 'name' => $name,
  6146. 'binding' => $this->serviceName . 'Binding',
  6147. 'endpoint' => $this->endpoint,
  6148. 'soapAction' => $soapaction,
  6149. 'style' => $style,
  6150. 'input' => array(
  6151. 'use' => $use,
  6152. 'namespace' => $namespace,
  6153. 'encodingStyle' => $encodingStyle,
  6154. 'message' => $name . 'Request',
  6155. 'parts' => $in),
  6156. 'output' => array(
  6157. 'use' => $use,
  6158. 'namespace' => $namespace,
  6159. 'encodingStyle' => $encodingStyle,
  6160. 'message' => $name . 'Response',
  6161. 'parts' => $out),
  6162. 'namespace' => $namespace,
  6163. 'transport' => 'http://schemas.xmlsoap.org/soap/http',
  6164. 'documentation' => $documentation);
  6165. // add portTypes
  6166. // add messages
  6167. if($in)
  6168. {
  6169. foreach($in as $pName => $pType)
  6170. {
  6171. if(strpos($pType,':')) {
  6172. $pType = $this->getNamespaceFromPrefix($this->getPrefix($pType)).":".$this->getLocalPart($pType);
  6173. }
  6174. $this->messages[$name.'Request'][$pName] = $pType;
  6175. }
  6176. } else {
  6177. $this->messages[$name.'Request']= '0';
  6178. }
  6179. if($out)
  6180. {
  6181. foreach($out as $pName => $pType)
  6182. {
  6183. if(strpos($pType,':')) {
  6184. $pType = $this->getNamespaceFromPrefix($this->getPrefix($pType)).":".$this->getLocalPart($pType);
  6185. }
  6186. $this->messages[$name.'Response'][$pName] = $pType;
  6187. }
  6188. } else {
  6189. $this->messages[$name.'Response']= '0';
  6190. }
  6191. return true;
  6192. }
  6193. }
  6194. ?><?php
  6195. /**
  6196. *
  6197. * nusoap_parser class parses SOAP XML messages into native PHP values
  6198. *
  6199. * @author Dietrich Ayala <dietrich@ganx4.com>
  6200. * @author Scott Nichol <snichol@users.sourceforge.net>
  6201. * @access public
  6202. */
  6203. class nusoap_parser extends nusoap_base {
  6204. var $xml = '';
  6205. var $xml_encoding = '';
  6206. var $method = '';
  6207. var $root_struct = '';
  6208. var $root_struct_name = '';
  6209. var $root_struct_namespace = '';
  6210. var $root_header = '';
  6211. var $document = ''; // incoming SOAP body (text)
  6212. // determines where in the message we are (envelope,header,body,method)
  6213. var $status = '';
  6214. var $position = 0;
  6215. var $depth = 0;
  6216. var $default_namespace = '';
  6217. var $namespaces = array();
  6218. var $message = array();
  6219. var $parent = '';
  6220. var $fault = false;
  6221. var $fault_code = '';
  6222. var $fault_str = '';
  6223. var $fault_detail = '';
  6224. var $depth_array = array();
  6225. var $debug_flag = true;
  6226. var $soapresponse = NULL; // parsed SOAP Body
  6227. var $soapheader = NULL; // parsed SOAP Header
  6228. var $responseHeaders = ''; // incoming SOAP headers (text)
  6229. var $body_position = 0;
  6230. // for multiref parsing:
  6231. // array of id => pos
  6232. var $ids = array();
  6233. // array of id => hrefs => pos
  6234. var $multirefs = array();
  6235. // toggle for auto-decoding element content
  6236. var $decode_utf8 = true;
  6237. /**
  6238. * constructor that actually does the parsing
  6239. *
  6240. * @param string $xml SOAP message
  6241. * @param string $encoding character encoding scheme of message
  6242. * @param string $method method for which XML is parsed (unused?)
  6243. * @param string $decode_utf8 whether to decode UTF-8 to ISO-8859-1
  6244. * @access public
  6245. */
  6246. function nusoap_parser($xml,$encoding='UTF-8',$method='',$decode_utf8=true){
  6247. parent::nusoap_base();
  6248. $this->xml = $xml;
  6249. $this->xml_encoding = $encoding;
  6250. $this->method = $method;
  6251. $this->decode_utf8 = $decode_utf8;
  6252. // Check whether content has been read.
  6253. if(!empty($xml)){
  6254. // Check XML encoding
  6255. $pos_xml = strpos($xml, '<?xml');
  6256. if ($pos_xml !== FALSE) {
  6257. $xml_decl = substr($xml, $pos_xml, strpos($xml, '?>', $pos_xml + 2) - $pos_xml + 1);
  6258. if (preg_match("/encoding=[\"']([^\"']*)[\"']/", $xml_decl, $res)) {
  6259. $xml_encoding = $res[1];
  6260. if (strtoupper($xml_encoding) != $encoding) {
  6261. $err = "Charset from HTTP Content-Type '" . $encoding . "' does not match encoding from XML declaration '" . $xml_encoding . "'";
  6262. $this->debug($err);
  6263. if ($encoding != 'ISO-8859-1' || strtoupper($xml_encoding) != 'UTF-8') {
  6264. $this->setError($err);
  6265. return;
  6266. }
  6267. // when HTTP says ISO-8859-1 (the default) and XML says UTF-8 (the typical), assume the other endpoint is just sloppy and proceed
  6268. } else {
  6269. $this->debug('Charset from HTTP Content-Type matches encoding from XML declaration');
  6270. }
  6271. } else {
  6272. $this->debug('No encoding specified in XML declaration');
  6273. }
  6274. } else {
  6275. $this->debug('No XML declaration');
  6276. }
  6277. $this->debug('Entering nusoap_parser(), length='.strlen($xml).', encoding='.$encoding);
  6278. // Create an XML parser - why not xml_parser_create_ns?
  6279. $this->parser = xml_parser_create($this->xml_encoding);
  6280. // Set the options for parsing the XML data.
  6281. //xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  6282. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
  6283. xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->xml_encoding);
  6284. // Set the object for the parser.
  6285. xml_set_object($this->parser, $this);
  6286. // Set the element handlers for the parser.
  6287. xml_set_element_handler($this->parser, 'start_element','end_element');
  6288. xml_set_character_data_handler($this->parser,'character_data');
  6289. // Parse the XML file.
  6290. if(!xml_parse($this->parser,$xml,true)){
  6291. // Display an error message.
  6292. $err = sprintf('XML error parsing SOAP payload on line %d: %s',
  6293. xml_get_current_line_number($this->parser),
  6294. xml_error_string(xml_get_error_code($this->parser)));
  6295. $this->debug($err);
  6296. $this->debug("XML payload:\n" . $xml);
  6297. $this->setError($err);
  6298. } else {
  6299. $this->debug('in nusoap_parser ctor, message:');
  6300. $this->appendDebug($this->varDump($this->message));
  6301. $this->debug('parsed successfully, found root struct: '.$this->root_struct.' of name '.$this->root_struct_name);
  6302. // get final value
  6303. $this->soapresponse = $this->message[$this->root_struct]['result'];
  6304. // get header value
  6305. if($this->root_header != '' && isset($this->message[$this->root_header]['result'])){
  6306. $this->soapheader = $this->message[$this->root_header]['result'];
  6307. }
  6308. // resolve hrefs/ids
  6309. if(sizeof($this->multirefs) > 0){
  6310. foreach($this->multirefs as $id => $hrefs){
  6311. $this->debug('resolving multirefs for id: '.$id);
  6312. $idVal = $this->buildVal($this->ids[$id]);
  6313. if (is_array($idVal) && isset($idVal['!id'])) {
  6314. unset($idVal['!id']);
  6315. }
  6316. foreach($hrefs as $refPos => $ref){
  6317. $this->debug('resolving href at pos '.$refPos);
  6318. $this->multirefs[$id][$refPos] = $idVal;
  6319. }
  6320. }
  6321. }
  6322. }
  6323. xml_parser_free($this->parser);
  6324. } else {
  6325. $this->debug('xml was empty, didn\'t parse!');
  6326. $this->setError('xml was empty, didn\'t parse!');
  6327. }
  6328. }
  6329. /**
  6330. * start-element handler
  6331. *
  6332. * @param resource $parser XML parser object
  6333. * @param string $name element name
  6334. * @param array $attrs associative array of attributes
  6335. * @access private
  6336. */
  6337. function start_element($parser, $name, $attrs) {
  6338. // position in a total number of elements, starting from 0
  6339. // update class level pos
  6340. $pos = $this->position++;
  6341. // and set mine
  6342. $this->message[$pos] = array('pos' => $pos,'children'=>'','cdata'=>'');
  6343. // depth = how many levels removed from root?
  6344. // set mine as current global depth and increment global depth value
  6345. $this->message[$pos]['depth'] = $this->depth++;
  6346. // else add self as child to whoever the current parent is
  6347. if($pos != 0){
  6348. $this->message[$this->parent]['children'] .= '|'.$pos;
  6349. }
  6350. // set my parent
  6351. $this->message[$pos]['parent'] = $this->parent;
  6352. // set self as current parent
  6353. $this->parent = $pos;
  6354. // set self as current value for this depth
  6355. $this->depth_array[$this->depth] = $pos;
  6356. // get element prefix
  6357. if(strpos($name,':')){
  6358. // get ns prefix
  6359. $prefix = substr($name,0,strpos($name,':'));
  6360. // get unqualified name
  6361. $name = substr(strstr($name,':'),1);
  6362. }
  6363. // set status
  6364. if ($name == 'Envelope' && $this->status == '') {
  6365. $this->status = 'envelope';
  6366. } elseif ($name == 'Header' && $this->status == 'envelope') {
  6367. $this->root_header = $pos;
  6368. $this->status = 'header';
  6369. } elseif ($name == 'Body' && $this->status == 'envelope'){
  6370. $this->status = 'body';
  6371. $this->body_position = $pos;
  6372. // set method
  6373. } elseif($this->status == 'body' && $pos == ($this->body_position+1)) {
  6374. $this->status = 'method';
  6375. $this->root_struct_name = $name;
  6376. $this->root_struct = $pos;
  6377. $this->message[$pos]['type'] = 'struct';
  6378. $this->debug("found root struct $this->root_struct_name, pos $this->root_struct");
  6379. }
  6380. // set my status
  6381. $this->message[$pos]['status'] = $this->status;
  6382. // set name
  6383. $this->message[$pos]['name'] = htmlspecialchars($name);
  6384. // set attrs
  6385. $this->message[$pos]['attrs'] = $attrs;
  6386. // loop through atts, logging ns and type declarations
  6387. $attstr = '';
  6388. foreach($attrs as $key => $value){
  6389. $key_prefix = $this->getPrefix($key);
  6390. $key_localpart = $this->getLocalPart($key);
  6391. // if ns declarations, add to class level array of valid namespaces
  6392. if($key_prefix == 'xmlns'){
  6393. if(preg_match('/^http:\/\/www.w3.org\/[0-9]{4}\/XMLSchema$/',$value)){
  6394. $this->XMLSchemaVersion = $value;
  6395. $this->namespaces['xsd'] = $this->XMLSchemaVersion;
  6396. $this->namespaces['xsi'] = $this->XMLSchemaVersion.'-instance';
  6397. }
  6398. $this->namespaces[$key_localpart] = $value;
  6399. // set method namespace
  6400. if($name == $this->root_struct_name){
  6401. $this->methodNamespace = $value;
  6402. }
  6403. // if it's a type declaration, set type
  6404. } elseif($key_localpart == 'type'){
  6405. if (isset($this->message[$pos]['type']) && $this->message[$pos]['type'] == 'array') {
  6406. // do nothing: already processed arrayType
  6407. } else {
  6408. $value_prefix = $this->getPrefix($value);
  6409. $value_localpart = $this->getLocalPart($value);
  6410. $this->message[$pos]['type'] = $value_localpart;
  6411. $this->message[$pos]['typePrefix'] = $value_prefix;
  6412. if(isset($this->namespaces[$value_prefix])){
  6413. $this->message[$pos]['type_namespace'] = $this->namespaces[$value_prefix];
  6414. } else if(isset($attrs['xmlns:'.$value_prefix])) {
  6415. $this->message[$pos]['type_namespace'] = $attrs['xmlns:'.$value_prefix];
  6416. }
  6417. // should do something here with the namespace of specified type?
  6418. }
  6419. } elseif($key_localpart == 'arrayType'){
  6420. $this->message[$pos]['type'] = 'array';
  6421. /* do arrayType ereg here
  6422. [1] arrayTypeValue ::= atype asize
  6423. [2] atype ::= QName rank*
  6424. [3] rank ::= '[' (',')* ']'
  6425. [4] asize ::= '[' length~ ']'
  6426. [5] length ::= nextDimension* Digit+
  6427. [6] nextDimension ::= Digit+ ','
  6428. */
  6429. $expr = '/([A-Za-z0-9_]+):([A-Za-z]+[A-Za-z0-9_]+)\[([0-9]+),?([0-9]*)\]/';
  6430. if(preg_match($expr,$value,$regs)){
  6431. $this->message[$pos]['typePrefix'] = $regs[1];
  6432. $this->message[$pos]['arrayTypePrefix'] = $regs[1];
  6433. if (isset($this->namespaces[$regs[1]])) {
  6434. $this->message[$pos]['arrayTypeNamespace'] = $this->namespaces[$regs[1]];
  6435. } else if (isset($attrs['xmlns:'.$regs[1]])) {
  6436. $this->message[$pos]['arrayTypeNamespace'] = $attrs['xmlns:'.$regs[1]];
  6437. }
  6438. $this->message[$pos]['arrayType'] = $regs[2];
  6439. $this->message[$pos]['arraySize'] = $regs[3];
  6440. $this->message[$pos]['arrayCols'] = $regs[4];
  6441. }
  6442. // specifies nil value (or not)
  6443. } elseif ($key_localpart == 'nil'){
  6444. $this->message[$pos]['nil'] = ($value == 'true' || $value == '1');
  6445. // some other attribute
  6446. } elseif ($key != 'href' && $key != 'xmlns' && $key_localpart != 'encodingStyle' && $key_localpart != 'root') {
  6447. $this->message[$pos]['xattrs']['!' . $key] = $value;
  6448. }
  6449. if ($key == 'xmlns') {
  6450. $this->default_namespace = $value;
  6451. }
  6452. // log id
  6453. if($key == 'id'){
  6454. $this->ids[$value] = $pos;
  6455. }
  6456. // root
  6457. if($key_localpart == 'root' && $value == 1){
  6458. $this->status = 'method';
  6459. $this->root_struct_name = $name;
  6460. $this->root_struct = $pos;
  6461. $this->debug("found root struct $this->root_struct_name, pos $pos");
  6462. }
  6463. // for doclit
  6464. $attstr .= " $key=\"$value\"";
  6465. }
  6466. // get namespace - must be done after namespace atts are processed
  6467. if(isset($prefix)){
  6468. $this->message[$pos]['namespace'] = $this->namespaces[$prefix];
  6469. $this->default_namespace = $this->namespaces[$prefix];
  6470. } else {
  6471. $this->message[$pos]['namespace'] = $this->default_namespace;
  6472. }
  6473. if($this->status == 'header'){
  6474. if ($this->root_header != $pos) {
  6475. $this->responseHeaders .= "<" . (isset($prefix) ? $prefix . ':' : '') . "$name$attstr>";
  6476. }
  6477. } elseif($this->root_struct_name != ''){
  6478. $this->document .= "<" . (isset($prefix) ? $prefix . ':' : '') . "$name$attstr>";
  6479. }
  6480. }
  6481. /**
  6482. * end-element handler
  6483. *
  6484. * @param resource $parser XML parser object
  6485. * @param string $name element name
  6486. * @access private
  6487. */
  6488. function end_element($parser, $name) {
  6489. // position of current element is equal to the last value left in depth_array for my depth
  6490. $pos = $this->depth_array[$this->depth--];
  6491. // get element prefix
  6492. if(strpos($name,':')){
  6493. // get ns prefix
  6494. $prefix = substr($name,0,strpos($name,':'));
  6495. // get unqualified name
  6496. $name = substr(strstr($name,':'),1);
  6497. }
  6498. // build to native type
  6499. if(isset($this->body_position) && $pos > $this->body_position){
  6500. // deal w/ multirefs
  6501. if(isset($this->message[$pos]['attrs']['href'])){
  6502. // get id
  6503. $id = substr($this->message[$pos]['attrs']['href'],1);
  6504. // add placeholder to href array
  6505. $this->multirefs[$id][$pos] = 'placeholder';
  6506. // add set a reference to it as the result value
  6507. $this->message[$pos]['result'] =& $this->multirefs[$id][$pos];
  6508. // build complexType values
  6509. } elseif($this->message[$pos]['children'] != ''){
  6510. // if result has already been generated (struct/array)
  6511. if(!isset($this->message[$pos]['result'])){
  6512. $this->message[$pos]['result'] = $this->buildVal($pos);
  6513. }
  6514. // build complexType values of attributes and possibly simpleContent
  6515. } elseif (isset($this->message[$pos]['xattrs'])) {
  6516. if (isset($this->message[$pos]['nil']) && $this->message[$pos]['nil']) {
  6517. $this->message[$pos]['xattrs']['!'] = null;
  6518. } elseif (isset($this->message[$pos]['cdata']) && trim($this->message[$pos]['cdata']) != '') {
  6519. if (isset($this->message[$pos]['type'])) {
  6520. $this->message[$pos]['xattrs']['!'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$pos]['type'], isset($this->message[$pos]['type_namespace']) ? $this->message[$pos]['type_namespace'] : '');
  6521. } else {
  6522. $parent = $this->message[$pos]['parent'];
  6523. if (isset($this->message[$parent]['type']) && ($this->message[$parent]['type'] == 'array') && isset($this->message[$parent]['arrayType'])) {
  6524. $this->message[$pos]['xattrs']['!'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$parent]['arrayType'], isset($this->message[$parent]['arrayTypeNamespace']) ? $this->message[$parent]['arrayTypeNamespace'] : '');
  6525. } else {
  6526. $this->message[$pos]['xattrs']['!'] = $this->message[$pos]['cdata'];
  6527. }
  6528. }
  6529. }
  6530. $this->message[$pos]['result'] = $this->message[$pos]['xattrs'];
  6531. // set value of simpleType (or nil complexType)
  6532. } else {
  6533. //$this->debug('adding data for scalar value '.$this->message[$pos]['name'].' of value '.$this->message[$pos]['cdata']);
  6534. if (isset($this->message[$pos]['nil']) && $this->message[$pos]['nil']) {
  6535. $this->message[$pos]['xattrs']['!'] = null;
  6536. } elseif (isset($this->message[$pos]['type'])) {
  6537. $this->message[$pos]['result'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$pos]['type'], isset($this->message[$pos]['type_namespace']) ? $this->message[$pos]['type_namespace'] : '');
  6538. } else {
  6539. $parent = $this->message[$pos]['parent'];
  6540. if (isset($this->message[$parent]['type']) && ($this->message[$parent]['type'] == 'array') && isset($this->message[$parent]['arrayType'])) {
  6541. $this->message[$pos]['result'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$parent]['arrayType'], isset($this->message[$parent]['arrayTypeNamespace']) ? $this->message[$parent]['arrayTypeNamespace'] : '');
  6542. } else {
  6543. $this->message[$pos]['result'] = $this->message[$pos]['cdata'];
  6544. }
  6545. }
  6546. /* add value to parent's result, if parent is struct/array
  6547. $parent = $this->message[$pos]['parent'];
  6548. if($this->message[$parent]['type'] != 'map'){
  6549. if(strtolower($this->message[$parent]['type']) == 'array'){
  6550. $this->message[$parent]['result'][] = $this->message[$pos]['result'];
  6551. } else {
  6552. $this->message[$parent]['result'][$this->message[$pos]['name']] = $this->message[$pos]['result'];
  6553. }
  6554. }
  6555. */
  6556. }
  6557. }
  6558. // for doclit
  6559. if($this->status == 'header'){
  6560. if ($this->root_header != $pos) {
  6561. $this->responseHeaders .= "</" . (isset($prefix) ? $prefix . ':' : '') . "$name>";
  6562. }
  6563. } elseif($pos >= $this->root_struct){
  6564. $this->document .= "</" . (isset($prefix) ? $prefix . ':' : '') . "$name>";
  6565. }
  6566. // switch status
  6567. if ($pos == $this->root_struct){
  6568. $this->status = 'body';
  6569. $this->root_struct_namespace = $this->message[$pos]['namespace'];
  6570. } elseif ($pos == $this->root_header) {
  6571. $this->status = 'envelope';
  6572. } elseif ($name == 'Body' && $this->status == 'body') {
  6573. $this->status = 'envelope';
  6574. } elseif ($name == 'Header' && $this->status == 'header') { // will never happen
  6575. $this->status = 'envelope';
  6576. } elseif ($name == 'Envelope' && $this->status == 'envelope') {
  6577. $this->status = '';
  6578. }
  6579. // set parent back to my parent
  6580. $this->parent = $this->message[$pos]['parent'];
  6581. }
  6582. /**
  6583. * element content handler
  6584. *
  6585. * @param resource $parser XML parser object
  6586. * @param string $data element content
  6587. * @access private
  6588. */
  6589. function character_data($parser, $data){
  6590. $pos = $this->depth_array[$this->depth];
  6591. if ($this->xml_encoding=='UTF-8'){
  6592. // TODO: add an option to disable this for folks who want
  6593. // raw UTF-8 that, e.g., might not map to iso-8859-1
  6594. // TODO: this can also be handled with xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
  6595. if($this->decode_utf8){
  6596. $data = utf8_decode($data);
  6597. }
  6598. }
  6599. $this->message[$pos]['cdata'] .= $data;
  6600. // for doclit
  6601. if($this->status == 'header'){
  6602. $this->responseHeaders .= $data;
  6603. } else {
  6604. $this->document .= $data;
  6605. }
  6606. }
  6607. /**
  6608. * get the parsed message (SOAP Body)
  6609. *
  6610. * @return mixed
  6611. * @access public
  6612. * @deprecated use get_soapbody instead
  6613. */
  6614. function get_response(){
  6615. return $this->soapresponse;
  6616. }
  6617. /**
  6618. * get the parsed SOAP Body (NULL if there was none)
  6619. *
  6620. * @return mixed
  6621. * @access public
  6622. */
  6623. function get_soapbody(){
  6624. return $this->soapresponse;
  6625. }
  6626. /**
  6627. * get the parsed SOAP Header (NULL if there was none)
  6628. *
  6629. * @return mixed
  6630. * @access public
  6631. */
  6632. function get_soapheader(){
  6633. return $this->soapheader;
  6634. }
  6635. /**
  6636. * get the unparsed SOAP Header
  6637. *
  6638. * @return string XML or empty if no Header
  6639. * @access public
  6640. */
  6641. function getHeaders(){
  6642. return $this->responseHeaders;
  6643. }
  6644. /**
  6645. * decodes simple types into PHP variables
  6646. *
  6647. * @param string $value value to decode
  6648. * @param string $type XML type to decode
  6649. * @param string $typens XML type namespace to decode
  6650. * @return mixed PHP value
  6651. * @access private
  6652. */
  6653. function decodeSimple($value, $type, $typens) {
  6654. // TODO: use the namespace!
  6655. if ((!isset($type)) || $type == 'string' || $type == 'long' || $type == 'unsignedLong') {
  6656. return (string) $value;
  6657. }
  6658. if ($type == 'int' || $type == 'integer' || $type == 'short' || $type == 'byte') {
  6659. return (int) $value;
  6660. }
  6661. if ($type == 'float' || $type == 'double' || $type == 'decimal') {
  6662. return (double) $value;
  6663. }
  6664. if ($type == 'boolean') {
  6665. if (strtolower($value) == 'false' || strtolower($value) == 'f') {
  6666. return false;
  6667. }
  6668. return (boolean) $value;
  6669. }
  6670. if ($type == 'base64' || $type == 'base64Binary') {
  6671. $this->debug('Decode base64 value');
  6672. return base64_decode($value);
  6673. }
  6674. // obscure numeric types
  6675. if ($type == 'nonPositiveInteger' || $type == 'negativeInteger'
  6676. || $type == 'nonNegativeInteger' || $type == 'positiveInteger'
  6677. || $type == 'unsignedInt'
  6678. || $type == 'unsignedShort' || $type == 'unsignedByte') {
  6679. return (int) $value;
  6680. }
  6681. // bogus: parser treats array with no elements as a simple type
  6682. if ($type == 'array') {
  6683. return array();
  6684. }
  6685. // everything else
  6686. return (string) $value;
  6687. }
  6688. /**
  6689. * builds response structures for compound values (arrays/structs)
  6690. * and scalars
  6691. *
  6692. * @param integer $pos position in node tree
  6693. * @return mixed PHP value
  6694. * @access private
  6695. */
  6696. function buildVal($pos){
  6697. if(!isset($this->message[$pos]['type'])){
  6698. $this->message[$pos]['type'] = '';
  6699. }
  6700. $this->debug('in buildVal() for '.$this->message[$pos]['name']."(pos $pos) of type ".$this->message[$pos]['type']);
  6701. // if there are children...
  6702. if($this->message[$pos]['children'] != ''){
  6703. $this->debug('in buildVal, there are children');
  6704. $children = explode('|',$this->message[$pos]['children']);
  6705. array_shift($children); // knock off empty
  6706. // md array
  6707. if(isset($this->message[$pos]['arrayCols']) && $this->message[$pos]['arrayCols'] != ''){
  6708. $r=0; // rowcount
  6709. $c=0; // colcount
  6710. foreach($children as $child_pos){
  6711. $this->debug("in buildVal, got an MD array element: $r, $c");
  6712. $params[$r][] = $this->message[$child_pos]['result'];
  6713. $c++;
  6714. if($c == $this->message[$pos]['arrayCols']){
  6715. $c = 0;
  6716. $r++;
  6717. }
  6718. }
  6719. // array
  6720. } elseif($this->message[$pos]['type'] == 'array' || $this->message[$pos]['type'] == 'Array'){
  6721. $this->debug('in buildVal, adding array '.$this->message[$pos]['name']);
  6722. foreach($children as $child_pos){
  6723. $params[] = &$this->message[$child_pos]['result'];
  6724. }
  6725. // apache Map type: java hashtable
  6726. } elseif($this->message[$pos]['type'] == 'Map' && $this->message[$pos]['type_namespace'] == 'http://xml.apache.org/xml-soap'){
  6727. $this->debug('in buildVal, Java Map '.$this->message[$pos]['name']);
  6728. foreach($children as $child_pos){
  6729. $kv = explode("|",$this->message[$child_pos]['children']);
  6730. $params[$this->message[$kv[1]]['result']] = &$this->message[$kv[2]]['result'];
  6731. }
  6732. // generic compound type
  6733. //} elseif($this->message[$pos]['type'] == 'SOAPStruct' || $this->message[$pos]['type'] == 'struct') {
  6734. } else {
  6735. // Apache Vector type: treat as an array
  6736. $this->debug('in buildVal, adding Java Vector or generic compound type '.$this->message[$pos]['name']);
  6737. if ($this->message[$pos]['type'] == 'Vector' && $this->message[$pos]['type_namespace'] == 'http://xml.apache.org/xml-soap') {
  6738. $notstruct = 1;
  6739. } else {
  6740. $notstruct = 0;
  6741. }
  6742. //
  6743. foreach($children as $child_pos){
  6744. if($notstruct){
  6745. $params[] = &$this->message[$child_pos]['result'];
  6746. } else {
  6747. if (isset($params[$this->message[$child_pos]['name']])) {
  6748. // de-serialize repeated element name into an array
  6749. if ((!is_array($params[$this->message[$child_pos]['name']])) || (!isset($params[$this->message[$child_pos]['name']][0]))) {
  6750. $params[$this->message[$child_pos]['name']] = array($params[$this->message[$child_pos]['name']]);
  6751. }
  6752. $params[$this->message[$child_pos]['name']][] = &$this->message[$child_pos]['result'];
  6753. } else {
  6754. $params[$this->message[$child_pos]['name']] = &$this->message[$child_pos]['result'];
  6755. }
  6756. }
  6757. }
  6758. }
  6759. if (isset($this->message[$pos]['xattrs'])) {
  6760. $this->debug('in buildVal, handling attributes');
  6761. foreach ($this->message[$pos]['xattrs'] as $n => $v) {
  6762. $params[$n] = $v;
  6763. }
  6764. }
  6765. // handle simpleContent
  6766. if (isset($this->message[$pos]['cdata']) && trim($this->message[$pos]['cdata']) != '') {
  6767. $this->debug('in buildVal, handling simpleContent');
  6768. if (isset($this->message[$pos]['type'])) {
  6769. $params['!'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$pos]['type'], isset($this->message[$pos]['type_namespace']) ? $this->message[$pos]['type_namespace'] : '');
  6770. } else {
  6771. $parent = $this->message[$pos]['parent'];
  6772. if (isset($this->message[$parent]['type']) && ($this->message[$parent]['type'] == 'array') && isset($this->message[$parent]['arrayType'])) {
  6773. $params['!'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$parent]['arrayType'], isset($this->message[$parent]['arrayTypeNamespace']) ? $this->message[$parent]['arrayTypeNamespace'] : '');
  6774. } else {
  6775. $params['!'] = $this->message[$pos]['cdata'];
  6776. }
  6777. }
  6778. }
  6779. $ret = is_array($params) ? $params : array();
  6780. $this->debug('in buildVal, return:');
  6781. $this->appendDebug($this->varDump($ret));
  6782. return $ret;
  6783. } else {
  6784. $this->debug('in buildVal, no children, building scalar');
  6785. $cdata = isset($this->message[$pos]['cdata']) ? $this->message[$pos]['cdata'] : '';
  6786. if (isset($this->message[$pos]['type'])) {
  6787. $ret = $this->decodeSimple($cdata, $this->message[$pos]['type'], isset($this->message[$pos]['type_namespace']) ? $this->message[$pos]['type_namespace'] : '');
  6788. $this->debug("in buildVal, return: $ret");
  6789. return $ret;
  6790. }
  6791. $parent = $this->message[$pos]['parent'];
  6792. if (isset($this->message[$parent]['type']) && ($this->message[$parent]['type'] == 'array') && isset($this->message[$parent]['arrayType'])) {
  6793. $ret = $this->decodeSimple($cdata, $this->message[$parent]['arrayType'], isset($this->message[$parent]['arrayTypeNamespace']) ? $this->message[$parent]['arrayTypeNamespace'] : '');
  6794. $this->debug("in buildVal, return: $ret");
  6795. return $ret;
  6796. }
  6797. $ret = $this->message[$pos]['cdata'];
  6798. $this->debug("in buildVal, return: $ret");
  6799. return $ret;
  6800. }
  6801. }
  6802. }
  6803. /**
  6804. * Backward compatibility
  6805. */
  6806. class soap_parser extends nusoap_parser {
  6807. }
  6808. ?><?php
  6809. /**
  6810. *
  6811. * [nu]soapclient higher level class for easy usage.
  6812. *
  6813. * usage:
  6814. *
  6815. * // instantiate client with server info
  6816. * $soapclient = new nusoap_client( string path [ ,mixed wsdl] );
  6817. *
  6818. * // call method, get results
  6819. * echo $soapclient->call( string methodname [ ,array parameters] );
  6820. *
  6821. * // bye bye client
  6822. * unset($soapclient);
  6823. *
  6824. * @author Dietrich Ayala <dietrich@ganx4.com>
  6825. * @author Scott Nichol <snichol@users.sourceforge.net>
  6826. * @access public
  6827. */
  6828. class nusoap_client extends nusoap_base {
  6829. var $username = ''; // Username for HTTP authentication
  6830. var $password = ''; // Password for HTTP authentication
  6831. var $authtype = ''; // Type of HTTP authentication
  6832. var $certRequest = array(); // Certificate for HTTP SSL authentication
  6833. var $requestHeaders = false; // SOAP headers in request (text)
  6834. var $responseHeaders = ''; // SOAP headers from response (incomplete namespace resolution) (text)
  6835. var $responseHeader = NULL; // SOAP Header from response (parsed)
  6836. var $document = ''; // SOAP body response portion (incomplete namespace resolution) (text)
  6837. var $endpoint;
  6838. var $forceEndpoint = ''; // overrides WSDL endpoint
  6839. var $proxyhost = '';
  6840. var $proxyport = '';
  6841. var $proxyusername = '';
  6842. var $proxypassword = '';
  6843. var $portName = ''; // port name to use in WSDL
  6844. var $xml_encoding = ''; // character set encoding of incoming (response) messages
  6845. var $http_encoding = false;
  6846. var $timeout = 0; // HTTP connection timeout
  6847. var $response_timeout = 30; // HTTP response timeout
  6848. var $endpointType = ''; // soap|wsdl, empty for WSDL initialization error
  6849. var $persistentConnection = false;
  6850. var $defaultRpcParams = false; // This is no longer used
  6851. var $request = ''; // HTTP request
  6852. var $response = ''; // HTTP response
  6853. var $responseData = ''; // SOAP payload of response
  6854. var $cookies = array(); // Cookies from response or for request
  6855. var $decode_utf8 = true; // toggles whether the parser decodes element content w/ utf8_decode()
  6856. var $operations = array(); // WSDL operations, empty for WSDL initialization error
  6857. var $curl_options = array(); // User-specified cURL options
  6858. var $bindingType = ''; // WSDL operation binding type
  6859. var $use_curl = false; // whether to always try to use cURL
  6860. /*
  6861. * fault related variables
  6862. */
  6863. /**
  6864. * @var fault
  6865. * @access public
  6866. */
  6867. var $fault;
  6868. /**
  6869. * @var faultcode
  6870. * @access public
  6871. */
  6872. var $faultcode;
  6873. /**
  6874. * @var faultstring
  6875. * @access public
  6876. */
  6877. var $faultstring;
  6878. /**
  6879. * @var faultdetail
  6880. * @access public
  6881. */
  6882. var $faultdetail;
  6883. /**
  6884. * constructor
  6885. *
  6886. * @param mixed $endpoint SOAP server or WSDL URL (string), or wsdl instance (object)
  6887. * @param mixed $wsdl optional, set to 'wsdl' or true if using WSDL
  6888. * @param string $proxyhost optional
  6889. * @param string $proxyport optional
  6890. * @param string $proxyusername optional
  6891. * @param string $proxypassword optional
  6892. * @param integer $timeout set the connection timeout
  6893. * @param integer $response_timeout set the response timeout
  6894. * @param string $portName optional portName in WSDL document
  6895. * @access public
  6896. */
  6897. function nusoap_client($endpoint,$wsdl = false,$proxyhost = false,$proxyport = false,$proxyusername = false, $proxypassword = false, $timeout = 0, $response_timeout = 30, $portName = ''){
  6898. parent::nusoap_base();
  6899. $this->endpoint = $endpoint;
  6900. $this->proxyhost = $proxyhost;
  6901. $this->proxyport = $proxyport;
  6902. $this->proxyusername = $proxyusername;
  6903. $this->proxypassword = $proxypassword;
  6904. $this->timeout = $timeout;
  6905. $this->response_timeout = $response_timeout;
  6906. $this->portName = $portName;
  6907. $this->debug("ctor wsdl=$wsdl timeout=$timeout response_timeout=$response_timeout");
  6908. $this->appendDebug('endpoint=' . $this->varDump($endpoint));
  6909. // make values
  6910. if($wsdl){
  6911. if (is_object($endpoint) && (get_class($endpoint) == 'wsdl')) {
  6912. $this->wsdl = $endpoint;
  6913. $this->endpoint = $this->wsdl->wsdl;
  6914. $this->wsdlFile = $this->endpoint;
  6915. $this->debug('existing wsdl instance created from ' . $this->endpoint);
  6916. $this->checkWSDL();
  6917. } else {
  6918. $this->wsdlFile = $this->endpoint;
  6919. $this->wsdl = null;
  6920. $this->debug('will use lazy evaluation of wsdl from ' . $this->endpoint);
  6921. }
  6922. $this->endpointType = 'wsdl';
  6923. } else {
  6924. $this->debug("instantiate SOAP with endpoint at $endpoint");
  6925. $this->endpointType = 'soap';
  6926. }
  6927. }
  6928. /**
  6929. * calls method, returns PHP native type
  6930. *
  6931. * @param string $operation SOAP server URL or path
  6932. * @param mixed $params An array, associative or simple, of the parameters
  6933. * for the method call, or a string that is the XML
  6934. * for the call. For rpc style, this call will
  6935. * wrap the XML in a tag named after the method, as
  6936. * well as the SOAP Envelope and Body. For document
  6937. * style, this will only wrap with the Envelope and Body.
  6938. * IMPORTANT: when using an array with document style,
  6939. * in which case there
  6940. * is really one parameter, the root of the fragment
  6941. * used in the call, which encloses what programmers
  6942. * normally think of parameters. A parameter array
  6943. * *must* include the wrapper.
  6944. * @param string $namespace optional method namespace (WSDL can override)
  6945. * @param string $soapAction optional SOAPAction value (WSDL can override)
  6946. * @param mixed $headers optional string of XML with SOAP header content, or array of soapval objects for SOAP headers, or associative array
  6947. * @param boolean $rpcParams optional (no longer used)
  6948. * @param string $style optional (rpc|document) the style to use when serializing parameters (WSDL can override)
  6949. * @param string $use optional (encoded|literal) the use when serializing parameters (WSDL can override)
  6950. * @return mixed response from SOAP call, normally an associative array mirroring the structure of the XML response, false for certain fatal errors
  6951. * @access public
  6952. */
  6953. function call($operation,$params=array(),$namespace='http://tempuri.org',$soapAction='',$headers=false,$rpcParams=null,$style='rpc',$use='encoded'){
  6954. $this->operation = $operation;
  6955. $this->fault = false;
  6956. $this->setError('');
  6957. $this->request = '';
  6958. $this->response = '';
  6959. $this->responseData = '';
  6960. $this->faultstring = '';
  6961. $this->faultcode = '';
  6962. $this->opData = array();
  6963. $this->debug("call: operation=$operation, namespace=$namespace, soapAction=$soapAction, rpcParams=$rpcParams, style=$style, use=$use, endpointType=$this->endpointType");
  6964. $this->appendDebug('params=' . $this->varDump($params));
  6965. $this->appendDebug('headers=' . $this->varDump($headers));
  6966. if ($headers) {
  6967. $this->requestHeaders = $headers;
  6968. }
  6969. if ($this->endpointType == 'wsdl' && is_null($this->wsdl)) {
  6970. $this->loadWSDL();
  6971. if ($this->getError())
  6972. return false;
  6973. }
  6974. // serialize parameters
  6975. if($this->endpointType == 'wsdl' && $opData = $this->getOperationData($operation)){
  6976. // use WSDL for operation
  6977. $this->opData = $opData;
  6978. $this->debug("found operation");
  6979. $this->appendDebug('opData=' . $this->varDump($opData));
  6980. if (isset($opData['soapAction'])) {
  6981. $soapAction = $opData['soapAction'];
  6982. }
  6983. if (! $this->forceEndpoint) {
  6984. $this->endpoint = $opData['endpoint'];
  6985. } else {
  6986. $this->endpoint = $this->forceEndpoint;
  6987. }
  6988. $namespace = isset($opData['input']['namespace']) ? $opData['input']['namespace'] : $namespace;
  6989. $style = $opData['style'];
  6990. $use = $opData['input']['use'];
  6991. // add ns to ns array
  6992. if($namespace != '' && !isset($this->wsdl->namespaces[$namespace])){
  6993. $nsPrefix = 'ns' . rand(1000, 9999);
  6994. $this->wsdl->namespaces[$nsPrefix] = $namespace;
  6995. }
  6996. $nsPrefix = $this->wsdl->getPrefixFromNamespace($namespace);
  6997. // serialize payload
  6998. if (is_string($params)) {
  6999. $this->debug("serializing param string for WSDL operation $operation");
  7000. $payload = $params;
  7001. } elseif (is_array($params)) {
  7002. $this->debug("serializing param array for WSDL operation $operation");
  7003. $payload = $this->wsdl->serializeRPCParameters($operation,'input',$params,$this->bindingType);
  7004. } else {
  7005. $this->debug('params must be array or string');
  7006. $this->setError('params must be array or string');
  7007. return false;
  7008. }
  7009. $usedNamespaces = $this->wsdl->usedNamespaces;
  7010. if (isset($opData['input']['encodingStyle'])) {
  7011. $encodingStyle = $opData['input']['encodingStyle'];
  7012. } else {
  7013. $encodingStyle = '';
  7014. }
  7015. $this->appendDebug($this->wsdl->getDebug());
  7016. $this->wsdl->clearDebug();
  7017. if ($errstr = $this->wsdl->getError()) {
  7018. $this->debug('got wsdl error: '.$errstr);
  7019. $this->setError('wsdl error: '.$errstr);
  7020. return false;
  7021. }
  7022. } elseif($this->endpointType == 'wsdl') {
  7023. // operation not in WSDL
  7024. $this->appendDebug($this->wsdl->getDebug());
  7025. $this->wsdl->clearDebug();
  7026. $this->setError('operation '.$operation.' not present in WSDL.');
  7027. $this->debug("operation '$operation' not present in WSDL.");
  7028. return false;
  7029. } else {
  7030. // no WSDL
  7031. //$this->namespaces['ns1'] = $namespace;
  7032. $nsPrefix = 'ns' . rand(1000, 9999);
  7033. // serialize
  7034. $payload = '';
  7035. if (is_string($params)) {
  7036. $this->debug("serializing param string for operation $operation");
  7037. $payload = $params;
  7038. } elseif (is_array($params)) {
  7039. $this->debug("serializing param array for operation $operation");
  7040. foreach($params as $k => $v){
  7041. $payload .= $this->serialize_val($v,$k,false,false,false,false,$use);
  7042. }
  7043. } else {
  7044. $this->debug('params must be array or string');
  7045. $this->setError('params must be array or string');
  7046. return false;
  7047. }
  7048. $usedNamespaces = array();
  7049. if ($use == 'encoded') {
  7050. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  7051. } else {
  7052. $encodingStyle = '';
  7053. }
  7054. }
  7055. // wrap RPC calls with method element
  7056. if ($style == 'rpc') {
  7057. if ($use == 'literal') {
  7058. $this->debug("wrapping RPC request with literal method element");
  7059. if ($namespace) {
  7060. // http://www.ws-i.org/Profiles/BasicProfile-1.1-2004-08-24.html R2735 says rpc/literal accessor elements should not be in a namespace
  7061. $payload = "<$nsPrefix:$operation xmlns:$nsPrefix=\"$namespace\">" .
  7062. $payload .
  7063. "</$nsPrefix:$operation>";
  7064. } else {
  7065. $payload = "<$operation>" . $payload . "</$operation>";
  7066. }
  7067. } else {
  7068. $this->debug("wrapping RPC request with encoded method element");
  7069. if ($namespace) {
  7070. $payload = "<$nsPrefix:$operation xmlns:$nsPrefix=\"$namespace\">" .
  7071. $payload .
  7072. "</$nsPrefix:$operation>";
  7073. } else {
  7074. $payload = "<$operation>" .
  7075. $payload .
  7076. "</$operation>";
  7077. }
  7078. }
  7079. }
  7080. // serialize envelope
  7081. $soapmsg = $this->serializeEnvelope($payload,$this->requestHeaders,$usedNamespaces,$style,$use,$encodingStyle);
  7082. $this->debug("endpoint=$this->endpoint, soapAction=$soapAction, namespace=$namespace, style=$style, use=$use, encodingStyle=$encodingStyle");
  7083. $this->debug('SOAP message length=' . strlen($soapmsg) . ' contents (max 1000 bytes)=' . substr($soapmsg, 0, 1000));
  7084. // send
  7085. $return = $this->send($this->getHTTPBody($soapmsg),$soapAction,$this->timeout,$this->response_timeout);
  7086. if($errstr = $this->getError()){
  7087. $this->debug('Error: '.$errstr);
  7088. return false;
  7089. } else {
  7090. $this->return = $return;
  7091. $this->debug('sent message successfully and got a(n) '.gettype($return));
  7092. $this->appendDebug('return=' . $this->varDump($return));
  7093. // fault?
  7094. if(is_array($return) && isset($return['faultcode'])){
  7095. $this->debug('got fault');
  7096. $this->setError($return['faultcode'].': '.$return['faultstring']);
  7097. $this->fault = true;
  7098. foreach($return as $k => $v){
  7099. $this->$k = $v;
  7100. $this->debug("$k = $v<br>");
  7101. }
  7102. return $return;
  7103. } elseif ($style == 'document') {
  7104. // NOTE: if the response is defined to have multiple parts (i.e. unwrapped),
  7105. // we are only going to return the first part here...sorry about that
  7106. return $return;
  7107. } else {
  7108. // array of return values
  7109. if(is_array($return)){
  7110. // multiple 'out' parameters, which we return wrapped up
  7111. // in the array
  7112. if(sizeof($return) > 1){
  7113. return $return;
  7114. }
  7115. // single 'out' parameter (normally the return value)
  7116. $return = array_shift($return);
  7117. $this->debug('return shifted value: ');
  7118. $this->appendDebug($this->varDump($return));
  7119. return $return;
  7120. // nothing returned (ie, echoVoid)
  7121. } else {
  7122. return "";
  7123. }
  7124. }
  7125. }
  7126. }
  7127. /**
  7128. * check WSDL passed as an instance or pulled from an endpoint
  7129. *
  7130. * @access private
  7131. */
  7132. function checkWSDL() {
  7133. $this->appendDebug($this->wsdl->getDebug());
  7134. $this->wsdl->clearDebug();
  7135. $this->debug('checkWSDL');
  7136. // catch errors
  7137. if ($errstr = $this->wsdl->getError()) {
  7138. $this->appendDebug($this->wsdl->getDebug());
  7139. $this->wsdl->clearDebug();
  7140. $this->debug('got wsdl error: '.$errstr);
  7141. $this->setError('wsdl error: '.$errstr);
  7142. } elseif ($this->operations = $this->wsdl->getOperations($this->portName, 'soap')) {
  7143. $this->appendDebug($this->wsdl->getDebug());
  7144. $this->wsdl->clearDebug();
  7145. $this->bindingType = 'soap';
  7146. $this->debug('got '.count($this->operations).' operations from wsdl '.$this->wsdlFile.' for binding type '.$this->bindingType);
  7147. } elseif ($this->operations = $this->wsdl->getOperations($this->portName, 'soap12')) {
  7148. $this->appendDebug($this->wsdl->getDebug());
  7149. $this->wsdl->clearDebug();
  7150. $this->bindingType = 'soap12';
  7151. $this->debug('got '.count($this->operations).' operations from wsdl '.$this->wsdlFile.' for binding type '.$this->bindingType);
  7152. $this->debug('**************** WARNING: SOAP 1.2 BINDING *****************');
  7153. } else {
  7154. $this->appendDebug($this->wsdl->getDebug());
  7155. $this->wsdl->clearDebug();
  7156. $this->debug('getOperations returned false');
  7157. $this->setError('no operations defined in the WSDL document!');
  7158. }
  7159. }
  7160. /**
  7161. * instantiate wsdl object and parse wsdl file
  7162. *
  7163. * @access public
  7164. */
  7165. function loadWSDL() {
  7166. $this->debug('instantiating wsdl class with doc: '.$this->wsdlFile);
  7167. $this->wsdl = new wsdl('',$this->proxyhost,$this->proxyport,$this->proxyusername,$this->proxypassword,$this->timeout,$this->response_timeout,$this->curl_options,$this->use_curl);
  7168. $this->wsdl->setCredentials($this->username, $this->password, $this->authtype, $this->certRequest);
  7169. $this->wsdl->fetchWSDL($this->wsdlFile);
  7170. $this->checkWSDL();
  7171. }
  7172. /**
  7173. * get available data pertaining to an operation
  7174. *
  7175. * @param string $operation operation name
  7176. * @return array array of data pertaining to the operation
  7177. * @access public
  7178. */
  7179. function getOperationData($operation){
  7180. if ($this->endpointType == 'wsdl' && is_null($this->wsdl)) {
  7181. $this->loadWSDL();
  7182. if ($this->getError())
  7183. return false;
  7184. }
  7185. if(isset($this->operations[$operation])){
  7186. return $this->operations[$operation];
  7187. }
  7188. $this->debug("No data for operation: $operation");
  7189. }
  7190. /**
  7191. * send the SOAP message
  7192. *
  7193. * Note: if the operation has multiple return values
  7194. * the return value of this method will be an array
  7195. * of those values.
  7196. *
  7197. * @param string $msg a SOAPx4 soapmsg object
  7198. * @param string $soapaction SOAPAction value
  7199. * @param integer $timeout set connection timeout in seconds
  7200. * @param integer $response_timeout set response timeout in seconds
  7201. * @return mixed native PHP types.
  7202. * @access private
  7203. */
  7204. function send($msg, $soapaction = '', $timeout=0, $response_timeout=30) {
  7205. $this->checkCookies();
  7206. // detect transport
  7207. switch(true){
  7208. // http(s)
  7209. case preg_match('/^http/',$this->endpoint):
  7210. $this->debug('transporting via HTTP');
  7211. if($this->persistentConnection == true && is_object($this->persistentConnection)){
  7212. $http =& $this->persistentConnection;
  7213. } else {
  7214. $http = new soap_transport_http($this->endpoint, $this->curl_options, $this->use_curl);
  7215. if ($this->persistentConnection) {
  7216. $http->usePersistentConnection();
  7217. }
  7218. }
  7219. $http->setContentType($this->getHTTPContentType(), $this->getHTTPContentTypeCharset());
  7220. $http->setSOAPAction($soapaction);
  7221. if($this->proxyhost && $this->proxyport){
  7222. $http->setProxy($this->proxyhost,$this->proxyport,$this->proxyusername,$this->proxypassword);
  7223. }
  7224. if($this->authtype != '') {
  7225. $http->setCredentials($this->username, $this->password, $this->authtype, array(), $this->certRequest);
  7226. }
  7227. if($this->http_encoding != ''){
  7228. $http->setEncoding($this->http_encoding);
  7229. }
  7230. $this->debug('sending message, length='.strlen($msg));
  7231. if(preg_match('/^http:/',$this->endpoint)){
  7232. //if(strpos($this->endpoint,'http:')){
  7233. $this->responseData = $http->send($msg,$timeout,$response_timeout,$this->cookies);
  7234. } elseif(preg_match('/^https/',$this->endpoint)){
  7235. //} elseif(strpos($this->endpoint,'https:')){
  7236. //if(phpversion() == '4.3.0-dev'){
  7237. //$response = $http->send($msg,$timeout,$response_timeout);
  7238. //$this->request = $http->outgoing_payload;
  7239. //$this->response = $http->incoming_payload;
  7240. //} else
  7241. $this->responseData = $http->sendHTTPS($msg,$timeout,$response_timeout,$this->cookies);
  7242. } else {
  7243. $this->setError('no http/s in endpoint url');
  7244. }
  7245. $this->request = $http->outgoing_payload;
  7246. $this->response = $http->incoming_payload;
  7247. $this->appendDebug($http->getDebug());
  7248. $this->UpdateCookies($http->incoming_cookies);
  7249. // save transport object if using persistent connections
  7250. if ($this->persistentConnection) {
  7251. $http->clearDebug();
  7252. if (!is_object($this->persistentConnection)) {
  7253. $this->persistentConnection = $http;
  7254. }
  7255. }
  7256. if($err = $http->getError()){
  7257. $this->setError('HTTP Error: '.$err);
  7258. return false;
  7259. } elseif($this->getError()){
  7260. return false;
  7261. } else {
  7262. $this->debug('got response, length='. strlen($this->responseData).' type='.$http->incoming_headers['content-type']);
  7263. return $this->parseResponse($http->incoming_headers, $this->responseData);
  7264. }
  7265. break;
  7266. default:
  7267. $this->setError('no transport found, or selected transport is not yet supported!');
  7268. return false;
  7269. break;
  7270. }
  7271. }
  7272. /**
  7273. * processes SOAP message returned from server
  7274. *
  7275. * @param array $headers The HTTP headers
  7276. * @param string $data unprocessed response data from server
  7277. * @return mixed value of the message, decoded into a PHP type
  7278. * @access private
  7279. */
  7280. function parseResponse($headers, $data) {
  7281. $this->debug('Entering parseResponse() for data of length ' . strlen($data) . ' headers:');
  7282. $this->appendDebug($this->varDump($headers));
  7283. if (!isset($headers['content-type'])) {
  7284. $this->setError('Response not of type text/xml (no content-type header)');
  7285. return false;
  7286. }
  7287. if (!strstr($headers['content-type'], 'text/xml')) {
  7288. $this->setError('Response not of type text/xml: ' . $headers['content-type']);
  7289. return false;
  7290. }
  7291. if (strpos($headers['content-type'], '=')) {
  7292. $enc = str_replace('"', '', substr(strstr($headers["content-type"], '='), 1));
  7293. $this->debug('Got response encoding: ' . $enc);
  7294. if(preg_match('/^(ISO-8859-1|US-ASCII|UTF-8)$/i',$enc)){
  7295. $this->xml_encoding = strtoupper($enc);
  7296. } else {
  7297. $this->xml_encoding = 'US-ASCII';
  7298. }
  7299. } else {
  7300. // should be US-ASCII for HTTP 1.0 or ISO-8859-1 for HTTP 1.1
  7301. $this->xml_encoding = 'ISO-8859-1';
  7302. }
  7303. $this->debug('Use encoding: ' . $this->xml_encoding . ' when creating nusoap_parser');
  7304. $parser = new nusoap_parser($data,$this->xml_encoding,$this->operation,$this->decode_utf8);
  7305. // add parser debug data to our debug
  7306. $this->appendDebug($parser->getDebug());
  7307. // if parse errors
  7308. if($errstr = $parser->getError()){
  7309. $this->setError( $errstr);
  7310. // destroy the parser object
  7311. unset($parser);
  7312. return false;
  7313. } else {
  7314. // get SOAP headers
  7315. $this->responseHeaders = $parser->getHeaders();
  7316. // get SOAP headers
  7317. $this->responseHeader = $parser->get_soapheader();
  7318. // get decoded message
  7319. $return = $parser->get_soapbody();
  7320. // add document for doclit support
  7321. $this->document = $parser->document;
  7322. // destroy the parser object
  7323. unset($parser);
  7324. // return decode message
  7325. return $return;
  7326. }
  7327. }
  7328. /**
  7329. * sets user-specified cURL options
  7330. *
  7331. * @param mixed $option The cURL option (always integer?)
  7332. * @param mixed $value The cURL option value
  7333. * @access public
  7334. */
  7335. function setCurlOption($option, $value) {
  7336. $this->debug("setCurlOption option=$option, value=");
  7337. $this->appendDebug($this->varDump($value));
  7338. $this->curl_options[$option] = $value;
  7339. }
  7340. /**
  7341. * sets the SOAP endpoint, which can override WSDL
  7342. *
  7343. * @param string $endpoint The endpoint URL to use, or empty string or false to prevent override
  7344. * @access public
  7345. */
  7346. function setEndpoint($endpoint) {
  7347. $this->debug("setEndpoint(\"$endpoint\")");
  7348. $this->forceEndpoint = $endpoint;
  7349. }
  7350. /**
  7351. * set the SOAP headers
  7352. *
  7353. * @param mixed $headers String of XML with SOAP header content, or array of soapval objects for SOAP headers
  7354. * @access public
  7355. */
  7356. function setHeaders($headers){
  7357. $this->debug("setHeaders headers=");
  7358. $this->appendDebug($this->varDump($headers));
  7359. $this->requestHeaders = $headers;
  7360. }
  7361. /**
  7362. * get the SOAP response headers (namespace resolution incomplete)
  7363. *
  7364. * @return string
  7365. * @access public
  7366. */
  7367. function getHeaders(){
  7368. return $this->responseHeaders;
  7369. }
  7370. /**
  7371. * get the SOAP response Header (parsed)
  7372. *
  7373. * @return mixed
  7374. * @access public
  7375. */
  7376. function getHeader(){
  7377. return $this->responseHeader;
  7378. }
  7379. /**
  7380. * set proxy info here
  7381. *
  7382. * @param string $proxyhost
  7383. * @param string $proxyport
  7384. * @param string $proxyusername
  7385. * @param string $proxypassword
  7386. * @access public
  7387. */
  7388. function setHTTPProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '') {
  7389. $this->proxyhost = $proxyhost;
  7390. $this->proxyport = $proxyport;
  7391. $this->proxyusername = $proxyusername;
  7392. $this->proxypassword = $proxypassword;
  7393. }
  7394. /**
  7395. * if authenticating, set user credentials here
  7396. *
  7397. * @param string $username
  7398. * @param string $password
  7399. * @param string $authtype (basic|digest|certificate|ntlm)
  7400. * @param array $certRequest (keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, verifypeer (optional), verifyhost (optional): see corresponding options in cURL docs)
  7401. * @access public
  7402. */
  7403. function setCredentials($username, $password, $authtype = 'basic', $certRequest = array()) {
  7404. $this->debug("setCredentials username=$username authtype=$authtype certRequest=");
  7405. $this->appendDebug($this->varDump($certRequest));
  7406. $this->username = $username;
  7407. $this->password = $password;
  7408. $this->authtype = $authtype;
  7409. $this->certRequest = $certRequest;
  7410. }
  7411. /**
  7412. * use HTTP encoding
  7413. *
  7414. * @param string $enc HTTP encoding
  7415. * @access public
  7416. */
  7417. function setHTTPEncoding($enc='gzip, deflate'){
  7418. $this->debug("setHTTPEncoding(\"$enc\")");
  7419. $this->http_encoding = $enc;
  7420. }
  7421. /**
  7422. * Set whether to try to use cURL connections if possible
  7423. *
  7424. * @param boolean $use Whether to try to use cURL
  7425. * @access public
  7426. */
  7427. function setUseCURL($use) {
  7428. $this->debug("setUseCURL($use)");
  7429. $this->use_curl = $use;
  7430. }
  7431. /**
  7432. * use HTTP persistent connections if possible
  7433. *
  7434. * @access public
  7435. */
  7436. function useHTTPPersistentConnection(){
  7437. $this->debug("useHTTPPersistentConnection");
  7438. $this->persistentConnection = true;
  7439. }
  7440. /**
  7441. * gets the default RPC parameter setting.
  7442. * If true, default is that call params are like RPC even for document style.
  7443. * Each call() can override this value.
  7444. *
  7445. * This is no longer used.
  7446. *
  7447. * @return boolean
  7448. * @access public
  7449. * @deprecated
  7450. */
  7451. function getDefaultRpcParams() {
  7452. return $this->defaultRpcParams;
  7453. }
  7454. /**
  7455. * sets the default RPC parameter setting.
  7456. * If true, default is that call params are like RPC even for document style
  7457. * Each call() can override this value.
  7458. *
  7459. * This is no longer used.
  7460. *
  7461. * @param boolean $rpcParams
  7462. * @access public
  7463. * @deprecated
  7464. */
  7465. function setDefaultRpcParams($rpcParams) {
  7466. $this->defaultRpcParams = $rpcParams;
  7467. }
  7468. /**
  7469. * dynamically creates an instance of a proxy class,
  7470. * allowing user to directly call methods from wsdl
  7471. *
  7472. * @return object soap_proxy object
  7473. * @access public
  7474. */
  7475. function getProxy() {
  7476. $r = rand();
  7477. $evalStr = $this->_getProxyClassCode($r);
  7478. //$this->debug("proxy class: $evalStr");
  7479. if ($this->getError()) {
  7480. $this->debug("Error from _getProxyClassCode, so return NULL");
  7481. return null;
  7482. }
  7483. // eval the class
  7484. eval($evalStr);
  7485. // instantiate proxy object
  7486. eval("\$proxy = new nusoap_proxy_$r('');");
  7487. // transfer current wsdl data to the proxy thereby avoiding parsing the wsdl twice
  7488. $proxy->endpointType = 'wsdl';
  7489. $proxy->wsdlFile = $this->wsdlFile;
  7490. $proxy->wsdl = $this->wsdl;
  7491. $proxy->operations = $this->operations;
  7492. $proxy->defaultRpcParams = $this->defaultRpcParams;
  7493. // transfer other state
  7494. $proxy->soap_defencoding = $this->soap_defencoding;
  7495. $proxy->username = $this->username;
  7496. $proxy->password = $this->password;
  7497. $proxy->authtype = $this->authtype;
  7498. $proxy->certRequest = $this->certRequest;
  7499. $proxy->requestHeaders = $this->requestHeaders;
  7500. $proxy->endpoint = $this->endpoint;
  7501. $proxy->forceEndpoint = $this->forceEndpoint;
  7502. $proxy->proxyhost = $this->proxyhost;
  7503. $proxy->proxyport = $this->proxyport;
  7504. $proxy->proxyusername = $this->proxyusername;
  7505. $proxy->proxypassword = $this->proxypassword;
  7506. $proxy->http_encoding = $this->http_encoding;
  7507. $proxy->timeout = $this->timeout;
  7508. $proxy->response_timeout = $this->response_timeout;
  7509. $proxy->persistentConnection = &$this->persistentConnection;
  7510. $proxy->decode_utf8 = $this->decode_utf8;
  7511. $proxy->curl_options = $this->curl_options;
  7512. $proxy->bindingType = $this->bindingType;
  7513. $proxy->use_curl = $this->use_curl;
  7514. return $proxy;
  7515. }
  7516. /**
  7517. * dynamically creates proxy class code
  7518. *
  7519. * @return string PHP/NuSOAP code for the proxy class
  7520. * @access private
  7521. */
  7522. function _getProxyClassCode($r) {
  7523. $this->debug("in getProxy endpointType=$this->endpointType");
  7524. $this->appendDebug("wsdl=" . $this->varDump($this->wsdl));
  7525. if ($this->endpointType != 'wsdl') {
  7526. $evalStr = 'A proxy can only be created for a WSDL client';
  7527. $this->setError($evalStr);
  7528. $evalStr = "echo \"$evalStr\";";
  7529. return $evalStr;
  7530. }
  7531. if ($this->endpointType == 'wsdl' && is_null($this->wsdl)) {
  7532. $this->loadWSDL();
  7533. if ($this->getError()) {
  7534. return "echo \"" . $this->getError() . "\";";
  7535. }
  7536. }
  7537. $evalStr = '';
  7538. foreach ($this->operations as $operation => $opData) {
  7539. if ($operation != '') {
  7540. // create param string and param comment string
  7541. if (sizeof($opData['input']['parts']) > 0) {
  7542. $paramStr = '';
  7543. $paramArrayStr = '';
  7544. $paramCommentStr = '';
  7545. foreach ($opData['input']['parts'] as $name => $type) {
  7546. $paramStr .= "\$$name, ";
  7547. $paramArrayStr .= "'$name' => \$$name, ";
  7548. $paramCommentStr .= "$type \$$name, ";
  7549. }
  7550. $paramStr = substr($paramStr, 0, strlen($paramStr)-2);
  7551. $paramArrayStr = substr($paramArrayStr, 0, strlen($paramArrayStr)-2);
  7552. $paramCommentStr = substr($paramCommentStr, 0, strlen($paramCommentStr)-2);
  7553. } else {
  7554. $paramStr = '';
  7555. $paramArrayStr = '';
  7556. $paramCommentStr = 'void';
  7557. }
  7558. $opData['namespace'] = !isset($opData['namespace']) ? 'http://testuri.com' : $opData['namespace'];
  7559. $evalStr .= "// $paramCommentStr
  7560. function " . str_replace('.', '__', $operation) . "($paramStr) {
  7561. \$params = array($paramArrayStr);
  7562. return \$this->call('$operation', \$params, '".$opData['namespace']."', '".(isset($opData['soapAction']) ? $opData['soapAction'] : '')."');
  7563. }
  7564. ";
  7565. unset($paramStr);
  7566. unset($paramCommentStr);
  7567. }
  7568. }
  7569. $evalStr = 'class nusoap_proxy_'.$r.' extends nusoap_client {
  7570. '.$evalStr.'
  7571. }';
  7572. return $evalStr;
  7573. }
  7574. /**
  7575. * dynamically creates proxy class code
  7576. *
  7577. * @return string PHP/NuSOAP code for the proxy class
  7578. * @access public
  7579. */
  7580. function getProxyClassCode() {
  7581. $r = rand();
  7582. return $this->_getProxyClassCode($r);
  7583. }
  7584. /**
  7585. * gets the HTTP body for the current request.
  7586. *
  7587. * @param string $soapmsg The SOAP payload
  7588. * @return string The HTTP body, which includes the SOAP payload
  7589. * @access private
  7590. */
  7591. function getHTTPBody($soapmsg) {
  7592. return $soapmsg;
  7593. }
  7594. /**
  7595. * gets the HTTP content type for the current request.
  7596. *
  7597. * Note: getHTTPBody must be called before this.
  7598. *
  7599. * @return string the HTTP content type for the current request.
  7600. * @access private
  7601. */
  7602. function getHTTPContentType() {
  7603. return 'text/xml';
  7604. }
  7605. /**
  7606. * gets the HTTP content type charset for the current request.
  7607. * returns false for non-text content types.
  7608. *
  7609. * Note: getHTTPBody must be called before this.
  7610. *
  7611. * @return string the HTTP content type charset for the current request.
  7612. * @access private
  7613. */
  7614. function getHTTPContentTypeCharset() {
  7615. return $this->soap_defencoding;
  7616. }
  7617. /*
  7618. * whether or not parser should decode utf8 element content
  7619. *
  7620. * @return always returns true
  7621. * @access public
  7622. */
  7623. function decodeUTF8($bool){
  7624. $this->decode_utf8 = $bool;
  7625. return true;
  7626. }
  7627. /**
  7628. * adds a new Cookie into $this->cookies array
  7629. *
  7630. * @param string $name Cookie Name
  7631. * @param string $value Cookie Value
  7632. * @return boolean if cookie-set was successful returns true, else false
  7633. * @access public
  7634. */
  7635. function setCookie($name, $value) {
  7636. if (strlen($name) == 0) {
  7637. return false;
  7638. }
  7639. $this->cookies[] = array('name' => $name, 'value' => $value);
  7640. return true;
  7641. }
  7642. /**
  7643. * gets all Cookies
  7644. *
  7645. * @return array with all internal cookies
  7646. * @access public
  7647. */
  7648. function getCookies() {
  7649. return $this->cookies;
  7650. }
  7651. /**
  7652. * checks all Cookies and delete those which are expired
  7653. *
  7654. * @return boolean always return true
  7655. * @access private
  7656. */
  7657. function checkCookies() {
  7658. if (sizeof($this->cookies) == 0) {
  7659. return true;
  7660. }
  7661. $this->debug('checkCookie: check ' . sizeof($this->cookies) . ' cookies');
  7662. $curr_cookies = $this->cookies;
  7663. $this->cookies = array();
  7664. foreach ($curr_cookies as $cookie) {
  7665. if (! is_array($cookie)) {
  7666. $this->debug('Remove cookie that is not an array');
  7667. continue;
  7668. }
  7669. if ((isset($cookie['expires'])) && (! empty($cookie['expires']))) {
  7670. if (strtotime($cookie['expires']) > time()) {
  7671. $this->cookies[] = $cookie;
  7672. } else {
  7673. $this->debug('Remove expired cookie ' . $cookie['name']);
  7674. }
  7675. } else {
  7676. $this->cookies[] = $cookie;
  7677. }
  7678. }
  7679. $this->debug('checkCookie: '.sizeof($this->cookies).' cookies left in array');
  7680. return true;
  7681. }
  7682. /**
  7683. * updates the current cookies with a new set
  7684. *
  7685. * @param array $cookies new cookies with which to update current ones
  7686. * @return boolean always return true
  7687. * @access private
  7688. */
  7689. function UpdateCookies($cookies) {
  7690. if (sizeof($this->cookies) == 0) {
  7691. // no existing cookies: take whatever is new
  7692. if (sizeof($cookies) > 0) {
  7693. $this->debug('Setting new cookie(s)');
  7694. $this->cookies = $cookies;
  7695. }
  7696. return true;
  7697. }
  7698. if (sizeof($cookies) == 0) {
  7699. // no new cookies: keep what we've got
  7700. return true;
  7701. }
  7702. // merge
  7703. foreach ($cookies as $newCookie) {
  7704. if (!is_array($newCookie)) {
  7705. continue;
  7706. }
  7707. if ((!isset($newCookie['name'])) || (!isset($newCookie['value']))) {
  7708. continue;
  7709. }
  7710. $newName = $newCookie['name'];
  7711. $found = false;
  7712. for ($i = 0; $i < count($this->cookies); $i++) {
  7713. $cookie = $this->cookies[$i];
  7714. if (!is_array($cookie)) {
  7715. continue;
  7716. }
  7717. if (!isset($cookie['name'])) {
  7718. continue;
  7719. }
  7720. if ($newName != $cookie['name']) {
  7721. continue;
  7722. }
  7723. $newDomain = isset($newCookie['domain']) ? $newCookie['domain'] : 'NODOMAIN';
  7724. $domain = isset($cookie['domain']) ? $cookie['domain'] : 'NODOMAIN';
  7725. if ($newDomain != $domain) {
  7726. continue;
  7727. }
  7728. $newPath = isset($newCookie['path']) ? $newCookie['path'] : 'NOPATH';
  7729. $path = isset($cookie['path']) ? $cookie['path'] : 'NOPATH';
  7730. if ($newPath != $path) {
  7731. continue;
  7732. }
  7733. $this->cookies[$i] = $newCookie;
  7734. $found = true;
  7735. $this->debug('Update cookie ' . $newName . '=' . $newCookie['value']);
  7736. break;
  7737. }
  7738. if (! $found) {
  7739. $this->debug('Add cookie ' . $newName . '=' . $newCookie['value']);
  7740. $this->cookies[] = $newCookie;
  7741. }
  7742. }
  7743. return true;
  7744. }
  7745. }
  7746. if (!extension_loaded('soap')) {
  7747. /**
  7748. * For backwards compatiblity, define soapclient unless the PHP SOAP extension is loaded.
  7749. */
  7750. class soapclient extends nusoap_client {
  7751. }
  7752. }
  7753. ?>