class.wsdl.php 75 KB

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