SugarOAuth::getClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
1
<?php
2
/*********************************************************************************
3
 * SugarCRM Community Edition is a customer relationship management program developed by
4
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5
6
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
7
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
8
 *
9
 * This program is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU Affero General Public License version 3 as published by the
11
 * Free Software Foundation with the addition of the following permission added
12
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15
 *
16
 * This program is distributed in the hope that it will be useful, but WITHOUT
17
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License along with
22
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
23
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
 * 02110-1301 USA.
25
 *
26
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28
 *
29
 * The interactive user interfaces in modified source and object code versions
30
 * of this program must display Appropriate Legal Notices, as required under
31
 * Section 5 of the GNU Affero General Public License version 3.
32
 *
33
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34
 * these Appropriate Legal Notices must retain the display of the "Powered by
35
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
37
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38
 ********************************************************************************/
39
40
41 1
    require_once 'Zend/Oauth/Consumer.php';
42
    // use ZF oauth
43
    /**
44
     * Sugar Oauth consumer
45
     * @api
46
     */
47
    class SugarOAuth extends Zend_Oauth_Consumer
48
    {
49
        protected $_last = '';
50
        protected $_oauth_config = array();
51
52
        /**
53
         * Create OAuth client
54
         * @param string $consumer_key
55
         * @param string $consumer_secret
56
         * @param array $params OAuth options
57
         */
58
        public function __construct($consumer_key , $consumer_secret, $params = null)
59
        {
60
            $this->_oauth_config = array(
61
                'consumerKey' => $consumer_key,
62
                'consumerSecret' => $consumer_secret,
63
            );
64
            if(!empty($params)) {
65
                $this->_oauth_config = array_merge($this->_oauth_config, $params);
66
            }
67
            parent::__construct($this->_oauth_config);
68
        }
69
70
        /**
71
         * Enable debugging
72
         * @return SugarOAuth
73
         */
74
        public function enableDebug()
75
        {
76
            return $this;
77
        }
78
79
        /**
80
         * Set token
81
         * @param string $token
82
         * @param string $secret
83
         */
84
        public function setToken($token, $secret)
85
        {
86
            $this->token = array($token, $secret);
87
        }
88
89
        /**
90
         * Create request token object for current token
91
         * @return Zend_Oauth_Token_Request
92
         */
93
        public function makeRequestToken()
94
        {
95
            $token = new Zend_Oauth_Token_Request();
96
            $token->setToken($this->token[0]);
97
            $token->setTokenSecret($this->token[1]);
98
            return $token;
99
        }
100
101
        /**
102
         * Create access token object for current token
103
         * @return Zend_Oauth_Token_Access
104
         */
105
        public function makeAccessToken()
106
        {
107
            $token = new Zend_Oauth_Token_Access();
108
            $token->setToken($this->token[0]);
109
            $token->setTokenSecret($this->token[1]);
110
            return $token;
111
        }
112
113
        /**
114
         * Retrieve request token from URL
115
         * @param string $url
116
         * @param string $callback Callback URL
117
         * @param array $params Query params
118
         * @return array
119
         * @see Zend_Oauth_Consumer::getRequestToken()
120
         */
121
        public function getRequestTokenByUrl($url, $callback = null, $params = array())
122
        {
123
            if(!empty($callback)) {
124
                $this->setCallbackUrl($callback);
125
            }
126
            list($clean_url, $query) = explode('?', $url);
127
            if($query) {
128
                $url = $clean_url;
129
                parse_str($query, $query_params);
130
                $params = array_merge($params, $query_params);
131
            }
132
            $this->setRequestTokenUrl($url);
133
            try{
134
                $this->_last = $token = parent::getRequestToken($params);
135
                return array('oauth_token' => $token->getToken(), 'oauth_token_secret' => $token->getTokenSecret());
136
            }catch(Zend_Oauth_Exception $e){
0 ignored issues
show
Bug introduced by
The class Zend_Oauth_Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
137
                return array('oauth_token' => '', 'oauth_token_secret' => '');
138
            }
139
        }
140
141
        /**
142
         * Retrieve access token from url
143
         * @param string $url
144
         * @see Zend_Oauth_Consumer::getAccessToken()
145
         * @return array
146
         */
147
        public function getAccessToken($url,
148
                                       Zend_Oauth_Token_Request $token = null,
149
                                       $httpMethod = null,
150
                                       Zend_Oauth_Http_AccessToken $request = null)
151
        {
152
            $this->setAccessTokenUrl($url);
153
            $this->_last = $token = parent::getAccessToken($_REQUEST, $this->makeRequestToken());
154
            return array('oauth_token' => $token->getToken(), 'oauth_token_secret' => $token->getTokenSecret());
155
        }
156
157
       /**
158
        * Fetch URL with OAuth
159
        * @param string $url
160
        * @param string $params Query params
161
        * @param string $method HTTP method
162
        * @param array $headers HTTP headers
163
        * @return string
164
        */
165
        
166
        public function fetch($url, $params = null, $method = 'GET', $headers = null)
167
        {
168
            $acc = $this->makeAccessToken();
169
            if ( strpos($url,'?') ) {
170
               list($clean_url, $query) = explode('?', $url);
171
               if($query) {
172
                   $url = $clean_url;
173
                   parse_str($query, $query_params);
174
                   $params = array_merge($params?$params:array(), $query_params);
175
               }
176
            }
177
            $client = $acc->getHttpClient($this->_oauth_config, $url);
178
            
179
            Zend_Loader::loadClass('Zend_Http_Client_Adapter_Proxy');
180
            $proxy_config = SugarModule::get('Administration')->loadBean();
181
            $proxy_config->retrieveSettings('proxy');
182
            
183
            if( !empty($proxy_config) && 
184
                !empty($proxy_config->settings['proxy_on']) &&
185
                $proxy_config->settings['proxy_on'] == 1) {
186
                
187
                $proxy_settings = array();                
188
                $proxy_settings['proxy_host'] = $proxy_config->settings['proxy_host'];
189
                $proxy_settings['proxy_port'] = $proxy_config->settings['proxy_port'];
190
    
191
                if(!empty($proxy_config->settings['proxy_auth'])){
192
                    $proxy_settings['proxy_user'] = $proxy_config->settings['proxy_username'];
193
                    $proxy_settings['proxy_pass'] = $proxy_config->settings['proxy_password'];
194
                }
195
                
196
                $adapter = new Zend_Http_Client_Adapter_Proxy();
197
                $adapter->setConfig($proxy_settings);
198
                $client->setAdapter($adapter);            
199
            }
200
            
201
            $client->setMethod($method);
202
            if(!empty($headers)) {
203
                $client->setHeaders($headers);
204
            }
205
            if(!empty($params)) {
206
                if($method == 'GET') {
207
                    $client->setParameterGet($params);
208
                } else {
209
                    $client->setParameterPost($params);
210
                }
211
            }
212
            $this->_last = $resp = $client->request();
213
            $this->_lastReq = $client->getLastRequest();
214
            return $resp->getBody();
215
       }
216
217
       /**
218
        * Get HTTP client
219
        * @return Zend_Oauth_Client
220
        */
221
       public function getClient()
222
       {
223
            $acc = $this->makeAccessToken();
224
            return $acc->getHttpClient($this->_oauth_config);
225
       }
226
227
       /**
228
        * Get last response
229
        * @return string
230
        */
231
       public function getLastResponse()
232
       {
233
            return $this->_last;
234
       }
235
236
       /**
237
        * Get last request
238
        * @return string
239
        */
240
       public function getLastRequest()
241
       {
242
            return $this->_lastReq;
243
       }
244
    }
245