class.wsdl.php 76 KB

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