]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/externalAPI/Base/ExternalAPIBase.php
Release 6.2.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-2011 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 abstract class ExternalAPIBase implements ExternalAPIPlugin
43 {
44     public $account_name;
45     public $account_password;
46     public $authMethod = 'password';
47     public $useAuth = true;
48     public $requireAuth = true;
49     
50     const APP_STRING_ERROR_PREFIX = 'ERR_EXTERNAL_API_';
51     protected $_appStringErrorPrefix = self::APP_STRING_ERROR_PREFIX;
52     
53     /**
54      * Authorization data
55      * @var EAPM
56      */
57     protected $authData;
58
59     /**
60      * Load authorization data
61      * @param EAPM $eapmBean
62      * @see ExternalAPIPlugin::loadEAPM()
63      */
64     public function loadEAPM($eapmBean)
65     {
66         // FIXME: check if the bean is validated, if not, refuse it?
67         $this->eapmBean = $eapmBean;
68         if ($this->authMethod == 'password') {
69             $this->account_name = $eapmBean->name;
70             $this->account_password = $eapmBean->password;
71         }
72         return true;
73     }
74
75     /**
76      * Check login
77      * @param EAPM $eapmBean
78      * @see ExternalAPIPlugin::checkLogin()
79      */
80     public function checkLogin($eapmBean = null)
81     {
82         if(!empty($eapmBean)) {
83             $this->loadEAPM($eapmBean);
84         }
85
86         if ( !isset($this->eapmBean) ) {
87             return array('success' => false);
88         }
89
90         return array('success' => true);
91     }
92
93     public function quickCheckLogin()
94     {
95         if ( !isset($this->eapmBean) ) {
96             return array('success' => false, 'errorMessage' => translate('LBL_ERR_NO_AUTHINFO','EAPM'));
97         }
98
99         if ( $this->eapmBean->validated==0 ) {
100             return array('success' => false, 'errorMessage' => translate('LBL_ERR_NO_AUTHINFO','EAPM'));
101         }
102
103         return array('success' => true);
104     }
105
106     protected function getValue($value)
107     {
108         if(!empty($this->$value)) {
109             return $this->$value;
110         }
111         return null;
112     }
113
114     public function logOff()
115     {
116         // Not sure if we should do anything.
117         return true;
118     }
119
120     /**
121      * Does API support this method?
122      * @see ExternalAPIPlugin::supports()
123      */
124     public function supports($method = '')
125         {
126         return $method==$this->authMethod;
127         }
128
129         protected function postData($url, $postfields, $headers)
130         {
131         $ch = curl_init($url);
132         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
133         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
134         if ( ( is_array($postfields) && count($postfields) == 0 ) ||
135              empty($postfields) ) {
136             curl_setopt($ch, CURLOPT_POST, false);
137         } else {
138             curl_setopt($ch, CURLOPT_POST, true);
139             curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
140         }
141         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
142         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
143
144         $GLOBALS['log']->debug("ExternalAPIBase->postData Where: ".$url);
145         $GLOBALS['log']->debug("Headers:\n".print_r($headers,true));
146         // $GLOBALS['log']->debug("Postfields:\n".print_r($postfields,true));
147         $rawResponse = curl_exec($ch);
148         $GLOBALS['log']->debug("Got:\n".print_r($rawResponse,true));
149
150         return $rawResponse;
151         }
152
153         /**
154          * Get connector for this API
155          * @return source|null
156          */
157         public function getConnector()
158         {
159             if(isset($this->connector)) {
160                 if(empty($this->connector_source)) {
161                     $this->connector_source = SourceFactory::getSource($this->connector, false);
162                 }
163                 return $this->connector_source;
164             }
165             return null;
166         }
167
168         /**
169          * Get parameter from source
170          * @param string $name
171          * @return mixed
172          */
173         public function getConnectorParam($name)
174         {
175         $connector =  $this->getConnector();
176         if(empty($connector)) return null;
177         return $connector->getProperty($name);
178         }
179         
180         
181         /**
182          * formatCallbackURL
183          * 
184          * This function takes a callback_url and checks the $_REQUEST variable to see if
185          * additional parameters should be appended to the callback_url value.  The $_REQUEST variables
186          * that are being checked deal with handling the behavior of closing/hiding windows/tabs that
187          * are displayed when prompting for OAUTH validation
188          * 
189          * @param $callback_url String value of callback URL
190          * @return String value of URL with applicable formatting
191          */
192         protected function formatCallbackURL($callback_url)
193         {
194                  // This is a tweak so that we can automatically close windows if requested by the external account system
195              if (isset($_REQUEST['closeWhenDone']) && $_REQUEST['closeWhenDone'] == 1 ) {
196              $callback_url .= '&closeWhenDone=1';
197          }
198
199          //Pass back the callbackFunction to call on the window.opener object
200          if (!empty($_REQUEST['callbackFunction']))
201          {
202              $callback_url .= '&callbackFunction=' . $_REQUEST['callbackFunction'];
203          }
204             
205          //Pass back the id of the application that triggered this oauth login
206          if (!empty($_REQUEST['application']))
207          {
208              $callback_url .= '&application=' . $_REQUEST['application'];
209          }              
210             
211              //Pass back the id of the application that triggered this oauth login
212          if (!empty($_REQUEST['refreshParentWindow']))
213          {
214              $callback_url .= '&refreshParentWindow=' . $_REQUEST['refreshParentWindow'];
215          }         
216          
217          return $callback_url;
218         }       
219         
220         /**
221          * Allow API clients to provide translated language strings for a given error code
222          *
223          * @param unknown_type $error_numb
224          */
225         protected function getErrorStringFromCode($error_numb)
226         {
227             $language_key = $this->_appStringErrorPrefix . $error_numb;
228             if( isset($GLOBALS['app_strings'][$language_key]) )
229                return $GLOBALS['app_strings'][$language_key];
230             else 
231                return $GLOBALS['app_strings']['ERR_EXTERNAL_API_SAVE_FAIL'];        
232         }
233
234     /**
235      * Determine if mime detection extensions are available.
236      *
237      * @return bool
238      */
239     public function isMimeDetectionAvailable()
240         {
241             return ( function_exists('mime_content_type') || function_exists( 'ext2mime' ) );
242         }
243 }