]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/core/SugarRestService.php
Release 6.4.0
[Github/sugarcrm.git] / service / core / SugarRestService.php
1 <?php
2  if(!defined('sugarEntry'))define('sugarEntry', true);
3 /*********************************************************************************
4  * SugarCRM Community Edition 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          * Get Sugar REST class name for input/return type
53          *
54          * @param string $name
55          * @return string
56          */
57         protected function _getTypeName($name)
58         {
59                 if(empty($name)) return 'SugarRest';
60
61                 $name = clean_string($name, 'ALPHANUM');
62                 $type = '';
63                 switch(strtolower($name)) {
64                         case 'json':
65                                 $type = 'JSON';
66                                 break;
67                         case 'rss':
68                                 $type = 'RSS';
69                                 break;
70                         case 'serialize':
71                                 $type = 'Serialize';
72                                 break;
73                 }
74                 $classname = "SugarRest$type";
75                 if(!file_exists('service/core/REST/' . $classname . '.php')) {
76                         return 'SugarRest';
77                 }
78                 return $classname;
79         }
80
81         /**
82          * Constructor.
83          *
84          * @param String $url - REST url
85          */
86         function __construct($url){
87                 $GLOBALS['log']->info('Begin: SugarRestService->__construct');
88                 $this->restURL = $url;
89
90                 $this->responseClass = $this->_getTypeName(@$_REQUEST['response_type']);
91                 $this->serverClass = $this->_getTypeName(@$_REQUEST['input_type']);
92                 $GLOBALS['log']->info('SugarRestService->__construct serverclass = ' . $this->serverClass);
93                 require_once('service/core/REST/'. $this->serverClass . '.php');
94                 $GLOBALS['log']->info('End: SugarRestService->__construct');
95         } // ctor
96
97         /**
98          * Its a no op method
99          *
100          * @access public
101          */
102         public function registerType($name, $typeClass, $phpType, $compositor, $restrictionBase, $elements, $attrs, $arrayType){
103         } // fn
104
105         /**
106          * This method registers all the functions you want to expose as services with REST
107          *
108          * @param String $function - name of the function
109          * @param Array $input - assoc array of input values: key = param name, value = param type
110          * @param Array $output - assoc array of output values: key = param name, value = param type
111          * @access public
112          */
113         function registerFunction($function, $input, $output){
114                 if(in_array($function, $this->excludeFunctions))return;
115                 $this->registeredFunc[$function] = array('input'=> $input, 'output'=>$output);
116         } // fn
117
118         /**
119          * It passes request data to REST server and sends response back to client
120          * @access public
121          */
122         function serve(){
123                 $GLOBALS['log']->info('Begin: SugarRestService->serve');
124                 require_once('service/core/REST/'. $this->responseClass . '.php');
125                 $response  = $this->responseClass;
126
127                 $responseServer = new $response($this->implementation);
128                 $this->server->faultServer = $responseServer;
129                 $this->responseServer->faultServer = $responseServer;
130                 $responseServer->generateResponse($this->server->serve());
131                 $GLOBALS['log']->info('End: SugarRestService->serve');
132         } // fn
133
134         /**
135          * Enter description here...
136          *
137          * @param Array $excludeFunctions - All the functions you don't want to register
138          */
139         function register($excludeFunctions = array()){
140
141         } // fn
142
143         /**
144          * This mehtod returns registered implementation class
145          *
146          * @return String - implementationClass
147          * @access public
148          */
149         public function getRegisteredImplClass() {
150                 return $this->implementationClass;
151         } // fn
152
153         /**
154          * This mehtod returns registry class
155          *
156          * @return String - registryClass
157          * @access public
158          */
159         public function getRegisteredClass() {
160                 return $this->registryClass;
161         } // fn
162
163         /**
164          * Sets the name of the registry class
165          *
166          * @param String $registryClass
167          * @access public
168          */
169         function registerClass($registryClass){
170                 $this->registryClass = $registryClass;
171         }
172
173         /**
174          * This function registers implementation class name and creates an instance of rest implementation class
175          * it will be made on this class object
176          *
177          * @param String $implementationClass
178          * @access public
179          */
180         function registerImplClass($className){
181                 $GLOBALS['log']->info('Begin: SugarRestService->registerImplClass');
182                 $this->implementationClass = $className;
183                 $this->implementation = new $this->implementationClass();
184                 $this->server = new $this->serverClass($this->implementation);
185                 $this->server->registerd = $this->registeredFunc;
186                 $GLOBALS['log']->info('End: SugarRestService->registerImplClass');
187         } // fn
188
189         /**
190          * This function sets the fault object on the REST
191          *
192          * @param SoapError $errorObject - This is an object of type SoapError
193          * @access public
194          */
195         function error($errorObject){
196                 $GLOBALS['log']->info('Begin: SugarRestService->error');
197                 $this->server->fault($errorObject);
198                 $GLOBALS['log']->info('End: SugarRestService->error');
199         } // fn
200
201         /**
202          * This mehtod returns server
203          *
204          * @return String - server
205          * @access public
206          */
207         function getServer(){
208                 return $this->server;
209         } // fn
210
211
212 }