]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarOauth.php
Release 6.4.0
[Github/sugarcrm.git] / include / SugarOauth.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 'Zend/Oauth/Consumer.php';
39     // use ZF oauth
40     /**
41      * Sugar Oauth consumer
42      * @api
43      */
44     class SugarOAuth extends Zend_Oauth_Consumer
45     {
46         protected $_last = '';
47         protected $_oauth_config = array();
48
49         /**
50          * Create OAuth client
51          * @param string $consumer_key
52          * @param string $consumer_secret
53          * @param array $params OAuth options
54          */
55         public function __construct($consumer_key , $consumer_secret, $params = null)
56         {
57             $this->_oauth_config = array(
58                 'consumerKey' => $consumer_key,
59                 'consumerSecret' => $consumer_secret,
60             );
61             if(!empty($params)) {
62                 $this->_oauth_config = array_merge($this->_oauth_config, $params);
63             }
64             parent::__construct($this->_oauth_config);
65         }
66
67         /**
68          * Enable debugging
69          * @return SugarOAuth
70          */
71         public function enableDebug()
72         {
73             return $this;
74         }
75
76         /**
77          * Set token
78          * @param string $token
79          * @param string $secret
80          */
81         public function setToken($token, $secret)
82         {
83             $this->token = array($token, $secret);
84         }
85
86         /**
87          * Create request token object for current token
88          * @return Zend_Oauth_Token_Request
89          */
90         public function makeRequestToken()
91         {
92             $token = new Zend_Oauth_Token_Request();
93             $token->setToken($this->token[0]);
94             $token->setTokenSecret($this->token[1]);
95             return $token;
96         }
97
98         /**
99          * Create access token object for current token
100          * @return Zend_Oauth_Token_Access
101          */
102         public function makeAccessToken()
103         {
104             $token = new Zend_Oauth_Token_Access();
105             $token->setToken($this->token[0]);
106             $token->setTokenSecret($this->token[1]);
107             return $token;
108         }
109
110         /**
111          * Retrieve request token from URL
112          * @param string $url
113          * @param string $callback Callback URL
114          * @param array $params Query params
115          * @return array
116          * @see Zend_Oauth_Consumer::getRequestToken()
117          */
118         public function getRequestToken($url, $callback = null, $params = array())
119         {
120             if(!empty($callback)) {
121                 $this->setCallbackUrl($callback);
122             }
123             list($clean_url, $query) = explode('?', $url);
124             if($query) {
125                 $url = $clean_url;
126                 parse_str($query, $query_params);
127                 $params = array_merge($params, $query_params);
128             }
129             $this->setRequestTokenUrl($url);
130             try{
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' => '');
135             }
136         }
137
138         /**
139          * Retrieve access token from url
140          * @param string $url
141          * @see Zend_Oauth_Consumer::getAccessToken()
142          * @return array
143          */
144         public function getAccessToken($url)
145         {
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());
149         }
150
151        /**
152         * Fetch URL with OAuth
153         * @param string $url
154         * @param string $params Query params
155         * @param string $method HTTP method
156         * @param array $headers HTTP headers
157         * @return string
158         */
159        public function fetch($url, $params = null, $method = 'GET', $headers = null)
160        {
161            $acc = $this->makeAccessToken();
162            if ( strpos($url,'?') ) {
163                list($clean_url, $query) = explode('?', $url);
164                if($query) {
165                    $url = $clean_url;
166                    parse_str($query, $query_params);
167                    $params = array_merge($params?$params:array(), $query_params);
168                }
169            }
170             $client = $acc->getHttpClient($this->_oauth_config, $url);
171             $client->setMethod($method);
172             if(!empty($headers)) {
173                 $client->setHeaders($headers);
174             }
175             if(!empty($params)) {
176                 if($method == 'GET') {
177                     $client->setParameterGet($params);
178                 } else {
179                     $client->setParameterPost($params);
180                 }
181             }
182             $this->_last = $resp = $client->request();
183             $this->_lastReq = $client->getLastRequest();
184             return $resp->getBody();
185        }
186
187        /**
188         * Get HTTP client
189         * @return Zend_Oauth_Client
190         */
191        public function getClient()
192        {
193             $acc = $this->makeAccessToken();
194             return $acc->getHttpClient($this->_oauth_config);
195        }
196
197        /**
198         * Get last response
199         * @return string
200         */
201        public function getLastResponse()
202        {
203             return $this->_last;
204        }
205
206        /**
207         * Get last request
208         * @return string
209         */
210        public function getLastRequest()
211        {
212             return $this->_lastReq;
213        }
214     }