]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarOauth.php
Release 6.2.0beta4
[Github/sugarcrm.git] / include / SugarOauth.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM 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 'Zend/Oauth/Consumer.php';
39     // use ZF oauth
40     class SugarOAuth extends Zend_Oauth_Consumer
41     {
42         protected $_last = '';
43         protected $_oauth_config = array();
44
45         public function __construct($consumer_key , $consumer_secret, $params = null)
46         {
47             $this->_oauth_config = array(
48                 'consumerKey' => $consumer_key,
49                 'consumerSecret' => $consumer_secret,
50             );
51             if(!empty($params)) {
52                 $this->_oauth_config = array_merge($this->_oauth_config, $params);
53             }
54             parent::__construct($this->_oauth_config);
55         }
56
57         public function enableDebug()
58         {
59             return $this;
60         }
61
62         public function setToken($token, $secret)
63         {
64             $this->token = array($token, $secret);
65         }
66
67         public function makeRequestToken()
68         {
69             $token = new Zend_Oauth_Token_Request();
70             $token->setToken($this->token[0]);
71             $token->setTokenSecret($this->token[1]);
72             return $token;
73         }
74
75         public function makeAccessToken()
76         {
77             $token = new Zend_Oauth_Token_Access();
78             $token->setToken($this->token[0]);
79             $token->setTokenSecret($this->token[1]);
80             return $token;
81         }
82
83         public function getRequestToken($url, $callback = null, $params = array())
84         {
85             if(!empty($callback)) {
86                 $this->setCallbackUrl($callback);
87             }
88             list($clean_url, $query) = explode('?', $url);
89             if($query) {
90                 $url = $clean_url;
91                 parse_str($query, $query_params);
92                 $params = array_merge($params, $query_params);
93             }
94             $this->setRequestTokenUrl($url);
95             try{
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' => '');
100             }
101         }
102
103         public function getAccessToken($url)
104         {
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());
108         }
109
110        public function fetch($url, $params = null, $method = 'GET', $headers = null)
111        {
112            $acc = $this->makeAccessToken();
113            if ( strpos($url,'?') ) {
114                list($clean_url, $query) = explode('?', $url);
115                if($query) {
116                    $url = $clean_url;
117                    parse_str($query, $query_params);
118                    $params = array_merge($params?$params:array(), $query_params);
119                }
120            }
121             $client = $acc->getHttpClient($this->_oauth_config, $url);
122             $client->setMethod($method);
123             if(!empty($headers)) {
124                 $client->setHeaders($headers);
125             }
126             if(!empty($params)) {
127                 if($method == 'GET') {
128                     $client->setParameterGet($params);
129                 } else {
130                     $client->setParameterPost($params);
131                 }
132             }
133             $this->_last = $resp = $client->request();
134             $this->_lastReq = $client->getLastRequest();
135             return $resp->getBody();
136        }
137
138        public function getClient()
139        {
140             $acc = $this->makeAccessToken();
141             return $acc->getHttpClient($this->_oauth_config);
142        }
143
144        public function getLastResponse()
145        {
146             return $this->_last;
147        }
148
149        public function getLastRequest()
150        {
151             return $this->_lastReq;
152        }
153     }