class.wsdl.php 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. <?php
  2. /**
  3. * parses a WSDL file, allows access to it's data, other utility methods.
  4. * also builds WSDL structures programmatically.
  5. *
  6. * @author Dietrich Ayala <dietrich@ganx4.com>
  7. * @author Scott Nichol <snichol@users.sourceforge.net>
  8. * @access public
  9. */
  10. class wsdl extends nusoap_base {
  11. // URL or filename of the root of this WSDL
  12. var $wsdl;
  13. // define internal arrays of bindings, ports, operations, messages, etc.
  14. var $schemas = array();
  15. var $currentSchema;
  16. var $message = array();
  17. var $complexTypes = array();
  18. var $messages = array();
  19. var $currentMessage;
  20. var $currentOperation;
  21. var $portTypes = array();
  22. var $currentPortType;
  23. var $bindings = array();
  24. var $currentBinding;
  25. var $ports = array();
  26. var $currentPort;
  27. var $opData = array();
  28. var $status = '';
  29. var $documentation = false;
  30. var $endpoint = '';
  31. // array of wsdl docs to import
  32. var $import = array();
  33. // parser vars
  34. var $parser;
  35. var $position = 0;
  36. var $depth = 0;
  37. var $depth_array = array();
  38. // for getting wsdl
  39. var $proxyhost = '';
  40. var $proxyport = '';
  41. var $proxyusername = '';
  42. var $proxypassword = '';
  43. var $timeout = 0;
  44. var $response_timeout = 30;
  45. var $curl_options = array(); // User-specified cURL options
  46. var $use_curl = false; // whether to always try to use cURL
  47. // for HTTP authentication
  48. var $username = ''; // Username for HTTP authentication
  49. var $password = ''; // Password for HTTP authentication
  50. var $authtype = ''; // Type of HTTP authentication
  51. var $certRequest = array(); // Certificate for HTTP SSL authentication
  52. /**
  53. * constructor
  54. *
  55. * @param string $wsdl WSDL document URL
  56. * @param string $proxyhost
  57. * @param string $proxyport
  58. * @param string $proxyusername
  59. * @param string $proxypassword
  60. * @param integer $timeout set the connection timeout
  61. * @param integer $response_timeout set the response timeout
  62. * @param array $curl_options user-specified cURL options
  63. * @param boolean $use_curl try to use cURL
  64. * @access public
  65. */
  66. function __construct($wsdl = '',$proxyhost=false,$proxyport=false,$proxyusername=false,$proxypassword=false,$timeout=0,$response_timeout=30,$curl_options=null,$use_curl=false){
  67. parent::__construct();
  68. $this->debug("ctor wsdl=$wsdl timeout=$timeout response_timeout=$response_timeout");
  69. $this->proxyhost = $proxyhost;
  70. $this->proxyport = $proxyport;
  71. $this->proxyusername = $proxyusername;
  72. $this->proxypassword = $proxypassword;
  73. $this->timeout = $timeout;
  74. $this->response_timeout = $response_timeout;
  75. if (is_array($curl_options))
  76. $this->curl_options = $curl_options;
  77. $this->use_curl = $use_curl;
  78. $this->fetchWSDL($wsdl);
  79. }
  80. /**
  81. * fetches the WSDL document and parses it
  82. *
  83. * @access public
  84. */
  85. function fetchWSDL($wsdl) {
  86. $this->debug("parse and process WSDL path=$wsdl");
  87. $this->wsdl = $wsdl;
  88. // parse wsdl file
  89. if ($this->wsdl != "") {
  90. $this->parseWSDL($this->wsdl);
  91. }
  92. // imports
  93. // TODO: handle imports more properly, grabbing them in-line and nesting them
  94. $imported_urls = array();
  95. $imported = 1;
  96. while ($imported > 0) {
  97. $imported = 0;
  98. // Schema imports
  99. foreach ($this->schemas as $ns => $list) {
  100. foreach ($list as $xs) {
  101. $wsdlparts = parse_url($this->wsdl); // this is bogusly simple!
  102. foreach ($xs->imports as $ns2 => $list2) {
  103. for ($ii = 0; $ii < count($list2); $ii++) {
  104. if (! $list2[$ii]['loaded']) {
  105. $this->schemas[$ns]->imports[$ns2][$ii]['loaded'] = true;
  106. $url = $list2[$ii]['location'];
  107. if ($url != '') {
  108. $urlparts = parse_url($url);
  109. if (!isset($urlparts['host'])) {
  110. $url = $wsdlparts['scheme'] . '://' . $wsdlparts['host'] . (isset($wsdlparts['port']) ? ':' .$wsdlparts['port'] : '') .
  111. substr($wsdlparts['path'],0,strrpos($wsdlparts['path'],'/') + 1) .$urlparts['path'];
  112. }
  113. if (! in_array($url, $imported_urls)) {
  114. $this->parseWSDL($url);
  115. $imported++;
  116. $imported_urls[] = $url;
  117. }
  118. } else {
  119. $this->debug("Unexpected scenario: empty URL for unloaded import");
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. // WSDL imports
  127. $wsdlparts = parse_url($this->wsdl); // this is bogusly simple!
  128. foreach ($this->import as $ns => $list) {
  129. for ($ii = 0; $ii < count($list); $ii++) {
  130. if (! $list[$ii]['loaded']) {
  131. $this->import[$ns][$ii]['loaded'] = true;
  132. $url = $list[$ii]['location'];
  133. if ($url != '') {
  134. $urlparts = parse_url($url);
  135. if (!isset($urlparts['host'])) {
  136. $url = $wsdlparts['scheme'] . '://' . $wsdlparts['host'] . (isset($wsdlparts['port']) ? ':' . $wsdlparts['port'] : '') .
  137. substr($wsdlparts['path'],0,strrpos($wsdlparts['path'],'/') + 1) .$urlparts['path'];
  138. }
  139. if (! in_array($url, $imported_urls)) {
  140. $this->parseWSDL($url);
  141. $imported++;
  142. $imported_urls[] = $url;
  143. }
  144. } else {
  145. $this->debug("Unexpected scenario: empty URL for unloaded import");
  146. }
  147. }
  148. }
  149. }
  150. }
  151. // add new data to operation data
  152. foreach($this->bindings as $binding => $bindingData) {
  153. if (isset($bindingData['operations']) && is_array($bindingData['operations'])) {
  154. foreach($bindingData['operations'] as $operation => $data) {
  155. $this->debug('post-parse data gathering for ' . $operation);
  156. $this->bindings[$binding]['operations'][$operation]['input'] =
  157. isset($this->bindings[$binding]['operations'][$operation]['input']) ?
  158. array_merge($this->bindings[$binding]['operations'][$operation]['input'], $this->portTypes[ $bindingData['portType'] ][$operation]['input']) :
  159. $this->portTypes[ $bindingData['portType'] ][$operation]['input'];
  160. $this->bindings[$binding]['operations'][$operation]['output'] =
  161. isset($this->bindings[$binding]['operations'][$operation]['output']) ?
  162. array_merge($this->bindings[$binding]['operations'][$operation]['output'], $this->portTypes[ $bindingData['portType'] ][$operation]['output']) :
  163. $this->portTypes[ $bindingData['portType'] ][$operation]['output'];
  164. if(isset($this->messages[ $this->bindings[$binding]['operations'][$operation]['input']['message'] ])){
  165. $this->bindings[$binding]['operations'][$operation]['input']['parts'] = $this->messages[ $this->bindings[$binding]['operations'][$operation]['input']['message'] ];
  166. }
  167. if(isset($this->messages[ $this->bindings[$binding]['operations'][$operation]['output']['message'] ])){
  168. $this->bindings[$binding]['operations'][$operation]['output']['parts'] = $this->messages[ $this->bindings[$binding]['operations'][$operation]['output']['message'] ];
  169. }
  170. // Set operation style if necessary, but do not override one already provided
  171. if (isset($bindingData['style']) && !isset($this->bindings[$binding]['operations'][$operation]['style'])) {
  172. $this->bindings[$binding]['operations'][$operation]['style'] = $bindingData['style'];
  173. }
  174. $this->bindings[$binding]['operations'][$operation]['transport'] = isset($bindingData['transport']) ? $bindingData['transport'] : '';
  175. $this->bindings[$binding]['operations'][$operation]['documentation'] = isset($this->portTypes[ $bindingData['portType'] ][$operation]['documentation']) ? $this->portTypes[ $bindingData['portType'] ][$operation]['documentation'] : '';
  176. $this->bindings[$binding]['operations'][$operation]['endpoint'] = isset($bindingData['endpoint']) ? $bindingData['endpoint'] : '';
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * parses the wsdl document
  183. *
  184. * @param string $wsdl path or URL
  185. * @access private
  186. */
  187. function parseWSDL($wsdl = '') {
  188. $this->debug("parse WSDL at path=$wsdl");
  189. if ($wsdl == '') {
  190. $this->debug('no wsdl passed to parseWSDL()!!');
  191. $this->setError('no wsdl passed to parseWSDL()!!');
  192. return false;
  193. }
  194. // parse $wsdl for url format
  195. $wsdl_props = parse_url($wsdl);
  196. if (isset($wsdl_props['scheme']) && ($wsdl_props['scheme'] == 'http' || $wsdl_props['scheme'] == 'https')) {
  197. $this->debug('getting WSDL http(s) URL ' . $wsdl);
  198. // get wsdl
  199. $tr = new soap_transport_http($wsdl, $this->curl_options, $this->use_curl);
  200. $tr->request_method = 'GET';
  201. $tr->useSOAPAction = false;
  202. if($this->proxyhost && $this->proxyport){
  203. $tr->setProxy($this->proxyhost,$this->proxyport,$this->proxyusername,$this->proxypassword);
  204. }
  205. if ($this->authtype != '') {
  206. $tr->setCredentials($this->username, $this->password, $this->authtype, array(), $this->certRequest);
  207. }
  208. $tr->setEncoding('gzip, deflate');
  209. $wsdl_string = $tr->send('', $this->timeout, $this->response_timeout);
  210. //$this->debug("WSDL request\n" . $tr->outgoing_payload);
  211. //$this->debug("WSDL response\n" . $tr->incoming_payload);
  212. $this->appendDebug($tr->getDebug());
  213. // catch errors
  214. if($err = $tr->getError() ){
  215. $errstr = 'Getting ' . $wsdl . ' - HTTP ERROR: '.$err;
  216. $this->debug($errstr);
  217. $this->setError($errstr);
  218. unset($tr);
  219. return false;
  220. }
  221. unset($tr);
  222. $this->debug("got WSDL URL");
  223. } else {
  224. // $wsdl is not http(s), so treat it as a file URL or plain file path
  225. if (isset($wsdl_props['scheme']) && ($wsdl_props['scheme'] == 'file') && isset($wsdl_props['path'])) {
  226. $path = isset($wsdl_props['host']) ? ($wsdl_props['host'] . ':' . $wsdl_props['path']) : $wsdl_props['path'];
  227. } else {
  228. $path = $wsdl;
  229. }
  230. $this->debug('getting WSDL file ' . $path);
  231. if ($fp = @fopen($path, 'r')) {
  232. $wsdl_string = '';
  233. while ($data = fread($fp, 32768)) {
  234. $wsdl_string .= $data;
  235. }
  236. fclose($fp);
  237. } else {
  238. $errstr = "Bad path to WSDL file $path";
  239. $this->debug($errstr);
  240. $this->setError($errstr);
  241. return false;
  242. }
  243. }
  244. $this->debug('Parse WSDL');
  245. // end new code added
  246. // Create an XML parser.
  247. $this->parser = xml_parser_create();
  248. // Set the options for parsing the XML data.
  249. // xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  250. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
  251. // Set the object for the parser.
  252. xml_set_object($this->parser, $this);
  253. // Set the element handlers for the parser.
  254. xml_set_element_handler($this->parser, 'start_element', 'end_element');
  255. xml_set_character_data_handler($this->parser, 'character_data');
  256. // Parse the XML file.
  257. if (!xml_parse($this->parser, $wsdl_string, true)) {
  258. // Display an error message.
  259. $errstr = sprintf(
  260. 'XML error parsing WSDL from %s on line %d: %s',
  261. $wsdl,
  262. xml_get_current_line_number($this->parser),
  263. xml_error_string(xml_get_error_code($this->parser))
  264. );
  265. $this->debug($errstr);
  266. $this->debug("XML payload:\n" . $wsdl_string);
  267. $this->setError($errstr);
  268. return false;
  269. }
  270. // free the parser
  271. xml_parser_free($this->parser);
  272. $this->debug('Parsing WSDL done');
  273. // catch wsdl parse errors
  274. if($this->getError()){
  275. return false;
  276. }
  277. return true;
  278. }
  279. /**
  280. * start-element handler
  281. *
  282. * @param string $parser XML parser object
  283. * @param string $name element name
  284. * @param string $attrs associative array of attributes
  285. * @access private
  286. */
  287. function start_element($parser, $name, $attrs)
  288. {
  289. if ($this->status == 'schema') {
  290. $this->currentSchema->schemaStartElement($parser, $name, $attrs);
  291. $this->appendDebug($this->currentSchema->getDebug());
  292. $this->currentSchema->clearDebug();
  293. } elseif (preg_match('/schema$/', $name)) {
  294. $this->debug('Parsing WSDL schema');
  295. // $this->debug("startElement for $name ($attrs[name]). status = $this->status (".$this->getLocalPart($name).")");
  296. $this->status = 'schema';
  297. $this->currentSchema = new nusoap_xmlschema('', '', $this->namespaces);
  298. $this->currentSchema->schemaStartElement($parser, $name, $attrs);
  299. $this->appendDebug($this->currentSchema->getDebug());
  300. $this->currentSchema->clearDebug();
  301. } else {
  302. // position in the total number of elements, starting from 0
  303. $pos = $this->position++;
  304. $depth = $this->depth++;
  305. // set self as current value for this depth
  306. $this->depth_array[$depth] = $pos;
  307. $this->message[$pos] = array('cdata' => '');
  308. // process attributes
  309. if (count($attrs) > 0) {
  310. // register namespace declarations
  311. foreach($attrs as $k => $v) {
  312. if (preg_match('/^xmlns/',$k)) {
  313. if ($ns_prefix = substr(strrchr($k, ':'), 1)) {
  314. $this->namespaces[$ns_prefix] = $v;
  315. } else {
  316. $this->namespaces['ns' . (count($this->namespaces) + 1)] = $v;
  317. }
  318. if ($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema') {
  319. $this->XMLSchemaVersion = $v;
  320. $this->namespaces['xsi'] = $v . '-instance';
  321. }
  322. }
  323. }
  324. // expand each attribute prefix to its namespace
  325. foreach($attrs as $k => $v) {
  326. $k = strpos($k, ':') ? $this->expandQname($k) : $k;
  327. if ($k != 'location' && $k != 'soapAction' && $k != 'namespace') {
  328. $v = strpos($v, ':') ? $this->expandQname($v) : $v;
  329. }
  330. $eAttrs[$k] = $v;
  331. }
  332. $attrs = $eAttrs;
  333. } else {
  334. $attrs = array();
  335. }
  336. // get element prefix, namespace and name
  337. if (preg_match('/:/', $name)) {
  338. // get ns prefix
  339. $prefix = substr($name, 0, strpos($name, ':'));
  340. // get ns
  341. $namespace = isset($this->namespaces[$prefix]) ? $this->namespaces[$prefix] : '';
  342. // get unqualified name
  343. $name = substr(strstr($name, ':'), 1);
  344. }
  345. // process attributes, expanding any prefixes to namespaces
  346. // find status, register data
  347. switch ($this->status) {
  348. case 'message':
  349. if ($name == 'part') {
  350. if (isset($attrs['type'])) {
  351. $this->debug("msg " . $this->currentMessage . ": found part (with type) $attrs[name]: " . implode(',', $attrs));
  352. $this->messages[$this->currentMessage][$attrs['name']] = $attrs['type'];
  353. }
  354. if (isset($attrs['element'])) {
  355. $this->debug("msg " . $this->currentMessage . ": found part (with element) $attrs[name]: " . implode(',', $attrs));
  356. $this->messages[$this->currentMessage][$attrs['name']] = $attrs['element'] . '^';
  357. }
  358. }
  359. break;
  360. case 'portType':
  361. switch ($name) {
  362. case 'operation':
  363. $this->currentPortOperation = $attrs['name'];
  364. $this->debug("portType $this->currentPortType operation: $this->currentPortOperation");
  365. if (isset($attrs['parameterOrder'])) {
  366. $this->portTypes[$this->currentPortType][$attrs['name']]['parameterOrder'] = $attrs['parameterOrder'];
  367. }
  368. break;
  369. case 'documentation':
  370. $this->documentation = true;
  371. break;
  372. // merge input/output data
  373. default:
  374. $m = isset($attrs['message']) ? $this->getLocalPart($attrs['message']) : '';
  375. $this->portTypes[$this->currentPortType][$this->currentPortOperation][$name]['message'] = $m;
  376. break;
  377. }
  378. break;
  379. case 'binding':
  380. switch ($name) {
  381. case 'binding':
  382. // get ns prefix
  383. if (isset($attrs['style'])) {
  384. $this->bindings[$this->currentBinding]['prefix'] = $prefix;
  385. }
  386. $this->bindings[$this->currentBinding] = array_merge($this->bindings[$this->currentBinding], $attrs);
  387. break;
  388. case 'header':
  389. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus]['headers'][] = $attrs;
  390. break;
  391. case 'operation':
  392. if (isset($attrs['soapAction'])) {
  393. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['soapAction'] = $attrs['soapAction'];
  394. }
  395. if (isset($attrs['style'])) {
  396. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['style'] = $attrs['style'];
  397. }
  398. if (isset($attrs['name'])) {
  399. $this->currentOperation = $attrs['name'];
  400. $this->debug("current binding operation: $this->currentOperation");
  401. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['name'] = $attrs['name'];
  402. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['binding'] = $this->currentBinding;
  403. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation]['endpoint'] = isset($this->bindings[$this->currentBinding]['endpoint']) ? $this->bindings[$this->currentBinding]['endpoint'] : '';
  404. }
  405. break;
  406. case 'input':
  407. $this->opStatus = 'input';
  408. break;
  409. case 'output':
  410. $this->opStatus = 'output';
  411. break;
  412. case 'body':
  413. if (isset($this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus])) {
  414. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus] = array_merge($this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus], $attrs);
  415. } else {
  416. $this->bindings[$this->currentBinding]['operations'][$this->currentOperation][$this->opStatus] = $attrs;
  417. }
  418. break;
  419. }
  420. break;
  421. case 'service':
  422. switch ($name) {
  423. case 'port':
  424. $this->currentPort = $attrs['name'];
  425. $this->debug('current port: ' . $this->currentPort);
  426. $this->ports[$this->currentPort]['binding'] = $this->getLocalPart($attrs['binding']);
  427. break;
  428. case 'address':
  429. $this->ports[$this->currentPort]['location'] = $attrs['location'];
  430. $this->ports[$this->currentPort]['bindingType'] = $namespace;
  431. $this->bindings[ $this->ports[$this->currentPort]['binding'] ]['bindingType'] = $namespace;
  432. $this->bindings[ $this->ports[$this->currentPort]['binding'] ]['endpoint'] = $attrs['location'];
  433. break;
  434. }
  435. break;
  436. }
  437. // set status
  438. switch ($name) {
  439. case 'import':
  440. if (isset($attrs['location'])) {
  441. $this->import[$attrs['namespace']][] = array('location' => $attrs['location'], 'loaded' => false);
  442. $this->debug('parsing import ' . $attrs['namespace']. ' - ' . $attrs['location'] . ' (' . count($this->import[$attrs['namespace']]).')');
  443. } else {
  444. $this->import[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
  445. if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
  446. $this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
  447. }
  448. $this->debug('parsing import ' . $attrs['namespace']. ' - [no location] (' . count($this->import[$attrs['namespace']]).')');
  449. }
  450. break;
  451. //wait for schema
  452. //case 'types':
  453. // $this->status = 'schema';
  454. // break;
  455. case 'message':
  456. $this->status = 'message';
  457. $this->messages[$attrs['name']] = array();
  458. $this->currentMessage = $attrs['name'];
  459. break;
  460. case 'portType':
  461. $this->status = 'portType';
  462. $this->portTypes[$attrs['name']] = array();
  463. $this->currentPortType = $attrs['name'];
  464. break;
  465. case "binding":
  466. if (isset($attrs['name'])) {
  467. // get binding name
  468. if (strpos($attrs['name'], ':')) {
  469. $this->currentBinding = $this->getLocalPart($attrs['name']);
  470. } else {
  471. $this->currentBinding = $attrs['name'];
  472. }
  473. $this->status = 'binding';
  474. $this->bindings[$this->currentBinding]['portType'] = $this->getLocalPart($attrs['type']);
  475. $this->debug("current binding: $this->currentBinding of portType: " . $attrs['type']);
  476. }
  477. break;
  478. case 'service':
  479. $this->serviceName = $attrs['name'];
  480. $this->status = 'service';
  481. $this->debug('current service: ' . $this->serviceName);
  482. break;
  483. case 'definitions':
  484. foreach ($attrs as $name => $value) {
  485. $this->wsdl_info[$name] = $value;
  486. }
  487. break;
  488. }
  489. }
  490. }
  491. /**
  492. * end-element handler
  493. *
  494. * @param string $parser XML parser object
  495. * @param string $name element name
  496. * @access private
  497. */
  498. function end_element($parser, $name){
  499. // unset schema status
  500. if (/*preg_match('/types$/', $name) ||*/ preg_match('/schema$/', $name)) {
  501. $this->status = "";
  502. $this->appendDebug($this->currentSchema->getDebug());
  503. $this->currentSchema->clearDebug();
  504. $this->schemas[$this->currentSchema->schemaTargetNamespace][] = $this->currentSchema;
  505. $this->debug('Parsing WSDL schema done');
  506. }
  507. if ($this->status == 'schema') {
  508. $this->currentSchema->schemaEndElement($parser, $name);
  509. } else {
  510. // bring depth down a notch
  511. $this->depth--;
  512. }
  513. // end documentation
  514. if ($this->documentation) {
  515. //TODO: track the node to which documentation should be assigned; it can be a part, message, etc.
  516. //$this->portTypes[$this->currentPortType][$this->currentPortOperation]['documentation'] = $this->documentation;
  517. $this->documentation = false;
  518. }
  519. }
  520. /**
  521. * element content handler
  522. *
  523. * @param string $parser XML parser object
  524. * @param string $data element content
  525. * @access private
  526. */
  527. function character_data($parser, $data)
  528. {
  529. $pos = isset($this->depth_array[$this->depth]) ? $this->depth_array[$this->depth] : 0;
  530. if (isset($this->message[$pos]['cdata'])) {
  531. $this->message[$pos]['cdata'] .= $data;
  532. }
  533. if ($this->documentation) {
  534. $this->documentation .= $data;
  535. }
  536. }
  537. /**
  538. * if authenticating, set user credentials here
  539. *
  540. * @param string $username
  541. * @param string $password
  542. * @param string $authtype (basic|digest|certificate|ntlm)
  543. * @param array $certRequest (keys must be cainfofile (optional), sslcertfile, sslkeyfile, passphrase, certpassword (optional), verifypeer (optional), verifyhost (optional): see corresponding options in cURL docs)
  544. * @access public
  545. */
  546. function setCredentials($username, $password, $authtype = 'basic', $certRequest = array()) {
  547. $this->debug("setCredentials username=$username authtype=$authtype certRequest=");
  548. $this->appendDebug($this->varDump($certRequest));
  549. $this->username = $username;
  550. $this->password = $password;
  551. $this->authtype = $authtype;
  552. $this->certRequest = $certRequest;
  553. }
  554. function getBindingData($binding)
  555. {
  556. if (is_array($this->bindings[$binding])) {
  557. return $this->bindings[$binding];
  558. }
  559. }
  560. /**
  561. * returns an assoc array of operation names => operation data
  562. *
  563. * @param string $portName WSDL port name
  564. * @param string $bindingType eg: soap, smtp, dime (only soap and soap12 are currently supported)
  565. * @return array
  566. * @access public
  567. */
  568. function getOperations($portName = '', $bindingType = 'soap') {
  569. $ops = array();
  570. if ($bindingType == 'soap') {
  571. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  572. } elseif ($bindingType == 'soap12') {
  573. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  574. } else {
  575. $this->debug("getOperations bindingType $bindingType may not be supported");
  576. }
  577. $this->debug("getOperations for port '$portName' bindingType $bindingType");
  578. // loop thru ports
  579. foreach($this->ports as $port => $portData) {
  580. $this->debug("getOperations checking port $port bindingType " . $portData['bindingType']);
  581. if ($portName == '' || $port == $portName) {
  582. // binding type of port matches parameter
  583. if ($portData['bindingType'] == $bindingType) {
  584. $this->debug("getOperations found port $port bindingType $bindingType");
  585. //$this->debug("port data: " . $this->varDump($portData));
  586. //$this->debug("bindings: " . $this->varDump($this->bindings[ $portData['binding'] ]));
  587. // merge bindings
  588. if (isset($this->bindings[ $portData['binding'] ]['operations'])) {
  589. $ops = array_merge ($ops, $this->bindings[ $portData['binding'] ]['operations']);
  590. }
  591. }
  592. }
  593. }
  594. if (count($ops) == 0) {
  595. $this->debug("getOperations found no operations for port '$portName' bindingType $bindingType");
  596. }
  597. return $ops;
  598. }
  599. /**
  600. * returns an associative array of data necessary for calling an operation
  601. *
  602. * @param string $operation name of operation
  603. * @param string $bindingType type of binding eg: soap, soap12
  604. * @return array
  605. * @access public
  606. */
  607. function getOperationData($operation, $bindingType = 'soap')
  608. {
  609. if ($bindingType == 'soap') {
  610. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  611. } elseif ($bindingType == 'soap12') {
  612. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  613. }
  614. // loop thru ports
  615. foreach($this->ports as $port => $portData) {
  616. // binding type of port matches parameter
  617. if ($portData['bindingType'] == $bindingType) {
  618. // get binding
  619. //foreach($this->bindings[ $portData['binding'] ]['operations'] as $bOperation => $opData) {
  620. foreach(array_keys($this->bindings[ $portData['binding'] ]['operations']) as $bOperation) {
  621. // note that we could/should also check the namespace here
  622. if ($operation == $bOperation) {
  623. $opData = $this->bindings[ $portData['binding'] ]['operations'][$operation];
  624. return $opData;
  625. }
  626. }
  627. }
  628. }
  629. }
  630. /**
  631. * returns an associative array of data necessary for calling an operation
  632. *
  633. * @param string $soapAction soapAction for operation
  634. * @param string $bindingType type of binding eg: soap, soap12
  635. * @return array
  636. * @access public
  637. */
  638. function getOperationDataForSoapAction($soapAction, $bindingType = 'soap') {
  639. if ($bindingType == 'soap') {
  640. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  641. } elseif ($bindingType == 'soap12') {
  642. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  643. }
  644. // loop thru ports
  645. foreach($this->ports as $port => $portData) {
  646. // binding type of port matches parameter
  647. if ($portData['bindingType'] == $bindingType) {
  648. // loop through operations for the binding
  649. foreach ($this->bindings[ $portData['binding'] ]['operations'] as $bOperation => $opData) {
  650. if ($opData['soapAction'] == $soapAction) {
  651. return $opData;
  652. }
  653. }
  654. }
  655. }
  656. }
  657. /**
  658. * returns an array of information about a given type
  659. * returns false if no type exists by the given name
  660. *
  661. * typeDef = array(
  662. * 'elements' => array(), // refs to elements array
  663. * 'restrictionBase' => '',
  664. * 'phpType' => '',
  665. * 'order' => '(sequence|all)',
  666. * 'attrs' => array() // refs to attributes array
  667. * )
  668. *
  669. * @param string $type the type
  670. * @param string $ns namespace (not prefix) of the type
  671. * @return mixed
  672. * @access public
  673. * @see nusoap_xmlschema
  674. */
  675. function getTypeDef($type, $ns) {
  676. $this->debug("in getTypeDef: type=$type, ns=$ns");
  677. if ((! $ns) && isset($this->namespaces['tns'])) {
  678. $ns = $this->namespaces['tns'];
  679. $this->debug("in getTypeDef: type namespace forced to $ns");
  680. }
  681. if (!isset($this->schemas[$ns])) {
  682. foreach ($this->schemas as $ns0 => $schema0) {
  683. if (strcasecmp($ns, $ns0) == 0) {
  684. $this->debug("in getTypeDef: replacing schema namespace $ns with $ns0");
  685. $ns = $ns0;
  686. break;
  687. }
  688. }
  689. }
  690. if (isset($this->schemas[$ns])) {
  691. $this->debug("in getTypeDef: have schema for namespace $ns");
  692. for ($i = 0; $i < count($this->schemas[$ns]); $i++) {
  693. $xs = &$this->schemas[$ns][$i];
  694. $t = $xs->getTypeDef($type);
  695. $this->appendDebug($xs->getDebug());
  696. $xs->clearDebug();
  697. if ($t) {
  698. $this->debug("in getTypeDef: found type $type");
  699. if (!isset($t['phpType'])) {
  700. // get info for type to tack onto the element
  701. $uqType = substr($t['type'], strrpos($t['type'], ':') + 1);
  702. $ns = substr($t['type'], 0, strrpos($t['type'], ':'));
  703. $etype = $this->getTypeDef($uqType, $ns);
  704. if ($etype) {
  705. $this->debug("found type for [element] $type:");
  706. $this->debug($this->varDump($etype));
  707. if (isset($etype['phpType'])) {
  708. $t['phpType'] = $etype['phpType'];
  709. }
  710. if (isset($etype['elements'])) {
  711. $t['elements'] = $etype['elements'];
  712. }
  713. if (isset($etype['attrs'])) {
  714. $t['attrs'] = $etype['attrs'];
  715. }
  716. } else {
  717. $this->debug("did not find type for [element] $type");
  718. }
  719. }
  720. return $t;
  721. }
  722. }
  723. $this->debug("in getTypeDef: did not find type $type");
  724. } else {
  725. $this->debug("in getTypeDef: do not have schema for namespace $ns");
  726. }
  727. return false;
  728. }
  729. /**
  730. * prints html description of services
  731. *
  732. * @access private
  733. */
  734. function webDescription(){
  735. global $HTTP_SERVER_VARS;
  736. if (isset($_SERVER)) {
  737. $PHP_SELF = $_SERVER['PHP_SELF'];
  738. } elseif (isset($HTTP_SERVER_VARS)) {
  739. $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
  740. } else {
  741. $this->setError("Neither _SERVER nor HTTP_SERVER_VARS is available");
  742. }
  743. $b = '
  744. <html><head><title>NuSOAP: '.$this->serviceName.'</title>
  745. <style type="text/css">
  746. body { font-family: arial; color: #000000; background-color: #ffffff; margin: 0px 0px 0px 0px; }
  747. p { font-family: arial; color: #000000; margin-top: 0px; margin-bottom: 12px; }
  748. pre { background-color: silver; padding: 5px; font-family: Courier New; font-size: x-small; color: #000000;}
  749. ul { margin-top: 10px; margin-left: 20px; }
  750. li { list-style-type: none; margin-top: 10px; color: #000000; }
  751. .content{
  752. margin-left: 0px; padding-bottom: 2em; }
  753. .nav {
  754. padding-top: 10px; padding-bottom: 10px; padding-left: 15px; font-size: .70em;
  755. margin-top: 10px; margin-left: 0px; color: #000000;
  756. background-color: #ccccff; width: 20%; margin-left: 20px; margin-top: 20px; }
  757. .title {
  758. font-family: arial; font-size: 26px; color: #ffffff;
  759. background-color: #999999; width: 100%;
  760. margin-left: 0px; margin-right: 0px;
  761. padding-top: 10px; padding-bottom: 10px;}
  762. .hidden {
  763. position: absolute; visibility: hidden; z-index: 200; left: 250px; top: 100px;
  764. font-family: arial; overflow: hidden; width: 600;
  765. padding: 20px; font-size: 10px; background-color: #999999;
  766. layer-background-color:#FFFFFF; }
  767. a,a:active { color: charcoal; font-weight: bold; }
  768. a:visited { color: #666666; font-weight: bold; }
  769. a:hover { color: cc3300; font-weight: bold; }
  770. </style>
  771. <script language="JavaScript" type="text/javascript">
  772. <!--
  773. // POP-UP CAPTIONS...
  774. function lib_bwcheck(){ //Browsercheck (needed)
  775. this.ver=navigator.appVersion
  776. this.agent=navigator.userAgent
  777. this.dom=document.getElementById?1:0
  778. this.opera5=this.agent.indexOf("Opera 5")>-1
  779. this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
  780. this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  781. this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  782. this.ie=this.ie4||this.ie5||this.ie6
  783. this.mac=this.agent.indexOf("Mac")>-1
  784. this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
  785. this.ns4=(document.layers && !this.dom)?1:0;
  786. this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
  787. return this
  788. }
  789. var bw = new lib_bwcheck()
  790. //Makes crossbrowser object.
  791. function makeObj(obj){
  792. this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj]:0;
  793. if(!this.evnt) return false
  794. this.css=bw.dom||bw.ie4?this.evnt.style:bw.ns4?this.evnt:0;
  795. this.wref=bw.dom||bw.ie4?this.evnt:bw.ns4?this.css.document:0;
  796. this.writeIt=b_writeIt;
  797. return this
  798. }
  799. // A unit of measure that will be added when setting the position of a layer.
  800. //var px = bw.ns4||window.opera?"":"px";
  801. function b_writeIt(text){
  802. if (bw.ns4){this.wref.write(text);this.wref.close()}
  803. else this.wref.innerHTML = text
  804. }
  805. //Shows the messages
  806. var oDesc;
  807. function popup(divid){
  808. if(oDesc = new makeObj(divid)){
  809. oDesc.css.visibility = "visible"
  810. }
  811. }
  812. function popout(){ // Hides message
  813. if(oDesc) oDesc.css.visibility = "hidden"
  814. }
  815. //-->
  816. </script>
  817. </head>
  818. <body>
  819. <div class=content>
  820. <br><br>
  821. <div class=title>'.$this->serviceName.'</div>
  822. <div class=nav>
  823. <p>View the <a href="'.$PHP_SELF.'?wsdl">WSDL</a> for the service.
  824. Click on an operation name to view it&apos;s details.</p>
  825. <ul>';
  826. foreach($this->getOperations() as $op => $data){
  827. $b .= "<li><a href='#' onclick=\"popout();popup('$op')\">$op</a></li>";
  828. // create hidden div
  829. $b .= "<div id='$op' class='hidden'>
  830. <a href='#' onclick='popout()'><font color='#ffffff'>Close</font></a><br><br>";
  831. foreach($data as $donnie => $marie){ // loop through opdata
  832. if($donnie == 'input' || $donnie == 'output'){ // show input/output data
  833. $b .= "<font color='white'>".ucfirst($donnie).':</font><br>';
  834. foreach($marie as $captain => $tenille){ // loop through data
  835. if($captain == 'parts'){ // loop thru parts
  836. $b .= "&nbsp;&nbsp;$captain:<br>";
  837. //if(is_array($tenille)){
  838. foreach($tenille as $joanie => $chachi){
  839. $b .= "&nbsp;&nbsp;&nbsp;&nbsp;$joanie: $chachi<br>";
  840. }
  841. //}
  842. } else {
  843. $b .= "&nbsp;&nbsp;$captain: $tenille<br>";
  844. }
  845. }
  846. } else {
  847. $b .= "<font color='white'>".ucfirst($donnie).":</font> $marie<br>";
  848. }
  849. }
  850. $b .= '</div>';
  851. }
  852. $b .= '
  853. <ul>
  854. </div>
  855. </div></body></html>';
  856. return $b;
  857. }
  858. /**
  859. * serialize the parsed wsdl
  860. *
  861. * @param mixed $debug whether to put debug=1 in endpoint URL
  862. * @return string serialization of WSDL
  863. * @access public
  864. */
  865. function serialize($debug = 0)
  866. {
  867. $xml = '<?xml version="1.0" encoding="ISO-8859-1"?>';
  868. $xml .= "\n<definitions";
  869. foreach($this->namespaces as $k => $v) {
  870. $xml .= " xmlns:$k=\"$v\"";
  871. }
  872. // 10.9.02 - add poulter fix for wsdl and tns declarations
  873. if (isset($this->namespaces['wsdl'])) {
  874. $xml .= " xmlns=\"" . $this->namespaces['wsdl'] . "\"";
  875. }
  876. if (isset($this->namespaces['tns'])) {
  877. $xml .= " targetNamespace=\"" . $this->namespaces['tns'] . "\"";
  878. }
  879. $xml .= '>';
  880. // imports
  881. if (sizeof($this->import) > 0) {
  882. foreach($this->import as $ns => $list) {
  883. foreach ($list as $ii) {
  884. if ($ii['location'] != '') {
  885. $xml .= '<import location="' . $ii['location'] . '" namespace="' . $ns . '" />';
  886. } else {
  887. $xml .= '<import namespace="' . $ns . '" />';
  888. }
  889. }
  890. }
  891. }
  892. // types
  893. if (count($this->schemas)>=1) {
  894. $xml .= "\n<types>\n";
  895. foreach ($this->schemas as $ns => $list) {
  896. foreach ($list as $xs) {
  897. $xml .= $xs->serializeSchema();
  898. }
  899. }
  900. $xml .= '</types>';
  901. }
  902. // messages
  903. if (count($this->messages) >= 1) {
  904. foreach($this->messages as $msgName => $msgParts) {
  905. $xml .= "\n<message name=\"" . $msgName . '">';
  906. if(is_array($msgParts)){
  907. foreach($msgParts as $partName => $partType) {
  908. // print 'serializing '.$partType.', sv: '.$this->XMLSchemaVersion.'<br>';
  909. if (strpos($partType, ':')) {
  910. $typePrefix = $this->getPrefixFromNamespace($this->getPrefix($partType));
  911. } elseif (isset($this->typemap[$this->namespaces['xsd']][$partType])) {
  912. // print 'checking typemap: '.$this->XMLSchemaVersion.'<br>';
  913. $typePrefix = 'xsd';
  914. } else {
  915. foreach($this->typemap as $ns => $types) {
  916. if (isset($types[$partType])) {
  917. $typePrefix = $this->getPrefixFromNamespace($ns);
  918. }
  919. }
  920. if (!isset($typePrefix)) {
  921. die("$partType has no namespace!");
  922. }
  923. }
  924. $ns = $this->getNamespaceFromPrefix($typePrefix);
  925. $localPart = $this->getLocalPart($partType);
  926. $typeDef = $this->getTypeDef($localPart, $ns);
  927. if ($typeDef['typeClass'] == 'element') {
  928. $elementortype = 'element';
  929. if (substr($localPart, -1) == '^') {
  930. $localPart = substr($localPart, 0, -1);
  931. }
  932. } else {
  933. $elementortype = 'type';
  934. }
  935. $xml .= "\n" . ' <part name="' . $partName . '" ' . $elementortype . '="' . $typePrefix . ':' . $localPart . '" />';
  936. }
  937. }
  938. $xml .= '</message>';
  939. }
  940. }
  941. // bindings & porttypes
  942. if (count($this->bindings) >= 1) {
  943. $binding_xml = '';
  944. $portType_xml = '';
  945. foreach($this->bindings as $bindingName => $attrs) {
  946. $binding_xml .= "\n<binding name=\"" . $bindingName . '" type="tns:' . $attrs['portType'] . '">';
  947. $binding_xml .= "\n" . ' <soap:binding style="' . $attrs['style'] . '" transport="' . $attrs['transport'] . '"/>';
  948. $portType_xml .= "\n<portType name=\"" . $attrs['portType'] . '">';
  949. foreach($attrs['operations'] as $opName => $opParts) {
  950. $binding_xml .= "\n" . ' <operation name="' . $opName . '">';
  951. $binding_xml .= "\n" . ' <soap:operation soapAction="' . $opParts['soapAction'] . '" style="'. $opParts['style'] . '"/>';
  952. if (isset($opParts['input']['encodingStyle']) && $opParts['input']['encodingStyle'] != '') {
  953. $enc_style = ' encodingStyle="' . $opParts['input']['encodingStyle'] . '"';
  954. } else {
  955. $enc_style = '';
  956. }
  957. $binding_xml .= "\n" . ' <input><soap:body use="' . $opParts['input']['use'] . '" namespace="' . $opParts['input']['namespace'] . '"' . $enc_style . '/></input>';
  958. if (isset($opParts['output']['encodingStyle']) && $opParts['output']['encodingStyle'] != '') {
  959. $enc_style = ' encodingStyle="' . $opParts['output']['encodingStyle'] . '"';
  960. } else {
  961. $enc_style = '';
  962. }
  963. $binding_xml .= "\n" . ' <output><soap:body use="' . $opParts['output']['use'] . '" namespace="' . $opParts['output']['namespace'] . '"' . $enc_style . '/></output>';
  964. $binding_xml .= "\n" . ' </operation>';
  965. $portType_xml .= "\n" . ' <operation name="' . $opParts['name'] . '"';
  966. if (isset($opParts['parameterOrder'])) {
  967. $portType_xml .= ' parameterOrder="' . $opParts['parameterOrder'] . '"';
  968. }
  969. $portType_xml .= '>';
  970. if(isset($opParts['documentation']) && $opParts['documentation'] != '') {
  971. $portType_xml .= "\n" . ' <documentation>' . htmlspecialchars($opParts['documentation']) . '</documentation>';
  972. }
  973. $portType_xml .= "\n" . ' <input message="tns:' . $opParts['input']['message'] . '"/>';
  974. $portType_xml .= "\n" . ' <output message="tns:' . $opParts['output']['message'] . '"/>';
  975. $portType_xml .= "\n" . ' </operation>';
  976. }
  977. $portType_xml .= "\n" . '</portType>';
  978. $binding_xml .= "\n" . '</binding>';
  979. }
  980. $xml .= $portType_xml . $binding_xml;
  981. }
  982. // services
  983. $xml .= "\n<service name=\"" . $this->serviceName . '">';
  984. if (count($this->ports) >= 1) {
  985. foreach($this->ports as $pName => $attrs) {
  986. $xml .= "\n" . ' <port name="' . $pName . '" binding="tns:' . $attrs['binding'] . '">';
  987. $xml .= "\n" . ' <soap:address location="' . $attrs['location'] . ($debug ? '?debug=1' : '') . '"/>';
  988. $xml .= "\n" . ' </port>';
  989. }
  990. }
  991. $xml .= "\n" . '</service>';
  992. return $xml . "\n</definitions>";
  993. }
  994. /**
  995. * determine whether a set of parameters are unwrapped
  996. * when they are expect to be wrapped, Microsoft-style.
  997. *
  998. * @param string $type the type (element name) of the wrapper
  999. * @param array $parameters the parameter values for the SOAP call
  1000. * @return boolean whether they parameters are unwrapped (and should be wrapped)
  1001. * @access private
  1002. */
  1003. function parametersMatchWrapped($type, &$parameters) {
  1004. $this->debug("in parametersMatchWrapped type=$type, parameters=");
  1005. $this->appendDebug($this->varDump($parameters));
  1006. // split type into namespace:unqualified-type
  1007. if (strpos($type, ':')) {
  1008. $uqType = substr($type, strrpos($type, ':') + 1);
  1009. $ns = substr($type, 0, strrpos($type, ':'));
  1010. $this->debug("in parametersMatchWrapped: got a prefixed type: $uqType, $ns");
  1011. if ($this->getNamespaceFromPrefix($ns)) {
  1012. $ns = $this->getNamespaceFromPrefix($ns);
  1013. $this->debug("in parametersMatchWrapped: expanded prefixed type: $uqType, $ns");
  1014. }
  1015. } else {
  1016. // TODO: should the type be compared to types in XSD, and the namespace
  1017. // set to XSD if the type matches?
  1018. $this->debug("in parametersMatchWrapped: No namespace for type $type");
  1019. $ns = '';
  1020. $uqType = $type;
  1021. }
  1022. // get the type information
  1023. if (!$typeDef = $this->getTypeDef($uqType, $ns)) {
  1024. $this->debug("in parametersMatchWrapped: $type ($uqType) is not a supported type.");
  1025. return false;
  1026. }
  1027. $this->debug("in parametersMatchWrapped: found typeDef=");
  1028. $this->appendDebug($this->varDump($typeDef));
  1029. if (substr($uqType, -1) == '^') {
  1030. $uqType = substr($uqType, 0, -1);
  1031. }
  1032. $phpType = $typeDef['phpType'];
  1033. $arrayType = (isset($typeDef['arrayType']) ? $typeDef['arrayType'] : '');
  1034. $this->debug("in parametersMatchWrapped: uqType: $uqType, ns: $ns, phptype: $phpType, arrayType: $arrayType");
  1035. // we expect a complexType or element of complexType
  1036. if ($phpType != 'struct') {
  1037. $this->debug("in parametersMatchWrapped: not a struct");
  1038. return false;
  1039. }
  1040. // see whether the parameter names match the elements
  1041. if (isset($typeDef['elements']) && is_array($typeDef['elements'])) {
  1042. $elements = 0;
  1043. $matches = 0;
  1044. foreach ($typeDef['elements'] as $name => $attrs) {
  1045. if (isset($parameters[$name])) {
  1046. $this->debug("in parametersMatchWrapped: have parameter named $name");
  1047. $matches++;
  1048. } else {
  1049. $this->debug("in parametersMatchWrapped: do not have parameter named $name");
  1050. }
  1051. $elements++;
  1052. }
  1053. $this->debug("in parametersMatchWrapped: $matches parameter names match $elements wrapped parameter names");
  1054. if ($matches == 0) {
  1055. return false;
  1056. }
  1057. return true;
  1058. }
  1059. // since there are no elements for the type, if the user passed no
  1060. // parameters, the parameters match wrapped.
  1061. $this->debug("in parametersMatchWrapped: no elements type $ns:$uqType");
  1062. return count($parameters) == 0;
  1063. }
  1064. /**
  1065. * serialize PHP values according to a WSDL message definition
  1066. * contrary to the method name, this is not limited to RPC
  1067. *
  1068. * TODO
  1069. * - multi-ref serialization
  1070. * - validate PHP values against type definitions, return errors if invalid
  1071. *
  1072. * @param string $operation operation name
  1073. * @param string $direction (input|output)
  1074. * @param mixed $parameters parameter value(s)
  1075. * @param string $bindingType (soap|soap12)
  1076. * @return mixed parameters serialized as XML or false on error (e.g. operation not found)
  1077. * @access public
  1078. */
  1079. function serializeRPCParameters($operation, $direction, $parameters, $bindingType = 'soap') {
  1080. $this->debug("in serializeRPCParameters: operation=$operation, direction=$direction, XMLSchemaVersion=$this->XMLSchemaVersion, bindingType=$bindingType");
  1081. $this->appendDebug('parameters=' . $this->varDump($parameters));
  1082. if ($direction != 'input' && $direction != 'output') {
  1083. $this->debug('The value of the \$direction argument needs to be either "input" or "output"');
  1084. $this->setError('The value of the \$direction argument needs to be either "input" or "output"');
  1085. return false;
  1086. }
  1087. if (!$opData = $this->getOperationData($operation, $bindingType)) {
  1088. $this->debug('Unable to retrieve WSDL data for operation: ' . $operation . ' bindingType: ' . $bindingType);
  1089. $this->setError('Unable to retrieve WSDL data for operation: ' . $operation . ' bindingType: ' . $bindingType);
  1090. return false;
  1091. }
  1092. $this->debug('in serializeRPCParameters: opData:');
  1093. $this->appendDebug($this->varDump($opData));
  1094. // Get encoding style for output and set to current
  1095. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  1096. if(($direction == 'input') && isset($opData['output']['encodingStyle']) && ($opData['output']['encodingStyle'] != $encodingStyle)) {
  1097. $encodingStyle = $opData['output']['encodingStyle'];
  1098. $enc_style = $encodingStyle;
  1099. }
  1100. // set input params
  1101. $xml = '';
  1102. if (isset($opData[$direction]['parts']) && sizeof($opData[$direction]['parts']) > 0) {
  1103. $parts = &$opData[$direction]['parts'];
  1104. $part_count = sizeof($parts);
  1105. $style = $opData['style'];
  1106. $use = $opData[$direction]['use'];
  1107. $this->debug("have $part_count part(s) to serialize using $style/$use");
  1108. if (is_array($parameters)) {
  1109. $parametersArrayType = $this->isArraySimpleOrStruct($parameters);
  1110. $parameter_count = count($parameters);
  1111. $this->debug("have $parameter_count parameter(s) provided as $parametersArrayType to serialize");
  1112. // check for Microsoft-style wrapped parameters
  1113. if ($style == 'document' && $use == 'literal' && $part_count == 1 && isset($parts['parameters'])) {
  1114. $this->debug('check whether the caller has wrapped the parameters');
  1115. if ($direction == 'output' && $parametersArrayType == 'arraySimple' && $parameter_count == 1) {
  1116. // TODO: consider checking here for double-wrapping, when
  1117. // service function wraps, then NuSOAP wraps again
  1118. $this->debug("change simple array to associative with 'parameters' element");
  1119. $parameters['parameters'] = $parameters[0];
  1120. unset($parameters[0]);
  1121. }
  1122. if (($parametersArrayType == 'arrayStruct' || $parameter_count == 0) && !isset($parameters['parameters'])) {
  1123. $this->debug('check whether caller\'s parameters match the wrapped ones');
  1124. if ($this->parametersMatchWrapped($parts['parameters'], $parameters)) {
  1125. $this->debug('wrap the parameters for the caller');
  1126. $parameters = array('parameters' => $parameters);
  1127. $parameter_count = 1;
  1128. }
  1129. }
  1130. }
  1131. foreach ($parts as $name => $type) {
  1132. $this->debug("serializing part $name of type $type");
  1133. // Track encoding style
  1134. if (isset($opData[$direction]['encodingStyle']) && $encodingStyle != $opData[$direction]['encodingStyle']) {
  1135. $encodingStyle = $opData[$direction]['encodingStyle'];
  1136. $enc_style = $encodingStyle;
  1137. } else {
  1138. $enc_style = false;
  1139. }
  1140. // NOTE: add error handling here
  1141. // if serializeType returns false, then catch global error and fault
  1142. if ($parametersArrayType == 'arraySimple') {
  1143. $p = array_shift($parameters);
  1144. $this->debug('calling serializeType w/indexed param');
  1145. $xml .= $this->serializeType($name, $type, $p, $use, $enc_style);
  1146. } elseif (isset($parameters[$name])) {
  1147. $this->debug('calling serializeType w/named param');
  1148. $xml .= $this->serializeType($name, $type, $parameters[$name], $use, $enc_style);
  1149. } else {
  1150. // TODO: only send nillable
  1151. $this->debug('calling serializeType w/null param');
  1152. $xml .= $this->serializeType($name, $type, null, $use, $enc_style);
  1153. }
  1154. }
  1155. } else {
  1156. $this->debug('no parameters passed.');
  1157. }
  1158. }
  1159. $this->debug("serializeRPCParameters returning: $xml");
  1160. return $xml;
  1161. }
  1162. /**
  1163. * serialize a PHP value according to a WSDL message definition
  1164. *
  1165. * TODO
  1166. * - multi-ref serialization
  1167. * - validate PHP values against type definitions, return errors if invalid
  1168. *
  1169. * @param string $operation operation name
  1170. * @param string $direction (input|output)
  1171. * @param mixed $parameters parameter value(s)
  1172. * @return mixed parameters serialized as XML or false on error (e.g. operation not found)
  1173. * @access public
  1174. * @deprecated
  1175. */
  1176. function serializeParameters($operation, $direction, $parameters)
  1177. {
  1178. $this->debug("in serializeParameters: operation=$operation, direction=$direction, XMLSchemaVersion=$this->XMLSchemaVersion");
  1179. $this->appendDebug('parameters=' . $this->varDump($parameters));
  1180. if ($direction != 'input' && $direction != 'output') {
  1181. $this->debug('The value of the \$direction argument needs to be either "input" or "output"');
  1182. $this->setError('The value of the \$direction argument needs to be either "input" or "output"');
  1183. return false;
  1184. }
  1185. if (!$opData = $this->getOperationData($operation)) {
  1186. $this->debug('Unable to retrieve WSDL data for operation: ' . $operation);
  1187. $this->setError('Unable to retrieve WSDL data for operation: ' . $operation);
  1188. return false;
  1189. }
  1190. $this->debug('opData:');
  1191. $this->appendDebug($this->varDump($opData));
  1192. // Get encoding style for output and set to current
  1193. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  1194. if(($direction == 'input') && isset($opData['output']['encodingStyle']) && ($opData['output']['encodingStyle'] != $encodingStyle)) {
  1195. $encodingStyle = $opData['output']['encodingStyle'];
  1196. $enc_style = $encodingStyle;
  1197. }
  1198. // set input params
  1199. $xml = '';
  1200. if (isset($opData[$direction]['parts']) && sizeof($opData[$direction]['parts']) > 0) {
  1201. $use = $opData[$direction]['use'];
  1202. $this->debug("use=$use");
  1203. $this->debug('got ' . count($opData[$direction]['parts']) . ' part(s)');
  1204. if (is_array($parameters)) {
  1205. $parametersArrayType = $this->isArraySimpleOrStruct($parameters);
  1206. $this->debug('have ' . $parametersArrayType . ' parameters');
  1207. foreach($opData[$direction]['parts'] as $name => $type) {
  1208. $this->debug('serializing part "'.$name.'" of type "'.$type.'"');
  1209. // Track encoding style
  1210. if(isset($opData[$direction]['encodingStyle']) && $encodingStyle != $opData[$direction]['encodingStyle']) {
  1211. $encodingStyle = $opData[$direction]['encodingStyle'];
  1212. $enc_style = $encodingStyle;
  1213. } else {
  1214. $enc_style = false;
  1215. }
  1216. // NOTE: add error handling here
  1217. // if serializeType returns false, then catch global error and fault
  1218. if ($parametersArrayType == 'arraySimple') {
  1219. $p = array_shift($parameters);
  1220. $this->debug('calling serializeType w/indexed param');
  1221. $xml .= $this->serializeType($name, $type, $p, $use, $enc_style);
  1222. } elseif (isset($parameters[$name])) {
  1223. $this->debug('calling serializeType w/named param');
  1224. $xml .= $this->serializeType($name, $type, $parameters[$name], $use, $enc_style);
  1225. } else {
  1226. // TODO: only send nillable
  1227. $this->debug('calling serializeType w/null param');
  1228. $xml .= $this->serializeType($name, $type, null, $use, $enc_style);
  1229. }
  1230. }
  1231. } else {
  1232. $this->debug('no parameters passed.');
  1233. }
  1234. }
  1235. $this->debug("serializeParameters returning: $xml");
  1236. return $xml;
  1237. }
  1238. /**
  1239. * serializes a PHP value according a given type definition
  1240. *
  1241. * @param string $name name of value (part or element)
  1242. * @param string $type XML schema type of value (type or element)
  1243. * @param mixed $value a native PHP value (parameter value)
  1244. * @param string $use use for part (encoded|literal)
  1245. * @param string $encodingStyle SOAP encoding style for the value (if different than the enclosing style)
  1246. * @param boolean $unqualified a kludge for what should be XML namespace form handling
  1247. * @return string value serialized as an XML string
  1248. * @access private
  1249. */
  1250. function serializeType($name, $type, $value, $use='encoded', $encodingStyle=false, $unqualified=false)
  1251. {
  1252. $this->debug("in serializeType: name=$name, type=$type, use=$use, encodingStyle=$encodingStyle, unqualified=" . ($unqualified ? "unqualified" : "qualified"));
  1253. $this->appendDebug("value=" . $this->varDump($value));
  1254. if($use == 'encoded' && $encodingStyle) {
  1255. $encodingStyle = ' SOAP-ENV:encodingStyle="' . $encodingStyle . '"';
  1256. }
  1257. // if a soapval has been supplied, let its type override the WSDL
  1258. if (is_object($value) && get_class($value) == 'soapval') {
  1259. if ($value->type_ns) {
  1260. $type = $value->type_ns . ':' . $value->type;
  1261. $forceType = true;
  1262. $this->debug("in serializeType: soapval overrides type to $type");
  1263. } elseif ($value->type) {
  1264. $type = $value->type;
  1265. $forceType = true;
  1266. $this->debug("in serializeType: soapval overrides type to $type");
  1267. } else {
  1268. $forceType = false;
  1269. $this->debug("in serializeType: soapval does not override type");
  1270. }
  1271. $attrs = $value->attributes;
  1272. $value = $value->value;
  1273. $this->debug("in serializeType: soapval overrides value to $value");
  1274. if ($attrs) {
  1275. if (!is_array($value)) {
  1276. $value['!'] = $value;
  1277. }
  1278. foreach ($attrs as $n => $v) {
  1279. $value['!' . $n] = $v;
  1280. }
  1281. $this->debug("in serializeType: soapval provides attributes");
  1282. }
  1283. } else {
  1284. $forceType = false;
  1285. }
  1286. $xml = '';
  1287. if (strpos($type, ':')) {
  1288. $uqType = substr($type, strrpos($type, ':') + 1);
  1289. $ns = substr($type, 0, strrpos($type, ':'));
  1290. $this->debug("in serializeType: got a prefixed type: $uqType, $ns");
  1291. if ($this->getNamespaceFromPrefix($ns)) {
  1292. $ns = $this->getNamespaceFromPrefix($ns);
  1293. $this->debug("in serializeType: expanded prefixed type: $uqType, $ns");
  1294. }
  1295. if($ns == $this->XMLSchemaVersion || $ns == 'http://schemas.xmlsoap.org/soap/encoding/'){
  1296. $this->debug('in serializeType: type namespace indicates XML Schema or SOAP Encoding type');
  1297. if ($unqualified && $use == 'literal') {
  1298. $elementNS = " xmlns=\"\"";
  1299. } else {
  1300. $elementNS = '';
  1301. }
  1302. if (is_null($value)) {
  1303. if ($use == 'literal') {
  1304. // TODO: depends on minOccurs
  1305. $xml = "<$name$elementNS/>";
  1306. } else {
  1307. // TODO: depends on nillable, which should be checked before calling this method
  1308. $xml = "<$name$elementNS xsi:nil=\"true\" xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"/>";
  1309. }
  1310. $this->debug("in serializeType: returning: $xml");
  1311. return $xml;
  1312. }
  1313. if ($uqType == 'Array') {
  1314. // JBoss/Axis does this sometimes
  1315. return $this->serialize_val($value, $name, false, false, false, false, $use);
  1316. }
  1317. if ($uqType == 'boolean') {
  1318. if ((is_string($value) && $value == 'false') || (! $value)) {
  1319. $value = 'false';
  1320. } else {
  1321. $value = 'true';
  1322. }
  1323. }
  1324. if ($uqType == 'string' && gettype($value) == 'string') {
  1325. $value = $this->expandEntities($value);
  1326. }
  1327. if (($uqType == 'long' || $uqType == 'unsignedLong') && gettype($value) == 'double') {
  1328. $value = sprintf("%.0lf", $value);
  1329. }
  1330. // it's a scalar
  1331. // TODO: what about null/nil values?
  1332. // check type isn't a custom type extending xmlschema namespace
  1333. if (!$this->getTypeDef($uqType, $ns)) {
  1334. if ($use == 'literal') {
  1335. if ($forceType) {
  1336. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">$value</$name>";
  1337. } else {
  1338. $xml = "<$name$elementNS>$value</$name>";
  1339. }
  1340. } else {
  1341. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>$value</$name>";
  1342. }
  1343. $this->debug("in serializeType: returning: $xml");
  1344. return $xml;
  1345. }
  1346. $this->debug('custom type extends XML Schema or SOAP Encoding namespace (yuck)');
  1347. } else if ($ns == 'http://xml.apache.org/xml-soap') {
  1348. $this->debug('in serializeType: appears to be Apache SOAP type');
  1349. if ($uqType == 'Map') {
  1350. $tt_prefix = $this->getPrefixFromNamespace('http://xml.apache.org/xml-soap');
  1351. if (! $tt_prefix) {
  1352. $this->debug('in serializeType: Add namespace for Apache SOAP type');
  1353. $tt_prefix = 'ns' . rand(1000, 9999);
  1354. $this->namespaces[$tt_prefix] = 'http://xml.apache.org/xml-soap';
  1355. // force this to be added to usedNamespaces
  1356. $tt_prefix = $this->getPrefixFromNamespace('http://xml.apache.org/xml-soap');
  1357. }
  1358. $contents = '';
  1359. foreach($value as $k => $v) {
  1360. $this->debug("serializing map element: key $k, value $v");
  1361. $contents .= '<item>';
  1362. $contents .= $this->serialize_val($k,'key',false,false,false,false,$use);
  1363. $contents .= $this->serialize_val($v,'value',false,false,false,false,$use);
  1364. $contents .= '</item>';
  1365. }
  1366. if ($use == 'literal') {
  1367. if ($forceType) {
  1368. $xml = "<$name xsi:type=\"" . $tt_prefix . ":$uqType\">$contents</$name>";
  1369. } else {
  1370. $xml = "<$name>$contents</$name>";
  1371. }
  1372. } else {
  1373. $xml = "<$name xsi:type=\"" . $tt_prefix . ":$uqType\"$encodingStyle>$contents</$name>";
  1374. }
  1375. $this->debug("in serializeType: returning: $xml");
  1376. return $xml;
  1377. }
  1378. $this->debug('in serializeType: Apache SOAP type, but only support Map');
  1379. }
  1380. } else {
  1381. // TODO: should the type be compared to types in XSD, and the namespace
  1382. // set to XSD if the type matches?
  1383. $this->debug("in serializeType: No namespace for type $type");
  1384. $ns = '';
  1385. $uqType = $type;
  1386. }
  1387. if(!$typeDef = $this->getTypeDef($uqType, $ns)){
  1388. $this->setError("$type ($uqType) is not a supported type.");
  1389. $this->debug("in serializeType: $type ($uqType) is not a supported type.");
  1390. return false;
  1391. } else {
  1392. $this->debug("in serializeType: found typeDef");
  1393. $this->appendDebug('typeDef=' . $this->varDump($typeDef));
  1394. if (substr($uqType, -1) == '^') {
  1395. $uqType = substr($uqType, 0, -1);
  1396. }
  1397. }
  1398. if (!isset($typeDef['phpType'])) {
  1399. $this->setError("$type ($uqType) has no phpType.");
  1400. $this->debug("in serializeType: $type ($uqType) has no phpType.");
  1401. return false;
  1402. }
  1403. $phpType = $typeDef['phpType'];
  1404. $this->debug("in serializeType: uqType: $uqType, ns: $ns, phptype: $phpType, arrayType: " . (isset($typeDef['arrayType']) ? $typeDef['arrayType'] : '') );
  1405. // if php type == struct, map value to the <all> element names
  1406. if ($phpType == 'struct') {
  1407. if (isset($typeDef['typeClass']) && $typeDef['typeClass'] == 'element') {
  1408. $elementName = $uqType;
  1409. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  1410. $elementNS = " xmlns=\"$ns\"";
  1411. } else {
  1412. $elementNS = " xmlns=\"\"";
  1413. }
  1414. } else {
  1415. $elementName = $name;
  1416. if ($unqualified) {
  1417. $elementNS = " xmlns=\"\"";
  1418. } else {
  1419. $elementNS = '';
  1420. }
  1421. }
  1422. if (is_null($value)) {
  1423. if ($use == 'literal') {
  1424. // TODO: depends on minOccurs and nillable
  1425. $xml = "<$elementName$elementNS/>";
  1426. } else {
  1427. $xml = "<$elementName$elementNS xsi:nil=\"true\" xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"/>";
  1428. }
  1429. $this->debug("in serializeType: returning: $xml");
  1430. return $xml;
  1431. }
  1432. if (is_object($value)) {
  1433. $value = get_object_vars($value);
  1434. }
  1435. if (is_array($value)) {
  1436. $elementAttrs = $this->serializeComplexTypeAttributes($typeDef, $value, $ns, $uqType);
  1437. if ($use == 'literal') {
  1438. if ($forceType) {
  1439. $xml = "<$elementName$elementNS$elementAttrs xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">";
  1440. } else {
  1441. $xml = "<$elementName$elementNS$elementAttrs>";
  1442. }
  1443. } else {
  1444. $xml = "<$elementName$elementNS$elementAttrs xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>";
  1445. }
  1446. if (isset($typeDef['simpleContent']) && $typeDef['simpleContent'] == 'true') {
  1447. if (isset($value['!'])) {
  1448. $xml .= $value['!'];
  1449. $this->debug("in serializeType: serialized simpleContent for type $type");
  1450. } else {
  1451. $this->debug("in serializeType: no simpleContent to serialize for type $type");
  1452. }
  1453. } else {
  1454. // complexContent
  1455. $xml .= $this->serializeComplexTypeElements($typeDef, $value, $ns, $uqType, $use, $encodingStyle);
  1456. }
  1457. $xml .= "</$elementName>";
  1458. } else {
  1459. $this->debug("in serializeType: phpType is struct, but value is not an array");
  1460. $this->setError("phpType is struct, but value is not an array: see debug output for details");
  1461. $xml = '';
  1462. }
  1463. } elseif ($phpType == 'array') {
  1464. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  1465. $elementNS = " xmlns=\"$ns\"";
  1466. } else {
  1467. if ($unqualified) {
  1468. $elementNS = " xmlns=\"\"";
  1469. } else {
  1470. $elementNS = '';
  1471. }
  1472. }
  1473. if (is_null($value)) {
  1474. if ($use == 'literal') {
  1475. // TODO: depends on minOccurs
  1476. $xml = "<$name$elementNS/>";
  1477. } else {
  1478. $xml = "<$name$elementNS xsi:nil=\"true\" xsi:type=\"" .
  1479. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/') .
  1480. ":Array\" " .
  1481. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/') .
  1482. ':arrayType="' .
  1483. $this->getPrefixFromNamespace($this->getPrefix($typeDef['arrayType'])) .
  1484. ':' .
  1485. $this->getLocalPart($typeDef['arrayType'])."[0]\"/>";
  1486. }
  1487. $this->debug("in serializeType: returning: $xml");
  1488. return $xml;
  1489. }
  1490. if (isset($typeDef['multidimensional'])) {
  1491. $nv = array();
  1492. foreach($value as $v) {
  1493. $cols = ',' . sizeof($v);
  1494. $nv = array_merge($nv, $v);
  1495. }
  1496. $value = $nv;
  1497. } else {
  1498. $cols = '';
  1499. }
  1500. if (is_array($value) && sizeof($value) >= 1) {
  1501. $rows = sizeof($value);
  1502. $contents = '';
  1503. foreach($value as $k => $v) {
  1504. $this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
  1505. //if (strpos($typeDef['arrayType'], ':') ) {
  1506. if (!in_array($typeDef['arrayType'],$this->typemap['http://www.w3.org/2001/XMLSchema'])) {
  1507. $contents .= $this->serializeType('item', $typeDef['arrayType'], $v, $use);
  1508. } else {
  1509. $contents .= $this->serialize_val($v, 'item', $typeDef['arrayType'], null, $this->XMLSchemaVersion, false, $use);
  1510. }
  1511. }
  1512. } else {
  1513. $rows = 0;
  1514. $contents = null;
  1515. }
  1516. // TODO: for now, an empty value will be serialized as a zero element
  1517. // array. Revisit this when coding the handling of null/nil values.
  1518. if ($use == 'literal') {
  1519. $xml = "<$name$elementNS>"
  1520. .$contents
  1521. ."</$name>";
  1522. } else {
  1523. $xml = "<$name$elementNS xsi:type=\"".$this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/').':Array" '.
  1524. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/')
  1525. .':arrayType="'
  1526. .$this->getPrefixFromNamespace($this->getPrefix($typeDef['arrayType']))
  1527. .":".$this->getLocalPart($typeDef['arrayType'])."[$rows$cols]\">"
  1528. .$contents
  1529. ."</$name>";
  1530. }
  1531. } elseif ($phpType == 'scalar') {
  1532. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  1533. $elementNS = " xmlns=\"$ns\"";
  1534. } else {
  1535. if ($unqualified) {
  1536. $elementNS = " xmlns=\"\"";
  1537. } else {
  1538. $elementNS = '';
  1539. }
  1540. }
  1541. if ($use == 'literal') {
  1542. if ($forceType) {
  1543. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">$value</$name>";
  1544. } else {
  1545. $xml = "<$name$elementNS>$value</$name>";
  1546. }
  1547. } else {
  1548. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>$value</$name>";
  1549. }
  1550. }
  1551. $this->debug("in serializeType: returning: $xml");
  1552. return $xml;
  1553. }
  1554. /**
  1555. * serializes the attributes for a complexType
  1556. *
  1557. * @param array $typeDef our internal representation of an XML schema type (or element)
  1558. * @param mixed $value a native PHP value (parameter value)
  1559. * @param string $ns the namespace of the type
  1560. * @param string $uqType the local part of the type
  1561. * @return string value serialized as an XML string
  1562. * @access private
  1563. */
  1564. function serializeComplexTypeAttributes($typeDef, $value, $ns, $uqType) {
  1565. $this->debug("serializeComplexTypeAttributes for XML Schema type $ns:$uqType");
  1566. $xml = '';
  1567. if (isset($typeDef['extensionBase'])) {
  1568. $nsx = $this->getPrefix($typeDef['extensionBase']);
  1569. $uqTypex = $this->getLocalPart($typeDef['extensionBase']);
  1570. if ($this->getNamespaceFromPrefix($nsx)) {
  1571. $nsx = $this->getNamespaceFromPrefix($nsx);
  1572. }
  1573. if ($typeDefx = $this->getTypeDef($uqTypex, $nsx)) {
  1574. $this->debug("serialize attributes for extension base $nsx:$uqTypex");
  1575. $xml .= $this->serializeComplexTypeAttributes($typeDefx, $value, $nsx, $uqTypex);
  1576. } else {
  1577. $this->debug("extension base $nsx:$uqTypex is not a supported type");
  1578. }
  1579. }
  1580. if (isset($typeDef['attrs']) && is_array($typeDef['attrs'])) {
  1581. $this->debug("serialize attributes for XML Schema type $ns:$uqType");
  1582. if (is_array($value)) {
  1583. $xvalue = $value;
  1584. } elseif (is_object($value)) {
  1585. $xvalue = get_object_vars($value);
  1586. } else {
  1587. $this->debug("value is neither an array nor an object for XML Schema type $ns:$uqType");
  1588. $xvalue = array();
  1589. }
  1590. foreach ($typeDef['attrs'] as $aName => $attrs) {
  1591. if (isset($xvalue['!' . $aName])) {
  1592. $xname = '!' . $aName;
  1593. $this->debug("value provided for attribute $aName with key $xname");
  1594. } elseif (isset($xvalue[$aName])) {
  1595. $xname = $aName;
  1596. $this->debug("value provided for attribute $aName with key $xname");
  1597. } elseif (isset($attrs['default'])) {
  1598. $xname = '!' . $aName;
  1599. $xvalue[$xname] = $attrs['default'];
  1600. $this->debug('use default value of ' . $xvalue[$aName] . ' for attribute ' . $aName);
  1601. } else {
  1602. $xname = '';
  1603. $this->debug("no value provided for attribute $aName");
  1604. }
  1605. if ($xname) {
  1606. $xml .= " $aName=\"" . $this->expandEntities($xvalue[$xname]) . "\"";
  1607. }
  1608. }
  1609. } else {
  1610. $this->debug("no attributes to serialize for XML Schema type $ns:$uqType");
  1611. }
  1612. return $xml;
  1613. }
  1614. /**
  1615. * serializes the elements for a complexType
  1616. *
  1617. * @param array $typeDef our internal representation of an XML schema type (or element)
  1618. * @param mixed $value a native PHP value (parameter value)
  1619. * @param string $ns the namespace of the type
  1620. * @param string $uqType the local part of the type
  1621. * @param string $use use for part (encoded|literal)
  1622. * @param string $encodingStyle SOAP encoding style for the value (if different than the enclosing style)
  1623. * @return string value serialized as an XML string
  1624. * @access private
  1625. */
  1626. function serializeComplexTypeElements($typeDef, $value, $ns, $uqType, $use='encoded', $encodingStyle=false) {
  1627. $this->debug("in serializeComplexTypeElements for XML Schema type $ns:$uqType");
  1628. $xml = '';
  1629. if (isset($typeDef['extensionBase'])) {
  1630. $nsx = $this->getPrefix($typeDef['extensionBase']);
  1631. $uqTypex = $this->getLocalPart($typeDef['extensionBase']);
  1632. if ($this->getNamespaceFromPrefix($nsx)) {
  1633. $nsx = $this->getNamespaceFromPrefix($nsx);
  1634. }
  1635. if ($typeDefx = $this->getTypeDef($uqTypex, $nsx)) {
  1636. $this->debug("serialize elements for extension base $nsx:$uqTypex");
  1637. $xml .= $this->serializeComplexTypeElements($typeDefx, $value, $nsx, $uqTypex, $use, $encodingStyle);
  1638. } else {
  1639. $this->debug("extension base $nsx:$uqTypex is not a supported type");
  1640. }
  1641. }
  1642. if (isset($typeDef['elements']) && is_array($typeDef['elements'])) {
  1643. $this->debug("in serializeComplexTypeElements, serialize elements for XML Schema type $ns:$uqType");
  1644. if (is_array($value)) {
  1645. $xvalue = $value;
  1646. } elseif (is_object($value)) {
  1647. $xvalue = get_object_vars($value);
  1648. } else {
  1649. $this->debug("value is neither an array nor an object for XML Schema type $ns:$uqType");
  1650. $xvalue = array();
  1651. }
  1652. // toggle whether all elements are present - ideally should validate against schema
  1653. if (count($typeDef['elements']) != count($xvalue)){
  1654. $optionals = true;
  1655. }
  1656. foreach ($typeDef['elements'] as $eName => $attrs) {
  1657. if (!isset($xvalue[$eName])) {
  1658. if (isset($attrs['default'])) {
  1659. $xvalue[$eName] = $attrs['default'];
  1660. $this->debug('use default value of ' . $xvalue[$eName] . ' for element ' . $eName);
  1661. }
  1662. }
  1663. // if user took advantage of a minOccurs=0, then only serialize named parameters
  1664. if (isset($optionals)
  1665. && (!isset($xvalue[$eName]))
  1666. && ( (!isset($attrs['nillable'])) || $attrs['nillable'] != 'true')
  1667. ){
  1668. if (isset($attrs['minOccurs']) && $attrs['minOccurs'] <> '0') {
  1669. $this->debug("apparent error: no value provided for element $eName with minOccurs=" . $attrs['minOccurs']);
  1670. }
  1671. // do nothing
  1672. $this->debug("no value provided for complexType element $eName and element is not nillable, so serialize nothing");
  1673. } else {
  1674. // get value
  1675. if (isset($xvalue[$eName])) {
  1676. $v = $xvalue[$eName];
  1677. } else {
  1678. $v = null;
  1679. }
  1680. if (isset($attrs['form'])) {
  1681. $unqualified = ($attrs['form'] == 'unqualified');
  1682. } else {
  1683. $unqualified = false;
  1684. }
  1685. if (isset($attrs['maxOccurs']) && ($attrs['maxOccurs'] == 'unbounded' || $attrs['maxOccurs'] > 1) && isset($v) && is_array($v) && $this->isArraySimpleOrStruct($v) == 'arraySimple') {
  1686. $vv = $v;
  1687. foreach ($vv as $k => $v) {
  1688. if (isset($attrs['type']) || isset($attrs['ref'])) {
  1689. // serialize schema-defined type
  1690. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  1691. } else {
  1692. // serialize generic type (can this ever really happen?)
  1693. $this->debug("calling serialize_val() for $v, $eName, false, false, false, false, $use");
  1694. $xml .= $this->serialize_val($v, $eName, false, false, false, false, $use);
  1695. }
  1696. }
  1697. } else {
  1698. if (is_null($v) && isset($attrs['minOccurs']) && $attrs['minOccurs'] == '0') {
  1699. // do nothing
  1700. } elseif (is_null($v) && isset($attrs['nillable']) && $attrs['nillable'] == 'true') {
  1701. // TODO: serialize a nil correctly, but for now serialize schema-defined type
  1702. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  1703. } elseif (isset($attrs['type']) || isset($attrs['ref'])) {
  1704. // serialize schema-defined type
  1705. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  1706. } else {
  1707. // serialize generic type (can this ever really happen?)
  1708. $this->debug("calling serialize_val() for $v, $eName, false, false, false, false, $use");
  1709. $xml .= $this->serialize_val($v, $eName, false, false, false, false, $use);
  1710. }
  1711. }
  1712. }
  1713. }
  1714. } else {
  1715. $this->debug("no elements to serialize for XML Schema type $ns:$uqType");
  1716. }
  1717. return $xml;
  1718. }
  1719. /**
  1720. * adds an XML Schema complex type to the WSDL types
  1721. *
  1722. * @param string $name
  1723. * @param string $typeClass (complexType|simpleType|attribute)
  1724. * @param string $phpType currently supported are array and struct (php assoc array)
  1725. * @param string $compositor (all|sequence|choice)
  1726. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  1727. * @param array $elements e.g. array ( name => array(name=>'',type=>'') )
  1728. * @param array $attrs e.g. array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:string[]'))
  1729. * @param string $arrayType as namespace:name (xsd:string)
  1730. * @see nusoap_xmlschema
  1731. * @access public
  1732. */
  1733. function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType='') {
  1734. if (count($elements) > 0) {
  1735. $eElements = array();
  1736. foreach($elements as $n => $e){
  1737. // expand each element
  1738. $ee = array();
  1739. foreach ($e as $k => $v) {
  1740. $k = strpos($k,':') ? $this->expandQname($k) : $k;
  1741. $v = strpos($v,':') ? $this->expandQname($v) : $v;
  1742. $ee[$k] = $v;
  1743. }
  1744. $eElements[$n] = $ee;
  1745. }
  1746. $elements = $eElements;
  1747. }
  1748. if (count($attrs) > 0) {
  1749. foreach($attrs as $n => $a){
  1750. // expand each attribute
  1751. foreach ($a as $k => $v) {
  1752. $k = strpos($k,':') ? $this->expandQname($k) : $k;
  1753. $v = strpos($v,':') ? $this->expandQname($v) : $v;
  1754. $aa[$k] = $v;
  1755. }
  1756. $eAttrs[$n] = $aa;
  1757. }
  1758. $attrs = $eAttrs;
  1759. }
  1760. $restrictionBase = strpos($restrictionBase,':') ? $this->expandQname($restrictionBase) : $restrictionBase;
  1761. $arrayType = strpos($arrayType,':') ? $this->expandQname($arrayType) : $arrayType;
  1762. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  1763. $this->schemas[$typens][0]->addComplexType($name,$typeClass,$phpType,$compositor,$restrictionBase,$elements,$attrs,$arrayType);
  1764. }
  1765. /**
  1766. * adds an XML Schema simple type to the WSDL types
  1767. *
  1768. * @param string $name
  1769. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  1770. * @param string $typeClass (should always be simpleType)
  1771. * @param string $phpType (should always be scalar)
  1772. * @param array $enumeration array of values
  1773. * @see nusoap_xmlschema
  1774. * @access public
  1775. */
  1776. function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
  1777. $restrictionBase = strpos($restrictionBase,':') ? $this->expandQname($restrictionBase) : $restrictionBase;
  1778. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  1779. $this->schemas[$typens][0]->addSimpleType($name, $restrictionBase, $typeClass, $phpType, $enumeration);
  1780. }
  1781. /**
  1782. * adds an element to the WSDL types
  1783. *
  1784. * @param array $attrs attributes that must include name and type
  1785. * @see nusoap_xmlschema
  1786. * @access public
  1787. */
  1788. function addElement($attrs) {
  1789. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  1790. $this->schemas[$typens][0]->addElement($attrs);
  1791. }
  1792. /**
  1793. * register an operation with the server
  1794. *
  1795. * @param string $name operation (method) name
  1796. * @param array $in assoc array of input values: key = param name, value = param type
  1797. * @param array $out assoc array of output values: key = param name, value = param type
  1798. * @param string $namespace optional The namespace for the operation
  1799. * @param string $soapaction optional The soapaction for the operation
  1800. * @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
  1801. * @param string $use (encoded|literal) optional The use for the parameters (cannot mix right now)
  1802. * @param string $documentation optional The description to include in the WSDL
  1803. * @param string $encodingStyle optional (usually 'http://schemas.xmlsoap.org/soap/encoding/' for encoded)
  1804. * @access public
  1805. */
  1806. function addOperation($name, $in = false, $out = false, $namespace = false, $soapaction = false, $style = 'rpc', $use = 'encoded', $documentation = '', $encodingStyle = ''){
  1807. if ($use == 'encoded' && $encodingStyle == '') {
  1808. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  1809. }
  1810. if ($style == 'document') {
  1811. $elements = array();
  1812. foreach ($in as $n => $t) {
  1813. $elements[$n] = array('name' => $n, 'type' => $t, 'form' => 'unqualified');
  1814. }
  1815. $this->addComplexType($name . 'RequestType', 'complexType', 'struct', 'all', '', $elements);
  1816. $this->addElement(array('name' => $name, 'type' => $name . 'RequestType'));
  1817. $in = array('parameters' => 'tns:' . $name . '^');
  1818. $elements = array();
  1819. foreach ($out as $n => $t) {
  1820. $elements[$n] = array('name' => $n, 'type' => $t, 'form' => 'unqualified');
  1821. }
  1822. $this->addComplexType($name . 'ResponseType', 'complexType', 'struct', 'all', '', $elements);
  1823. $this->addElement(array('name' => $name . 'Response', 'type' => $name . 'ResponseType', 'form' => 'qualified'));
  1824. $out = array('parameters' => 'tns:' . $name . 'Response' . '^');
  1825. }
  1826. // get binding
  1827. $this->bindings[ $this->serviceName . 'Binding' ]['operations'][$name] =
  1828. array(
  1829. 'name' => $name,
  1830. 'binding' => $this->serviceName . 'Binding',
  1831. 'endpoint' => $this->endpoint,
  1832. 'soapAction' => $soapaction,
  1833. 'style' => $style,
  1834. 'input' => array(
  1835. 'use' => $use,
  1836. 'namespace' => $namespace,
  1837. 'encodingStyle' => $encodingStyle,
  1838. 'message' => $name . 'Request',
  1839. 'parts' => $in),
  1840. 'output' => array(
  1841. 'use' => $use,
  1842. 'namespace' => $namespace,
  1843. 'encodingStyle' => $encodingStyle,
  1844. 'message' => $name . 'Response',
  1845. 'parts' => $out),
  1846. 'namespace' => $namespace,
  1847. 'transport' => 'http://schemas.xmlsoap.org/soap/http',
  1848. 'documentation' => $documentation);
  1849. // add portTypes
  1850. // add messages
  1851. if($in)
  1852. {
  1853. foreach($in as $pName => $pType)
  1854. {
  1855. if(strpos($pType,':')) {
  1856. $pType = $this->getNamespaceFromPrefix($this->getPrefix($pType)).":".$this->getLocalPart($pType);
  1857. }
  1858. $this->messages[$name.'Request'][$pName] = $pType;
  1859. }
  1860. } else {
  1861. $this->messages[$name.'Request']= '0';
  1862. }
  1863. if($out)
  1864. {
  1865. foreach($out as $pName => $pType)
  1866. {
  1867. if(strpos($pType,':')) {
  1868. $pType = $this->getNamespaceFromPrefix($this->getPrefix($pType)).":".$this->getLocalPart($pType);
  1869. }
  1870. $this->messages[$name.'Response'][$pName] = $pType;
  1871. }
  1872. } else {
  1873. $this->messages[$name.'Response']= '0';
  1874. }
  1875. return true;
  1876. }
  1877. }
  1878. ?>