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.
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();
54 * @param String $url - REST url
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';
70 $this->responseClass = $responseTypeString;
71 //$this->responseClass = (!empty($_REQUEST['response_type']))?'SugarRest'.clean_string($_REQUEST['response_type'], 'ALPHANUM'): 'SugarRest';
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');
86 public function registerType($name, $typeClass, $phpType, $compositor, $restrictionBase, $elements, $attrs, $arrayType){
90 * This method registers all the functions you want to expose as services with REST
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
97 function registerFunction($function, $input, $output){
98 if(in_array($function, $this->excludeFunctions))return;
99 $this->registeredFunc[$function] = array('input'=> $input, 'output'=>$output);
103 * It passes request data to REST server and sends response back to client
107 $GLOBALS['log']->info('Begin: SugarRestService->serve');
108 require_once('service/core/REST/'. $this->responseClass . '.php');
109 $response = $this->responseClass;
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');
119 * Enter description here...
121 * @param Array $excludeFunctions - All the functions you don't want to register
123 function register($excludeFunctions = array()){
128 * This mehtod returns registered implementation class
130 * @return String - implementationClass
133 public function getRegisteredImplClass() {
134 return $this->implementationClass;
138 * This mehtod returns registry class
140 * @return String - registryClass
143 public function getRegisteredClass() {
144 return $this->registryClass;
148 * Sets the name of the registry class
150 * @param String $registryClass
153 function registerClass($registryClass){
154 $this->registryClass = $registryClass;
158 * This function registers implementation class name and creates an instance of rest implementation class
159 * it will be made on this class object
161 * @param String $implementationClass
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');
174 * This function sets the fault object on the REST
176 * @param SoapError $errorObject - This is an object of type SoapError
179 function error($errorObject){
180 $GLOBALS['log']->info('Begin: SugarRestService->error');
181 $this->server->fault($errorObject);
182 $GLOBALS['log']->info('Begin: SugarRestService->error');
186 * This mehtod returns server
188 * @return String - server
191 function getServer(){
192 return $this->server;