]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/core/SugarRestService.php
Release 6.1.4
[Github/sugarcrm.git] / service / core / SugarRestService.php
1 <?php
2  if(!defined('sugarEntry'))define('sugarEntry', true);
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 require_once('service/core/SugarWebService.php');
40 require_once('service/core/SugarRestServiceImpl.php');
41
42 /**
43  * Base class for rest services
44  *
45  */
46 class SugarRestService extends SugarWebService{
47         protected $implementationClass = 'SugarRestServiceImpl';
48         protected $restURL = "";
49         protected $registeredFunc = array();
50         
51         /**
52          * Constructor.
53          *
54          * @param String $url - REST url
55          */
56         function __construct($url){
57                 $GLOBALS['log']->info('Begin: SugarRestService->__construct');
58                 $this->restURL = $url;
59                 $responseTypeString = 'SugarRest';
60                 if(!empty($_REQUEST['response_type'])) {
61                         $responseTypeString = clean_string($_REQUEST['response_type'], 'ALPHANUM');
62                         if (strcasecmp($responseTypeString, 'JSON') === 0) {
63                                 $responseTypeString = 'SugarRest'. 'JSON';
64                         } elseif (strcasecmp($responseTypeString, 'RSS') === 0) {
65                                 $responseTypeString = 'SugarRest'. 'RSS';
66                         } elseif(strcasecmp($responseTypeString, 'Serialize') === 0) {
67                                 $responseTypeString = 'SugarRest'. 'Serialize';
68                         }
69                 } // if
70                 $this->responseClass = $responseTypeString;
71                 //$this->responseClass = (!empty($_REQUEST['response_type']))?'SugarRest'.clean_string($_REQUEST['response_type'], 'ALPHANUM'): 'SugarRest';
72                 
73                 if(!file_exists('service/core/REST/' . $this->responseClass. '.php'))$this->responseClass = 'SugarRest';
74                 $this->serverClass = (!empty($_REQUEST['input_type']))?'SugarRest'.clean_string($_REQUEST['input_type'], 'ALPHANUM'): 'SugarRest';
75                 $GLOBALS['log']->info('SugarRestService->__construct serverclass = ' . $this->serverClass);
76                 if(!file_exists('service/core/REST/' . $this->serverClass . '.php'))$this->serverClass = 'SugarRest';
77                 require_once('service/core/REST/'. $this->serverClass . '.php');
78                 $GLOBALS['log']->info('End: SugarRestService->__construct');
79         } // ctor
80         
81         /**
82          * Its a no op method
83          *
84          * @access public
85          */
86         public function registerType($name, $typeClass, $phpType, $compositor, $restrictionBase, $elements, $attrs, $arrayType){
87         } // fn
88         
89         /**
90          * This method registers all the functions you want to expose as services with REST
91          *
92          * @param String $function - name of the function
93          * @param Array $input - assoc array of input values: key = param name, value = param type
94          * @param Array $output - assoc array of output values: key = param name, value = param type
95          * @access public
96          */
97         function registerFunction($function, $input, $output){
98                 if(in_array($function, $this->excludeFunctions))return;
99                 $this->registeredFunc[$function] = array('input'=> $input, 'output'=>$output);
100         } // fn
101         
102         /**
103          * It passes request data to REST server and sends response back to client
104          * @access public
105          */     
106         function serve(){
107                 $GLOBALS['log']->info('Begin: SugarRestService->serve');
108                 require_once('service/core/REST/'. $this->responseClass . '.php');
109                 $response  = $this->responseClass;
110                 
111                 $responseServer = new $response($this->implementation);
112                 $this->server->faultServer = $responseServer;
113                 $this->responseServer->faultServer = $responseServer;
114                 $responseServer->generateResponse($this->server->serve());
115                 $GLOBALS['log']->info('End: SugarRestService->serve');
116         } // fn
117         
118         /**
119          * Enter description here...
120          *
121          * @param Array $excludeFunctions - All the functions you don't want to register
122          */
123         function register($excludeFunctions = array()){
124                 
125         } // fn
126         
127         /**
128          * This mehtod returns registered implementation class
129          *
130          * @return String - implementationClass
131          * @access public
132          */
133         public function getRegisteredImplClass() {
134                 return $this->implementationClass;      
135         } // fn
136                 
137         /**
138          * This mehtod returns registry class
139          *
140          * @return String - registryClass
141          * @access public
142          */
143         public function getRegisteredClass() {
144                 return $this->registryClass;    
145         } // fn
146         
147         /**
148          * Sets the name of the registry class
149          *
150          * @param String $registryClass
151          * @access public
152          */
153         function registerClass($registryClass){
154                 $this->registryClass = $registryClass;
155         }
156         
157         /**
158          * This function registers implementation class name and creates an instance of rest implementation class
159          * it will be made on this class object
160          *
161          * @param String $implementationClass
162          * @access public
163          */
164         function registerImplClass($className){
165                 $GLOBALS['log']->info('Begin: SugarRestService->registerImplClass');
166                 $this->implementationClass = $className;
167                 $this->implementation = new $this->implementationClass();
168                 $this->server = new $this->serverClass($this->implementation);
169                 $this->server->registerd = $this->registeredFunc;               
170                 $GLOBALS['log']->info('End: SugarRestService->registerImplClass');
171         } // fn
172                 
173         /**
174          * This function sets the fault object on the REST
175          *
176          * @param SoapError $errorObject - This is an object of type SoapError
177          * @access public
178          */
179         function error($errorObject){
180                 $GLOBALS['log']->info('Begin: SugarRestService->error');
181                 $this->server->fault($errorObject);
182                 $GLOBALS['log']->info('Begin: SugarRestService->error');
183         } // fn
184         
185         /**
186          * This mehtod returns server
187          *
188          * @return String - server
189          * @access public
190          */
191         function getServer(){
192                 return $this->server;
193         } // fn
194         
195         
196 }