]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/core/PHP5Soap.php
Release 6.1.4
[Github/sugarcrm.git] / service / core / PHP5Soap.php
1 /*********************************************************************************
2  * SugarCRM 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                             if($fh = @sugar_fopen($wsdlCacheFile, "r")) {
77                                 $contents = fread($fh, filesize($wsdlCacheFile));
78                                 fclose($fh);
79                                         header("Content-Type: text/xml; charset=ISO-8859-1\r\n");
80                                         print $contents;
81                             } // if
82                         } else {
83                                 $this->nusoap_server->service($HTTP_RAW_POST_DATA);
84                         } // else
85                 } else {
86                         $this->server->handle();
87                 }
88                 ob_end_flush();
89                 flush();
90         }
91
92         private function generateSoapServer() {
93                 if ($this->server == null) {
94                         $soap_url = $this->getSoapURL() . "?wsdl";
95                         $this->server = new SoapServer($this->getWSDLPath(true), array('soap_version'=>SOAP_1_2, 'encoding'=>'ISO-8859-1'));
96                 }
97         } // fn
98
99         private function generateNuSoap() {
100                 if ($this->nusoap_server == null) {
101                         $this->nusoap_server = new soap_server();
102                         $this->nusoap_server->configureWSDL('sugarsoap', $this->getNameSpace(), "");
103                         $this->nusoap_server->register_class('SugarWebServiceImpl');
104                 } // if
105         } // fn
106
107         public function getWSDLPath($generateWSDL) {
108                 global $sugar_config;
109                 $uploadDir = $sugar_config['upload_dir'];
110                 $wsdlURL = $this->getSoapURL().'?wsdl';
111                 $wsdlCacheFile = $uploadDir.'/wsdlcache-' . md5($wsdlURL);
112
113                 if ($generateWSDL) {
114                         $oldqs = $_SERVER['QUERY_STRING'];
115                         $_SERVER['QUERY_STRING'] = "wsdl";
116                         $this->nusoap_server->service($wsdlURL);
117                         $_SERVER['QUERY_STRING'] = $oldqs;
118                     if($fh = @sugar_fopen($wsdlCacheFile, "w")) {
119                         fputs($fh, ob_get_contents());
120                         fclose($fh);
121                     } // if
122                         return $wsdlCacheFile;
123                     //ob_clean();
124                 } else {
125                         return $wsdlCacheFile;
126                 }
127
128         } // fn
129
130         public function getNameSpace() {
131                 return $this->soapURL;
132         } // fn
133
134         /**
135          * This function allows specifying what version of PHP soap to use
136          * PHP soap supports version 1.1 and 1.2.
137          * @return
138          * @param $version String[optional]
139          */
140         public function setSoapVersion($version='1.1'){
141                 //PHP SOAP supports 1.1 and 1.2 only currently
142                 $this->soap_version = ($version == '1.2')?'1.2':'1.1';
143         }
144
145         public function error($errorObject){
146                 $this->server->fault($errorObject->getFaultCode(), $errorObject->getName(), '', $errorObject->getDescription());        }
147
148         public function registerImplClass($implementationClass){
149                 if (empty($implementationClass)) {
150                         $implementationClass = $this->implementationClass;
151                 } // if
152                 $this->generateSoapServer();
153                 $this->server->setClass($implementationClass);
154                 parent::setObservers();
155         }
156
157         function registerClass($registryClass){
158                 $this->registryClass = $registryClass;
159         }
160
161         public function registerType($name, $typeClass, $phpType, $compositor, $restrictionBase, $elements, $attrs=array(), $arrayType=''){
162                 $this->nusoap_server->wsdl->addComplexType($name, $typeClass, $phpType, $compositor, $restrictionBase, $elements, $attrs, $arrayType);
163         }
164
165         function registerFunction($function, $input, $output){
166                 if(in_array($function, $this->excludeFunctions))return;
167                 if ($this->nusoap_server == null) {
168                         $this->generateNuSoap();
169                 } // if
170                 $this->nusoap_server->register($function, $input, $output, $this->getNameSpace());
171
172         }
173
174 }