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 'Zend/Oauth/Consumer.php';
40 class SugarOAuth extends Zend_Oauth_Consumer
42 protected $_last = '';
43 protected $_oauth_config = array();
45 public function __construct($consumer_key , $consumer_secret, $params = null)
47 $this->_oauth_config = array(
48 'consumerKey' => $consumer_key,
49 'consumerSecret' => $consumer_secret,
52 $this->_oauth_config = array_merge($this->_oauth_config, $params);
54 parent::__construct($this->_oauth_config);
57 public function enableDebug()
62 public function setToken($token, $secret)
64 $this->token = array($token, $secret);
67 public function makeRequestToken()
69 $token = new Zend_Oauth_Token_Request();
70 $token->setToken($this->token[0]);
71 $token->setTokenSecret($this->token[1]);
75 public function makeAccessToken()
77 $token = new Zend_Oauth_Token_Access();
78 $token->setToken($this->token[0]);
79 $token->setTokenSecret($this->token[1]);
83 public function getRequestToken($url, $callback = null, $params = array())
85 if(!empty($callback)) {
86 $this->setCallbackUrl($callback);
88 list($clean_url, $query) = explode('?', $url);
91 parse_str($query, $query_params);
92 $params = array_merge($params, $query_params);
94 $this->setRequestTokenUrl($url);
96 $this->_last = $token = parent::getRequestToken($params);
97 return array('oauth_token' => $token->getToken(), 'oauth_token_secret' => $token->getTokenSecret());
98 }catch(Zend_Oauth_Exception $e){
99 return array('oauth_token' => '', 'oauth_token_secret' => '');
103 public function getAccessToken($url)
105 $this->setAccessTokenUrl($url);
106 $this->_last = $token = parent::getAccessToken($_REQUEST, $this->makeRequestToken());
107 return array('oauth_token' => $token->getToken(), 'oauth_token_secret' => $token->getTokenSecret());
110 public function fetch($url, $params = null, $method = 'GET', $headers = null)
112 $acc = $this->makeAccessToken();
113 if ( strpos($url,'?') ) {
114 list($clean_url, $query) = explode('?', $url);
117 parse_str($query, $query_params);
118 $params = array_merge($params?$params:array(), $query_params);
121 $client = $acc->getHttpClient($this->_oauth_config, $url);
122 $client->setMethod($method);
123 if(!empty($headers)) {
124 $client->setHeaders($headers);
126 if(!empty($params)) {
127 if($method == 'GET') {
128 $client->setParameterGet($params);
130 $client->setParameterPost($params);
133 $this->_last = $resp = $client->request();
134 $this->_lastReq = $client->getLastRequest();
135 return $resp->getBody();
138 public function getClient()
140 $acc = $this->makeAccessToken();
141 return $acc->getHttpClient($this->_oauth_config);
144 public function getLastResponse()
149 public function getLastRequest()
151 return $this->_lastReq;