SOAP
[ class tree: SOAP ] [ index: SOAP ] [ all elements ]

Source for file Disco.php

Documentation is available at Disco.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Dmitri Vinogradov <dimitri@vinogradov.de>                    |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Disco.php,v 1.11 2005/05/26 22:26:17 yunosh Exp $
  20.  
  21. require_once 'SOAP/Base.php';
  22.  
  23. class SOAP_DISCO_Server extends SOAP_Base_Object {
  24.  
  25.     var $namespaces     = array(SCHEMA_WSDL => 'wsdl'SCHEMA_SOAP => 'soap');
  26.     var $import_ns      = array();
  27.     var $wsdl           '';
  28.     var $disco          '';
  29.     var $_wsdl          = array();
  30.     var $_disco         = array();
  31.     var $_service_name  '';
  32.     var $_service_ns    '';
  33.     var $_service_desc  '';
  34.     var $_portname      '';
  35.     var $_bindingname   '';
  36.     var $soap_server    = NULL;
  37.  
  38.  
  39.     function SOAP_DISCO_Server($soap_server$service_name$service_desc '',
  40.                                $import_ns = null)
  41.     {
  42.         parent::SOAP_Base_Object('Server');
  43.  
  44.         if !is_object($soap_server)
  45.             || !get_class($soap_server== 'soap_server'return;
  46.  
  47.         $this->_service_name $service_name;
  48.         $this->_service_ns = "urn:$service_name";
  49.         $this->_service_desc $service_desc;
  50.         $this->import_ns = isset($import_ns$import_ns $this->import_ns;
  51.         $this->soap_server $soap_server;
  52.         $this->host = isset($_SERVER['HTTP_HOST']$_SERVER['HTTP_HOST''localhost';
  53.     }
  54.  
  55.     function getDISCO()
  56.     {
  57.         $this->_generate_DISCO();
  58.         return $this->disco;
  59.     }
  60.  
  61.     function getWSDL()
  62.     {
  63.         $this->_generate_WSDL();
  64.         return $this->wsdl;
  65.     }
  66.  
  67.     function _generate_DISCO()
  68.     {
  69.         // DISCO
  70.         $this->_disco['disco:discovery']['attr']['xmlns:disco'SCHEMA_DISCO;
  71.         $this->_disco['disco:discovery']['attr']['xmlns:scl'SCHEMA_DISCO_SCL;
  72.         $this->_disco['disco:discovery']['scl:contractRef']['attr']['ref'=
  73.             (array_key_exists('HTTPS'$_SERVER&& $_SERVER['HTTPS'== 'on')
  74.             ? 'https://' $this->host $_SERVER['PHP_SELF''?wsdl'
  75.             : 'http://'  $this->host $_SERVER['PHP_SELF''?wsdl';
  76.  
  77.         // generate disco xml
  78.         $this->_generate_DISCO_XML($this->_disco);
  79.     }
  80.  
  81.     function _generate_WSDL()
  82.     {
  83.         // WSDL
  84.         if (is_array($this->soap_server->_namespaces)) {
  85.             // need to get: typens, xsd & SOAP-ENC
  86.             $flipped array_flip($this->soap_server->_namespaces);
  87.             $this->namespaces[$this->_service_ns'tns';
  88.             $this->namespaces[$flipped['xsd']] 'xsd';
  89.             $this->namespaces[$flipped['SOAP-ENC']] 'SOAP-ENC';
  90.         }
  91.  
  92.         // DEFINITIONS
  93.         $this->_wsdl['definitions']['attr']['name'$this->_service_name;
  94.         $this->_wsdl['definitions']['attr']['targetNamespace'$this->_service_ns;
  95.         foreach ($this->namespaces as $ns => $prefix{
  96.             $this->_wsdl['definitions']['attr']['xmlns:' $prefix$ns;
  97.         }
  98.         $this->_wsdl['definitions']['attr']['xmlns'SCHEMA_WSDL;
  99.  
  100.         // Import namespaces. Seems to not work yet: wsdl.exe fom .NET can't
  101.         // handle imported complete wsdl-definitions.
  102.         if (count($this->import_ns)) {
  103.             $i = 0;
  104.             foreach ($this->import_ns as $_ns => $_location{
  105.                 $this->_wsdl['definitions']['import'][$i]['attr']['location'$_location;
  106.                 $this->_wsdl['definitions']['import'][$i]['attr']['namespace'$_ns;
  107.                 $i++;
  108.             }
  109.         }
  110.         $this->_wsdl['definitions']['types']['attr']['xmlns']='http://schemas.xmlsoap.org/wsdl/';
  111.         $this->_wsdl['definitions']['types']['schema']=array();
  112.  
  113.         // Placeholder for messages
  114.         $this->_wsdl['definitions']['message'= array();
  115.  
  116.         // PORTTYPE-NAME
  117.         $this->_portname $this->_service_name 'Port';
  118.         $this->_wsdl['definitions']['portType']['attr']['name'$this->_portname;
  119.  
  120.         // BINDING-NAME
  121.         $this->_bindingname $this->_service_name 'Binding';
  122.         $this->_wsdl['definitions']['binding']['attr']['name'$this->_bindingname;
  123.         $this->_wsdl['definitions']['binding']['attr']['type''tns:' $this->_portname;
  124.         $this->_wsdl['definitions']['binding']['soap:binding']['attr']['style''rpc';
  125.         $this->_wsdl['definitions']['binding']['soap:binding']['attr']['transport'SCHEMA_SOAP_HTTP;
  126.  
  127.         // SERVICE
  128.         $this->_wsdl['definitions']['service']['attr']['name'$this->_service_name 'Service';
  129.         $this->_wsdl['definitions']['service']['documentation']['attr''';
  130.         $this->_wsdl['definitions']['service']['documentation'htmlentities($this->_service_desc);
  131.         $this->_wsdl['definitions']['service']['port']['attr']['name'$this->_portname;
  132.         $this->_wsdl['definitions']['service']['port']['attr']['binding''tns:' $this->_bindingname;
  133.         $this->_wsdl['definitions']['service']['port']['soap:address']['attr']['location'=
  134.             (array_key_exists('HTTPS'$_SERVER&& $_SERVER['HTTPS'== 'on')
  135.             ? 'https://' $this->host $_SERVER['PHP_SELF']
  136.             : 'http://'  $this->host $_SERVER['PHP_SELF'];
  137.  
  138.         //
  139.         $dispatch_keys array_keys($this->soap_server->dispatch_objects);
  140.         $dc count($dispatch_keys);
  141.         for ($di = 0; $di $dc$di++{
  142.             $namespace $dispatch_keys[$di];
  143.             $namespace_objects =$this->soap_server->dispatch_objects[$namespace];
  144.             $oc count($namespace_objects);
  145.             for ($oi = 0; $oi $oc$oi++{
  146.                 $object $namespace_objects[$oi];
  147.                 // types definitions
  148.                 $this->addSchemaFromMap($object->__typedef);
  149.                 // MESSAGES
  150.                 $this->addMethodsFromMap($object->__dispatch_map$namespaceget_class($object));
  151.             }
  152.         }
  153.         if (isset($server->dispatch_map)) {
  154.             $this->addMethodsFromMap($server->dispatch_map$namespace);
  155.         }
  156.  
  157.         // generate wsdl
  158.         $this->_generate_WSDL_XML();
  159.     }
  160.  
  161.     function &_getSchema($namespace)
  162.     {
  163.         // SCHEMA
  164.         $c count($this->_wsdl['definitions']['types']['schema']);
  165.         for($i = 0; $i $c$i++{
  166.             if ($this->_wsdl['definitions']['types']['schema'][$i]['attr']['targetNamespace'== $namespace{
  167.                 return $this->_wsdl['definitions']['types']['schema'][$i];
  168.             }
  169.         }
  170.  
  171.         // don't have this namespace
  172.         $schema = array();
  173.         $schema['attr'= array();
  174.         $schema['complexType'= array();
  175.         $schema['attr']['xmlns'array_search('xsd',$this->namespaces);
  176.         $schema['attr']['targetNamespace'$namespace;
  177.         $this->_wsdl['definitions']['types']['schema'][=$schema;
  178.  
  179.         return $schema;
  180.     }
  181.  
  182.     function addSchemaFromMap(&$map)
  183.     {
  184.         if (!$map{
  185.             return;
  186.         }
  187.  
  188.         foreach ($map as $_type_name => $_type_def{
  189.             list($typens,$type$this->_getTypeNs($_type_name);
  190.             if ($typens == 'xsd'{
  191.                 // cannot add to xsd, lets use method_namespace
  192.                 $typens 'tns';
  193.             }
  194.             $schema =$this->_getSchema(array_search($typens$this->namespaces));
  195.             if (!$this->_ifComplexTypeExists($schema['complexType']$type)) {
  196.                 $ctype =$schema['complexType'][];
  197.                 $ctype['attr']['name'$type;
  198.                 foreach ($_type_def as $_varname => $_vartype{
  199.                     if (!is_int($_varname)) {
  200.                         list($_vartypens,$_vartype$this->_getTypeNs($_vartype);
  201.                         $ctype['all']['attr''';
  202.                         $el =$ctype['all']['element'][];
  203.                         $el['attr']['name'$_varname;
  204.                         $el['attr']['type'$_vartypens ':' $_vartype;
  205.                     else {
  206.                         $ctype['complexContent']['attr''';
  207.                         $ctype['complexContent']['restriction']['attr']['base''SOAP-ENC:Array';
  208.                         foreach ($_vartype as $array_var => $array_type{
  209.                             list($_vartypens$_vartype$this->_getTypeNs($array_type);
  210.                             $ctype['complexContent']['restriction']['attribute']['attr']['ref''SOAP-ENC:arrayType';
  211.                             $ctype['complexContent']['restriction']['attribute']['attr']['wsdl:arrayType'$_vartypens ':' $_vartype '[]';
  212.                         }
  213.                     }
  214.                 }
  215.             }
  216.         }
  217.     }
  218.  
  219.     function addMethodsFromMap(&$map$namespace$classname = null)
  220.     {
  221.         if (!$map{
  222.             return;
  223.         }
  224.  
  225.         foreach ($map as $method_name => $method_types{
  226.             if (array_key_exists('namespace'$method_types)) {
  227.                 $method_namespace $method_types['namespace'];
  228.             else {
  229.                 $method_namespace $namespace;
  230.             }
  231.             // INPUT
  232.             if (isset($method_types['in']&& is_array($method_types['in'])) {
  233.                 $input_message =$this->_wsdl['definitions']['message'][];
  234.                 $input_message['attr']['name'$method_name 'Request';
  235.                 foreach ($method_types['in'as $name => $type{
  236.                     list($typens$type$this->_getTypeNs($type);
  237.                     $part =$input_message['part'][];
  238.                     $part['attr']['name'$name;
  239.                     $part['attr']['type'$typens ':' $type;
  240.                 }
  241.             }
  242.  
  243.             // OUTPUT
  244.             if (isset($method_types['out']&& is_array($method_types['out'])) {
  245.                 $output_message =$this->_wsdl['definitions']['message'][];
  246.                 $output_message['attr']['name'$method_name 'Response';
  247.                 foreach ($method_types['out'as $name => $type{
  248.                     list($typens$type$this->_getTypeNs($type);
  249.                     $part =$output_message['part'][];
  250.                     $part['attr']['name'$name;
  251.                     $part['attr']['type'$typens ':' $type;
  252.                 }
  253.             }
  254.  
  255.             // PORTTYPES
  256.             $operation =$this->_wsdl['definitions']['portType']['operation'][];
  257.             $operation['attr']['name'$method_name;
  258.  
  259.             // INPUT
  260.             $operation['input']['attr']['message''tns:'
  261.                             . $input_message['attr']['name'];
  262.  
  263.             // OUTPUT
  264.             $operation['output']['attr']['message''tns:'
  265.                             . $output_message['attr']['name'];
  266.  
  267.             // BINDING
  268.             $binding =$this->_wsdl['definitions']['binding']['operation'][];
  269.             $binding['attr']['name'$method_name;
  270.             $action $method_namespace '#' ($classname $classname '#' ''$method_name;
  271.             $binding['soap:operation']['attr']['soapAction'$action;
  272.  
  273.             // INPUT
  274.             $binding['input']['attr''';
  275.             $binding['input']['soap:body']['attr']['use''encoded';
  276.             $binding['input']['soap:body']['attr']['namespace'$method_namespace;
  277.             $binding['input']['soap:body']['attr']['encodingStyle'SOAP_SCHEMA_ENCODING;
  278.  
  279.             // OUTPUT
  280.             $binding['output']['attr''';
  281.             $binding['output']['soap:body']['attr']['use''encoded';
  282.             $binding['output']['soap:body']['attr']['namespace'$method_namespace;
  283.             $binding['output']['soap:body']['attr']['encodingStyle'SOAP_SCHEMA_ENCODING;
  284.         }
  285.     }
  286.  
  287.     function _generate_DISCO_XML($disco_array)
  288.     {
  289.         $disco '<?xml version="1.0"?>';
  290.         foreach ($disco_array as $key => $val{
  291.             $disco .= $this->_arrayToNode($key,$val);
  292.         }
  293.         $this->disco $disco;
  294.     }
  295.  
  296.     function _generate_WSDL_XML()
  297.     {
  298.         $wsdl '<?xml version="1.0"?>';
  299.         foreach ($this->_wsdl as $key => $val{
  300.             $wsdl .= $this->_arrayToNode($key$val);
  301.         }
  302.         $this->wsdl $wsdl;
  303.     }
  304.  
  305.     function _arrayToNode($node_name ''$array)
  306.     {
  307.         $return '';
  308.         if (is_array($array)) {
  309.             // we have a node if there's key 'attr'
  310.             if (array_key_exists('attr',$array)) {
  311.                 $return .= "<$node_name";
  312.                 if (is_array($array['attr'])) {
  313.                     foreach ($array['attr'as $attr_name => $attr_value{
  314.                         $return .= " $attr_name=\"$attr_value\"";
  315.                     }
  316.                 }
  317.  
  318.                 // unset 'attr' and proceed other childs...
  319.                 unset($array['attr']);
  320.  
  321.                 if (count($array> 0{
  322.                     $i = 0;
  323.                     foreach ($array as $child_node_name => $child_node_value{
  324.                         $return .= $i == 0 ? ">\n" '';
  325.                         $return .= $this->_arrayToNode($child_node_name,$child_node_value);
  326.                         $i++;
  327.                     }
  328.                     $return .= "</$node_name>\n";
  329.                 else {
  330.                     $return .= " />\n";
  331.                 }
  332.             else {
  333.                 // we have no 'attr' key in array - so it's list of nodes with
  334.                 // the same name ...
  335.                 foreach ($array as $child_node_name => $child_node_value{
  336.                     $return .= $this->_arrayToNode($node_name,$child_node_value);
  337.                 }
  338.             }
  339.         else {
  340.             // $array is not an array
  341.             if ($array !=''{
  342.                 // and its not empty
  343.                 $return .= "<$node_name>$array</$node_name>\n";
  344.             else {
  345.                 // and its empty...
  346.                 $return .= "<$node_name />\n";
  347.             }
  348.         }
  349.         return $return;
  350.     }
  351.  
  352.     function _getTypeNs($type)
  353.     {
  354.         preg_match_all("'\{(.*)\}'sm"$type$m);
  355.         if (isset($m[1][0]&& $m[1][0!= ''{
  356.             if (!array_key_exists($m[1][0],$this->namespaces)) {
  357.                 $ns_pref 'ns' count($this->namespaces);
  358.                 $this->namespaces[$m[1][0]] $ns_pref;
  359.                 $this->_wsdl['definitions']['attr']['xmlns:' $ns_pref$m[1][0];
  360.             }
  361.             $typens $this->namespaces[$m[1][0]];
  362.             $type ereg_replace($m[0][0],'',$type);
  363.         else {
  364.             $typens 'xsd';
  365.         }
  366.         return array($typens,$type);
  367.     }
  368.  
  369.     function _ifComplexTypeExists($typesArray$type_name)
  370.     {
  371.         if (is_array($typesArray)) {
  372.             foreach ($typesArray as $index => $type_data{
  373.                 if ($typesArray[$index]['attr']['name'== $type_name{
  374.                     return true;
  375.                 }
  376.             }
  377.         }
  378.         return false;
  379.     }
  380. }

Documentation generated on Mon, 11 Mar 2019 14:20:03 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.