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.
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.
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
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
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.
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.
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 ********************************************************************************/
39 require_once('service/core/SugarWebService.php');
40 require_once('service/core/SugarRestServiceImpl.php');
43 * Base class for rest services
46 class SugarRestService extends SugarWebService{
47 protected $implementationClass = 'SugarRestServiceImpl';
48 protected $restURL = "";
49 protected $registeredFunc = array();
52 * Get Sugar REST class name for input/return type
57 protected function _getTypeName($name)
59 if(empty($name)) return 'SugarRest';
61 $name = clean_string($name, 'ALPHANUM');
63 switch(strtolower($name)) {
74 $classname = "SugarRest$type";
75 if(!file_exists('service/core/REST/' . $classname . '.php')) {
84 * @param String $url - REST url
86 function __construct($url){
87 $GLOBALS['log']->info('Begin: SugarRestService->__construct');
88 $this->restURL = $url;
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');
102 public function registerType($name, $typeClass, $phpType, $compositor, $restrictionBase, $elements, $attrs, $arrayType){
106 * This method registers all the functions you want to expose as services with REST
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
113 function registerFunction($function, $input, $output){
114 if(in_array($function, $this->excludeFunctions))return;
115 $this->registeredFunc[$function] = array('input'=> $input, 'output'=>$output);
119 * It passes request data to REST server and sends response back to client
123 $GLOBALS['log']->info('Begin: SugarRestService->serve');
124 require_once('service/core/REST/'. $this->responseClass . '.php');
125 $response = $this->responseClass;
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');
135 * Enter description here...
137 * @param Array $excludeFunctions - All the functions you don't want to register
139 function register($excludeFunctions = array()){
144 * This mehtod returns registered implementation class
146 * @return String - implementationClass
149 public function getRegisteredImplClass() {
150 return $this->implementationClass;
154 * This mehtod returns registry class
156 * @return String - registryClass
159 public function getRegisteredClass() {
160 return $this->registryClass;
164 * Sets the name of the registry class
166 * @param String $registryClass
169 function registerClass($registryClass){
170 $this->registryClass = $registryClass;
174 * This function registers implementation class name and creates an instance of rest implementation class
175 * it will be made on this class object
177 * @param String $implementationClass
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');
190 * This function sets the fault object on the REST
192 * @param SoapError $errorObject - This is an object of type SoapError
195 function error($errorObject){
196 $GLOBALS['log']->info('Begin: SugarRestService->error');
197 $this->server->fault($errorObject);
198 $GLOBALS['log']->info('Begin: SugarRestService->error');
202 * This mehtod returns server
204 * @return String - server
207 function getServer(){
208 return $this->server;