[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/Soap/ -> AutoDiscover.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1