Completed
Push — master ( 470825...143e87 )
by Joram van den
06:11
created

twitteroauth::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 4
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Abraham Williams ([email protected]) http://abrah.am
5
 *
6
 * The first PHP Library to support OAuth for Twitter's REST API.
7
 */
8
9
/* Load OAuth lib. You can find it at http://oauth.net */
10
require_once 'OAuth.php';
11
12
/**
13
 * Twitter OAuth class.
14
 */
15
class twitteroauth
16
{
17
    /* Contains the last HTTP status code returned. */
18
    public $http_code;
19
    /* Contains the last API call. */
20
    public $url;
21
    /* Set up the API root URL. */
22
    public $host = 'https://api.twitter.com/1.1/';
23
    /* Set timeout default. */
24
    public $timeout = 30;
25
    /* Set connect timeout. */
26
    public $connecttimeout = 30;
27
    /* Verify SSL Cert. */
28
    public $ssl_verifypeer = false;
29
    /* Respons format. */
30
    public $format = 'json';
31
    /* Decode returned json data. */
32
    public $decode_json = true;
33
    /* Contains the last HTTP headers returned. */
34
    public $http_info;
35
    /* Set the useragnet. */
36
    public $useragent = 'TwitterOAuth v0.2.0-beta2';
37
    /* Immediately retry the API call if the response was not successful. */
38
    //public $retry = TRUE;
39
40
    /**
41
     * Set API URLS.
42
     */
43
    public function accessTokenURL()
44
    {
45
        return 'https://api.twitter.com/oauth/access_token';
46
    }
47
48
    public function authenticateURL()
49
    {
50
        return 'https://api.twitter.com/oauth/authenticate';
51
    }
52
53
    public function authorizeURL()
54
    {
55
        return 'https://api.twitter.com/oauth/authorize';
56
    }
57
58
    public function requestTokenURL()
59
    {
60
        return 'https://api.twitter.com/oauth/request_token';
61
    }
62
63
    /**
64
     * Debug helpers.
65
     */
66
    public function lastStatusCode()
67
    {
68
        return $this->http_status;
0 ignored issues
show
Bug introduced by
The property http_status does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
    }
70
71
    public function lastAPICall()
72
    {
73
        return $this->last_api_call;
0 ignored issues
show
Bug introduced by
The property last_api_call does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
    }
75
76
    /**
77
     * construct TwitterOAuth object.
78
     */
79
    public function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
80
    {
81
        $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
0 ignored issues
show
Bug introduced by
The property sha1_method does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82
        $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
0 ignored issues
show
Bug introduced by
The property consumer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
83
        if (!empty($oauth_token) && !empty($oauth_token_secret)) {
84
            $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
0 ignored issues
show
Bug introduced by
The property token does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
        } else {
86
            $this->token = null;
87
        }
88
    }
89
90
    /**
91
     * Get a request_token from Twitter.
92
     *
93
     * @returns a key/value array containing oauth_token and oauth_token_secret
94
     */
95 View Code Duplication
    public function getRequestToken($oauth_callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $parameters = [];
98
        $parameters['oauth_callback'] = $oauth_callback;
99
        $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
100
        $token = OAuthUtil::parse_parameters($request);
101
        $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
102
103
        return $token;
104
    }
105
106
    /**
107
     * Get the authorize URL.
108
     *
109
     * @returns a string
110
     */
111
    public function getAuthorizeURL($token, $sign_in_with_twitter = true)
112
    {
113
        if (is_array($token)) {
114
            $token = $token['oauth_token'];
115
        }
116
        if (empty($sign_in_with_twitter)) {
117
            return $this->authorizeURL()."?oauth_token={$token}";
118
        } else {
119
            return $this->authenticateURL()."?oauth_token={$token}";
120
        }
121
    }
122
123
    /**
124
     * Exchange request token and secret for an access token and
125
     * secret, to sign API calls.
126
     *
127
     * @returns array("oauth_token" => "the-access-token",
128
     *                "oauth_token_secret" => "the-access-secret",
129
     *                "user_id" => "9436992",
130
     *                "screen_name" => "abraham")
131
     */
132 View Code Duplication
    public function getAccessToken($oauth_verifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        $parameters = [];
135
        $parameters['oauth_verifier'] = $oauth_verifier;
136
        $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
137
        $token = OAuthUtil::parse_parameters($request);
138
        $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
139
140
        return $token;
141
    }
142
143
    /**
144
     * One time exchange of username and password for access token and secret.
145
     *
146
     * @returns array("oauth_token" => "the-access-token",
147
     *                "oauth_token_secret" => "the-access-secret",
148
     *                "user_id" => "9436992",
149
     *                "screen_name" => "abraham",
150
     *                "x_auth_expires" => "0")
151
     */
152
    public function getXAuthToken($username, $password)
153
    {
154
        $parameters = [];
155
        $parameters['x_auth_username'] = $username;
156
        $parameters['x_auth_password'] = $password;
157
        $parameters['x_auth_mode'] = 'client_auth';
158
        $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
159
        $token = OAuthUtil::parse_parameters($request);
160
        $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
161
162
        return $token;
163
    }
164
165
    /**
166
     * GET wrapper for oAuthRequest.
167
     */
168 View Code Duplication
    public function get($url, $parameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $response = $this->oAuthRequest($url, 'GET', $parameters);
171
        if ($this->format === 'json' && $this->decode_json) {
172
            return json_decode($response);
173
        }
174
175
        return $response;
176
    }
177
178
    /**
179
     * POST wrapper for oAuthRequest.
180
     */
181 View Code Duplication
    public function post($url, $parameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $response = $this->oAuthRequest($url, 'POST', $parameters);
184
        if ($this->format === 'json' && $this->decode_json) {
185
            return json_decode($response);
186
        }
187
188
        return $response;
189
    }
190
191
    /**
192
     * DELETE wrapper for oAuthReqeust.
193
     */
194 View Code Duplication
    public function delete($url, $parameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
    {
196
        $response = $this->oAuthRequest($url, 'DELETE', $parameters);
197
        if ($this->format === 'json' && $this->decode_json) {
198
            return json_decode($response);
199
        }
200
201
        return $response;
202
    }
203
204
    /**
205
     * Format and sign an OAuth / API request.
206
     */
207
    public function oAuthRequest($url, $method, $parameters)
208
    {
209
        if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
210
            $url = "{$this->host}{$url}.{$this->format}";
211
        }
212
        $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
213
        $request->sign_request($this->sha1_method, $this->consumer, $this->token);
214
        switch ($method) {
215
            case 'GET':
216
                return $this->http($request->to_url(), 'GET');
217
            default:
218
                return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
219
        }
220
    }
221
222
    /**
223
     * Make an HTTP request.
224
     *
225
     * @return API results
226
     */
227
    public function http($url, $method, $postfields = null)
228
    {
229
        $this->http_info = [];
230
        $ci = curl_init();
231
        /* Curl settings */
232
        curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
233
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
234
        curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
235
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
236
        curl_setopt($ci, CURLOPT_HTTPHEADER, ['Expect:']);
237
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
238
        curl_setopt($ci, CURLOPT_HEADERFUNCTION, [$this, 'getHeader']);
239
        curl_setopt($ci, CURLOPT_HEADER, false);
240
241
        switch ($method) {
242
            case 'POST':
243
                curl_setopt($ci, CURLOPT_POST, true);
244
                if (!empty($postfields)) {
245
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
246
                }
247
                break;
248
            case 'DELETE':
249
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
250
                if (!empty($postfields)) {
251
                    $url = "{$url}?{$postfields}";
252
                }
253
        }
254
255
        curl_setopt($ci, CURLOPT_URL, $url);
256
        $response = curl_exec($ci);
257
        $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
258
        $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
259
        $this->url = $url;
260
        curl_close($ci);
261
262
        return $response;
263
    }
264
265
    /**
266
     * Get the header info to store.
267
     */
268
    public function getHeader($ch, $header)
269
    {
270
        $i = strpos($header, ':');
271
        if (!empty($i)) {
272
            $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
273
            $value = trim(substr($header, $i + 2));
274
            $this->http_header[$key] = $value;
0 ignored issues
show
Bug introduced by
The property http_header does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
275
        }
276
277
        return strlen($header);
278
    }
279
}
280