AutoDiscover.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. * @throws Zend_Soap_AutoDiscover_Exception
  114. * @param Zend_Uri|string $uri
  115. * @return Zend_Soap_AutoDiscover
  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. * @throws Zend_Soap_AutoDiscover_Exception
  154. * @param string $wsdlClass
  155. * @return Zend_Soap_AutoDiscover
  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. */
  185. public function setOperationBodyStyle(array $operationStyle=array())
  186. {
  187. if(!isset($operationStyle['use'])) {
  188. require_once "Zend/Soap/AutoDiscover/Exception.php";
  189. throw new Zend_Soap_AutoDiscover_Exception("Key 'use' is required in Operation soap:body style.");
  190. }
  191. $this->_operationBodyStyle = $operationStyle;
  192. return $this;
  193. }
  194. /**
  195. * Set Binding soap:binding style.
  196. *
  197. * By default 'style' is 'rpc' and 'transport' is 'http://schemas.xmlsoap.org/soap/http'.
  198. *
  199. * @param array $bindingStyle
  200. * @return Zend_Soap_AutoDiscover
  201. */
  202. public function setBindingStyle(array $bindingStyle=array())
  203. {
  204. if(isset($bindingStyle['style'])) {
  205. $this->_bindingStyle['style'] = $bindingStyle['style'];
  206. }
  207. if(isset($bindingStyle['transport'])) {
  208. $this->_bindingStyle['transport'] = $bindingStyle['transport'];
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Detect and returns the current HTTP/HTTPS Schema
  214. *
  215. * @return string
  216. */
  217. protected function getSchema()
  218. {
  219. $schema = "http";
  220. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  221. $schema = 'https';
  222. }
  223. return $schema;
  224. }
  225. /**
  226. * Detect and return the current hostname
  227. *
  228. * @return string
  229. */
  230. protected function getHostName()
  231. {
  232. if(isset($_SERVER['HTTP_HOST'])) {
  233. $host = $_SERVER['HTTP_HOST'];
  234. } else {
  235. $host = $_SERVER['SERVER_NAME'];
  236. }
  237. return $host;
  238. }
  239. /**
  240. * Detect and return the current script name without parameters
  241. *
  242. * @return string
  243. */
  244. protected function getRequestUriWithoutParameters()
  245. {
  246. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
  247. $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
  248. } elseif (isset($_SERVER['REQUEST_URI'])) {
  249. $requestUri = $_SERVER['REQUEST_URI'];
  250. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
  251. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  252. } else {
  253. $requestUri = $_SERVER['SCRIPT_NAME'];
  254. }
  255. if( ($pos = strpos($requestUri, "?")) !== false) {
  256. $requestUri = substr($requestUri, 0, $pos);
  257. }
  258. return $requestUri;
  259. }
  260. /**
  261. * Set the strategy that handles functions and classes that are added AFTER this call.
  262. *
  263. * @param boolean|string|Zend_Soap_Wsdl_Strategy_Interface $strategy
  264. * @return Zend_Soap_AutoDiscover
  265. */
  266. public function setComplexTypeStrategy($strategy)
  267. {
  268. $this->_strategy = $strategy;
  269. if($this->_wsdl instanceof Zend_Soap_Wsdl) {
  270. $this->_wsdl->setComplexTypeStrategy($strategy);
  271. }
  272. return $this;
  273. }
  274. /**
  275. * Set the Class the SOAP server will use
  276. *
  277. * @param string $class Class Name
  278. * @param string $namespace Class Namspace - Not Used
  279. * @param array $argv Arguments to instantiate the class - Not Used
  280. */
  281. public function setClass($class, $namespace = '', $argv = null)
  282. {
  283. $uri = $this->getUri();
  284. $wsdl = new $this->_wsdlClass($class, $uri, $this->_strategy);
  285. // The wsdl:types element must precede all other elements (WS-I Basic Profile 1.1 R2023)
  286. $wsdl->addSchemaTypeSection();
  287. $port = $wsdl->addPortType($class . 'Port');
  288. $binding = $wsdl->addBinding($class . 'Binding', 'tns:' .$class. 'Port');
  289. $wsdl->addSoapBinding($binding, $this->_bindingStyle['style'], $this->_bindingStyle['transport']);
  290. $wsdl->addService($class . 'Service', $class . 'Port', 'tns:' . $class . 'Binding', $uri);
  291. foreach ($this->_reflection->reflectClass($class)->getMethods() as $method) {
  292. $this->_addFunctionToWsdl($method, $wsdl, $port, $binding);
  293. }
  294. $this->_wsdl = $wsdl;
  295. }
  296. /**
  297. * Add a Single or Multiple Functions to the WSDL
  298. *
  299. * @param string $function Function Name
  300. * @param string $namespace Function namespace - Not Used
  301. */
  302. public function addFunction($function, $namespace = '')
  303. {
  304. static $port;
  305. static $operation;
  306. static $binding;
  307. if (!is_array($function)) {
  308. $function = (array) $function;
  309. }
  310. $uri = $this->getUri();
  311. if (!($this->_wsdl instanceof Zend_Soap_Wsdl)) {
  312. $parts = explode('.', basename($_SERVER['SCRIPT_NAME']));
  313. $name = $parts[0];
  314. $wsdl = new Zend_Soap_Wsdl($name, $uri, $this->_strategy);
  315. // The wsdl:types element must precede all other elements (WS-I Basic Profile 1.1 R2023)
  316. $wsdl->addSchemaTypeSection();
  317. $port = $wsdl->addPortType($name . 'Port');
  318. $binding = $wsdl->addBinding($name . 'Binding', 'tns:' .$name. 'Port');
  319. $wsdl->addSoapBinding($binding, $this->_bindingStyle['style'], $this->_bindingStyle['transport']);
  320. $wsdl->addService($name . 'Service', $name . 'Port', 'tns:' . $name . 'Binding', $uri);
  321. } else {
  322. $wsdl = $this->_wsdl;
  323. }
  324. foreach ($function as $func) {
  325. $method = $this->_reflection->reflectFunction($func);
  326. $this->_addFunctionToWsdl($method, $wsdl, $port, $binding);
  327. }
  328. $this->_wsdl = $wsdl;
  329. }
  330. /**
  331. * Add a function to the WSDL document.
  332. *
  333. * @param $function Zend_Server_Reflection_Function_Abstract function to add
  334. * @param $wsdl Zend_Soap_Wsdl WSDL document
  335. * @param $port object wsdl:portType
  336. * @param $binding object wsdl:binding
  337. * @return void
  338. */
  339. protected function _addFunctionToWsdl($function, $wsdl, $port, $binding)
  340. {
  341. $uri = $this->getUri();
  342. // We only support one prototype: the one with the maximum number of arguments
  343. $prototype = null;
  344. $maxNumArgumentsOfPrototype = -1;
  345. foreach ($function->getPrototypes() as $tmpPrototype) {
  346. $numParams = count($tmpPrototype->getParameters());
  347. if ($numParams > $maxNumArgumentsOfPrototype) {
  348. $maxNumArgumentsOfPrototype = $numParams;
  349. $prototype = $tmpPrototype;
  350. }
  351. }
  352. if ($prototype === null) {
  353. require_once "Zend/Soap/AutoDiscover/Exception.php";
  354. throw new Zend_Soap_AutoDiscover_Exception("No prototypes could be found for the '" . $function->getName() . "' function");
  355. }
  356. // Add the input message (parameters)
  357. $args = array();
  358. if ($this->_bindingStyle['style'] == 'document') {
  359. // Document style: wrap all parameters in a sequence element
  360. $sequence = array();
  361. foreach ($prototype->getParameters() as $param) {
  362. $sequenceElement = array(
  363. 'name' => $param->getName(),
  364. 'type' => $wsdl->getType($param->getType())
  365. );
  366. if ($param->isOptional()) {
  367. $sequenceElement['nillable'] = 'true';
  368. }
  369. $sequence[] = $sequenceElement;
  370. }
  371. $element = array(
  372. 'name' => $function->getName(),
  373. 'sequence' => $sequence
  374. );
  375. // Add the wrapper element part, which must be named 'parameters'
  376. $args['parameters'] = array('element' => $wsdl->addElement($element));
  377. } else {
  378. // RPC style: add each parameter as a typed part
  379. foreach ($prototype->getParameters() as $param) {
  380. $args[$param->getName()] = array('type' => $wsdl->getType($param->getType()));
  381. }
  382. }
  383. $wsdl->addMessage($function->getName() . 'In', $args);
  384. $isOneWayMessage = false;
  385. if($prototype->getReturnType() == "void") {
  386. $isOneWayMessage = true;
  387. }
  388. if($isOneWayMessage == false) {
  389. // Add the output message (return value)
  390. $args = array();
  391. if ($this->_bindingStyle['style'] == 'document') {
  392. // Document style: wrap the return value in a sequence element
  393. $sequence = array();
  394. if ($prototype->getReturnType() != "void") {
  395. $sequence[] = array(
  396. 'name' => $function->getName() . 'Result',
  397. 'type' => $wsdl->getType($prototype->getReturnType())
  398. );
  399. }
  400. $element = array(
  401. 'name' => $function->getName() . 'Response',
  402. 'sequence' => $sequence
  403. );
  404. // Add the wrapper element part, which must be named 'parameters'
  405. $args['parameters'] = array('element' => $wsdl->addElement($element));
  406. } else if ($prototype->getReturnType() != "void") {
  407. // RPC style: add the return value as a typed part
  408. $args['return'] = array('type' => $wsdl->getType($prototype->getReturnType()));
  409. }
  410. $wsdl->addMessage($function->getName() . 'Out', $args);
  411. }
  412. // Add the portType operation
  413. if($isOneWayMessage == false) {
  414. $portOperation = $wsdl->addPortOperation($port, $function->getName(), 'tns:' . $function->getName() . 'In', 'tns:' . $function->getName() . 'Out');
  415. } else {
  416. $portOperation = $wsdl->addPortOperation($port, $function->getName(), 'tns:' . $function->getName() . 'In', false);
  417. }
  418. $desc = $function->getDescription();
  419. if (strlen($desc) > 0) {
  420. $wsdl->addDocumentation($portOperation, $desc);
  421. }
  422. // When using the RPC style, make sure the operation style includes a 'namespace' attribute (WS-I Basic Profile 1.1 R2717)
  423. if ($this->_bindingStyle['style'] == 'rpc' && !isset($this->_operationBodyStyle['namespace'])) {
  424. $this->_operationBodyStyle['namespace'] = ''.$uri;
  425. }
  426. // Add the binding operation
  427. $operation = $wsdl->addBindingOperation($binding, $function->getName(), $this->_operationBodyStyle, $this->_operationBodyStyle);
  428. $wsdl->addSoapOperation($operation, $uri . '#' .$function->getName());
  429. // Add the function name to the list
  430. $this->_functions[] = $function->getName();
  431. }
  432. /**
  433. * Action to take when an error occurs
  434. *
  435. * @param string $fault
  436. * @param string|int $code
  437. */
  438. public function fault($fault = null, $code = null)
  439. {
  440. require_once "Zend/Soap/AutoDiscover/Exception.php";
  441. throw new Zend_Soap_AutoDiscover_Exception("Function has no use in AutoDiscover.");
  442. }
  443. /**
  444. * Handle the Request
  445. *
  446. * @param string $request A non-standard request - Not Used
  447. */
  448. public function handle($request = false)
  449. {
  450. if (!headers_sent()) {
  451. header('Content-Type: text/xml');
  452. }
  453. $this->_wsdl->dump();
  454. }
  455. /**
  456. * Proxy to WSDL dump function
  457. *
  458. * @param string $filename
  459. */
  460. public function dump($filename)
  461. {
  462. if($this->_wsdl !== null) {
  463. return $this->_wsdl->dump($filename);
  464. } else {
  465. /**
  466. * @see Zend_Soap_AutoDiscover_Exception
  467. */
  468. require_once "Zend/Soap/AutoDiscover/Exception.php";
  469. throw new Zend_Soap_AutoDiscover_Exception("Cannot dump autodiscovered contents, WSDL file has not been generated yet.");
  470. }
  471. }
  472. /**
  473. * Proxy to WSDL toXml() function
  474. */
  475. public function toXml()
  476. {
  477. if($this->_wsdl !== null) {
  478. return $this->_wsdl->toXml();
  479. } else {
  480. /**
  481. * @see Zend_Soap_AutoDiscover_Exception
  482. */
  483. require_once "Zend/Soap/AutoDiscover/Exception.php";
  484. throw new Zend_Soap_AutoDiscover_Exception("Cannot return autodiscovered contents, WSDL file has not been generated yet.");
  485. }
  486. }
  487. /**
  488. * Return an array of functions in the WSDL
  489. *
  490. * @return array
  491. */
  492. public function getFunctions()
  493. {
  494. return $this->_functions;
  495. }
  496. /**
  497. * Load Functions
  498. *
  499. * @param unknown_type $definition
  500. */
  501. public function loadFunctions($definition)
  502. {
  503. require_once "Zend/Soap/AutoDiscover/Exception.php";
  504. throw new Zend_Soap_AutoDiscover_Exception("Function has no use in AutoDiscover.");
  505. }
  506. /**
  507. * Set Persistance
  508. *
  509. * @param int $mode
  510. */
  511. public function setPersistence($mode)
  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. * Returns an XSD Type for the given PHP type
  518. *
  519. * @param string $type PHP Type to get the XSD type for
  520. * @return string
  521. */
  522. public function getType($type)
  523. {
  524. if (!($this->_wsdl instanceof Zend_Soap_Wsdl)) {
  525. /** @todo Exception throwing may be more correct */
  526. // WSDL is not defined yet, so we can't recognize type in context of current service
  527. return '';
  528. } else {
  529. return $this->_wsdl->getType($type);
  530. }
  531. }
  532. }