]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/externalAPI/Base/ExternalAPIBase.php
Release 6.5.0
[Github/sugarcrm.git] / include / externalAPI / Base / ExternalAPIBase.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 require_once ('include/externalAPI/Base/ExternalAPIPlugin.php');
39 require_once ('include/externalAPI/Base/ExternalOAuthAPIPlugin.php');
40 require_once('include/connectors/sources/SourceFactory.php');
41
42 /**
43  * Base implementation for external API
44  * @api
45  */
46 abstract class ExternalAPIBase implements ExternalAPIPlugin
47 {
48     public $account_name;
49     public $account_password;
50     public $authMethod = 'password';
51     public $useAuth = true;
52     public $requireAuth = true;
53
54     const APP_STRING_ERROR_PREFIX = 'ERR_EXTERNAL_API_';
55     protected $_appStringErrorPrefix = self::APP_STRING_ERROR_PREFIX;
56
57     /**
58      * Authorization data
59      * @var EAPM
60      */
61     protected $authData;
62
63     /**
64      * Load authorization data
65      * @param EAPM $eapmBean
66      * @see ExternalAPIPlugin::loadEAPM()
67      */
68     public function loadEAPM($eapmBean)
69     {
70         // FIXME: check if the bean is validated, if not, refuse it?
71         $this->eapmBean = $eapmBean;
72         if ($this->authMethod == 'password') {
73             $this->account_name = $eapmBean->name;
74             $this->account_password = $eapmBean->password;
75         }
76         return true;
77     }
78
79     /**
80      * Check login
81      * @param EAPM $eapmBean
82      * @see ExternalAPIPlugin::checkLogin()
83      */
84     public function checkLogin($eapmBean = null)
85     {
86         if(!empty($eapmBean)) {
87             $this->loadEAPM($eapmBean);
88         }
89
90         if ( !isset($this->eapmBean) ) {
91             return array('success' => false);
92         }
93
94         return array('success' => true);
95     }
96
97     public function quickCheckLogin()
98     {
99         if ( !isset($this->eapmBean) ) {
100             return array('success' => false, 'errorMessage' => translate('LBL_ERR_NO_AUTHINFO','EAPM'));
101         }
102
103         if ( $this->eapmBean->validated==0 ) {
104             return array('success' => false, 'errorMessage' => translate('LBL_ERR_NO_AUTHINFO','EAPM'));
105         }
106
107         return array('success' => true);
108     }
109
110     protected function getValue($value)
111     {
112         if(!empty($this->$value)) {
113             return $this->$value;
114         }
115         return null;
116     }
117
118     public function logOff()
119     {
120         // Not sure if we should do anything.
121         return true;
122     }
123
124     /**
125      * Does API support this method?
126      * @see ExternalAPIPlugin::supports()
127      */
128     public function supports($method = '')
129         {
130         return $method==$this->authMethod;
131         }
132
133         protected function postData($url, $postfields, $headers)
134         {
135         $ch = curl_init($url);
136         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
137         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
138         
139         $proxy_config = SugarModule::get('Administration')->loadBean();
140         $proxy_config->retrieveSettings('proxy');
141         
142         if( !empty($proxy_config) && 
143             !empty($proxy_config->settings['proxy_on']) &&
144             $proxy_config->settings['proxy_on'] == 1) {
145
146             curl_setopt($ch, CURLOPT_PROXY, $proxy_config->settings['proxy_host']);
147             curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_config->settings['proxy_port']);
148             if (!empty($proxy_settings['proxy_auth'])) {
149                 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_settings['proxy_username'] . ':' . $proxy_settings['proxy_password']);
150             }
151         }   
152         
153         if ( ( is_array($postfields) && count($postfields) == 0 ) ||
154              empty($postfields) ) {
155             curl_setopt($ch, CURLOPT_POST, false);
156         } else {
157             curl_setopt($ch, CURLOPT_POST, true);
158             curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
159         }
160         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
161         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
162
163         $GLOBALS['log']->debug("ExternalAPIBase->postData Where: ".$url);
164         $GLOBALS['log']->debug("Headers:\n".print_r($headers,true));
165         // $GLOBALS['log']->debug("Postfields:\n".print_r($postfields,true));
166         $rawResponse = curl_exec($ch);
167         $GLOBALS['log']->debug("Got:\n".print_r($rawResponse,true));
168
169         return $rawResponse;
170         }
171
172         /**
173          * Get connector for this API
174          * @return source|null
175          */
176         public function getConnector()
177         {
178             if(isset($this->connector)) {
179                 if(empty($this->connector_source)) {
180                     $this->connector_source = SourceFactory::getSource($this->connector, false);
181                 $this->connector_source->setEAPM($this);
182                 }
183                 return $this->connector_source;
184             }
185             return null;
186         }
187
188         /**
189          * Get parameter from source
190          * @param string $name
191          * @return mixed
192          */
193         public function getConnectorParam($name)
194         {
195         $connector =  $this->getConnector();
196         if(empty($connector)) return null;
197         return $connector->getProperty($name);
198         }
199
200
201         /**
202          * formatCallbackURL
203          *
204          * This function takes a callback_url and checks the $_REQUEST variable to see if
205          * additional parameters should be appended to the callback_url value.  The $_REQUEST variables
206          * that are being checked deal with handling the behavior of closing/hiding windows/tabs that
207          * are displayed when prompting for OAUTH validation
208          *
209          * @param $callback_url String value of callback URL
210          * @return String value of URL with applicable formatting
211          */
212         protected function formatCallbackURL($callback_url)
213         {
214                  // This is a tweak so that we can automatically close windows if requested by the external account system
215              if (isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
216              $callback_url .= '&closeWhenDone=1';
217          }
218
219          //Pass back the callbackFunction to call on the window.opener object
220          if (!empty($_REQUEST['callbackFunction']))
221          {
222              $callback_url .= '&callbackFunction=' . $_REQUEST['callbackFunction'];
223          }
224
225          //Pass back the id of the application that triggered this oauth login
226          if (!empty($_REQUEST['application']))
227          {
228              $callback_url .= '&application=' . $_REQUEST['application'];
229          }
230
231              //Pass back the id of the application that triggered this oauth login
232          if (!empty($_REQUEST['refreshParentWindow']))
233          {
234              $callback_url .= '&refreshParentWindow=' . $_REQUEST['refreshParentWindow'];
235          }
236
237          return $callback_url;
238         }
239
240         /**
241          * Allow API clients to provide translated language strings for a given error code
242          *
243          * @param unknown_type $error_numb
244          */
245         protected function getErrorStringFromCode($error_numb)
246         {
247             $language_key = $this->_appStringErrorPrefix . $error_numb;
248             if( isset($GLOBALS['app_strings'][$language_key]) )
249                return $GLOBALS['app_strings'][$language_key];
250             else
251                return $GLOBALS['app_strings']['ERR_EXTERNAL_API_SAVE_FAIL'];
252         }
253
254     /**
255      * Determine if mime detection extensions are available.
256      *
257      * @return bool
258      */
259     public function isMimeDetectionAvailable()
260         {
261             return ( function_exists('mime_content_type') || function_exists( 'ext2mime' ) );
262         }
263 }