2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 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 'Zend/Oauth/Consumer.php';
41 * Sugar Oauth consumer
44 class SugarOAuth extends Zend_Oauth_Consumer
46 protected $_last = '';
47 protected $_oauth_config = array();
51 * @param string $consumer_key
52 * @param string $consumer_secret
53 * @param array $params OAuth options
55 public function __construct($consumer_key , $consumer_secret, $params = null)
57 $this->_oauth_config = array(
58 'consumerKey' => $consumer_key,
59 'consumerSecret' => $consumer_secret,
62 $this->_oauth_config = array_merge($this->_oauth_config, $params);
64 parent::__construct($this->_oauth_config);
71 public function enableDebug()
78 * @param string $token
79 * @param string $secret
81 public function setToken($token, $secret)
83 $this->token = array($token, $secret);
87 * Create request token object for current token
88 * @return Zend_Oauth_Token_Request
90 public function makeRequestToken()
92 $token = new Zend_Oauth_Token_Request();
93 $token->setToken($this->token[0]);
94 $token->setTokenSecret($this->token[1]);
99 * Create access token object for current token
100 * @return Zend_Oauth_Token_Access
102 public function makeAccessToken()
104 $token = new Zend_Oauth_Token_Access();
105 $token->setToken($this->token[0]);
106 $token->setTokenSecret($this->token[1]);
111 * Retrieve request token from URL
113 * @param string $callback Callback URL
114 * @param array $params Query params
116 * @see Zend_Oauth_Consumer::getRequestToken()
118 public function getRequestToken($url, $callback = null, $params = array())
120 if(!empty($callback)) {
121 $this->setCallbackUrl($callback);
123 list($clean_url, $query) = explode('?', $url);
126 parse_str($query, $query_params);
127 $params = array_merge($params, $query_params);
129 $this->setRequestTokenUrl($url);
131 $this->_last = $token = parent::getRequestToken($params);
132 return array('oauth_token' => $token->getToken(), 'oauth_token_secret' => $token->getTokenSecret());
133 }catch(Zend_Oauth_Exception $e){
134 return array('oauth_token' => '', 'oauth_token_secret' => '');
139 * Retrieve access token from url
141 * @see Zend_Oauth_Consumer::getAccessToken()
144 public function getAccessToken($url)
146 $this->setAccessTokenUrl($url);
147 $this->_last = $token = parent::getAccessToken($_REQUEST, $this->makeRequestToken());
148 return array('oauth_token' => $token->getToken(), 'oauth_token_secret' => $token->getTokenSecret());
152 * Fetch URL with OAuth
154 * @param string $params Query params
155 * @param string $method HTTP method
156 * @param array $headers HTTP headers
160 public function fetch($url, $params = null, $method = 'GET', $headers = null)
162 $acc = $this->makeAccessToken();
163 if ( strpos($url,'?') ) {
164 list($clean_url, $query) = explode('?', $url);
167 parse_str($query, $query_params);
168 $params = array_merge($params?$params:array(), $query_params);
171 $client = $acc->getHttpClient($this->_oauth_config, $url);
173 Zend_Loader::loadClass('Zend_Http_Client_Adapter_Proxy');
174 $proxy_config = SugarModule::get('Administration')->loadBean();
175 $proxy_config->retrieveSettings('proxy');
177 if( !empty($proxy_config) &&
178 !empty($proxy_config->settings['proxy_on']) &&
179 $proxy_config->settings['proxy_on'] == 1) {
181 $proxy_settings = array();
182 $proxy_settings['proxy_host'] = $proxy_config->settings['proxy_host'];
183 $proxy_settings['proxy_port'] = $proxy_config->settings['proxy_port'];
185 if(!empty($proxy_config->settings['proxy_auth'])){
186 $proxy_settings['proxy_user'] = $proxy_config->settings['proxy_username'];
187 $proxy_settings['proxy_pass'] = $proxy_config->settings['proxy_password'];
190 $adapter = new Zend_Http_Client_Adapter_Proxy();
191 $adapter->setConfig($proxy_settings);
192 $client->setAdapter($adapter);
195 $client->setMethod($method);
196 if(!empty($headers)) {
197 $client->setHeaders($headers);
199 if(!empty($params)) {
200 if($method == 'GET') {
201 $client->setParameterGet($params);
203 $client->setParameterPost($params);
206 $this->_last = $resp = $client->request();
207 $this->_lastReq = $client->getLastRequest();
208 return $resp->getBody();
213 * @return Zend_Oauth_Client
215 public function getClient()
217 $acc = $this->makeAccessToken();
218 return $acc->getHttpClient($this->_oauth_config);
225 public function getLastResponse()
234 public function getLastRequest()
236 return $this->_lastReq;