]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarOauth.php
Release 6.5.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-2012 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         
160         public function fetch($url, $params = null, $method = 'GET', $headers = null)
161         {
162             $acc = $this->makeAccessToken();
163             if ( strpos($url,'?') ) {
164                list($clean_url, $query) = explode('?', $url);
165                if($query) {
166                    $url = $clean_url;
167                    parse_str($query, $query_params);
168                    $params = array_merge($params?$params:array(), $query_params);
169                }
170             }
171             $client = $acc->getHttpClient($this->_oauth_config, $url);
172             
173             Zend_Loader::loadClass('Zend_Http_Client_Adapter_Proxy');
174             $proxy_config = SugarModule::get('Administration')->loadBean();
175             $proxy_config->retrieveSettings('proxy');
176             
177             if( !empty($proxy_config) && 
178                 !empty($proxy_config->settings['proxy_on']) &&
179                 $proxy_config->settings['proxy_on'] == 1) {
180                 
181                 $proxy_settings = array();                
182                 $proxy_settings['proxy_host'] = $proxy_config->settings['proxy_host'];
183                 $proxy_settings['proxy_port'] = $proxy_config->settings['proxy_port'];
184     
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'];
188                 }
189                 
190                 $adapter = new Zend_Http_Client_Adapter_Proxy();
191                 $adapter->setConfig($proxy_settings);
192                 $client->setAdapter($adapter);            
193             }
194             
195             $client->setMethod($method);
196             if(!empty($headers)) {
197                 $client->setHeaders($headers);
198             }
199             if(!empty($params)) {
200                 if($method == 'GET') {
201                     $client->setParameterGet($params);
202                 } else {
203                     $client->setParameterPost($params);
204                 }
205             }
206             $this->_last = $resp = $client->request();
207             $this->_lastReq = $client->getLastRequest();
208             return $resp->getBody();
209        }
210
211        /**
212         * Get HTTP client
213         * @return Zend_Oauth_Client
214         */
215        public function getClient()
216        {
217             $acc = $this->makeAccessToken();
218             return $acc->getHttpClient($this->_oauth_config);
219        }
220
221        /**
222         * Get last response
223         * @return string
224         */
225        public function getLastResponse()
226        {
227             return $this->_last;
228        }
229
230        /**
231         * Get last request
232         * @return string
233         */
234        public function getLastRequest()
235        {
236             return $this->_lastReq;
237        }
238     }