AutoDiscover.php 17 KB

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