2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once ('include/externalAPI/Base/ExternalAPIPlugin.php');
39 require_once ('include/externalAPI/Base/ExternalOAuthAPIPlugin.php');
40 require_once('include/connectors/sources/SourceFactory.php');
43 * Base implementation for external API
46 abstract class ExternalAPIBase implements ExternalAPIPlugin
49 public $account_password;
50 public $authMethod = 'password';
51 public $useAuth = true;
52 public $requireAuth = true;
54 const APP_STRING_ERROR_PREFIX = 'ERR_EXTERNAL_API_';
55 protected $_appStringErrorPrefix = self::APP_STRING_ERROR_PREFIX;
64 * Load authorization data
65 * @param EAPM $eapmBean
66 * @see ExternalAPIPlugin::loadEAPM()
68 public function loadEAPM($eapmBean)
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;
81 * @param EAPM $eapmBean
82 * @see ExternalAPIPlugin::checkLogin()
84 public function checkLogin($eapmBean = null)
86 if(!empty($eapmBean)) {
87 $this->loadEAPM($eapmBean);
90 if ( !isset($this->eapmBean) ) {
91 return array('success' => false);
94 return array('success' => true);
97 public function quickCheckLogin()
99 if ( !isset($this->eapmBean) ) {
100 return array('success' => false, 'errorMessage' => translate('LBL_ERR_NO_AUTHINFO','EAPM'));
103 if ( $this->eapmBean->validated==0 ) {
104 return array('success' => false, 'errorMessage' => translate('LBL_ERR_NO_AUTHINFO','EAPM'));
107 return array('success' => true);
110 protected function getValue($value)
112 if(!empty($this->$value)) {
113 return $this->$value;
118 public function logOff()
120 // Not sure if we should do anything.
125 * Does API support this method?
126 * @see ExternalAPIPlugin::supports()
128 public function supports($method = '')
130 return $method==$this->authMethod;
133 protected function postData($url, $postfields, $headers)
135 $ch = curl_init($url);
136 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
137 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
138 if ( ( is_array($postfields) && count($postfields) == 0 ) ||
139 empty($postfields) ) {
140 curl_setopt($ch, CURLOPT_POST, false);
142 curl_setopt($ch, CURLOPT_POST, true);
143 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
145 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
146 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
148 $GLOBALS['log']->debug("ExternalAPIBase->postData Where: ".$url);
149 $GLOBALS['log']->debug("Headers:\n".print_r($headers,true));
150 // $GLOBALS['log']->debug("Postfields:\n".print_r($postfields,true));
151 $rawResponse = curl_exec($ch);
152 $GLOBALS['log']->debug("Got:\n".print_r($rawResponse,true));
158 * Get connector for this API
159 * @return source|null
161 public function getConnector()
163 if(isset($this->connector)) {
164 if(empty($this->connector_source)) {
165 $this->connector_source = SourceFactory::getSource($this->connector, false);
166 $this->connector_source->setEAPM($this);
168 return $this->connector_source;
174 * Get parameter from source
175 * @param string $name
178 public function getConnectorParam($name)
180 $connector = $this->getConnector();
181 if(empty($connector)) return null;
182 return $connector->getProperty($name);
189 * This function takes a callback_url and checks the $_REQUEST variable to see if
190 * additional parameters should be appended to the callback_url value. The $_REQUEST variables
191 * that are being checked deal with handling the behavior of closing/hiding windows/tabs that
192 * are displayed when prompting for OAUTH validation
194 * @param $callback_url String value of callback URL
195 * @return String value of URL with applicable formatting
197 protected function formatCallbackURL($callback_url)
199 // This is a tweak so that we can automatically close windows if requested by the external account system
200 if (isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
201 $callback_url .= '&closeWhenDone=1';
204 //Pass back the callbackFunction to call on the window.opener object
205 if (!empty($_REQUEST['callbackFunction']))
207 $callback_url .= '&callbackFunction=' . $_REQUEST['callbackFunction'];
210 //Pass back the id of the application that triggered this oauth login
211 if (!empty($_REQUEST['application']))
213 $callback_url .= '&application=' . $_REQUEST['application'];
216 //Pass back the id of the application that triggered this oauth login
217 if (!empty($_REQUEST['refreshParentWindow']))
219 $callback_url .= '&refreshParentWindow=' . $_REQUEST['refreshParentWindow'];
222 return $callback_url;
226 * Allow API clients to provide translated language strings for a given error code
228 * @param unknown_type $error_numb
230 protected function getErrorStringFromCode($error_numb)
232 $language_key = $this->_appStringErrorPrefix . $error_numb;
233 if( isset($GLOBALS['app_strings'][$language_key]) )
234 return $GLOBALS['app_strings'][$language_key];
236 return $GLOBALS['app_strings']['ERR_EXTERNAL_API_SAVE_FAIL'];
240 * Determine if mime detection extensions are available.
244 public function isMimeDetectionAvailable()
246 return ( function_exists('mime_content_type') || function_exists( 'ext2mime' ) );