class.wsdl.php 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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. // 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 = 'Getting ' . $wsdl . ' - 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 (preg_match('/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 (preg_match('/^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 (preg_match('/:/', $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 (/*preg_match('/types$/', $name) ||*/ preg_match('/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 $portName WSDL port name
  565. * @param string $bindingType eg: soap, smtp, dime (only soap and soap12 are currently supported)
  566. * @return array
  567. * @access public
  568. */
  569. function getOperations($portName = '', $bindingType = 'soap') {
  570. $ops = array();
  571. if ($bindingType == 'soap') {
  572. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  573. } elseif ($bindingType == 'soap12') {
  574. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  575. } else {
  576. $this->debug("getOperations bindingType $bindingType may not be supported");
  577. }
  578. $this->debug("getOperations for port '$portName' bindingType $bindingType");
  579. // loop thru ports
  580. foreach($this->ports as $port => $portData) {
  581. $this->debug("getOperations checking port $port bindingType " . $portData['bindingType']);
  582. if ($portName == '' || $port == $portName) {
  583. // binding type of port matches parameter
  584. if ($portData['bindingType'] == $bindingType) {
  585. $this->debug("getOperations found port $port bindingType $bindingType");
  586. //$this->debug("port data: " . $this->varDump($portData));
  587. //$this->debug("bindings: " . $this->varDump($this->bindings[ $portData['binding'] ]));
  588. // merge bindings
  589. if (isset($this->bindings[ $portData['binding'] ]['operations'])) {
  590. $ops = array_merge ($ops, $this->bindings[ $portData['binding'] ]['operations']);
  591. }
  592. }
  593. }
  594. }
  595. if (count($ops) == 0) {
  596. $this->debug("getOperations found no operations for port '$portName' bindingType $bindingType");
  597. }
  598. return $ops;
  599. }
  600. /**
  601. * returns an associative array of data necessary for calling an operation
  602. *
  603. * @param string $operation name of operation
  604. * @param string $bindingType type of binding eg: soap, soap12
  605. * @return array
  606. * @access public
  607. */
  608. function getOperationData($operation, $bindingType = 'soap')
  609. {
  610. if ($bindingType == 'soap') {
  611. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  612. } elseif ($bindingType == 'soap12') {
  613. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  614. }
  615. // loop thru ports
  616. foreach($this->ports as $port => $portData) {
  617. // binding type of port matches parameter
  618. if ($portData['bindingType'] == $bindingType) {
  619. // get binding
  620. //foreach($this->bindings[ $portData['binding'] ]['operations'] as $bOperation => $opData) {
  621. foreach(array_keys($this->bindings[ $portData['binding'] ]['operations']) as $bOperation) {
  622. // note that we could/should also check the namespace here
  623. if ($operation == $bOperation) {
  624. $opData = $this->bindings[ $portData['binding'] ]['operations'][$operation];
  625. return $opData;
  626. }
  627. }
  628. }
  629. }
  630. }
  631. /**
  632. * returns an associative array of data necessary for calling an operation
  633. *
  634. * @param string $soapAction soapAction for operation
  635. * @param string $bindingType type of binding eg: soap, soap12
  636. * @return array
  637. * @access public
  638. */
  639. function getOperationDataForSoapAction($soapAction, $bindingType = 'soap') {
  640. if ($bindingType == 'soap') {
  641. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap/';
  642. } elseif ($bindingType == 'soap12') {
  643. $bindingType = 'http://schemas.xmlsoap.org/wsdl/soap12/';
  644. }
  645. // loop thru ports
  646. foreach($this->ports as $port => $portData) {
  647. // binding type of port matches parameter
  648. if ($portData['bindingType'] == $bindingType) {
  649. // loop through operations for the binding
  650. foreach ($this->bindings[ $portData['binding'] ]['operations'] as $bOperation => $opData) {
  651. if ($opData['soapAction'] == $soapAction) {
  652. return $opData;
  653. }
  654. }
  655. }
  656. }
  657. }
  658. /**
  659. * returns an array of information about a given type
  660. * returns false if no type exists by the given name
  661. *
  662. * typeDef = array(
  663. * 'elements' => array(), // refs to elements array
  664. * 'restrictionBase' => '',
  665. * 'phpType' => '',
  666. * 'order' => '(sequence|all)',
  667. * 'attrs' => array() // refs to attributes array
  668. * )
  669. *
  670. * @param string $type the type
  671. * @param string $ns namespace (not prefix) of the type
  672. * @return mixed
  673. * @access public
  674. * @see nusoap_xmlschema
  675. */
  676. function getTypeDef($type, $ns) {
  677. $this->debug("in getTypeDef: type=$type, ns=$ns");
  678. if ((! $ns) && isset($this->namespaces['tns'])) {
  679. $ns = $this->namespaces['tns'];
  680. $this->debug("in getTypeDef: type namespace forced to $ns");
  681. }
  682. if (!isset($this->schemas[$ns])) {
  683. foreach ($this->schemas as $ns0 => $schema0) {
  684. if (strcasecmp($ns, $ns0) == 0) {
  685. $this->debug("in getTypeDef: replacing schema namespace $ns with $ns0");
  686. $ns = $ns0;
  687. break;
  688. }
  689. }
  690. }
  691. if (isset($this->schemas[$ns])) {
  692. $this->debug("in getTypeDef: have schema for namespace $ns");
  693. for ($i = 0; $i < count($this->schemas[$ns]); $i++) {
  694. $xs = &$this->schemas[$ns][$i];
  695. $t = $xs->getTypeDef($type);
  696. $this->appendDebug($xs->getDebug());
  697. $xs->clearDebug();
  698. if ($t) {
  699. $this->debug("in getTypeDef: found type $type");
  700. if (!isset($t['phpType'])) {
  701. // get info for type to tack onto the element
  702. $uqType = substr($t['type'], strrpos($t['type'], ':') + 1);
  703. $ns = substr($t['type'], 0, strrpos($t['type'], ':'));
  704. $etype = $this->getTypeDef($uqType, $ns);
  705. if ($etype) {
  706. $this->debug("found type for [element] $type:");
  707. $this->debug($this->varDump($etype));
  708. if (isset($etype['phpType'])) {
  709. $t['phpType'] = $etype['phpType'];
  710. }
  711. if (isset($etype['elements'])) {
  712. $t['elements'] = $etype['elements'];
  713. }
  714. if (isset($etype['attrs'])) {
  715. $t['attrs'] = $etype['attrs'];
  716. }
  717. } else {
  718. $this->debug("did not find type for [element] $type");
  719. }
  720. }
  721. return $t;
  722. }
  723. }
  724. $this->debug("in getTypeDef: did not find type $type");
  725. } else {
  726. $this->debug("in getTypeDef: do not have schema for namespace $ns");
  727. }
  728. return false;
  729. }
  730. /**
  731. * prints html description of services
  732. *
  733. * @access private
  734. */
  735. function webDescription(){
  736. global $HTTP_SERVER_VARS;
  737. if (isset($_SERVER)) {
  738. $PHP_SELF = $_SERVER['PHP_SELF'];
  739. } elseif (isset($HTTP_SERVER_VARS)) {
  740. $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
  741. } else {
  742. $this->setError("Neither _SERVER nor HTTP_SERVER_VARS is available");
  743. }
  744. $b = '
  745. <html><head><title>NuSOAP: '.$this->serviceName.'</title>
  746. <style type="text/css">
  747. body { font-family: arial; color: #000000; background-color: #ffffff; margin: 0px 0px 0px 0px; }
  748. p { font-family: arial; color: #000000; margin-top: 0px; margin-bottom: 12px; }
  749. pre { background-color: silver; padding: 5px; font-family: Courier New; font-size: x-small; color: #000000;}
  750. ul { margin-top: 10px; margin-left: 20px; }
  751. li { list-style-type: none; margin-top: 10px; color: #000000; }
  752. .content{
  753. margin-left: 0px; padding-bottom: 2em; }
  754. .nav {
  755. padding-top: 10px; padding-bottom: 10px; padding-left: 15px; font-size: .70em;
  756. margin-top: 10px; margin-left: 0px; color: #000000;
  757. background-color: #ccccff; width: 20%; margin-left: 20px; margin-top: 20px; }
  758. .title {
  759. font-family: arial; font-size: 26px; color: #ffffff;
  760. background-color: #999999; width: 100%;
  761. margin-left: 0px; margin-right: 0px;
  762. padding-top: 10px; padding-bottom: 10px;}
  763. .hidden {
  764. position: absolute; visibility: hidden; z-index: 200; left: 250px; top: 100px;
  765. font-family: arial; overflow: hidden; width: 600;
  766. padding: 20px; font-size: 10px; background-color: #999999;
  767. layer-background-color:#FFFFFF; }
  768. a,a:active { color: charcoal; font-weight: bold; }
  769. a:visited { color: #666666; font-weight: bold; }
  770. a:hover { color: cc3300; font-weight: bold; }
  771. </style>
  772. <script language="JavaScript" type="text/javascript">
  773. <!--
  774. // POP-UP CAPTIONS...
  775. function lib_bwcheck(){ //Browsercheck (needed)
  776. this.ver=navigator.appVersion
  777. this.agent=navigator.userAgent
  778. this.dom=document.getElementById?1:0
  779. this.opera5=this.agent.indexOf("Opera 5")>-1
  780. this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
  781. this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  782. this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
  783. this.ie=this.ie4||this.ie5||this.ie6
  784. this.mac=this.agent.indexOf("Mac")>-1
  785. this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
  786. this.ns4=(document.layers && !this.dom)?1:0;
  787. this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
  788. return this
  789. }
  790. var bw = new lib_bwcheck()
  791. //Makes crossbrowser object.
  792. function makeObj(obj){
  793. this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?document.layers[obj]:0;
  794. if(!this.evnt) return false
  795. this.css=bw.dom||bw.ie4?this.evnt.style:bw.ns4?this.evnt:0;
  796. this.wref=bw.dom||bw.ie4?this.evnt:bw.ns4?this.css.document:0;
  797. this.writeIt=b_writeIt;
  798. return this
  799. }
  800. // A unit of measure that will be added when setting the position of a layer.
  801. //var px = bw.ns4||window.opera?"":"px";
  802. function b_writeIt(text){
  803. if (bw.ns4){this.wref.write(text);this.wref.close()}
  804. else this.wref.innerHTML = text
  805. }
  806. //Shows the messages
  807. var oDesc;
  808. function popup(divid){
  809. if(oDesc = new makeObj(divid)){
  810. oDesc.css.visibility = "visible"
  811. }
  812. }
  813. function popout(){ // Hides message
  814. if(oDesc) oDesc.css.visibility = "hidden"
  815. }
  816. //-->
  817. </script>
  818. </head>
  819. <body>
  820. <div class=content>
  821. <br><br>
  822. <div class=title>'.$this->serviceName.'</div>
  823. <div class=nav>
  824. <p>View the <a href="'.$PHP_SELF.'?wsdl">WSDL</a> for the service.
  825. Click on an operation name to view it&apos;s details.</p>
  826. <ul>';
  827. foreach($this->getOperations() as $op => $data){
  828. $b .= "<li><a href='#' onclick=\"popout();popup('$op')\">$op</a></li>";
  829. // create hidden div
  830. $b .= "<div id='$op' class='hidden'>
  831. <a href='#' onclick='popout()'><font color='#ffffff'>Close</font></a><br><br>";
  832. foreach($data as $donnie => $marie){ // loop through opdata
  833. if($donnie == 'input' || $donnie == 'output'){ // show input/output data
  834. $b .= "<font color='white'>".ucfirst($donnie).':</font><br>';
  835. foreach($marie as $captain => $tenille){ // loop through data
  836. if($captain == 'parts'){ // loop thru parts
  837. $b .= "&nbsp;&nbsp;$captain:<br>";
  838. //if(is_array($tenille)){
  839. foreach($tenille as $joanie => $chachi){
  840. $b .= "&nbsp;&nbsp;&nbsp;&nbsp;$joanie: $chachi<br>";
  841. }
  842. //}
  843. } else {
  844. $b .= "&nbsp;&nbsp;$captain: $tenille<br>";
  845. }
  846. }
  847. } else {
  848. $b .= "<font color='white'>".ucfirst($donnie).":</font> $marie<br>";
  849. }
  850. }
  851. $b .= '</div>';
  852. }
  853. $b .= '
  854. <ul>
  855. </div>
  856. </div></body></html>';
  857. return $b;
  858. }
  859. /**
  860. * serialize the parsed wsdl
  861. *
  862. * @param mixed $debug whether to put debug=1 in endpoint URL
  863. * @return string serialization of WSDL
  864. * @access public
  865. */
  866. function serialize($debug = 0)
  867. {
  868. $xml = '<?xml version="1.0" encoding="ISO-8859-1"?>';
  869. $xml .= "\n<definitions";
  870. foreach($this->namespaces as $k => $v) {
  871. $xml .= " xmlns:$k=\"$v\"";
  872. }
  873. // 10.9.02 - add poulter fix for wsdl and tns declarations
  874. if (isset($this->namespaces['wsdl'])) {
  875. $xml .= " xmlns=\"" . $this->namespaces['wsdl'] . "\"";
  876. }
  877. if (isset($this->namespaces['tns'])) {
  878. $xml .= " targetNamespace=\"" . $this->namespaces['tns'] . "\"";
  879. }
  880. $xml .= '>';
  881. // imports
  882. if (sizeof($this->import) > 0) {
  883. foreach($this->import as $ns => $list) {
  884. foreach ($list as $ii) {
  885. if ($ii['location'] != '') {
  886. $xml .= '<import location="' . $ii['location'] . '" namespace="' . $ns . '" />';
  887. } else {
  888. $xml .= '<import namespace="' . $ns . '" />';
  889. }
  890. }
  891. }
  892. }
  893. // types
  894. if (count($this->schemas)>=1) {
  895. $xml .= "\n<types>\n";
  896. foreach ($this->schemas as $ns => $list) {
  897. foreach ($list as $xs) {
  898. $xml .= $xs->serializeSchema();
  899. }
  900. }
  901. $xml .= '</types>';
  902. }
  903. // messages
  904. if (count($this->messages) >= 1) {
  905. foreach($this->messages as $msgName => $msgParts) {
  906. $xml .= "\n<message name=\"" . $msgName . '">';
  907. if(is_array($msgParts)){
  908. foreach($msgParts as $partName => $partType) {
  909. // print 'serializing '.$partType.', sv: '.$this->XMLSchemaVersion.'<br>';
  910. if (strpos($partType, ':')) {
  911. $typePrefix = $this->getPrefixFromNamespace($this->getPrefix($partType));
  912. } elseif (isset($this->typemap[$this->namespaces['xsd']][$partType])) {
  913. // print 'checking typemap: '.$this->XMLSchemaVersion.'<br>';
  914. $typePrefix = 'xsd';
  915. } else {
  916. foreach($this->typemap as $ns => $types) {
  917. if (isset($types[$partType])) {
  918. $typePrefix = $this->getPrefixFromNamespace($ns);
  919. }
  920. }
  921. if (!isset($typePrefix)) {
  922. die("$partType has no namespace!");
  923. }
  924. }
  925. $ns = $this->getNamespaceFromPrefix($typePrefix);
  926. $localPart = $this->getLocalPart($partType);
  927. $typeDef = $this->getTypeDef($localPart, $ns);
  928. if ($typeDef['typeClass'] == 'element') {
  929. $elementortype = 'element';
  930. if (substr($localPart, -1) == '^') {
  931. $localPart = substr($localPart, 0, -1);
  932. }
  933. } else {
  934. $elementortype = 'type';
  935. }
  936. $xml .= "\n" . ' <part name="' . $partName . '" ' . $elementortype . '="' . $typePrefix . ':' . $localPart . '" />';
  937. }
  938. }
  939. $xml .= '</message>';
  940. }
  941. }
  942. // bindings & porttypes
  943. if (count($this->bindings) >= 1) {
  944. $binding_xml = '';
  945. $portType_xml = '';
  946. foreach($this->bindings as $bindingName => $attrs) {
  947. $binding_xml .= "\n<binding name=\"" . $bindingName . '" type="tns:' . $attrs['portType'] . '">';
  948. $binding_xml .= "\n" . ' <soap:binding style="' . $attrs['style'] . '" transport="' . $attrs['transport'] . '"/>';
  949. $portType_xml .= "\n<portType name=\"" . $attrs['portType'] . '">';
  950. foreach($attrs['operations'] as $opName => $opParts) {
  951. $binding_xml .= "\n" . ' <operation name="' . $opName . '">';
  952. $binding_xml .= "\n" . ' <soap:operation soapAction="' . $opParts['soapAction'] . '" style="'. $opParts['style'] . '"/>';
  953. if (isset($opParts['input']['encodingStyle']) && $opParts['input']['encodingStyle'] != '') {
  954. $enc_style = ' encodingStyle="' . $opParts['input']['encodingStyle'] . '"';
  955. } else {
  956. $enc_style = '';
  957. }
  958. $binding_xml .= "\n" . ' <input><soap:body use="' . $opParts['input']['use'] . '" namespace="' . $opParts['input']['namespace'] . '"' . $enc_style . '/></input>';
  959. if (isset($opParts['output']['encodingStyle']) && $opParts['output']['encodingStyle'] != '') {
  960. $enc_style = ' encodingStyle="' . $opParts['output']['encodingStyle'] . '"';
  961. } else {
  962. $enc_style = '';
  963. }
  964. $binding_xml .= "\n" . ' <output><soap:body use="' . $opParts['output']['use'] . '" namespace="' . $opParts['output']['namespace'] . '"' . $enc_style . '/></output>';
  965. $binding_xml .= "\n" . ' </operation>';
  966. $portType_xml .= "\n" . ' <operation name="' . $opParts['name'] . '"';
  967. if (isset($opParts['parameterOrder'])) {
  968. $portType_xml .= ' parameterOrder="' . $opParts['parameterOrder'] . '"';
  969. }
  970. $portType_xml .= '>';
  971. if(isset($opParts['documentation']) && $opParts['documentation'] != '') {
  972. $portType_xml .= "\n" . ' <documentation>' . htmlspecialchars($opParts['documentation']) . '</documentation>';
  973. }
  974. $portType_xml .= "\n" . ' <input message="tns:' . $opParts['input']['message'] . '"/>';
  975. $portType_xml .= "\n" . ' <output message="tns:' . $opParts['output']['message'] . '"/>';
  976. $portType_xml .= "\n" . ' </operation>';
  977. }
  978. $portType_xml .= "\n" . '</portType>';
  979. $binding_xml .= "\n" . '</binding>';
  980. }
  981. $xml .= $portType_xml . $binding_xml;
  982. }
  983. // services
  984. $xml .= "\n<service name=\"" . $this->serviceName . '">';
  985. if (count($this->ports) >= 1) {
  986. foreach($this->ports as $pName => $attrs) {
  987. $xml .= "\n" . ' <port name="' . $pName . '" binding="tns:' . $attrs['binding'] . '">';
  988. $xml .= "\n" . ' <soap:address location="' . $attrs['location'] . ($debug ? '?debug=1' : '') . '"/>';
  989. $xml .= "\n" . ' </port>';
  990. }
  991. }
  992. $xml .= "\n" . '</service>';
  993. return $xml . "\n</definitions>";
  994. }
  995. /**
  996. * determine whether a set of parameters are unwrapped
  997. * when they are expect to be wrapped, Microsoft-style.
  998. *
  999. * @param string $type the type (element name) of the wrapper
  1000. * @param array $parameters the parameter values for the SOAP call
  1001. * @return boolean whether they parameters are unwrapped (and should be wrapped)
  1002. * @access private
  1003. */
  1004. function parametersMatchWrapped($type, &$parameters) {
  1005. $this->debug("in parametersMatchWrapped type=$type, parameters=");
  1006. $this->appendDebug($this->varDump($parameters));
  1007. // split type into namespace:unqualified-type
  1008. if (strpos($type, ':')) {
  1009. $uqType = substr($type, strrpos($type, ':') + 1);
  1010. $ns = substr($type, 0, strrpos($type, ':'));
  1011. $this->debug("in parametersMatchWrapped: got a prefixed type: $uqType, $ns");
  1012. if ($this->getNamespaceFromPrefix($ns)) {
  1013. $ns = $this->getNamespaceFromPrefix($ns);
  1014. $this->debug("in parametersMatchWrapped: expanded prefixed type: $uqType, $ns");
  1015. }
  1016. } else {
  1017. // TODO: should the type be compared to types in XSD, and the namespace
  1018. // set to XSD if the type matches?
  1019. $this->debug("in parametersMatchWrapped: No namespace for type $type");
  1020. $ns = '';
  1021. $uqType = $type;
  1022. }
  1023. // get the type information
  1024. if (!$typeDef = $this->getTypeDef($uqType, $ns)) {
  1025. $this->debug("in parametersMatchWrapped: $type ($uqType) is not a supported type.");
  1026. return false;
  1027. }
  1028. $this->debug("in parametersMatchWrapped: found typeDef=");
  1029. $this->appendDebug($this->varDump($typeDef));
  1030. if (substr($uqType, -1) == '^') {
  1031. $uqType = substr($uqType, 0, -1);
  1032. }
  1033. $phpType = $typeDef['phpType'];
  1034. $arrayType = (isset($typeDef['arrayType']) ? $typeDef['arrayType'] : '');
  1035. $this->debug("in parametersMatchWrapped: uqType: $uqType, ns: $ns, phptype: $phpType, arrayType: $arrayType");
  1036. // we expect a complexType or element of complexType
  1037. if ($phpType != 'struct') {
  1038. $this->debug("in parametersMatchWrapped: not a struct");
  1039. return false;
  1040. }
  1041. // see whether the parameter names match the elements
  1042. if (isset($typeDef['elements']) && is_array($typeDef['elements'])) {
  1043. $elements = 0;
  1044. $matches = 0;
  1045. foreach ($typeDef['elements'] as $name => $attrs) {
  1046. if (isset($parameters[$name])) {
  1047. $this->debug("in parametersMatchWrapped: have parameter named $name");
  1048. $matches++;
  1049. } else {
  1050. $this->debug("in parametersMatchWrapped: do not have parameter named $name");
  1051. }
  1052. $elements++;
  1053. }
  1054. $this->debug("in parametersMatchWrapped: $matches parameter names match $elements wrapped parameter names");
  1055. if ($matches == 0) {
  1056. return false;
  1057. }
  1058. return true;
  1059. }
  1060. // since there are no elements for the type, if the user passed no
  1061. // parameters, the parameters match wrapped.
  1062. $this->debug("in parametersMatchWrapped: no elements type $ns:$uqType");
  1063. return count($parameters) == 0;
  1064. }
  1065. /**
  1066. * serialize PHP values according to a WSDL message definition
  1067. * contrary to the method name, this is not limited to RPC
  1068. *
  1069. * TODO
  1070. * - multi-ref serialization
  1071. * - validate PHP values against type definitions, return errors if invalid
  1072. *
  1073. * @param string $operation operation name
  1074. * @param string $direction (input|output)
  1075. * @param mixed $parameters parameter value(s)
  1076. * @param string $bindingType (soap|soap12)
  1077. * @return mixed parameters serialized as XML or false on error (e.g. operation not found)
  1078. * @access public
  1079. */
  1080. function serializeRPCParameters($operation, $direction, $parameters, $bindingType = 'soap') {
  1081. $this->debug("in serializeRPCParameters: operation=$operation, direction=$direction, XMLSchemaVersion=$this->XMLSchemaVersion, bindingType=$bindingType");
  1082. $this->appendDebug('parameters=' . $this->varDump($parameters));
  1083. if ($direction != 'input' && $direction != 'output') {
  1084. $this->debug('The value of the \$direction argument needs to be either "input" or "output"');
  1085. $this->setError('The value of the \$direction argument needs to be either "input" or "output"');
  1086. return false;
  1087. }
  1088. if (!$opData = $this->getOperationData($operation, $bindingType)) {
  1089. $this->debug('Unable to retrieve WSDL data for operation: ' . $operation . ' bindingType: ' . $bindingType);
  1090. $this->setError('Unable to retrieve WSDL data for operation: ' . $operation . ' bindingType: ' . $bindingType);
  1091. return false;
  1092. }
  1093. $this->debug('in serializeRPCParameters: opData:');
  1094. $this->appendDebug($this->varDump($opData));
  1095. // Get encoding style for output and set to current
  1096. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  1097. if(($direction == 'input') && isset($opData['output']['encodingStyle']) && ($opData['output']['encodingStyle'] != $encodingStyle)) {
  1098. $encodingStyle = $opData['output']['encodingStyle'];
  1099. $enc_style = $encodingStyle;
  1100. }
  1101. // set input params
  1102. $xml = '';
  1103. if (isset($opData[$direction]['parts']) && sizeof($opData[$direction]['parts']) > 0) {
  1104. $parts = &$opData[$direction]['parts'];
  1105. $part_count = sizeof($parts);
  1106. $style = $opData['style'];
  1107. $use = $opData[$direction]['use'];
  1108. $this->debug("have $part_count part(s) to serialize using $style/$use");
  1109. if (is_array($parameters)) {
  1110. $parametersArrayType = $this->isArraySimpleOrStruct($parameters);
  1111. $parameter_count = count($parameters);
  1112. $this->debug("have $parameter_count parameter(s) provided as $parametersArrayType to serialize");
  1113. // check for Microsoft-style wrapped parameters
  1114. if ($style == 'document' && $use == 'literal' && $part_count == 1 && isset($parts['parameters'])) {
  1115. $this->debug('check whether the caller has wrapped the parameters');
  1116. if ($direction == 'output' && $parametersArrayType == 'arraySimple' && $parameter_count == 1) {
  1117. // TODO: consider checking here for double-wrapping, when
  1118. // service function wraps, then NuSOAP wraps again
  1119. $this->debug("change simple array to associative with 'parameters' element");
  1120. $parameters['parameters'] = $parameters[0];
  1121. unset($parameters[0]);
  1122. }
  1123. if (($parametersArrayType == 'arrayStruct' || $parameter_count == 0) && !isset($parameters['parameters'])) {
  1124. $this->debug('check whether caller\'s parameters match the wrapped ones');
  1125. if ($this->parametersMatchWrapped($parts['parameters'], $parameters)) {
  1126. $this->debug('wrap the parameters for the caller');
  1127. $parameters = array('parameters' => $parameters);
  1128. $parameter_count = 1;
  1129. }
  1130. }
  1131. }
  1132. foreach ($parts as $name => $type) {
  1133. $this->debug("serializing part $name of type $type");
  1134. // Track encoding style
  1135. if (isset($opData[$direction]['encodingStyle']) && $encodingStyle != $opData[$direction]['encodingStyle']) {
  1136. $encodingStyle = $opData[$direction]['encodingStyle'];
  1137. $enc_style = $encodingStyle;
  1138. } else {
  1139. $enc_style = false;
  1140. }
  1141. // NOTE: add error handling here
  1142. // if serializeType returns false, then catch global error and fault
  1143. if ($parametersArrayType == 'arraySimple') {
  1144. $p = array_shift($parameters);
  1145. $this->debug('calling serializeType w/indexed param');
  1146. $xml .= $this->serializeType($name, $type, $p, $use, $enc_style);
  1147. } elseif (isset($parameters[$name])) {
  1148. $this->debug('calling serializeType w/named param');
  1149. $xml .= $this->serializeType($name, $type, $parameters[$name], $use, $enc_style);
  1150. } else {
  1151. // TODO: only send nillable
  1152. $this->debug('calling serializeType w/null param');
  1153. $xml .= $this->serializeType($name, $type, null, $use, $enc_style);
  1154. }
  1155. }
  1156. } else {
  1157. $this->debug('no parameters passed.');
  1158. }
  1159. }
  1160. $this->debug("serializeRPCParameters returning: $xml");
  1161. return $xml;
  1162. }
  1163. /**
  1164. * serialize a PHP value according to a WSDL message definition
  1165. *
  1166. * TODO
  1167. * - multi-ref serialization
  1168. * - validate PHP values against type definitions, return errors if invalid
  1169. *
  1170. * @param string $operation operation name
  1171. * @param string $direction (input|output)
  1172. * @param mixed $parameters parameter value(s)
  1173. * @return mixed parameters serialized as XML or false on error (e.g. operation not found)
  1174. * @access public
  1175. * @deprecated
  1176. */
  1177. function serializeParameters($operation, $direction, $parameters)
  1178. {
  1179. $this->debug("in serializeParameters: operation=$operation, direction=$direction, XMLSchemaVersion=$this->XMLSchemaVersion");
  1180. $this->appendDebug('parameters=' . $this->varDump($parameters));
  1181. if ($direction != 'input' && $direction != 'output') {
  1182. $this->debug('The value of the \$direction argument needs to be either "input" or "output"');
  1183. $this->setError('The value of the \$direction argument needs to be either "input" or "output"');
  1184. return false;
  1185. }
  1186. if (!$opData = $this->getOperationData($operation)) {
  1187. $this->debug('Unable to retrieve WSDL data for operation: ' . $operation);
  1188. $this->setError('Unable to retrieve WSDL data for operation: ' . $operation);
  1189. return false;
  1190. }
  1191. $this->debug('opData:');
  1192. $this->appendDebug($this->varDump($opData));
  1193. // Get encoding style for output and set to current
  1194. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  1195. if(($direction == 'input') && isset($opData['output']['encodingStyle']) && ($opData['output']['encodingStyle'] != $encodingStyle)) {
  1196. $encodingStyle = $opData['output']['encodingStyle'];
  1197. $enc_style = $encodingStyle;
  1198. }
  1199. // set input params
  1200. $xml = '';
  1201. if (isset($opData[$direction]['parts']) && sizeof($opData[$direction]['parts']) > 0) {
  1202. $use = $opData[$direction]['use'];
  1203. $this->debug("use=$use");
  1204. $this->debug('got ' . count($opData[$direction]['parts']) . ' part(s)');
  1205. if (is_array($parameters)) {
  1206. $parametersArrayType = $this->isArraySimpleOrStruct($parameters);
  1207. $this->debug('have ' . $parametersArrayType . ' parameters');
  1208. foreach($opData[$direction]['parts'] as $name => $type) {
  1209. $this->debug('serializing part "'.$name.'" of type "'.$type.'"');
  1210. // Track encoding style
  1211. if(isset($opData[$direction]['encodingStyle']) && $encodingStyle != $opData[$direction]['encodingStyle']) {
  1212. $encodingStyle = $opData[$direction]['encodingStyle'];
  1213. $enc_style = $encodingStyle;
  1214. } else {
  1215. $enc_style = false;
  1216. }
  1217. // NOTE: add error handling here
  1218. // if serializeType returns false, then catch global error and fault
  1219. if ($parametersArrayType == 'arraySimple') {
  1220. $p = array_shift($parameters);
  1221. $this->debug('calling serializeType w/indexed param');
  1222. $xml .= $this->serializeType($name, $type, $p, $use, $enc_style);
  1223. } elseif (isset($parameters[$name])) {
  1224. $this->debug('calling serializeType w/named param');
  1225. $xml .= $this->serializeType($name, $type, $parameters[$name], $use, $enc_style);
  1226. } else {
  1227. // TODO: only send nillable
  1228. $this->debug('calling serializeType w/null param');
  1229. $xml .= $this->serializeType($name, $type, null, $use, $enc_style);
  1230. }
  1231. }
  1232. } else {
  1233. $this->debug('no parameters passed.');
  1234. }
  1235. }
  1236. $this->debug("serializeParameters returning: $xml");
  1237. return $xml;
  1238. }
  1239. /**
  1240. * serializes a PHP value according a given type definition
  1241. *
  1242. * @param string $name name of value (part or element)
  1243. * @param string $type XML schema type of value (type or element)
  1244. * @param mixed $value a native PHP value (parameter value)
  1245. * @param string $use use for part (encoded|literal)
  1246. * @param string $encodingStyle SOAP encoding style for the value (if different than the enclosing style)
  1247. * @param boolean $unqualified a kludge for what should be XML namespace form handling
  1248. * @return string value serialized as an XML string
  1249. * @access private
  1250. */
  1251. function serializeType($name, $type, $value, $use='encoded', $encodingStyle=false, $unqualified=false)
  1252. {
  1253. $this->debug("in serializeType: name=$name, type=$type, use=$use, encodingStyle=$encodingStyle, unqualified=" . ($unqualified ? "unqualified" : "qualified"));
  1254. $this->appendDebug("value=" . $this->varDump($value));
  1255. if($use == 'encoded' && $encodingStyle) {
  1256. $encodingStyle = ' SOAP-ENV:encodingStyle="' . $encodingStyle . '"';
  1257. }
  1258. // if a soapval has been supplied, let its type override the WSDL
  1259. if (is_object($value) && get_class($value) == 'soapval') {
  1260. if ($value->type_ns) {
  1261. $type = $value->type_ns . ':' . $value->type;
  1262. $forceType = true;
  1263. $this->debug("in serializeType: soapval overrides type to $type");
  1264. } elseif ($value->type) {
  1265. $type = $value->type;
  1266. $forceType = true;
  1267. $this->debug("in serializeType: soapval overrides type to $type");
  1268. } else {
  1269. $forceType = false;
  1270. $this->debug("in serializeType: soapval does not override type");
  1271. }
  1272. $attrs = $value->attributes;
  1273. $value = $value->value;
  1274. $this->debug("in serializeType: soapval overrides value to $value");
  1275. if ($attrs) {
  1276. if (!is_array($value)) {
  1277. $value['!'] = $value;
  1278. }
  1279. foreach ($attrs as $n => $v) {
  1280. $value['!' . $n] = $v;
  1281. }
  1282. $this->debug("in serializeType: soapval provides attributes");
  1283. }
  1284. } else {
  1285. $forceType = false;
  1286. }
  1287. $xml = '';
  1288. if (strpos($type, ':')) {
  1289. $uqType = substr($type, strrpos($type, ':') + 1);
  1290. $ns = substr($type, 0, strrpos($type, ':'));
  1291. $this->debug("in serializeType: got a prefixed type: $uqType, $ns");
  1292. if ($this->getNamespaceFromPrefix($ns)) {
  1293. $ns = $this->getNamespaceFromPrefix($ns);
  1294. $this->debug("in serializeType: expanded prefixed type: $uqType, $ns");
  1295. }
  1296. if($ns == $this->XMLSchemaVersion || $ns == 'http://schemas.xmlsoap.org/soap/encoding/'){
  1297. $this->debug('in serializeType: type namespace indicates XML Schema or SOAP Encoding type');
  1298. if ($unqualified && $use == 'literal') {
  1299. $elementNS = " xmlns=\"\"";
  1300. } else {
  1301. $elementNS = '';
  1302. }
  1303. if (is_null($value)) {
  1304. if ($use == 'literal') {
  1305. // TODO: depends on minOccurs
  1306. $xml = "<$name$elementNS/>";
  1307. } else {
  1308. // TODO: depends on nillable, which should be checked before calling this method
  1309. $xml = "<$name$elementNS xsi:nil=\"true\" xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"/>";
  1310. }
  1311. $this->debug("in serializeType: returning: $xml");
  1312. return $xml;
  1313. }
  1314. if ($uqType == 'Array') {
  1315. // JBoss/Axis does this sometimes
  1316. return $this->serialize_val($value, $name, false, false, false, false, $use);
  1317. }
  1318. if ($uqType == 'boolean') {
  1319. if ((is_string($value) && $value == 'false') || (! $value)) {
  1320. $value = 'false';
  1321. } else {
  1322. $value = 'true';
  1323. }
  1324. }
  1325. if ($uqType == 'string' && gettype($value) == 'string') {
  1326. $value = $this->expandEntities($value);
  1327. }
  1328. if (($uqType == 'long' || $uqType == 'unsignedLong') && gettype($value) == 'double') {
  1329. $value = sprintf("%.0lf", $value);
  1330. }
  1331. // it's a scalar
  1332. // TODO: what about null/nil values?
  1333. // check type isn't a custom type extending xmlschema namespace
  1334. if (!$this->getTypeDef($uqType, $ns)) {
  1335. if ($use == 'literal') {
  1336. if ($forceType) {
  1337. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">$value</$name>";
  1338. } else {
  1339. $xml = "<$name$elementNS>$value</$name>";
  1340. }
  1341. } else {
  1342. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>$value</$name>";
  1343. }
  1344. $this->debug("in serializeType: returning: $xml");
  1345. return $xml;
  1346. }
  1347. $this->debug('custom type extends XML Schema or SOAP Encoding namespace (yuck)');
  1348. } else if ($ns == 'http://xml.apache.org/xml-soap') {
  1349. $this->debug('in serializeType: appears to be Apache SOAP type');
  1350. if ($uqType == 'Map') {
  1351. $tt_prefix = $this->getPrefixFromNamespace('http://xml.apache.org/xml-soap');
  1352. if (! $tt_prefix) {
  1353. $this->debug('in serializeType: Add namespace for Apache SOAP type');
  1354. $tt_prefix = 'ns' . rand(1000, 9999);
  1355. $this->namespaces[$tt_prefix] = 'http://xml.apache.org/xml-soap';
  1356. // force this to be added to usedNamespaces
  1357. $tt_prefix = $this->getPrefixFromNamespace('http://xml.apache.org/xml-soap');
  1358. }
  1359. $contents = '';
  1360. foreach($value as $k => $v) {
  1361. $this->debug("serializing map element: key $k, value $v");
  1362. $contents .= '<item>';
  1363. $contents .= $this->serialize_val($k,'key',false,false,false,false,$use);
  1364. $contents .= $this->serialize_val($v,'value',false,false,false,false,$use);
  1365. $contents .= '</item>';
  1366. }
  1367. if ($use == 'literal') {
  1368. if ($forceType) {
  1369. $xml = "<$name xsi:type=\"" . $tt_prefix . ":$uqType\">$contents</$name>";
  1370. } else {
  1371. $xml = "<$name>$contents</$name>";
  1372. }
  1373. } else {
  1374. $xml = "<$name xsi:type=\"" . $tt_prefix . ":$uqType\"$encodingStyle>$contents</$name>";
  1375. }
  1376. $this->debug("in serializeType: returning: $xml");
  1377. return $xml;
  1378. }
  1379. $this->debug('in serializeType: Apache SOAP type, but only support Map');
  1380. }
  1381. } else {
  1382. // TODO: should the type be compared to types in XSD, and the namespace
  1383. // set to XSD if the type matches?
  1384. $this->debug("in serializeType: No namespace for type $type");
  1385. $ns = '';
  1386. $uqType = $type;
  1387. }
  1388. if(!$typeDef = $this->getTypeDef($uqType, $ns)){
  1389. $this->setError("$type ($uqType) is not a supported type.");
  1390. $this->debug("in serializeType: $type ($uqType) is not a supported type.");
  1391. return false;
  1392. } else {
  1393. $this->debug("in serializeType: found typeDef");
  1394. $this->appendDebug('typeDef=' . $this->varDump($typeDef));
  1395. if (substr($uqType, -1) == '^') {
  1396. $uqType = substr($uqType, 0, -1);
  1397. }
  1398. }
  1399. if (!isset($typeDef['phpType'])) {
  1400. $this->setError("$type ($uqType) has no phpType.");
  1401. $this->debug("in serializeType: $type ($uqType) has no phpType.");
  1402. return false;
  1403. }
  1404. $phpType = $typeDef['phpType'];
  1405. $this->debug("in serializeType: uqType: $uqType, ns: $ns, phptype: $phpType, arrayType: " . (isset($typeDef['arrayType']) ? $typeDef['arrayType'] : '') );
  1406. // if php type == struct, map value to the <all> element names
  1407. if ($phpType == 'struct') {
  1408. if (isset($typeDef['typeClass']) && $typeDef['typeClass'] == 'element') {
  1409. $elementName = $uqType;
  1410. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  1411. $elementNS = " xmlns=\"$ns\"";
  1412. } else {
  1413. $elementNS = " xmlns=\"\"";
  1414. }
  1415. } else {
  1416. $elementName = $name;
  1417. if ($unqualified) {
  1418. $elementNS = " xmlns=\"\"";
  1419. } else {
  1420. $elementNS = '';
  1421. }
  1422. }
  1423. if (is_null($value)) {
  1424. if ($use == 'literal') {
  1425. // TODO: depends on minOccurs and nillable
  1426. $xml = "<$elementName$elementNS/>";
  1427. } else {
  1428. $xml = "<$elementName$elementNS xsi:nil=\"true\" xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"/>";
  1429. }
  1430. $this->debug("in serializeType: returning: $xml");
  1431. return $xml;
  1432. }
  1433. if (is_object($value)) {
  1434. $value = get_object_vars($value);
  1435. }
  1436. if (is_array($value)) {
  1437. $elementAttrs = $this->serializeComplexTypeAttributes($typeDef, $value, $ns, $uqType);
  1438. if ($use == 'literal') {
  1439. if ($forceType) {
  1440. $xml = "<$elementName$elementNS$elementAttrs xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">";
  1441. } else {
  1442. $xml = "<$elementName$elementNS$elementAttrs>";
  1443. }
  1444. } else {
  1445. $xml = "<$elementName$elementNS$elementAttrs xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>";
  1446. }
  1447. if (isset($typeDef['simpleContent']) && $typeDef['simpleContent'] == 'true') {
  1448. if (isset($value['!'])) {
  1449. $xml .= $value['!'];
  1450. $this->debug("in serializeType: serialized simpleContent for type $type");
  1451. } else {
  1452. $this->debug("in serializeType: no simpleContent to serialize for type $type");
  1453. }
  1454. } else {
  1455. // complexContent
  1456. $xml .= $this->serializeComplexTypeElements($typeDef, $value, $ns, $uqType, $use, $encodingStyle);
  1457. }
  1458. $xml .= "</$elementName>";
  1459. } else {
  1460. $this->debug("in serializeType: phpType is struct, but value is not an array");
  1461. $this->setError("phpType is struct, but value is not an array: see debug output for details");
  1462. $xml = '';
  1463. }
  1464. } elseif ($phpType == 'array') {
  1465. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  1466. $elementNS = " xmlns=\"$ns\"";
  1467. } else {
  1468. if ($unqualified) {
  1469. $elementNS = " xmlns=\"\"";
  1470. } else {
  1471. $elementNS = '';
  1472. }
  1473. }
  1474. if (is_null($value)) {
  1475. if ($use == 'literal') {
  1476. // TODO: depends on minOccurs
  1477. $xml = "<$name$elementNS/>";
  1478. } else {
  1479. $xml = "<$name$elementNS xsi:nil=\"true\" xsi:type=\"" .
  1480. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/') .
  1481. ":Array\" " .
  1482. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/') .
  1483. ':arrayType="' .
  1484. $this->getPrefixFromNamespace($this->getPrefix($typeDef['arrayType'])) .
  1485. ':' .
  1486. $this->getLocalPart($typeDef['arrayType'])."[0]\"/>";
  1487. }
  1488. $this->debug("in serializeType: returning: $xml");
  1489. return $xml;
  1490. }
  1491. if (isset($typeDef['multidimensional'])) {
  1492. $nv = array();
  1493. foreach($value as $v) {
  1494. $cols = ',' . sizeof($v);
  1495. $nv = array_merge($nv, $v);
  1496. }
  1497. $value = $nv;
  1498. } else {
  1499. $cols = '';
  1500. }
  1501. if (is_array($value) && sizeof($value) >= 1) {
  1502. $rows = sizeof($value);
  1503. $contents = '';
  1504. foreach($value as $k => $v) {
  1505. //$this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
  1506. //if (strpos($typeDef['arrayType'], ':') ) {
  1507. if (!in_array($typeDef['arrayType'],$this->typemap['http://www.w3.org/2001/XMLSchema'])) {
  1508. $contents .= $this->serializeType('item', $typeDef['arrayType'], $v, $use);
  1509. } else {
  1510. $contents .= $this->serialize_val($v, 'item', $typeDef['arrayType'], null, $this->XMLSchemaVersion, false, $use);
  1511. }
  1512. }
  1513. } else {
  1514. $rows = 0;
  1515. $contents = null;
  1516. }
  1517. // TODO: for now, an empty value will be serialized as a zero element
  1518. // array. Revisit this when coding the handling of null/nil values.
  1519. if ($use == 'literal') {
  1520. $xml = "<$name$elementNS>"
  1521. .$contents
  1522. ."</$name>";
  1523. } else {
  1524. $xml = "<$name$elementNS xsi:type=\"".$this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/').':Array" '.
  1525. $this->getPrefixFromNamespace('http://schemas.xmlsoap.org/soap/encoding/')
  1526. .':arrayType="'
  1527. .$this->getPrefixFromNamespace($this->getPrefix($typeDef['arrayType']))
  1528. .":".$this->getLocalPart($typeDef['arrayType'])."[$rows$cols]\">"
  1529. .$contents
  1530. ."</$name>";
  1531. }
  1532. } elseif ($phpType == 'scalar') {
  1533. if (isset($typeDef['form']) && ($typeDef['form'] == 'qualified')) {
  1534. $elementNS = " xmlns=\"$ns\"";
  1535. } else {
  1536. if ($unqualified) {
  1537. $elementNS = " xmlns=\"\"";
  1538. } else {
  1539. $elementNS = '';
  1540. }
  1541. }
  1542. if ($use == 'literal') {
  1543. if ($forceType) {
  1544. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\">$value</$name>";
  1545. } else {
  1546. $xml = "<$name$elementNS>$value</$name>";
  1547. }
  1548. } else {
  1549. $xml = "<$name$elementNS xsi:type=\"" . $this->getPrefixFromNamespace($ns) . ":$uqType\"$encodingStyle>$value</$name>";
  1550. }
  1551. }
  1552. $this->debug("in serializeType: returning: $xml");
  1553. return $xml;
  1554. }
  1555. /**
  1556. * serializes the attributes for a complexType
  1557. *
  1558. * @param array $typeDef our internal representation of an XML schema type (or element)
  1559. * @param mixed $value a native PHP value (parameter value)
  1560. * @param string $ns the namespace of the type
  1561. * @param string $uqType the local part of the type
  1562. * @return string value serialized as an XML string
  1563. * @access private
  1564. */
  1565. function serializeComplexTypeAttributes($typeDef, $value, $ns, $uqType) {
  1566. $this->debug("serializeComplexTypeAttributes for XML Schema type $ns:$uqType");
  1567. $xml = '';
  1568. if (isset($typeDef['extensionBase'])) {
  1569. $nsx = $this->getPrefix($typeDef['extensionBase']);
  1570. $uqTypex = $this->getLocalPart($typeDef['extensionBase']);
  1571. if ($this->getNamespaceFromPrefix($nsx)) {
  1572. $nsx = $this->getNamespaceFromPrefix($nsx);
  1573. }
  1574. if ($typeDefx = $this->getTypeDef($uqTypex, $nsx)) {
  1575. $this->debug("serialize attributes for extension base $nsx:$uqTypex");
  1576. $xml .= $this->serializeComplexTypeAttributes($typeDefx, $value, $nsx, $uqTypex);
  1577. } else {
  1578. $this->debug("extension base $nsx:$uqTypex is not a supported type");
  1579. }
  1580. }
  1581. if (isset($typeDef['attrs']) && is_array($typeDef['attrs'])) {
  1582. $this->debug("serialize attributes for XML Schema type $ns:$uqType");
  1583. if (is_array($value)) {
  1584. $xvalue = $value;
  1585. } elseif (is_object($value)) {
  1586. $xvalue = get_object_vars($value);
  1587. } else {
  1588. $this->debug("value is neither an array nor an object for XML Schema type $ns:$uqType");
  1589. $xvalue = array();
  1590. }
  1591. foreach ($typeDef['attrs'] as $aName => $attrs) {
  1592. if (isset($xvalue['!' . $aName])) {
  1593. $xname = '!' . $aName;
  1594. $this->debug("value provided for attribute $aName with key $xname");
  1595. } elseif (isset($xvalue[$aName])) {
  1596. $xname = $aName;
  1597. $this->debug("value provided for attribute $aName with key $xname");
  1598. } elseif (isset($attrs['default'])) {
  1599. $xname = '!' . $aName;
  1600. $xvalue[$xname] = $attrs['default'];
  1601. $this->debug('use default value of ' . $xvalue[$aName] . ' for attribute ' . $aName);
  1602. } else {
  1603. $xname = '';
  1604. $this->debug("no value provided for attribute $aName");
  1605. }
  1606. if ($xname) {
  1607. $xml .= " $aName=\"" . $this->expandEntities($xvalue[$xname]) . "\"";
  1608. }
  1609. }
  1610. } else {
  1611. $this->debug("no attributes to serialize for XML Schema type $ns:$uqType");
  1612. }
  1613. return $xml;
  1614. }
  1615. /**
  1616. * serializes the elements for a complexType
  1617. *
  1618. * @param array $typeDef our internal representation of an XML schema type (or element)
  1619. * @param mixed $value a native PHP value (parameter value)
  1620. * @param string $ns the namespace of the type
  1621. * @param string $uqType the local part of the type
  1622. * @param string $use use for part (encoded|literal)
  1623. * @param string $encodingStyle SOAP encoding style for the value (if different than the enclosing style)
  1624. * @return string value serialized as an XML string
  1625. * @access private
  1626. */
  1627. function serializeComplexTypeElements($typeDef, $value, $ns, $uqType, $use='encoded', $encodingStyle=false) {
  1628. $this->debug("in serializeComplexTypeElements for XML Schema type $ns:$uqType");
  1629. $xml = '';
  1630. if (isset($typeDef['extensionBase'])) {
  1631. $nsx = $this->getPrefix($typeDef['extensionBase']);
  1632. $uqTypex = $this->getLocalPart($typeDef['extensionBase']);
  1633. if ($this->getNamespaceFromPrefix($nsx)) {
  1634. $nsx = $this->getNamespaceFromPrefix($nsx);
  1635. }
  1636. if ($typeDefx = $this->getTypeDef($uqTypex, $nsx)) {
  1637. $this->debug("serialize elements for extension base $nsx:$uqTypex");
  1638. $xml .= $this->serializeComplexTypeElements($typeDefx, $value, $nsx, $uqTypex, $use, $encodingStyle);
  1639. } else {
  1640. $this->debug("extension base $nsx:$uqTypex is not a supported type");
  1641. }
  1642. }
  1643. if (isset($typeDef['elements']) && is_array($typeDef['elements'])) {
  1644. $this->debug("in serializeComplexTypeElements, serialize elements for XML Schema type $ns:$uqType");
  1645. if (is_array($value)) {
  1646. $xvalue = $value;
  1647. } elseif (is_object($value)) {
  1648. $xvalue = get_object_vars($value);
  1649. } else {
  1650. $this->debug("value is neither an array nor an object for XML Schema type $ns:$uqType");
  1651. $xvalue = array();
  1652. }
  1653. // toggle whether all elements are present - ideally should validate against schema
  1654. if (count($typeDef['elements']) != count($xvalue)){
  1655. $optionals = true;
  1656. }
  1657. foreach ($typeDef['elements'] as $eName => $attrs) {
  1658. if (!isset($xvalue[$eName])) {
  1659. if (isset($attrs['default'])) {
  1660. $xvalue[$eName] = $attrs['default'];
  1661. $this->debug('use default value of ' . $xvalue[$eName] . ' for element ' . $eName);
  1662. }
  1663. }
  1664. // if user took advantage of a minOccurs=0, then only serialize named parameters
  1665. if (isset($optionals)
  1666. && (!isset($xvalue[$eName]))
  1667. && ( (!isset($attrs['nillable'])) || $attrs['nillable'] != 'true')
  1668. ){
  1669. if (isset($attrs['minOccurs']) && $attrs['minOccurs'] <> '0') {
  1670. $this->debug("apparent error: no value provided for element $eName with minOccurs=" . $attrs['minOccurs']);
  1671. }
  1672. // do nothing
  1673. $this->debug("no value provided for complexType element $eName and element is not nillable, so serialize nothing");
  1674. } else {
  1675. // get value
  1676. if (isset($xvalue[$eName])) {
  1677. $v = $xvalue[$eName];
  1678. } else {
  1679. $v = null;
  1680. }
  1681. if (isset($attrs['form'])) {
  1682. $unqualified = ($attrs['form'] == 'unqualified');
  1683. } else {
  1684. $unqualified = false;
  1685. }
  1686. if (isset($attrs['maxOccurs']) && ($attrs['maxOccurs'] == 'unbounded' || $attrs['maxOccurs'] > 1) && isset($v) && is_array($v) && $this->isArraySimpleOrStruct($v) == 'arraySimple') {
  1687. $vv = $v;
  1688. foreach ($vv as $k => $v) {
  1689. if (isset($attrs['type']) || isset($attrs['ref'])) {
  1690. // serialize schema-defined type
  1691. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  1692. } else {
  1693. // serialize generic type (can this ever really happen?)
  1694. $this->debug("calling serialize_val() for $v, $eName, false, false, false, false, $use");
  1695. $xml .= $this->serialize_val($v, $eName, false, false, false, false, $use);
  1696. }
  1697. }
  1698. } else {
  1699. if (is_null($v) && isset($attrs['minOccurs']) && $attrs['minOccurs'] == '0') {
  1700. // do nothing
  1701. } elseif (is_null($v) && isset($attrs['nillable']) && $attrs['nillable'] == 'true') {
  1702. // TODO: serialize a nil correctly, but for now serialize schema-defined type
  1703. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  1704. } elseif (isset($attrs['type']) || isset($attrs['ref'])) {
  1705. // serialize schema-defined type
  1706. $xml .= $this->serializeType($eName, isset($attrs['type']) ? $attrs['type'] : $attrs['ref'], $v, $use, $encodingStyle, $unqualified);
  1707. } else {
  1708. // serialize generic type (can this ever really happen?)
  1709. $this->debug("calling serialize_val() for $v, $eName, false, false, false, false, $use");
  1710. $xml .= $this->serialize_val($v, $eName, false, false, false, false, $use);
  1711. }
  1712. }
  1713. }
  1714. }
  1715. } else {
  1716. $this->debug("no elements to serialize for XML Schema type $ns:$uqType");
  1717. }
  1718. return $xml;
  1719. }
  1720. /**
  1721. * adds an XML Schema complex type to the WSDL types
  1722. *
  1723. * @param string $name
  1724. * @param string $typeClass (complexType|simpleType|attribute)
  1725. * @param string $phpType currently supported are array and struct (php assoc array)
  1726. * @param string $compositor (all|sequence|choice)
  1727. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  1728. * @param array $elements e.g. array ( name => array(name=>'',type=>'') )
  1729. * @param array $attrs e.g. array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:string[]'))
  1730. * @param string $arrayType as namespace:name (xsd:string)
  1731. * @see nusoap_xmlschema
  1732. * @access public
  1733. */
  1734. function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType='') {
  1735. if (count($elements) > 0) {
  1736. $eElements = array();
  1737. foreach($elements as $n => $e){
  1738. // expand each element
  1739. $ee = array();
  1740. foreach ($e as $k => $v) {
  1741. $k = strpos($k,':') ? $this->expandQname($k) : $k;
  1742. $v = strpos($v,':') ? $this->expandQname($v) : $v;
  1743. $ee[$k] = $v;
  1744. }
  1745. $eElements[$n] = $ee;
  1746. }
  1747. $elements = $eElements;
  1748. }
  1749. if (count($attrs) > 0) {
  1750. foreach($attrs as $n => $a){
  1751. // expand each attribute
  1752. foreach ($a as $k => $v) {
  1753. $k = strpos($k,':') ? $this->expandQname($k) : $k;
  1754. $v = strpos($v,':') ? $this->expandQname($v) : $v;
  1755. $aa[$k] = $v;
  1756. }
  1757. $eAttrs[$n] = $aa;
  1758. }
  1759. $attrs = $eAttrs;
  1760. }
  1761. $restrictionBase = strpos($restrictionBase,':') ? $this->expandQname($restrictionBase) : $restrictionBase;
  1762. $arrayType = strpos($arrayType,':') ? $this->expandQname($arrayType) : $arrayType;
  1763. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  1764. $this->schemas[$typens][0]->addComplexType($name,$typeClass,$phpType,$compositor,$restrictionBase,$elements,$attrs,$arrayType);
  1765. }
  1766. /**
  1767. * adds an XML Schema simple type to the WSDL types
  1768. *
  1769. * @param string $name
  1770. * @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
  1771. * @param string $typeClass (should always be simpleType)
  1772. * @param string $phpType (should always be scalar)
  1773. * @param array $enumeration array of values
  1774. * @see nusoap_xmlschema
  1775. * @access public
  1776. */
  1777. function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
  1778. $restrictionBase = strpos($restrictionBase,':') ? $this->expandQname($restrictionBase) : $restrictionBase;
  1779. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  1780. $this->schemas[$typens][0]->addSimpleType($name, $restrictionBase, $typeClass, $phpType, $enumeration);
  1781. }
  1782. /**
  1783. * adds an element to the WSDL types
  1784. *
  1785. * @param array $attrs attributes that must include name and type
  1786. * @see nusoap_xmlschema
  1787. * @access public
  1788. */
  1789. function addElement($attrs) {
  1790. $typens = isset($this->namespaces['types']) ? $this->namespaces['types'] : $this->namespaces['tns'];
  1791. $this->schemas[$typens][0]->addElement($attrs);
  1792. }
  1793. /**
  1794. * register an operation with the server
  1795. *
  1796. * @param string $name operation (method) name
  1797. * @param array $in assoc array of input values: key = param name, value = param type
  1798. * @param array $out assoc array of output values: key = param name, value = param type
  1799. * @param string $namespace optional The namespace for the operation
  1800. * @param string $soapaction optional The soapaction for the operation
  1801. * @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
  1802. * @param string $use (encoded|literal) optional The use for the parameters (cannot mix right now)
  1803. * @param string $documentation optional The description to include in the WSDL
  1804. * @param string $encodingStyle optional (usually 'http://schemas.xmlsoap.org/soap/encoding/' for encoded)
  1805. * @access public
  1806. */
  1807. function addOperation($name, $in = false, $out = false, $namespace = false, $soapaction = false, $style = 'rpc', $use = 'encoded', $documentation = '', $encodingStyle = ''){
  1808. if ($use == 'encoded' && $encodingStyle == '') {
  1809. $encodingStyle = 'http://schemas.xmlsoap.org/soap/encoding/';
  1810. }
  1811. if ($style == 'document') {
  1812. $elements = array();
  1813. foreach ($in as $n => $t) {
  1814. $elements[$n] = array('name' => $n, 'type' => $t, 'form' => 'unqualified');
  1815. }
  1816. $this->addComplexType($name . 'RequestType', 'complexType', 'struct', 'all', '', $elements);
  1817. $this->addElement(array('name' => $name, 'type' => $name . 'RequestType'));
  1818. $in = array('parameters' => 'tns:' . $name . '^');
  1819. $elements = array();
  1820. foreach ($out as $n => $t) {
  1821. $elements[$n] = array('name' => $n, 'type' => $t, 'form' => 'unqualified');
  1822. }
  1823. $this->addComplexType($name . 'ResponseType', 'complexType', 'struct', 'all', '', $elements);
  1824. $this->addElement(array('name' => $name . 'Response', 'type' => $name . 'ResponseType', 'form' => 'qualified'));
  1825. $out = array('parameters' => 'tns:' . $name . 'Response' . '^');
  1826. }
  1827. // get binding
  1828. $this->bindings[ $this->serviceName . 'Binding' ]['operations'][$name] =
  1829. array(
  1830. 'name' => $name,
  1831. 'binding' => $this->serviceName . 'Binding',
  1832. 'endpoint' => $this->endpoint,
  1833. 'soapAction' => $soapaction,
  1834. 'style' => $style,
  1835. 'input' => array(
  1836. 'use' => $use,
  1837. 'namespace' => $namespace,
  1838. 'encodingStyle' => $encodingStyle,
  1839. 'message' => $name . 'Request',
  1840. 'parts' => $in),
  1841. 'output' => array(
  1842. 'use' => $use,
  1843. 'namespace' => $namespace,
  1844. 'encodingStyle' => $encodingStyle,
  1845. 'message' => $name . 'Response',
  1846. 'parts' => $out),
  1847. 'namespace' => $namespace,
  1848. 'transport' => 'http://schemas.xmlsoap.org/soap/http',
  1849. 'documentation' => $documentation);
  1850. // add portTypes
  1851. // add messages
  1852. if($in)
  1853. {
  1854. foreach($in as $pName => $pType)
  1855. {
  1856. if(strpos($pType,':')) {
  1857. $pType = $this->getNamespaceFromPrefix($this->getPrefix($pType)).":".$this->getLocalPart($pType);
  1858. }
  1859. $this->messages[$name.'Request'][$pName] = $pType;
  1860. }
  1861. } else {
  1862. $this->messages[$name.'Request']= '0';
  1863. }
  1864. if($out)
  1865. {
  1866. foreach($out as $pName => $pType)
  1867. {
  1868. if(strpos($pType,':')) {
  1869. $pType = $this->getNamespaceFromPrefix($this->getPrefix($pType)).":".$this->getLocalPart($pType);
  1870. }
  1871. $this->messages[$name.'Response'][$pName] = $pType;
  1872. }
  1873. } else {
  1874. $this->messages[$name.'Response']= '0';
  1875. }
  1876. return true;
  1877. }
  1878. }
  1879. ?>