]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/core/PHP5Soap.php
Release 6.4.0
[Github/sugarcrm.git] / service / core / PHP5Soap.php
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36 <?php
37 require('service/core/SugarSoapService.php');
38 require('include/nusoap/nusoap.php');
39
40 abstract class PHP5Soap extends SugarSoapService{
41         private $nusoap_server = null;
42         public function __construct($url){
43                 $this->soapURL = $url;
44                 ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
45                 global $HTTP_RAW_POST_DATA;
46                 if(!isset($HTTP_RAW_POST_DATA)) {
47                         $HTTP_RAW_POST_DATA = file_get_contents('php://input');
48                 }
49                 parent::__construct();
50         }
51
52         public function setObservers() {
53
54         } // fn
55
56         /**
57          * Serves the Soap Request
58          * @return
59          */
60         public function serve(){
61                 ob_clean();
62                 global $HTTP_RAW_POST_DATA;
63                 $GLOBALS['log']->debug("I am here1 ". $HTTP_RAW_POST_DATA);
64                 $qs = '';
65                 if (isset($_SERVER['QUERY_STRING'])) {
66                         $qs = $_SERVER['QUERY_STRING'];
67                 } elseif (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
68                         $qs = $HTTP_SERVER_VARS['QUERY_STRING'];
69                 } else {
70                         $qs = '';
71                 }
72
73                 if (stristr($qs, 'wsdl') || $HTTP_RAW_POST_DATA == ''){
74                         $wsdlCacheFile = $this->getWSDLPath(false);
75                         if (stristr($qs, 'wsdl')) {
76                             $contents = @sugar_file_get_contents($wsdlCacheFile);
77                             if($contents !== false) {
78                                         header("Content-Type: text/xml; charset=ISO-8859-1\r\n");
79                                         print $contents;
80                             } // if
81                         } else {
82                                 $this->nusoap_server->service($HTTP_RAW_POST_DATA);
83                         } // else
84                 } else {
85                         $this->server->handle();
86                 }
87                 ob_end_flush();
88                 flush();
89         }
90
91         private function generateSoapServer() {
92                 if ($this->server == null) {
93                         $soap_url = $this->getSoapURL() . "?wsdl";
94                         $this->server = new SoapServer($this->getWSDLPath(true), array('soap_version'=>SOAP_1_2, 'encoding'=>'ISO-8859-1'));
95                 }
96         } // fn
97
98         private function generateNuSoap() {
99                 if ($this->nusoap_server == null) {
100                         $this->nusoap_server = new soap_server();
101                         $this->nusoap_server->configureWSDL('sugarsoap', $this->getNameSpace(), "");
102                         $this->nusoap_server->register_class('SugarWebServiceImpl');
103                 } // if
104         } // fn
105
106         public function getWSDLPath($generateWSDL) {
107                 $wsdlURL = $this->getSoapURL().'?wsdl';
108                 $wsdlCacheFile = 'upload://wsdlcache-' . md5($wsdlURL);
109
110                 if ($generateWSDL) {
111                         $oldqs = $_SERVER['QUERY_STRING'];
112                         $_SERVER['QUERY_STRING'] = "wsdl";
113                         $this->nusoap_server->service($wsdlURL);
114                         $_SERVER['QUERY_STRING'] = $oldqs;
115                         file_put_contents($wsdlCacheFile, ob_get_contents());
116                         return $wsdlCacheFile;
117                     //ob_clean();
118                 } else {
119                         return $wsdlCacheFile;
120                 }
121
122         } // fn
123
124         public function getNameSpace() {
125                 return $this->soapURL;
126         } // fn
127
128         /**
129          * This function allows specifying what version of PHP soap to use
130          * PHP soap supports version 1.1 and 1.2.
131          * @return
132          * @param $version String[optional]
133          */
134         public function setSoapVersion($version='1.1'){
135                 //PHP SOAP supports 1.1 and 1.2 only currently
136                 $this->soap_version = ($version == '1.2')?'1.2':'1.1';
137         }
138
139         public function error($errorObject){
140                 $this->server->fault($errorObject->getFaultCode(), $errorObject->getName(), '', $errorObject->getDescription());        }
141
142         public function registerImplClass($implementationClass){
143                 if (empty($implementationClass)) {
144                         $implementationClass = $this->implementationClass;
145                 } // if
146                 $this->generateSoapServer();
147                 $this->server->setClass($implementationClass);
148                 parent::setObservers();
149         }
150
151         function registerClass($registryClass){
152                 $this->registryClass = $registryClass;
153         }
154
155         public function registerType($name, $typeClass, $phpType, $compositor, $restrictionBase, $elements, $attrs=array(), $arrayType=''){
156                 $this->nusoap_server->wsdl->addComplexType($name, $typeClass, $phpType, $compositor, $restrictionBase, $elements, $attrs, $arrayType);
157         }
158
159         function registerFunction($function, $input, $output){
160                 if(in_array($function, $this->excludeFunctions))return;
161                 if ($this->nusoap_server == null) {
162                         $this->generateNuSoap();
163                 } // if
164                 $this->nusoap_server->register($function, $input, $output, $this->getNameSpace());
165
166         }
167
168 }