AutoDiscover.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. * @subpackage AutoDiscover
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Server_Interface
  24. */
  25. require_once 'Zend/Server/Interface.php';
  26. /**
  27. * @see Zend_Soap_Wsdl
  28. */
  29. require_once 'Zend/Soap/Wsdl.php';
  30. /**
  31. * @see Zend_Server_Reflection
  32. */
  33. require_once 'Zend/Server/Reflection.php';
  34. /**
  35. * @see Zend_Server_Abstract
  36. */
  37. require_once 'Zend/Server/Abstract.php';
  38. /**
  39. * @see Zend_Uri
  40. */
  41. require_once 'Zend/Uri.php';
  42. /**
  43. * Zend_Soap_AutoDiscover
  44. *
  45. * @category Zend
  46. * @package Zend_Soap
  47. * @subpackage AutoDiscover
  48. */
  49. class Zend_Soap_AutoDiscover implements Zend_Server_Interface
  50. {
  51. /**
  52. * @var Zend_Soap_Wsdl
  53. */
  54. protected $_wsdl = null;
  55. /**
  56. * @var Zend_Server_Reflection
  57. */
  58. protected $_reflection = null;
  59. /**
  60. * @var array
  61. */
  62. protected $_functions = array();
  63. /**
  64. * @var boolean
  65. */
  66. protected $_strategy;
  67. /**
  68. * Url where the WSDL file will be available at.
  69. *
  70. * @var WSDL Uri
  71. */
  72. protected $_uri;
  73. /**
  74. * soap:body operation style options
  75. *
  76. * @var array
  77. */
  78. protected $_operationBodyStyle = array('use' => 'encoded', 'encodingStyle' => "http://schemas.xmlsoap.org/soap/encoding/");
  79. /**
  80. * soap:operation style
  81. *
  82. * @var array
  83. */
  84. protected $_bindingStyle = array('style' => 'rpc', 'transport' => 'http://schemas.xmlsoap.org/soap/http');
  85. /**
  86. * Name of the class to handle the WSDL creation.
  87. *
  88. * @var string
  89. */
  90. protected $_wsdlClass = 'Zend_Soap_Wsdl';
  91. /**
  92. * Constructor
  93. *
  94. * @param boolean|string|Zend_Soap_Wsdl_Strategy_Interface $strategy
  95. * @param string|Zend_Uri $uri
  96. * @param string $wsdlClass
  97. */
  98. public function __construct($strategy = true, $uri=null, $wsdlClass=null)
  99. {
  100. $this->_reflection = new Zend_Server_Reflection();
  101. $this->setComplexTypeStrategy($strategy);
  102. if($uri !== null) {
  103. $this->setUri($uri);
  104. }
  105. if($wsdlClass !== null) {
  106. $this->setWsdlClass($wsdlClass);
  107. }
  108. }
  109. /**
  110. * Set the location at which the WSDL file will be availabe.
  111. *
  112. * @see Zend_Soap_Exception
  113. * @param Zend_Uri|string $uri
  114. * @return Zend_Soap_AutoDiscover
  115. * @throws Zend_Soap_AutoDiscover_Exception
  116. */
  117. public function setUri($uri)
  118. {
  119. if (!is_string($uri) && !($uri instanceof Zend_Uri)) {
  120. require_once "Zend/Soap/AutoDiscover/Exception.php";
  121. throw new Zend_Soap_AutoDiscover_Exception("No uri given to Zend_Soap_AutoDiscover::setUri as string or Zend_Uri instance.");
  122. }
  123. $this->_uri = $uri;
  124. // change uri in WSDL file also if existant
  125. if ($this->_wsdl instanceof Zend_Soap_Wsdl) {
  126. $this->_wsdl->setUri($uri);
  127. }
  128. return $this;
  129. }
  130. /**
  131. * Return the current Uri that the SOAP WSDL Service will be located at.
  132. *
  133. * @return Zend_Uri
  134. */
  135. public function getUri()
  136. {
  137. if($this->_uri !== null) {
  138. $uri = $this->_uri;
  139. } else {
  140. $schema = $this->getSchema();
  141. $host = $this->getHostName();
  142. $scriptName = $this->getRequestUriWithoutParameters();
  143. $uri = Zend_Uri::factory($schema . '://' . $host . $scriptName);
  144. $this->setUri($uri);
  145. }
  146. return $uri;
  147. }
  148. /**
  149. * Set the name of the WSDL handling class.
  150. *
  151. * @see Zend_Soap_Exception
  152. * @see Zend_Soap_Exception
  153. * @param string $wsdlClass
  154. * @return Zend_Soap_AutoDiscover
  155. * @throws Zend_Soap_AutoDiscover_Exception
  156. */
  157. public function setWsdlClass($wsdlClass)
  158. {
  159. if (!is_string($wsdlClass) && !is_subclass_of($wsdlClass, 'Zend_Soap_Wsdl')) {
  160. require_once "Zend/Soap/AutoDiscover/Exception.php";
  161. throw new Zend_Soap_AutoDiscover_Exception("No Zend_Soap_Wsdl subclass given to Zend_Soap_AutoDiscover::setWsdlClass as string.");
  162. }
  163. $this->_wsdlClass = $wsdlClass;
  164. return $this;
  165. }
  166. /**
  167. * Return the name of the WSDL handling class.
  168. *
  169. * @return string
  170. */
  171. public function getWsdlClass()
  172. {
  173. return $this->_wsdlClass;
  174. }
  175. /**
  176. * Set options for all the binding operations soap:body elements.
  177. *
  178. * By default the options are set to 'use' => 'encoded' and
  179. * 'encodingStyle' => "http://schemas.xmlsoap.org/soap/encoding/".
  180. *
  181. * @see Zend_Soap_AutoDiscover_Exception
  182. * @param array $operationStyle
  183. * @return Zend_Soap_AutoDiscover
  184. * @throws Zend_Soap_AutoDiscover_Exception
  185. */
  186. public function setOperationBodyStyle(array $operationStyle=array())
  187. {
  188. if(!isset($operationStyle['use'])) {
  189. require_once "Zend/Soap/AutoDiscover/Exception.php";
  190. throw new Zend_Soap_AutoDiscover_Exception("Key 'use' is required in Operation soap:body style.");
  191. }
  192. $this->_operationBodyStyle = $operationStyle;
  193. return $this;
  194. }
  195. /**
  196. * Set Binding soap:binding style.
  197. *
  198. * By default 'style' is 'rpc' and 'transport' is 'http://schemas.xmlsoap.org/soap/http'.
  199. *
  200. * @param array $bindingStyle
  201. * @return Zend_Soap_AutoDiscover
  202. */
  203. public function setBindingStyle(array $bindingStyle=array())
  204. {
  205. if(isset($bindingStyle['style'])) {
  206. $this->_bindingStyle['style'] = $bindingStyle['style'];
  207. }
  208. if(isset($bindingStyle['transport'])) {
  209. $this->_bindingStyle['transport'] = $bindingStyle['transport'];
  210. }
  211. return $this;
  212. }
  213. /**
  214. * Detect and returns the current HTTP/HTTPS Schema
  215. *
  216. * @return string
  217. */
  218. protected function getSchema()
  219. {
  220. $schema = "http";
  221. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  222. $schema = 'https';
  223. }
  224. return $schema;
  225. }
  226. /**
  227. * Detect and return the current hostname
  228. *
  229. * @return string
  230. */
  231. protected function getHostName()
  232. {
  233. if(isset($_SERVER['HTTP_HOST'])) {
  234. $host = $_SERVER['HTTP_HOST'];
  235. } else {
  236. $host = $_SERVER['SERVER_NAME'];
  237. }
  238. return $host;
  239. }
  240. /**
  241. * Detect and return the current script name without parameters
  242. *
  243. * @return string
  244. */
  245. protected function getRequestUriWithoutParameters()
  246. {
  247. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
  248. $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
  249. } elseif (isset($_SERVER['REQUEST_URI'])) {
  250. $requestUri = $_SERVER['REQUEST_URI'];
  251. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
  252. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  253. } else {
  254. $requestUri = $_SERVER['SCRIPT_NAME'];
  255. }
  256. if( ($pos = strpos($requestUri, "?")) !== false) {
  257. $requestUri = substr($requestUri, 0, $pos);
  258. }
  259. return $requestUri;
  260. }
  261. /**
  262. * Set the strategy that handles functions and classes that are added AFTER this call.
  263. *
  264. * @param boolean|string|Zend_Soap_Wsdl_Strategy_Interface $strategy
  265. * @return Zend_Soap_AutoDiscover
  266. */
  267. public function setComplexTypeStrategy($strategy)
  268. {
  269. $this->_strategy = $strategy;
  270. if($this->_wsdl instanceof Zend_Soap_Wsdl) {
  271. $this->_wsdl->setComplexTypeStrategy($strategy);
  272. }
  273. return $this;
  274. }
  275. /**
  276. * Set the Class the SOAP server will use
  277. *
  278. * @param string $class Class Name
  279. * @param string $namespace Class Namspace - Not Used
  280. * @param array $argv Arguments to instantiate the class - Not Used
  281. * @return Zend_Soap_AutoDiscover
  282. */
  283. public function setClass($class, $namespace = '', $argv = null)
  284. {
  285. $uri = $this->getUri();
  286. $wsdl = new $this->_wsdlClass($class, $uri, $this->_strategy);
  287. // The wsdl:types element must precede all other elements (WS-I Basic Profile 1.1 R2023)
  288. $wsdl->addSchemaTypeSection();
  289. $port = $wsdl->addPortType($class . 'Port');
  290. $binding = $wsdl->addBinding($class . 'Binding', 'tns:' .$class. 'Port');
  291. $wsdl->addSoapBinding($binding, $this->_bindingStyle['style'], $this->_bindingStyle['transport']);
  292. $wsdl->addService($class . 'Service', $class . 'Port', 'tns:' . $class . 'Binding', $uri);
  293. foreach ($this->_reflection->reflectClass($class)->getMethods() as $method) {
  294. $this->_addFunctionToWsdl($method, $wsdl, $port, $binding);
  295. }
  296. $this->_wsdl = $wsdl;
  297. return this;
  298. }
  299. /**
  300. * Add a Single or Multiple Functions to the WSDL
  301. *
  302. * @param string $function Function Name
  303. * @param string $namespace Function namespace - Not Used
  304. */
  305. public function addFunction($function, $namespace = '')
  306. {
  307. static $port;
  308. static $operation;
  309. static $binding;
  310. if (!is_array($function)) {
  311. $function = (array) $function;
  312. }
  313. $uri = $this->getUri();
  314. if (!($this->_wsdl instanceof Zend_Soap_Wsdl)) {
  315. $parts = explode('.', basename($_SERVER['SCRIPT_NAME']));
  316. $name = $parts[0];
  317. $wsdl = new Zend_Soap_Wsdl($name, $uri, $this->_strategy);
  318. // The wsdl:types element must precede all other elements (WS-I Basic Profile 1.1 R2023)
  319. $wsdl->addSchemaTypeSection();
  320. $port = $wsdl->addPortType($name . 'Port');
  321. $binding = $wsdl->addBinding($name . 'Binding', 'tns:' .$name. 'Port');
  322. $wsdl->addSoapBinding($binding, $this->_bindingStyle['style'], $this->_bindingStyle['transport']);
  323. $wsdl->addService($name . 'Service', $name . 'Port', 'tns:' . $name . 'Binding', $uri);
  324. } else {
  325. $wsdl = $this->_wsdl;
  326. }
  327. foreach ($function as $func) {
  328. $method = $this->_reflection->reflectFunction($func);
  329. $this->_addFunctionToWsdl($method, $wsdl, $port, $binding);
  330. }
  331. $this->_wsdl = $wsdl;
  332. }
  333. /**
  334. * Add a function to the WSDL document.
  335. *
  336. * @param $function Zend_Server_Reflection_Function_Abstract function to add
  337. * @param $wsdl Zend_Soap_Wsdl WSDL document
  338. * @param $port object wsdl:portType
  339. * @param $binding object wsdl:binding
  340. * @return void
  341. */
  342. protected function _addFunctionToWsdl($function, $wsdl, $port, $binding)
  343. {
  344. $uri = $this->getUri();
  345. // We only support one prototype: the one with the maximum number of arguments
  346. $prototype = null;
  347. $maxNumArgumentsOfPrototype = -1;
  348. foreach ($function->getPrototypes() as $tmpPrototype) {
  349. $numParams = count($tmpPrototype->getParameters());
  350. if ($numParams > $maxNumArgumentsOfPrototype) {
  351. $maxNumArgumentsOfPrototype = $numParams;
  352. $prototype = $tmpPrototype;
  353. }
  354. }
  355. if ($prototype === null) {
  356. require_once "Zend/Soap/AutoDiscover/Exception.php";
  357. throw new Zend_Soap_AutoDiscover_Exception("No prototypes could be found for the '" . $function->getName() . "' function");
  358. }
  359. // Add the input message (parameters)
  360. $args = array();
  361. if ($this->_bindingStyle['style'] == 'document') {
  362. // Document style: wrap all parameters in a sequence element
  363. $sequence = array();
  364. foreach ($prototype->getParameters() as $param) {
  365. $sequenceElement = array(
  366. 'name' => $param->getName(),
  367. 'type' => $wsdl->getType($param->getType())
  368. );
  369. if ($param->isOptional()) {
  370. $sequenceElement['nillable'] = 'true';
  371. }
  372. $sequence[] = $sequenceElement;
  373. }
  374. $element = array(
  375. 'name' => $function->getName(),
  376. 'sequence' => $sequence
  377. );
  378. // Add the wrapper element part, which must be named 'parameters'
  379. $args['parameters'] = array('element' => $wsdl->addElement($element));
  380. } else {
  381. // RPC style: add each parameter as a typed part
  382. foreach ($prototype->getParameters() as $param) {
  383. $args[$param->getName()] = array('type' => $wsdl->getType($param->getType()));
  384. }
  385. }
  386. $wsdl->addMessage($function->getName() . 'In', $args);
  387. $isOneWayMessage = false;
  388. if($prototype->getReturnType() == "void") {
  389. $isOneWayMessage = true;
  390. }
  391. if($isOneWayMessage == false) {
  392. // Add the output message (return value)
  393. $args = array();
  394. if ($this->_bindingStyle['style'] == 'document') {
  395. // Document style: wrap the return value in a sequence element
  396. $sequence = array();
  397. if ($prototype->getReturnType() != "void") {
  398. $sequence[] = array(
  399. 'name' => $function->getName() . 'Result',
  400. 'type' => $wsdl->getType($prototype->getReturnType())
  401. );
  402. }
  403. $element = array(
  404. 'name' => $function->getName() . 'Response',
  405. 'sequence' => $sequence
  406. );
  407. // Add the wrapper element part, which must be named 'parameters'
  408. $args['parameters'] = array('element' => $wsdl->addElement($element));
  409. } else if ($prototype->getReturnType() != "void") {
  410. // RPC style: add the return value as a typed part
  411. $args['return'] = array('type' => $wsdl->getType($prototype->getReturnType()));
  412. }
  413. $wsdl->addMessage($function->getName() . 'Out', $args);
  414. }
  415. // Add the portType operation
  416. if($isOneWayMessage == false) {
  417. $portOperation = $wsdl->addPortOperation($port, $function->getName(), 'tns:' . $function->getName() . 'In', 'tns:' . $function->getName() . 'Out');
  418. } else {
  419. $portOperation = $wsdl->addPortOperation($port, $function->getName(), 'tns:' . $function->getName() . 'In', false);
  420. }
  421. $desc = $function->getDescription();
  422. if (strlen($desc) > 0) {
  423. $wsdl->addDocumentation($portOperation, $desc);
  424. }
  425. // When using the RPC style, make sure the operation style includes a 'namespace' attribute (WS-I Basic Profile 1.1 R2717)
  426. if ($this->_bindingStyle['style'] == 'rpc' && !isset($this->_operationBodyStyle['namespace'])) {
  427. $this->_operationBodyStyle['namespace'] = ''.$uri;
  428. }
  429. // Add the binding operation
  430. $operation = $wsdl->addBindingOperation($binding, $function->getName(), $this->_operationBodyStyle, $this->_operationBodyStyle);
  431. $wsdl->addSoapOperation($operation, $uri . '#' .$function->getName());
  432. // Add the function name to the list
  433. $this->_functions[] = $function->getName();
  434. }
  435. /**
  436. * Action to take when an error occurs
  437. *
  438. * @param string $fault
  439. * @param string|int $code
  440. * @throws Zend_Soap_AutoDiscover_Exception
  441. */
  442. public function fault($fault = null, $code = null)
  443. {
  444. require_once "Zend/Soap/AutoDiscover/Exception.php";
  445. throw new Zend_Soap_AutoDiscover_Exception("Function has no use in AutoDiscover.");
  446. }
  447. /**
  448. * Handle the Request
  449. *
  450. * @param string $request A non-standard request - Not Used
  451. */
  452. public function handle($request = false)
  453. {
  454. if (!headers_sent()) {
  455. header('Content-Type: text/xml');
  456. }
  457. $this->_wsdl->dump();
  458. }
  459. /**
  460. * Proxy to WSDL dump function
  461. *
  462. * @param string $filename
  463. * @return boolean
  464. * @throws Zend_Soap_AutoDiscover_Exception
  465. */
  466. public function dump($filename)
  467. {
  468. if($this->_wsdl !== null) {
  469. return $this->_wsdl->dump($filename);
  470. } else {
  471. /**
  472. * @see Zend_Soap_AutoDiscover_Exception
  473. */
  474. require_once "Zend/Soap/AutoDiscover/Exception.php";
  475. throw new Zend_Soap_AutoDiscover_Exception("Cannot dump autodiscovered contents, WSDL file has not been generated yet.");
  476. }
  477. }
  478. /**
  479. * Proxy to WSDL toXml() function
  480. *
  481. * @return string
  482. * @throws Zend_Soap_AutoDiscover_Exception
  483. */
  484. public function toXml()
  485. {
  486. if($this->_wsdl !== null) {
  487. return $this->_wsdl->toXml();
  488. } else {
  489. /**
  490. * @see Zend_Soap_AutoDiscover_Exception
  491. */
  492. require_once "Zend/Soap/AutoDiscover/Exception.php";
  493. throw new Zend_Soap_AutoDiscover_Exception("Cannot return autodiscovered contents, WSDL file has not been generated yet.");
  494. }
  495. }
  496. /**
  497. * Return an array of functions in the WSDL
  498. *
  499. * @return array
  500. */
  501. public function getFunctions()
  502. {
  503. return $this->_functions;
  504. }
  505. /**
  506. * Load Functions
  507. *
  508. * @param unknown_type $definition
  509. * @throws Zend_Soap_AutoDiscover_Exception
  510. */
  511. public function loadFunctions($definition)
  512. {
  513. require_once "Zend/Soap/AutoDiscover/Exception.php";
  514. throw new Zend_Soap_AutoDiscover_Exception("Function has no use in AutoDiscover.");
  515. }
  516. /**
  517. * Set Persistance
  518. *
  519. * @param int $mode
  520. * @throws Zend_Soap_AutoDiscover_Exception
  521. */
  522. public function setPersistence($mode)
  523. {
  524. require_once "Zend/Soap/AutoDiscover/Exception.php";
  525. throw new Zend_Soap_AutoDiscover_Exception("Function has no use in AutoDiscover.");
  526. }
  527. /**
  528. * Returns an XSD Type for the given PHP type
  529. *
  530. * @param string $type PHP Type to get the XSD type for
  531. * @return string
  532. */
  533. public function getType($type)
  534. {
  535. if (!($this->_wsdl instanceof Zend_Soap_Wsdl)) {
  536. /** @todo Exception throwing may be more correct */
  537. // WSDL is not defined yet, so we can't recognize type in context of current service
  538. return '';
  539. } else {
  540. return $this->_wsdl->getType($type);
  541. }
  542. }
  543. }