PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Soap/Wsdl.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 680 lines | 386 code | 67 blank | 227 comment | 49 complexity | 8fd7a0e88015f44fa275c9ff3f1ff02c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Soap
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Wsdl.php 25031 2012-08-17 19:42:00Z matthew $
  20. */
  21. /**
  22. * @see Zend_Soap_Wsdl_Strategy_Interface
  23. */
  24. require_once "Zend/Soap/Wsdl/Strategy/Interface.php";
  25. /**
  26. * @see Zend_Soap_Wsdl_Strategy_Abstract
  27. */
  28. require_once "Zend/Soap/Wsdl/Strategy/Abstract.php";
  29. /**
  30. * Zend_Soap_Wsdl
  31. *
  32. * @category Zend
  33. * @package Zend_Soap
  34. */
  35. class Zend_Soap_Wsdl
  36. {
  37. /**
  38. * @var object DomDocument Instance
  39. */
  40. private $_dom;
  41. /**
  42. * @var object WSDL Root XML_Tree_Node
  43. */
  44. private $_wsdl;
  45. /**
  46. * @var string URI where the WSDL will be available
  47. */
  48. private $_uri;
  49. /**
  50. * @var DOMElement
  51. */
  52. private $_schema = null;
  53. /**
  54. * Types defined on schema
  55. *
  56. * @var array
  57. */
  58. private $_includedTypes = array();
  59. /**
  60. * Strategy for detection of complex types
  61. */
  62. protected $_strategy = null;
  63. /**
  64. * Constructor
  65. *
  66. * @param string $name Name of the Web Service being Described
  67. * @param string $uri URI where the WSDL will be available
  68. * @param boolean|string|Zend_Soap_Wsdl_Strategy_Interface $strategy
  69. */
  70. public function __construct($name, $uri, $strategy = true)
  71. {
  72. if ($uri instanceof Zend_Uri_Http) {
  73. $uri = $uri->getUri();
  74. }
  75. $this->_uri = $uri;
  76. /**
  77. * @todo change DomDocument object creation from cparsing to construxting using API
  78. * It also should authomatically escape $name and $uri values if necessary
  79. */
  80. $wsdl = "<?xml version='1.0' ?>
  81. <definitions name='$name' targetNamespace='$uri'
  82. xmlns='http://schemas.xmlsoap.org/wsdl/'
  83. xmlns:tns='$uri'
  84. xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
  85. xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  86. xmlns:soap-enc='http://schemas.xmlsoap.org/soap/encoding/'
  87. xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'></definitions>";
  88. libxml_disable_entity_loader(true);
  89. $this->_dom = new DOMDocument();
  90. if (!$this->_dom->loadXML($wsdl)) {
  91. require_once 'Zend/Server/Exception.php';
  92. throw new Zend_Server_Exception('Unable to create DomDocument');
  93. } else {
  94. foreach ($this->_dom->childNodes as $child) {
  95. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  96. require_once 'Zend/Server/Exception.php';
  97. throw new Zend_Server_Exception(
  98. 'Invalid XML: Detected use of illegal DOCTYPE'
  99. );
  100. }
  101. }
  102. $this->_wsdl = $this->_dom->documentElement;
  103. }
  104. libxml_disable_entity_loader(false);
  105. $this->setComplexTypeStrategy($strategy);
  106. }
  107. /**
  108. * Set a new uri for this WSDL
  109. *
  110. * @param string|Zend_Uri_Http $uri
  111. * @return Zend_Server_Wsdl
  112. */
  113. public function setUri($uri)
  114. {
  115. if ($uri instanceof Zend_Uri_Http) {
  116. $uri = $uri->getUri();
  117. }
  118. $oldUri = $this->_uri;
  119. $this->_uri = $uri;
  120. if($this->_dom !== null) {
  121. // @todo: This is the worst hack ever, but its needed due to design and non BC issues of WSDL generation
  122. $xml = $this->_dom->saveXML();
  123. $xml = str_replace($oldUri, $uri, $xml);
  124. libxml_disable_entity_loader(true);
  125. $this->_dom = new DOMDocument();
  126. $this->_dom->loadXML($xml);
  127. libxml_disable_entity_loader(false);
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Set a strategy for complex type detection and handling
  133. *
  134. * @todo Boolean is for backwards compability with extractComplexType object var. Remove it in later versions.
  135. * @param boolean|string|Zend_Soap_Wsdl_Strategy_Interface $strategy
  136. * @return Zend_Soap_Wsdl
  137. */
  138. public function setComplexTypeStrategy($strategy)
  139. {
  140. if($strategy === true) {
  141. require_once "Zend/Soap/Wsdl/Strategy/DefaultComplexType.php";
  142. $strategy = new Zend_Soap_Wsdl_Strategy_DefaultComplexType();
  143. } else if($strategy === false) {
  144. require_once "Zend/Soap/Wsdl/Strategy/AnyType.php";
  145. $strategy = new Zend_Soap_Wsdl_Strategy_AnyType();
  146. } else if(is_string($strategy)) {
  147. if(class_exists($strategy)) {
  148. $strategy = new $strategy();
  149. } else {
  150. require_once "Zend/Soap/Wsdl/Exception.php";
  151. throw new Zend_Soap_Wsdl_Exception(
  152. sprintf("Strategy with name '%s does not exist.", $strategy
  153. ));
  154. }
  155. }
  156. if(!($strategy instanceof Zend_Soap_Wsdl_Strategy_Interface)) {
  157. require_once "Zend/Soap/Wsdl/Exception.php";
  158. throw new Zend_Soap_Wsdl_Exception("Set a strategy that is not of type 'Zend_Soap_Wsdl_Strategy_Interface'");
  159. }
  160. $this->_strategy = $strategy;
  161. return $this;
  162. }
  163. /**
  164. * Get the current complex type strategy
  165. *
  166. * @return Zend_Soap_Wsdl_Strategy_Interface
  167. */
  168. public function getComplexTypeStrategy()
  169. {
  170. return $this->_strategy;
  171. }
  172. /**
  173. * Add a {@link http://www.w3.org/TR/wsdl#_messages message} element to the WSDL
  174. *
  175. * @param string $name Name for the {@link http://www.w3.org/TR/wsdl#_messages message}
  176. * @param array $parts An array of {@link http://www.w3.org/TR/wsdl#_message parts}
  177. * The array is constructed like: 'name of part' => 'part xml schema data type'
  178. * or 'name of part' => array('type' => 'part xml schema type')
  179. * or 'name of part' => array('element' => 'part xml element name')
  180. * @return object The new message's XML_Tree_Node for use in {@link function addDocumentation}
  181. */
  182. public function addMessage($name, $parts)
  183. {
  184. $message = $this->_dom->createElement('message');
  185. $message->setAttribute('name', $name);
  186. if (sizeof($parts) > 0) {
  187. foreach ($parts as $name => $type) {
  188. $part = $this->_dom->createElement('part');
  189. $part->setAttribute('name', $name);
  190. if (is_array($type)) {
  191. foreach ($type as $key => $value) {
  192. $part->setAttribute($key, $value);
  193. }
  194. } else {
  195. $part->setAttribute('type', $type);
  196. }
  197. $message->appendChild($part);
  198. }
  199. }
  200. $this->_wsdl->appendChild($message);
  201. return $message;
  202. }
  203. /**
  204. * Add a {@link http://www.w3.org/TR/wsdl#_porttypes portType} element to the WSDL
  205. *
  206. * @param string $name portType element's name
  207. * @return object The new portType's XML_Tree_Node for use in {@link function addPortOperation} and {@link function addDocumentation}
  208. */
  209. public function addPortType($name)
  210. {
  211. $portType = $this->_dom->createElement('portType');
  212. $portType->setAttribute('name', $name);
  213. $this->_wsdl->appendChild($portType);
  214. return $portType;
  215. }
  216. /**
  217. * Add an {@link http://www.w3.org/TR/wsdl#_request-response operation} element to a portType element
  218. *
  219. * @param object $portType a portType XML_Tree_Node, from {@link function addPortType}
  220. * @param string $name Operation name
  221. * @param string $input Input Message
  222. * @param string $output Output Message
  223. * @param string $fault Fault Message
  224. * @return object The new operation's XML_Tree_Node for use in {@link function addDocumentation}
  225. */
  226. public function addPortOperation($portType, $name, $input = false, $output = false, $fault = false)
  227. {
  228. $operation = $this->_dom->createElement('operation');
  229. $operation->setAttribute('name', $name);
  230. if (is_string($input) && (strlen(trim($input)) >= 1)) {
  231. $node = $this->_dom->createElement('input');
  232. $node->setAttribute('message', $input);
  233. $operation->appendChild($node);
  234. }
  235. if (is_string($output) && (strlen(trim($output)) >= 1)) {
  236. $node= $this->_dom->createElement('output');
  237. $node->setAttribute('message', $output);
  238. $operation->appendChild($node);
  239. }
  240. if (is_string($fault) && (strlen(trim($fault)) >= 1)) {
  241. $node = $this->_dom->createElement('fault');
  242. $node->setAttribute('message', $fault);
  243. $operation->appendChild($node);
  244. }
  245. $portType->appendChild($operation);
  246. return $operation;
  247. }
  248. /**
  249. * Add a {@link http://www.w3.org/TR/wsdl#_bindings binding} element to WSDL
  250. *
  251. * @param string $name Name of the Binding
  252. * @param string $type name of the portType to bind
  253. * @return object The new binding's XML_Tree_Node for use with {@link function addBindingOperation} and {@link function addDocumentation}
  254. */
  255. public function addBinding($name, $portType)
  256. {
  257. $binding = $this->_dom->createElement('binding');
  258. $binding->setAttribute('name', $name);
  259. $binding->setAttribute('type', $portType);
  260. $this->_wsdl->appendChild($binding);
  261. return $binding;
  262. }
  263. /**
  264. * Add an operation to a binding element
  265. *
  266. * @param object $binding A binding XML_Tree_Node returned by {@link function addBinding}
  267. * @param array $input An array of attributes for the input element, allowed keys are: 'use', 'namespace', 'encodingStyle'. {@link http://www.w3.org/TR/wsdl#_soap:body More Information}
  268. * @param array $output An array of attributes for the output element, allowed keys are: 'use', 'namespace', 'encodingStyle'. {@link http://www.w3.org/TR/wsdl#_soap:body More Information}
  269. * @param array $fault An array of attributes for the fault element, allowed keys are: 'name', 'use', 'namespace', 'encodingStyle'. {@link http://www.w3.org/TR/wsdl#_soap:body More Information}
  270. * @return object The new Operation's XML_Tree_Node for use with {@link function addSoapOperation} and {@link function addDocumentation}
  271. */
  272. public function addBindingOperation($binding, $name, $input = false, $output = false, $fault = false)
  273. {
  274. $operation = $this->_dom->createElement('operation');
  275. $operation->setAttribute('name', $name);
  276. if (is_array($input)) {
  277. $node = $this->_dom->createElement('input');
  278. $soap_node = $this->_dom->createElement('soap:body');
  279. foreach ($input as $name => $value) {
  280. $soap_node->setAttribute($name, $value);
  281. }
  282. $node->appendChild($soap_node);
  283. $operation->appendChild($node);
  284. }
  285. if (is_array($output)) {
  286. $node = $this->_dom->createElement('output');
  287. $soap_node = $this->_dom->createElement('soap:body');
  288. foreach ($output as $name => $value) {
  289. $soap_node->setAttribute($name, $value);
  290. }
  291. $node->appendChild($soap_node);
  292. $operation->appendChild($node);
  293. }
  294. if (is_array($fault)) {
  295. $node = $this->_dom->createElement('fault');
  296. /**
  297. * Note. Do we really need name attribute to be also set at wsdl:fault node???
  298. * W3C standard doesn't mention it (http://www.w3.org/TR/wsdl#_soap:fault)
  299. * But some real world WSDLs use it, so it may be required for compatibility reasons.
  300. */
  301. if (isset($fault['name'])) {
  302. $node->setAttribute('name', $fault['name']);
  303. }
  304. $soap_node = $this->_dom->createElement('soap:fault');
  305. foreach ($fault as $name => $value) {
  306. $soap_node->setAttribute($name, $value);
  307. }
  308. $node->appendChild($soap_node);
  309. $operation->appendChild($node);
  310. }
  311. $binding->appendChild($operation);
  312. return $operation;
  313. }
  314. /**
  315. * Add a {@link http://www.w3.org/TR/wsdl#_soap:binding SOAP binding} element to a Binding element
  316. *
  317. * @param object $binding A binding XML_Tree_Node returned by {@link function addBinding}
  318. * @param string $style binding style, possible values are "rpc" (the default) and "document"
  319. * @param string $transport Transport method (defaults to HTTP)
  320. * @return boolean
  321. */
  322. public function addSoapBinding($binding, $style = 'document', $transport = 'http://schemas.xmlsoap.org/soap/http')
  323. {
  324. $soap_binding = $this->_dom->createElement('soap:binding');
  325. $soap_binding->setAttribute('style', $style);
  326. $soap_binding->setAttribute('transport', $transport);
  327. $binding->appendChild($soap_binding);
  328. return $soap_binding;
  329. }
  330. /**
  331. * Add a {@link http://www.w3.org/TR/wsdl#_soap:operation SOAP operation} to an operation element
  332. *
  333. * @param object $operation An operation XML_Tree_Node returned by {@link function addBindingOperation}
  334. * @param string $soap_action SOAP Action
  335. * @return boolean
  336. */
  337. public function addSoapOperation($binding, $soap_action)
  338. {
  339. if ($soap_action instanceof Zend_Uri_Http) {
  340. $soap_action = $soap_action->getUri();
  341. }
  342. $soap_operation = $this->_dom->createElement('soap:operation');
  343. $soap_operation->setAttribute('soapAction', $soap_action);
  344. $binding->insertBefore($soap_operation, $binding->firstChild);
  345. return $soap_operation;
  346. }
  347. /**
  348. * Add a {@link http://www.w3.org/TR/wsdl#_services service} element to the WSDL
  349. *
  350. * @param string $name Service Name
  351. * @param string $port_name Name of the port for the service
  352. * @param string $binding Binding for the port
  353. * @param string $location SOAP Address for the service
  354. * @return object The new service's XML_Tree_Node for use with {@link function addDocumentation}
  355. */
  356. public function addService($name, $port_name, $binding, $location)
  357. {
  358. if ($location instanceof Zend_Uri_Http) {
  359. $location = $location->getUri();
  360. }
  361. $service = $this->_dom->createElement('service');
  362. $service->setAttribute('name', $name);
  363. $port = $this->_dom->createElement('port');
  364. $port->setAttribute('name', $port_name);
  365. $port->setAttribute('binding', $binding);
  366. $soap_address = $this->_dom->createElement('soap:address');
  367. $soap_address->setAttribute('location', $location);
  368. $port->appendChild($soap_address);
  369. $service->appendChild($port);
  370. $this->_wsdl->appendChild($service);
  371. return $service;
  372. }
  373. /**
  374. * Add a documentation element to any element in the WSDL.
  375. *
  376. * Note that the WSDL {@link http://www.w3.org/TR/wsdl#_documentation specification} uses 'document',
  377. * but the WSDL {@link http://schemas.xmlsoap.org/wsdl/ schema} uses 'documentation' instead.
  378. * The {@link http://www.ws-i.org/Profiles/BasicProfile-1.1-2004-08-24.html#WSDL_documentation_Element WS-I Basic Profile 1.1} recommends using 'documentation'.
  379. *
  380. * @param object $input_node An XML_Tree_Node returned by another method to add the documentation to
  381. * @param string $documentation Human readable documentation for the node
  382. * @return DOMElement The documentation element
  383. */
  384. public function addDocumentation($input_node, $documentation)
  385. {
  386. if ($input_node === $this) {
  387. $node = $this->_dom->documentElement;
  388. } else {
  389. $node = $input_node;
  390. }
  391. $doc = $this->_dom->createElement('documentation');
  392. $doc_cdata = $this->_dom->createTextNode(str_replace(array("\r\n", "\r"), "\n", $documentation));
  393. $doc->appendChild($doc_cdata);
  394. if($node->hasChildNodes()) {
  395. $node->insertBefore($doc, $node->firstChild);
  396. } else {
  397. $node->appendChild($doc);
  398. }
  399. return $doc;
  400. }
  401. /**
  402. * Add WSDL Types element
  403. *
  404. * @param object $types A DomDocument|DomNode|DomElement|DomDocumentFragment with all the XML Schema types defined in it
  405. */
  406. public function addTypes($types)
  407. {
  408. if ($types instanceof DomDocument) {
  409. $dom = $this->_dom->importNode($types->documentElement);
  410. $this->_wsdl->appendChild($types->documentElement);
  411. } elseif ($types instanceof DomNode || $types instanceof DomElement || $types instanceof DomDocumentFragment ) {
  412. $dom = $this->_dom->importNode($types);
  413. $this->_wsdl->appendChild($dom);
  414. }
  415. }
  416. /**
  417. * Add a complex type name that is part of this WSDL and can be used in signatures.
  418. *
  419. * @param string $type
  420. * @return Zend_Soap_Wsdl
  421. */
  422. public function addType($type)
  423. {
  424. if(!in_array($type, $this->_includedTypes)) {
  425. $this->_includedTypes[] = $type;
  426. }
  427. return $this;
  428. }
  429. /**
  430. * Return an array of all currently included complex types
  431. *
  432. * @return array
  433. */
  434. public function getTypes()
  435. {
  436. return $this->_includedTypes;
  437. }
  438. /**
  439. * Return the Schema node of the WSDL
  440. *
  441. * @return DOMElement
  442. */
  443. public function getSchema()
  444. {
  445. if($this->_schema == null) {
  446. $this->addSchemaTypeSection();
  447. }
  448. return $this->_schema;
  449. }
  450. /**
  451. * Return the WSDL as XML
  452. *
  453. * @return string WSDL as XML
  454. */
  455. public function toXML()
  456. {
  457. return $this->_dom->saveXML();
  458. }
  459. /**
  460. * Return DOM Document
  461. *
  462. * @return object DomDocum ent
  463. */
  464. public function toDomDocument()
  465. {
  466. return $this->_dom;
  467. }
  468. /**
  469. * Echo the WSDL as XML
  470. *
  471. * @return boolean
  472. */
  473. public function dump($filename = false)
  474. {
  475. if (!$filename) {
  476. echo $this->toXML();
  477. return true;
  478. } else {
  479. return file_put_contents($filename, $this->toXML());
  480. }
  481. }
  482. /**
  483. * Returns an XSD Type for the given PHP type
  484. *
  485. * @param string $type PHP Type to get the XSD type for
  486. * @return string
  487. */
  488. public function getType($type)
  489. {
  490. switch (strtolower($type)) {
  491. case 'string':
  492. case 'str':
  493. return 'xsd:string';
  494. case 'long':
  495. return 'xsd:long';
  496. case 'int':
  497. case 'integer':
  498. return 'xsd:int';
  499. case 'float':
  500. return 'xsd:float';
  501. case 'double':
  502. return 'xsd:double';
  503. case 'boolean':
  504. case 'bool':
  505. return 'xsd:boolean';
  506. case 'array':
  507. return 'soap-enc:Array';
  508. case 'object':
  509. return 'xsd:struct';
  510. case 'mixed':
  511. return 'xsd:anyType';
  512. case 'void':
  513. return '';
  514. default:
  515. // delegate retrieval of complex type to current strategy
  516. return $this->addComplexType($type);
  517. }
  518. }
  519. /**
  520. * This function makes sure a complex types section and schema additions are set.
  521. *
  522. * @return Zend_Soap_Wsdl
  523. */
  524. public function addSchemaTypeSection()
  525. {
  526. if ($this->_schema === null) {
  527. $this->_schema = $this->_dom->createElement('xsd:schema');
  528. $this->_schema->setAttribute('targetNamespace', $this->_uri);
  529. $types = $this->_dom->createElement('types');
  530. $types->appendChild($this->_schema);
  531. $this->_wsdl->appendChild($types);
  532. }
  533. return $this;
  534. }
  535. /**
  536. * Add a {@link http://www.w3.org/TR/wsdl#_types types} data type definition
  537. *
  538. * @param string $type Name of the class to be specified
  539. * @return string XSD Type for the given PHP type
  540. */
  541. public function addComplexType($type)
  542. {
  543. if (in_array($type, $this->getTypes())) {
  544. return "tns:$type";
  545. }
  546. $this->addSchemaTypeSection();
  547. $strategy = $this->getComplexTypeStrategy();
  548. $strategy->setContext($this);
  549. // delegates the detection of a complex type to the current strategy
  550. return $strategy->addComplexType($type);
  551. }
  552. /**
  553. * Parse an xsd:element represented as an array into a DOMElement.
  554. *
  555. * @param array $element an xsd:element represented as an array
  556. * @return DOMElement parsed element
  557. */
  558. private function _parseElement($element)
  559. {
  560. if (!is_array($element)) {
  561. require_once "Zend/Soap/Wsdl/Exception.php";
  562. throw new Zend_Soap_Wsdl_Exception("The 'element' parameter needs to be an associative array.");
  563. }
  564. $elementXml = $this->_dom->createElement('xsd:element');
  565. foreach ($element as $key => $value) {
  566. if (in_array($key, array('sequence', 'all', 'choice'))) {
  567. if (is_array($value)) {
  568. $complexType = $this->_dom->createElement('xsd:complexType');
  569. if (count($value) > 0) {
  570. $container = $this->_dom->createElement('xsd:' . $key);
  571. foreach ($value as $subelement) {
  572. $subelementXml = $this->_parseElement($subelement);
  573. $container->appendChild($subelementXml);
  574. }
  575. $complexType->appendChild($container);
  576. }
  577. $elementXml->appendChild($complexType);
  578. }
  579. } else {
  580. $elementXml->setAttribute($key, $value);
  581. }
  582. }
  583. return $elementXml;
  584. }
  585. /**
  586. * Add an xsd:element represented as an array to the schema.
  587. *
  588. * Array keys represent attribute names and values their respective value.
  589. * The 'sequence', 'all' and 'choice' keys must have an array of elements as their value,
  590. * to add them to a nested complexType.
  591. *
  592. * Example: array( 'name' => 'MyElement',
  593. * 'sequence' => array( array('name' => 'myString', 'type' => 'string'),
  594. * array('name' => 'myInteger', 'type' => 'int') ) );
  595. * Resulting XML: <xsd:element name="MyElement"><xsd:complexType><xsd:sequence>
  596. * <xsd:element name="myString" type="string"/>
  597. * <xsd:element name="myInteger" type="int"/>
  598. * </xsd:sequence></xsd:complexType></xsd:element>
  599. *
  600. * @param array $element an xsd:element represented as an array
  601. * @return string xsd:element for the given element array
  602. */
  603. public function addElement($element)
  604. {
  605. $schema = $this->getSchema();
  606. $elementXml = $this->_parseElement($element);
  607. $schema->appendChild($elementXml);
  608. return 'tns:' . $element['name'];
  609. }
  610. }