oauth_client_class::GetAccessTokenURL()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 97 and the first side effect is on line 1840.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 * oauth_client.php
4
 *
5
 * @(#) $Id: oauth_client.php,v 1.46 2013/01/10 10:11:33 mlemos Exp $
6
 *
7
 */
8
9
/*
10
{metadocument}<?xml version="1.0" encoding="ISO-8859-1" ?>
11
<class>
12
13
    <package>net.manuellemos.oauth</package>
14
15
    <version>@(#) $Id: oauth_client.php,v 1.46 2013/01/10 10:11:33 mlemos Exp $</version>
16
    <copyright>Copyright � (C) Manuel Lemos 2012</copyright>
17
    <title>OAuth client</title>
18
    <author>Manuel Lemos</author>
19
    <authoraddress>mlemos-at-acm.org</authoraddress>
20
21
    <documentation>
22
        <idiom>en</idiom>
23
        <purpose>This class serves two main purposes:<paragraphbreak />
24
            1) Implement the OAuth protocol to retrieve a token from a server to
25
            authorize the access to an API on behalf of the current
26
            user.<paragraphbreak />
27
            2) Perform calls to a Web services API using a token previously
28
            obtained using this class or a token provided some other way by the
29
            Web services provider.</purpose>
30
        <usage>Regardless of your purposes, you always need to start calling
31
            the class <functionlink>Initialize</functionlink> function after
32
            initializing setup variables. After you are done with the class,
33
            always call the <functionlink>Finalize</functionlink> function at
34
            the end.<paragraphbreak />
35
            This class supports either OAuth protocol versions 1.0, 1.0a and
36
            2.0. It abstracts the differences between these protocol versions,
37
            so the class usage is the same independently of the OAuth
38
            version of the server.<paragraphbreak />
39
            The class also provides built-in support to several popular OAuth
40
            servers, so you do not have to manually configure all the details to
41
            access those servers. Just set the
42
            <variablelink>server</variablelink> variable to configure the class
43
            to access one of the built-in supported servers.<paragraphbreak />
44
            If you need to access one type of server that is not yet directly
45
            supported by the class, you need to configure it explicitly setting
46
            the variables: <variablelink>oauth_version</variablelink>,
47
            <variablelink>url_parameters</variablelink>,
48
            <variablelink>authorization_header</variablelink>,
49
            <variablelink>request_token_url</variablelink>,
50
            <variablelink>dialog_url</variablelink>,
51
            <variablelink>append_state_to_redirect_uri</variablelink> and
52
            <variablelink>access_token_url</variablelink>.<paragraphbreak />
53
            Before proceeding to the actual OAuth authorization process, you
54
            need to have registered your application with the OAuth server. The
55
            registration provides you values to set the variables
56
            <variablelink>client_id</variablelink> and
57
            <variablelink>client_secret</variablelink>.<paragraphbreak />
58
            You also need to set the variables
59
            <variablelink>redirect_uri</variablelink> and
60
            <variablelink>scope</variablelink> before calling the
61
            <functionlink>Process</functionlink> function to make the class
62
            perform the necessary interactions with the OAuth
63
            server.<paragraphbreak />
64
            The OAuth protocol involves multiple steps that include redirection
65
            to the OAuth server. There it asks permission to the current user to
66
            grant your application access to APIs on his/her behalf. When there
67
            is a redirection, the class will set the
68
            <variablelink>exit</variablelink> variable to
69
            <booleanvalue>1</booleanvalue>. Then your script should exit
70
            immediately without outputting anything.<paragraphbreak />
71
            When the OAuth access token is successfully obtained, the following
72
            variables are set by the class with the obtained values:
73
            <variablelink>access_token</variablelink>,
74
            <variablelink>access_token_secret</variablelink>,
75
            <variablelink>access_token_expiry</variablelink>,
76
            <variablelink>access_token_type</variablelink>. You may want to
77
            store these values to use them later when calling the server
78
            APIs.<paragraphbreak />
79
            If there was a problem during OAuth authorization process, check the
80
            variable <variablelink>authorization_error</variablelink> to
81
            determine the reason.<paragraphbreak />
82
            Once you get the access token, you can call the server APIs using
83
            the <functionlink>CallAPI</functionlink> function. Check the
84
            <variablelink>access_token_error</variablelink> variable to
85
            determine if there was an error when trying to to call the
86
            API.<paragraphbreak />
87
            If for some reason the user has revoked the access to your
88
            application, you need to ask the user to authorize your application
89
            again. First you may need to call the function
90
            <functionlink>ResetAccessToken</functionlink> to reset the value of
91
            the access token that may be cached in session variables.</usage>
92
    </documentation>
93
94
{/metadocument}
95
*/
96
97
class oauth_client_class
98
{
99
/*
100
{metadocument}
101
    <variable>
102
        <name>error</name>
103
        <type>STRING</type>
104
        <value></value>
105
        <documentation>
106
            <purpose>Store the message that is returned when an error
107
                occurs.</purpose>
108
            <usage>Check this variable to understand what happened when a call to
109
                any of the class functions has failed.<paragraphbreak />
110
                This class uses cumulative error handling. This means that if one
111
                class functions that may fail is called and this variable was
112
                already set to an error message due to a failure in a previous call
113
                to the same or other function, the function will also fail and does
114
                not do anything.<paragraphbreak />
115
                This allows programs using this class to safely call several
116
                functions that may fail and only check the failure condition after
117
                the last function call.<paragraphbreak />
118
                Just set this variable to an empty string to clear the error
119
                condition.</usage>
120
        </documentation>
121
    </variable>
122
{/metadocument}
123
*/
124
    var $error = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $error.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
125
126
/*
127
{metadocument}
128
    <variable>
129
        <name>debug</name>
130
        <type>BOOLEAN</type>
131
        <value>0</value>
132
        <documentation>
133
            <purpose>Control whether debug output is enabled</purpose>
134
            <usage>Set this variable to <booleanvalue>1</booleanvalue> if you
135
                need to check what is going on during calls to the class. When
136
                enabled, the debug output goes either to the variable
137
                <variablelink>debug_output</variablelink> and the PHP error log.</usage>
138
        </documentation>
139
    </variable>
140
{/metadocument}
141
*/
142
    var $debug = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $debug.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
143
144
/*
145
{metadocument}
146
    <variable>
147
        <name>debug_http</name>
148
        <type>BOOLEAN</type>
149
        <value>0</value>
150
        <documentation>
151
            <purpose>Control whether the dialog with the remote Web server
152
                should also be logged.</purpose>
153
            <usage>Set this variable to <booleanvalue>1</booleanvalue> if you
154
                want to inspect the data exchange with the OAuth server.</usage>
155
        </documentation>
156
    </variable>
157
{/metadocument}
158
*/
159
    var $debug_http = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $debug_http.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
160
161
/*
162
{metadocument}
163
    <variable>
164
        <name>exit</name>
165
        <type>BOOLEAN</type>
166
        <value>0</value>
167
        <documentation>
168
            <purpose>Determine if the current script should be exited.</purpose>
169
            <usage>Check this variable after calling the
170
                <functionlink>Process</functionlink> function and exit your script
171
                immediately if the variable is set to
172
                <booleanvalue>1</booleanvalue>.</usage>
173
        </documentation>
174
    </variable>
175
{/metadocument}
176
*/
177
    var $exit = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $exit.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
178
179
/*
180
{metadocument}
181
    <variable>
182
        <name>debug_output</name>
183
        <type>STRING</type>
184
        <value></value>
185
        <documentation>
186
            <purpose>Capture the debug output generated by the class</purpose>
187
            <usage>Inspect this variable if you need to see what happened during
188
                the class function calls.</usage>
189
        </documentation>
190
    </variable>
191
{/metadocument}
192
*/
193
    var $debug_output = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $debug_output.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
194
195
/*
196
{metadocument}
197
    <variable>
198
        <name>debug_prefix</name>
199
        <type>STRING</type>
200
        <value>OAuth client: </value>
201
        <documentation>
202
            <purpose>Mark the lines of the debug output to identify actions
203
                performed by this class.</purpose>
204
            <usage>Change this variable if you prefer the debug output lines to
205
                be prefixed with a different text.</usage>
206
        </documentation>
207
    </variable>
208
{/metadocument}
209
*/
210
    var $debug_prefix = 'OAuth client: ';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $debug_prefix.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
211
212
/*
213
{metadocument}
214
    <variable>
215
        <name>server</name>
216
        <type>STRING</type>
217
        <value></value>
218
        <documentation>
219
            <purpose>Identify the type of OAuth server to access.</purpose>
220
            <usage>The class provides built-in support to several types of OAuth
221
                servers. This means that the class can automatically initialize
222
                several configuration variables just by setting this server
223
                variable.<paragraphbreak />
224
                Currently it supports the following servers:
225
                <stringvalue>Bitbucket</stringvalue>,
226
                <stringvalue>Dropbox</stringvalue>,
227
                <stringvalue>Facebook</stringvalue>,
228
                <stringvalue>Fitbit</stringvalue>,
229
                <stringvalue>Flickr</stringvalue>,
230
                <stringvalue>Foursquare</stringvalue>,
231
                <stringvalue>github</stringvalue>,
232
                <stringvalue>Google</stringvalue>,
233
                <stringvalue>Instagram</stringvalue>,
234
                <stringvalue>LinkedIn</stringvalue>,
235
                <stringvalue>Microsoft</stringvalue>,
236
                <stringvalue>Scoop.it</stringvalue>,
237
                <stringvalue>Tumblr</stringvalue>,
238
                <stringvalue>Twitter</stringvalue> and
239
                <stringvalue>Yahoo</stringvalue>. Please contact the author if you
240
                would like to ask to add built-in support for other types of OAuth
241
                servers.<paragraphbreak />
242
                If you want to access other types of OAuth servers that are not
243
                yet supported, set this variable to an empty string and configure
244
                other variables with values specific to those servers.</usage>
245
        </documentation>
246
    </variable>
247
{/metadocument}
248
*/
249
    var $server = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $server.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
250
251
/*
252
{metadocument}
253
    <variable>
254
        <name>request_token_url</name>
255
        <type>STRING</type>
256
        <value></value>
257
        <documentation>
258
            <purpose>URL of the OAuth server to request the initial token for
259
                OAuth 1.0 and 1.0a servers.</purpose>
260
            <usage>Set this variable to the OAuth request token URL when you are
261
                not accessing one of the built-in supported OAuth
262
                servers.<paragraphbreak />
263
                For OAuth 1.0 and 1.0a servers, the request token URL can have
264
                certain marks that will act as template placeholders which will be
265
                replaced with given values before requesting the authorization
266
                token. Currently it supports the following placeholder
267
                marks:<paragraphbreak />
268
                {SCOPE} - scope of the requested permissions to the granted by the
269
                OAuth server with the user permissions</usage>
270
        </documentation>
271
    </variable>
272
{/metadocument}
273
*/
274
    var $request_token_url = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $request_token_url.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
275
276
/*
277
{metadocument}
278
    <variable>
279
        <name>dialog_url</name>
280
        <type>STRING</type>
281
        <value></value>
282
        <documentation>
283
            <purpose>URL of the OAuth server to redirect the browser so the user
284
                can grant access to your application.</purpose>
285
            <usage>Set this variable to the OAuth request token URL when you are
286
                not accessing one of the built-in supported OAuth servers.<paragraphbreak />
287
                For OAuth 2.0 servers, the dialog URL can have certain marks that
288
                will act as template placeholders which will be replaced with
289
                values defined before redirecting the users browser. Currently it
290
                supports the following placeholder marks:<paragraphbreak />
291
                {REDIRECT_URI} - URL to redirect when returning from the OAuth
292
                server authorization page<paragraphbreak />
293
                {CLIENT_ID} - client application identifier registered at the
294
                server<paragraphbreak />
295
                {SCOPE} - scope of the requested permissions to the granted by the
296
                OAuth server with the user permissions<paragraphbreak />
297
                {STATE} - identifier of the OAuth session state</usage>
298
        </documentation>
299
    </variable>
300
{/metadocument}
301
*/
302
    var $dialog_url = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $dialog_url.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
303
304
/*
305
{metadocument}
306
    <variable>
307
        <name>append_state_to_redirect_uri</name>
308
        <type>STRING</type>
309
        <value></value>
310
        <documentation>
311
            <purpose>Pass the OAuth session state in a variable with a different
312
                name to work around implementation bugs of certain OAuth
313
                servers</purpose>
314
            <usage>Set this variable  when you are not accessing one of the
315
                built-in supported OAuth servers if the OAuth server has a bug
316
                that makes it not pass back the OAuth state identifier in a
317
                request variable named state.</usage>
318
        </documentation>
319
    </variable>
320
{/metadocument}
321
*/
322
    var $append_state_to_redirect_uri = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $append_state_to_redirect_uri.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
323
324
/*
325
{metadocument}
326
    <variable>
327
        <name>access_token_url</name>
328
        <type>STRING</type>
329
        <value></value>
330
        <documentation>
331
            <purpose>OAuth server URL that will return the access token
332
                URL.</purpose>
333
            <usage>Set this variable to the OAuth access token URL when you are
334
                not accessing one of the built-in supported OAuth servers.</usage>
335
        </documentation>
336
    </variable>
337
{/metadocument}
338
*/
339
    var $access_token_url = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $access_token_url.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
340
341
342
/*
343
{metadocument}
344
    <variable>
345
        <name>oauth_version</name>
346
        <type>STRING</type>
347
        <value>2.0</value>
348
        <documentation>
349
            <purpose>Version of the protocol version supported by the OAuth
350
                server.</purpose>
351
            <usage>Set this variable to the OAuth server protocol version when
352
                you are not accessing one of the built-in supported OAuth
353
                servers.</usage>
354
        </documentation>
355
    </variable>
356
{/metadocument}
357
*/
358
    var $oauth_version = '2.0';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $oauth_version.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
359
360
/*
361
{metadocument}
362
    <variable>
363
        <name>url_parameters</name>
364
        <type>BOOLEAN</type>
365
        <value>0</value>
366
        <documentation>
367
            <purpose>Determine if the API call parameters should be moved to the
368
                call URL.</purpose>
369
            <usage>Set this variable to <booleanvalue>1</booleanvalue> if the
370
                API you need to call requires that the call parameters always be
371
                passed via the API URL.</usage>
372
        </documentation>
373
    </variable>
374
{/metadocument}
375
*/
376
    var $url_parameters = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $url_parameters.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
377
378
/*
379
{metadocument}
380
    <variable>
381
        <name>authorization_header</name>
382
        <type>BOOLEAN</type>
383
        <value>1</value>
384
        <documentation>
385
            <purpose>Determine if the OAuth parameters should be passed via HTTP
386
                Authorization request header.</purpose>
387
            <usage>Set this variable to <booleanvalue>1</booleanvalue> if the
388
                OAuth server requires that the OAuth parameters be passed using
389
                the HTTP Authorization instead of the request URI parameters.</usage>
390
        </documentation>
391
    </variable>
392
{/metadocument}
393
*/
394
    var $authorization_header = true;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $authorization_header.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
395
396
/*
397
{metadocument}
398
    <variable>
399
        <name>redirect_uri</name>
400
        <type>STRING</type>
401
        <value></value>
402
        <documentation>
403
            <purpose>URL of the current script page that is calling this
404
                class</purpose>
405
            <usage>Set this variable to the current script page URL before
406
                proceeding the the OAuth authorization process.</usage>
407
        </documentation>
408
    </variable>
409
{/metadocument}
410
*/
411
    var $redirect_uri = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $redirect_uri.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
412
413
/*
414
{metadocument}
415
    <variable>
416
        <name>client_id</name>
417
        <type>STRING</type>
418
        <value></value>
419
        <documentation>
420
            <purpose>Identifier of your application registered with the OAuth
421
                server</purpose>
422
            <usage>Set this variable to the application identifier that is
423
                provided by the OAuth server when you register the
424
                application.</usage>
425
        </documentation>
426
    </variable>
427
{/metadocument}
428
*/
429
    var $client_id = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $client_id.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
430
431
/*
432
{metadocument}
433
    <variable>
434
        <name>client_secret</name>
435
        <type>STRING</type>
436
        <value></value>
437
        <documentation>
438
            <purpose>Secret value assigned to your application when it is
439
                registered with the OAuth server.</purpose>
440
            <usage>Set this variable to the application secret that is provided
441
                by the OAuth server when you register the application.</usage>
442
        </documentation>
443
    </variable>
444
{/metadocument}
445
*/
446
    var $client_secret = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $client_secret.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
447
448
/*
449
{metadocument}
450
    <variable>
451
        <name>scope</name>
452
        <type>STRING</type>
453
        <value></value>
454
        <documentation>
455
            <purpose>Permissions that your application needs to call the OAuth
456
                server APIs</purpose>
457
            <usage>Check the documentation of the APIs that your application
458
                needs to call to set this variable with the identifiers of the
459
                permissions that the user needs to grant to your application.</usage>
460
        </documentation>
461
    </variable>
462
{/metadocument}
463
*/
464
    var $scope = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $scope.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
465
466
/*
467
{metadocument}
468
    <variable>
469
        <name>access_token</name>
470
        <type>STRING</type>
471
        <value></value>
472
        <documentation>
473
            <purpose>Access token obtained from the OAuth server</purpose>
474
            <usage>Check this variable to get the obtained access token upon
475
                successful OAuth authorization.</usage>
476
        </documentation>
477
    </variable>
478
{/metadocument}
479
*/
480
    var $access_token = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $access_token.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
481
482
/*
483
{metadocument}
484
    <variable>
485
        <name>access_token_secret</name>
486
        <type>STRING</type>
487
        <value></value>
488
        <documentation>
489
            <purpose>Access token secret obtained from the OAuth server</purpose>
490
            <usage>If the OAuth protocol version is 1.0 or 1.0a, check this
491
                variable to get the obtained access token secret upon successful
492
                OAuth authorization.</usage>
493
        </documentation>
494
    </variable>
495
{/metadocument}
496
*/
497
    var $access_token_secret = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $access_token_secret.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
498
499
/*
500
{metadocument}
501
    <variable>
502
        <name>access_token_expiry</name>
503
        <type>STRING</type>
504
        <value></value>
505
        <documentation>
506
            <purpose>Timestamp of the expiry of the access token obtained from
507
                the OAuth server.</purpose>
508
            <usage>Check this variable to get the obtained access token expiry
509
                time upon successful OAuth authorization. If this variable is
510
                empty, that means no expiry time was set.</usage>
511
        </documentation>
512
    </variable>
513
{/metadocument}
514
*/
515
    var $access_token_expiry = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $access_token_expiry.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
516
517
/*
518
{metadocument}
519
    <variable>
520
        <name>access_token_type</name>
521
        <type>STRING</type>
522
        <value></value>
523
        <documentation>
524
            <purpose>Type of access token obtained from the OAuth server.</purpose>
525
            <usage>Check this variable to get the obtained access token type
526
                upon successful OAuth authorization.</usage>
527
        </documentation>
528
    </variable>
529
{/metadocument}
530
*/
531
    var $access_token_type = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $access_token_type.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
532
533
/*
534
{metadocument}
535
    <variable>
536
        <name>access_token_error</name>
537
        <type>STRING</type>
538
        <value></value>
539
        <documentation>
540
            <purpose>Error message returned when a call to the API fails.</purpose>
541
            <usage>Check this variable to determine if there was an error while
542
                calling the Web services API when using the
543
                <functionlink>CallAPI</functionlink> function.</usage>
544
        </documentation>
545
    </variable>
546
{/metadocument}
547
*/
548
    var $access_token_error = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $access_token_error.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
549
550
/*
551
{metadocument}
552
    <variable>
553
        <name>authorization_error</name>
554
        <type>STRING</type>
555
        <value></value>
556
        <documentation>
557
            <purpose>Error message returned when it was not possible to obtain
558
                an OAuth access token</purpose>
559
            <usage>Check this variable to determine if there was an error while
560
                trying to obtain the OAuth access token.</usage>
561
        </documentation>
562
    </variable>
563
{/metadocument}
564
*/
565
    var $authorization_error = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $authorization_error.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
566
567
/*
568
{metadocument}
569
    <variable>
570
        <name>response_status</name>
571
        <type>INTEGER</type>
572
        <value>0</value>
573
        <documentation>
574
            <purpose>HTTP response status returned by the server when calling an
575
                API</purpose>
576
            <usage>Check this variable after calling the
577
                <functionlink>CallAPI</functionlink> function if the API calls and you
578
                need to process the error depending the response status.
579
                <integervalue>200</integervalue> means no error.
580
                <integervalue>0</integervalue> means the server response was not
581
                retrieved.</usage>
582
        </documentation>
583
    </variable>
584
{/metadocument}
585
*/
586
    var $response_status = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $response_status.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
587
588
    var $oauth_user_agent = 'PHP-OAuth-API (http://www.phpclasses.org/oauth-api $Revision: 1.46 $)';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $oauth_user_agent.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
589
    var $session_started = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $session_started.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
590
591
    Function SetError($error)
592
    {
593
        $this->error = $error;
594
        if($this->debug)
595
            $this->OutputDebug('Error: '.$error);
596
        return(false);
597
    }
598
599
    Function SetPHPError($error, &$php_error_message)
600
    {
601
        if(IsSet($php_error_message)
602
        && strlen($php_error_message))
603
            $error.=": ".$php_error_message;
604
        return($this->SetError($error));
605
    }
606
607
    Function OutputDebug($message)
608
    {
609
        if($this->debug)
610
        {
611
            $message = $this->debug_prefix.$message;
612
            $this->debug_output .= $message."\n";;
613
            error_log($message);
614
        }
615
        return(true);
616
    }
617
618
    Function GetRequestTokenURL(&$request_token_url)
619
    {
620
        $request_token_url = $this->request_token_url;
621
        return(true);
622
    }
623
624
    Function GetDialogURL(&$redirect_url)
625
    {
626
        $redirect_url = $this->dialog_url;
627
        return(true);
628
    }
629
630
    Function GetAccessTokenURL(&$access_token_url)
631
    {
632
        $access_token_url = $this->access_token_url;
633
        return(true);
634
    }
635
636
    Function GetStoredState(&$state)
0 ignored issues
show
Coding Style introduced by
GetStoredState uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
637
    {
638
        if(IsSet($_SESSION['OAUTH_STATE']))
639
            $state = $_SESSION['OAUTH_STATE'];
640
        else
641
            $state = $_SESSION['OAUTH_STATE'] = time().'-'.substr(md5(rand().time()), 0, 6);
642
        return(true);
643
    }
644
645
    Function GetRequestState(&$state)
0 ignored issues
show
Coding Style introduced by
GetRequestState uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
646
    {
647
        $check = (strlen($this->append_state_to_redirect_uri) ? $this->append_state_to_redirect_uri : 'state');
648
        $state = (IsSet($_GET[$check]) ? $_GET[$check] : null);
649
        return(true);
650
    }
651
652
    Function GetRequestCode(&$code)
0 ignored issues
show
Coding Style introduced by
GetRequestCode uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
653
    {
654
        $code = (IsSet($_GET['code']) ? $_GET['code'] : null);
655
        return(true);
656
    }
657
658
    Function GetRequestError(&$error)
0 ignored issues
show
Coding Style introduced by
GetRequestError uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
659
    {
660
        $error = (IsSet($_GET['error']) ? $_GET['error'] : null);
661
        return(true);
662
    }
663
664
    Function GetRequestDenied(&$denied)
0 ignored issues
show
Coding Style introduced by
GetRequestDenied uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
665
    {
666
        $denied = (IsSet($_GET['denied']) ? $_GET['denied'] : null);
667
        return(true);
668
    }
669
670
    Function GetRequestToken(&$token, &$verifier)
0 ignored issues
show
Coding Style introduced by
GetRequestToken uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
671
    {
672
        $token = (IsSet($_GET['oauth_token']) ? $_GET['oauth_token'] : null);
673
        $verifier = (IsSet($_GET['oauth_verifier']) ? $_GET['oauth_verifier'] : null);
674
        return(true);
675
    }
676
677
    Function GetRedirectURI(&$redirect_uri)
0 ignored issues
show
Coding Style introduced by
GetRedirectURI uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
678
    {
679
        if(strlen($this->redirect_uri))
680
            $redirect_uri = $this->redirect_uri;
681
        else
682
            $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
683
        return true;
684
    }
685
686
/*
687
{metadocument}
688
    <function>
689
        <name>StoreAccessToken</name>
690
        <type>BOOLEAN</type>
691
        <documentation>
692
            <purpose>Store the values of the access token when it is succefully
693
                retrieved from the OAuth server.</purpose>
694
            <usage>This function is meant to be only be called from inside the
695
                class. By default it stores access tokens in a session variable
696
                named <stringvalue>OAUTH_ACCESS_TOKEN</stringvalue>.<paragraphbreak />
697
                Actual implementations should create a sub-class and override this
698
                function to make the access token values be stored in other types
699
                of containers, like for instance databases.</usage>
700
            <returnvalue>This function should return
701
                <booleanvalue>1</booleanvalue> if the access token was stored
702
                successfully.</returnvalue>
703
        </documentation>
704
        <argument>
705
            <name>access_token</name>
706
            <type>HASH</type>
707
            <documentation>
708
                <purpose>Associative array with properties of the access token.
709
                    The array may have set the following
710
                    properties:<paragraphbreak />
711
                    <stringvalue>value</stringvalue>: string value of the access
712
                        token<paragraphbreak />
713
                    <stringvalue>authorized</stringvalue>: boolean value that
714
                        determines if the access token was obtained
715
                        successfully<paragraphbreak />
716
                    <stringvalue>expiry</stringvalue>: (optional) timestamp in ISO
717
                        format relative to UTC time zone of the access token expiry
718
                        time<paragraphbreak />
719
                    <stringvalue>type</stringvalue>: (optional) type of OAuth token
720
                        that may determine how it should be used when sending API call
721
                        requests.</purpose>
722
            </documentation>
723
        </argument>
724
        <do>
725
{/metadocument}
726
*/
727
    Function StoreAccessToken($access_token)
0 ignored issues
show
Coding Style introduced by
StoreAccessToken uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
728
    {
729
        $_SESSION['OAUTH_ACCESS_TOKEN'][$this->access_token_url] = $access_token;
730
        return true;
731
    }
732
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
733
{metadocument}
734
        </do>
735
    </function>
736
{/metadocument}
737
*/
738
739
/*
740
{metadocument}
741
    <function>
742
        <name>GetAccessToken</name>
743
        <type>BOOLEAN</type>
744
        <documentation>
745
            <purpose>Retrieve the OAuth access token if it was already
746
                previously stored by the
747
                <functionlink>StoreAccessToken</functionlink> function.</purpose>
748
            <usage>This function is meant to be only be called from inside the
749
                class. By default it retrieves access tokens stored in a session
750
                variable named
751
                <stringvalue>OAUTH_ACCESS_TOKEN</stringvalue>.<paragraphbreak />
752
                Actual implementations should create a sub-class and override this
753
                function to retrieve the access token values from other types of
754
                containers, like for instance databases.</usage>
755
            <returnvalue>This function should return
756
                <booleanvalue>1</booleanvalue> if the access token was retrieved
757
                successfully.</returnvalue>
758
        </documentation>
759
        <argument>
760
            <name>access_token</name>
761
            <type>STRING</type>
762
            <out />
763
            <documentation>
764
                <purpose>Return the properties of the access token in an
765
                    associative array. If the access token was not yet stored, it
766
                    returns an empty array. Otherwise, the properties it may return
767
                    are the same that may be passed to the
768
                    <functionlink>StoreAccessToken</functionlink>.</purpose>
769
            </documentation>
770
        </argument>
771
        <do>
772
{/metadocument}
773
*/
774
    Function GetAccessToken(&$access_token)
0 ignored issues
show
Coding Style introduced by
GetAccessToken uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
775
    {
776
        if(!$this->session_started
777
        && !session_start())
778
            return($this->SetPHPError('it was not possible to start the PHP session', $php_error_message));
779
        $this->session_started = true;
780
        if(IsSet($_SESSION['OAUTH_ACCESS_TOKEN'][$this->access_token_url]))
781
            $access_token = $_SESSION['OAUTH_ACCESS_TOKEN'][$this->access_token_url];
782
        else
783
            $access_token = array();
784
        return true;
785
    }
786
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
787
{metadocument}
788
        </do>
789
    </function>
790
{/metadocument}
791
*/
792
793
/*
794
{metadocument}
795
    <function>
796
        <name>ResetAccessToken</name>
797
        <type>BOOLEAN</type>
798
        <documentation>
799
            <purpose>Reset the access token to a state back when the user has
800
                not yet authorized the access to the OAuth server API.</purpose>
801
            <usage>Call this function if for some reason the token to access
802
                the API was revoked and you need to ask the user to authorize
803
                the access again.<paragraphbreak />
804
                By default the class stores and retrieves access tokens in a
805
                session variable named
806
                <stringvalue>OAUTH_ACCESS_TOKEN</stringvalue>.<paragraphbreak />
807
                This function must be called when the user is accessing your site
808
                pages, so it can reset the information stored in session variables
809
                that cache the state of a previously retrieved access
810
                token.<paragraphbreak />
811
                Actual implementations should create a sub-class and override this
812
                function to reset the access token state when it is stored in
813
                other types of containers, like for instance databases.</usage>
814
            <returnvalue>This function should return
815
                <booleanvalue>1</booleanvalue> if the access token was resetted
816
                successfully.</returnvalue>
817
        </documentation>
818
        <do>
819
{/metadocument}
820
*/
821
    Function ResetAccessToken()
0 ignored issues
show
Coding Style introduced by
ResetAccessToken uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
822
    {
823
        if($this->debug)
824
            $this->OutputDebug('Resetting the access token status for OAuth server located at '.$this->access_token_url);
825
        if(!$this->session_started
826
        && !session_start())
827
            return($this->SetPHPError('it was not possible to start the PHP session', $php_error_message));
828
        $this->session_started = true;
829
        if(IsSet($_SESSION['OAUTH_ACCESS_TOKEN'][$this->access_token_url]))
830
            Unset($_SESSION['OAUTH_ACCESS_TOKEN'][$this->access_token_url]);
831
        return true;
832
    }
833
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
834
{metadocument}
835
        </do>
836
    </function>
837
{/metadocument}
838
*/
839
840
    Function Encode($value)
841
    {
842
        return(is_array($value) ? $this->EncodeArray($value) : str_replace('%7E', '~', str_replace('+',' ', RawURLEncode($value))));
843
    }
844
845
    Function EncodeArray($array)
846
    {
847
        foreach($array as $key => $value)
848
            $array[$key] = $this->Encode($value);
849
        return $array;
850
    }
851
852
    Function HMAC($function, $data, $key)
853
    {
854
        switch($function)
855
        {
856
            case 'sha1':
857
                $pack = 'H40';
858
                break;
859
            default:
860
                if($this->debug)
861
                    $this->OutputDebug($function.' is not a supported an HMAC hash type');
862
                return('');
863
        }
864
        if(strlen($key) > 64)
865
            $key = pack($pack, $function($key));
866
        if(strlen($key) < 64)
867
            $key = str_pad($key, 64, "\0");
868
        return(pack($pack, $function((str_repeat("\x5c", 64) ^ $key).pack($pack, $function((str_repeat("\x36", 64) ^ $key).$data)))));
869
    }
870
871
    Function SendAPIRequest($url, $method, $parameters, $oauth, $options, &$response)
872
    {
873
        $this->response_status = 0;
874
        $http = new http_class;
875
        $http->debug = ($this->debug && $this->debug_http);
0 ignored issues
show
Documentation Bug introduced by
The property $debug was declared of type integer, but $this->debug && $this->debug_http is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
876
        $http->log_debug = true;
0 ignored issues
show
Documentation Bug introduced by
The property $log_debug was declared of type integer, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
877
        $http->sasl_authenticate = 0;
878
        $http->user_agent = $this->oauth_user_agent;
879
        if($this->debug)
880
            $this->OutputDebug('Accessing the '.$options['Resource'].' at '.$url);
881
        $arguments = array();
882
        $method = strtoupper($method);
883
        $authorization = '';
884
        $type = (IsSet($options['RequestContentType']) ? strtolower(trim(strtok($options['RequestContentType'], ';'))) : 'application/x-www-form-urlencoded');
885
        if(IsSet($oauth))
886
        {
887
            $values = array(
888
                'oauth_consumer_key'=>$this->client_id,
889
                'oauth_nonce'=>md5(uniqid(rand(), true)),
890
                'oauth_signature_method'=>'HMAC-SHA1',
891
                'oauth_timestamp'=>time(),
892
                'oauth_version'=>'1.0',
893
            );
894
            if($this->url_parameters
895
            && $type === 'application/x-www-form-urlencoded'
896
            && count($parameters))
897
            {
898
                $first = (strpos($url, '?') === false);
899
                foreach($parameters as $parameter => $value)
900
                    $url .= ($first ? '?' : '&').UrlEncode($parameter).'='.UrlEncode($value);
901
                $parameters = array();
902
            }
903
            $value_parameters = ($type !== 'application/x-www-form-urlencoded' ? array() : $parameters);
904
            $values = array_merge($values, $oauth, $value_parameters);
905
            $uri = strtok($url, '?');
906
            $sign = $method.'&'.$this->Encode($uri).'&';
907
            $first = true;
908
            $sign_values = $values;
909
            $u = parse_url($url);
910
            if(IsSet($u['query']))
911
            {
912
                parse_str($u['query'], $q);
913
                foreach($q as $parameter => $value)
0 ignored issues
show
Bug introduced by
The expression $q of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
914
                    $sign_values[$parameter] = $value;
915
            }
916
            KSort($sign_values);
917
            foreach($sign_values as $parameter => $value)
918
            {
919
                $sign .= $this->Encode(($first ? '' : '&').$parameter.'='.$this->Encode($value));
920
                $first = false;
921
            }
922
            $key = $this->Encode($this->client_secret).'&'.$this->Encode($this->access_token_secret);
923
            $values['oauth_signature'] = base64_encode($this->HMAC('sha1', $sign, $key));
924
            if($this->authorization_header)
925
            {
926
                $authorization = 'OAuth';
927
                $first = true;
928
                foreach($values as $parameter => $value)
929
                {
930
                    $authorization .= ($first ? ' ' : ',').$parameter.'="'.$this->Encode($value).'"';
931
                    $first = false;
932
                }
933
            }
934
            else
935
            {
936
                if($method === 'GET')
937
                {
938
                    $first = (strcspn($url, '?') == strlen($url));
939
                    foreach($values as $parameter => $value)
940
                    {
941
                        $url .= ($first ? '?' : '&').$parameter.'='.$this->Encode($value);
942
                        $first = false;
943
                    }
944
                    $post_values = array();
0 ignored issues
show
Unused Code introduced by
$post_values is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
945
                }
946
                else
947
                    $post_values = $values;
0 ignored issues
show
Unused Code introduced by
$post_values is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
948
            }
949
        }
950
        if(strlen($error = $http->GetRequestArguments($url, $arguments)))
951
            return($this->SetError('it was not possible to open the '.$options['Resource'].' URL: '.$error));
952
        if(strlen($error = $http->Open($arguments)))
953
            return($this->SetError('it was not possible to open the '.$options['Resource'].' URL: '.$error));
954
        $arguments['RequestMethod'] = $method;
955
        switch($type)
956
        {
957
            case 'application/x-www-form-urlencoded':
958
                if(IsSet($options['RequestBody']))
959
                    return($this->SetError('the request body is defined automatically from the parameters'));
960
                $arguments['PostValues'] = $parameters;
961
                break;
962
            case 'application/json':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
963
                $arguments['Headers']['Content-Type'] = $options['RequestContentType'];
964
                if(!IsSet($options['RequestBody']))
965
                {
966
                    $arguments['Body'] = json_encode($parameters);
967
                    break;
968
                }
969
            default:
970
                if(!IsSet($options['RequestBody']))
971
                    return($this->SetError('it was not specified the body value of the of the API call request'));
972
                $arguments['Headers']['Content-Type'] = $options['RequestContentType'];
973
                $arguments['Body'] = $options['RequestBody'];
974
                break;
975
        }
976
        $arguments['Headers']['Accept'] = (IsSet($options['Accept']) ? $options['Accept'] : '*/*');
977
        if(strlen($authorization))
978
            $arguments['Headers']['Authorization'] = $authorization;
979
        if(strlen($error = $http->SendRequest($arguments))
980
        || strlen($error = $http->ReadReplyHeaders($headers)))
981
        {
982
            $http->Close();
983
            return($this->SetError('it was not possible to retrieve the '.$options['Resource'].': '.$error));
984
        }
985
        $error = $http->ReadWholeReplyBody($data);
986
        $http->Close();
987
        if(strlen($error))
988
        {
989
            return($this->SetError('it was not possible to access the '.$options['Resource'].': '.$error));
990
        }
991
        $this->response_status = intval($http->response_status);
992
        $content_type = (IsSet($headers['content-type']) ? strtolower(trim(strtok($headers['content-type'], ';'))) : 'unspecified');
993
        switch($content_type)
994
        {
995
            case 'text/javascript':
996
            case 'application/json':
997
                if(!function_exists('json_decode'))
998
                    return($this->SetError('the JSON extension is not available in this PHP setup'));
999
                $object = json_decode($data);
1000
                switch(GetType($object))
1001
                {
1002
                    case 'object':
1003
                        if(!IsSet($options['ConvertObjects'])
1004
                        || !$options['ConvertObjects'])
1005
                            $response = $object;
1006
                        else
1007
                        {
1008
                            $response = array();
1009
                            foreach($object as $property => $value)
1010
                                $response[$property] = $value;
1011
                        }
1012
                        break;
1013
                    case 'array':
1014
                        $response = $object;
1015
                        break;
1016
                    default:
1017
                        if(!IsSet($object))
1018
                            return($this->SetError('it was not returned a valid JSON definition of the '.$options['Resource'].' values'));
1019
                        $response = $object;
1020
                        break;
1021
                }
1022
                break;
1023
            case 'application/x-www-form-urlencoded':
1024
            case 'text/plain':
1025
            case 'text/html':
1026
                parse_str($data, $response);
1027
                break;
1028
            default:
1029
                $response = $data;
1030
                break;
1031
        }
1032
        if($this->response_status >= 200
1033
        && $this->response_status < 300)
1034
            $this->access_token_error = '';
1035
        else
1036
        {
1037
            $this->access_token_error = 'it was not possible to access the '.$options['Resource'].': it was returned an unexpected response status '.$http->response_status.' Response: '.$data;
1038
            if($this->debug)
1039
                $this->OutputDebug('Could not retrieve the OAuth access. Error: '.$this->access_token_error);
1040
            if(IsSet($options['FailOnAccessError'])
1041
            && $options['FailOnAccessError'])
1042
            {
1043
                $this->error = $this->access_token_error;
1044
                return false;
1045
            }
1046
        }
1047
        return true;
1048
    }
1049
1050
/*
1051
{metadocument}
1052
    <function>
1053
        <name>CallAPI</name>
1054
        <type>BOOLEAN</type>
1055
        <documentation>
1056
            <purpose>Send a HTTP request to the Web services API using a
1057
                previously obtained authorization token via OAuth.</purpose>
1058
            <usage>This function can be used to call an API after having
1059
                previously obtained an access token through the OAuth protocol
1060
                using the <functionlink>Process</functionlink> function, or by
1061
                directly setting the variables
1062
                <variablelink>access_token</variablelink>, as well as
1063
                <variablelink>access_token_secret</variablelink> in case of using
1064
                OAuth 1.0 or 1.0a services.</usage>
1065
            <returnvalue>This function returns <booleanvalue>1</booleanvalue> if
1066
                the call was done successfully.</returnvalue>
1067
        </documentation>
1068
        <argument>
1069
            <name>url</name>
1070
            <type>STRING</type>
1071
            <documentation>
1072
                <purpose>URL of the API where the HTTP request will be sent.</purpose>
1073
            </documentation>
1074
        </argument>
1075
        <argument>
1076
            <name>method</name>
1077
            <type>STRING</type>
1078
            <documentation>
1079
                <purpose>HTTP method that will be used to send the request. It can
1080
                be <stringvalue>GET</stringvalue>,
1081
                <stringvalue>POST</stringvalue>,
1082
                <stringvalue>DELETE</stringvalue>, <stringvalue>PUT</stringvalue>,
1083
                etc..</purpose>
1084
            </documentation>
1085
        </argument>
1086
        <argument>
1087
            <name>parameters</name>
1088
            <type>HASH</type>
1089
            <documentation>
1090
                <purpose>Associative array with the names and values of the API
1091
                    call request parameters.</purpose>
1092
            </documentation>
1093
        </argument>
1094
        <argument>
1095
            <name>options</name>
1096
            <type>HASH</type>
1097
            <documentation>
1098
                <purpose>Associative array with additional options to configure
1099
                    the request. Currently it supports the following
1100
                    options:<paragraphbreak />
1101
                    <stringvalue>Resource</stringvalue>: string with a label that
1102
                        will be used in the error messages and debug log entries to
1103
                        identify what operation the request is performing. The default
1104
                        value is <stringvalue>API call</stringvalue>.<paragraphbreak />
1105
                    <stringvalue>ConvertObjects</stringvalue>: boolean option that
1106
                        determines if objects should be converted into arrays when the
1107
                        response is returned in JSON format. The default value is
1108
                        <booleanvalue>0</booleanvalue>.<paragraphbreak />
1109
                    <stringvalue>FailOnAccessError</stringvalue>: boolean option
1110
                        that determines if this functions should fail when the server
1111
                        response status is not between 200 and 299. The default value
1112
                        is <booleanvalue>0</booleanvalue>.<paragraphbreak />
1113
                    <stringvalue>Accept</stringvalue>: content type value of the
1114
                        Accept HTTP header to be sent in the API call HTTP request.
1115
                        Some APIs require that a certain value be sent to specify
1116
                        which version of the API is being called. The default value is
1117
                        <stringvalue>*&#47;*</stringvalue>.<paragraphbreak />
1118
                    <stringvalue>RequestContentType</stringvalue>: content type that
1119
                        should be used to send the request values. It can be either
1120
                        <stringvalue>application/x-www-form-urlencoded</stringvalue>
1121
                        for sending values like from Web forms, or
1122
                        <stringvalue>application/json</stringvalue> for sending the
1123
                        values encoded in JSON format. Other types are accepted if the
1124
                        <stringvalue>RequestBody</stringvalue> option is specified.
1125
                        The default value is
1126
                        <stringvalue>application/x-www-form-urlencoded</stringvalue>.<paragraphbreak />
1127
                    <stringvalue>RequestBody</stringvalue>: request body data of a
1128
                        custom type. The <stringvalue>RequestContentType</stringvalue>
1129
                        option must be specified, so the
1130
                        <stringvalue>RequestBody</stringvalue> option is considered.</purpose>
1131
            </documentation>
1132
        </argument>
1133
        <argument>
1134
            <name>response</name>
1135
            <type>STRING</type>
1136
            <out />
1137
            <documentation>
1138
                <purpose>Return the value of the API response. If the value is
1139
                    JSON encoded, this function will decode it and return the value
1140
                    converted to respective types. If the value is form encoded,
1141
                    this function will decode the response and return it as an
1142
                    array. Otherwise, the class will return the value as a
1143
                    string.</purpose>
1144
            </documentation>
1145
        </argument>
1146
        <do>
1147
{/metadocument}
1148
*/
1149
    Function CallAPI($url, $method, $parameters, $options, &$response)
1150
    {
1151
        if(!IsSet($options['Resource']))
1152
            $options['Resource'] = 'API call';
1153
        if(!IsSet($options['ConvertObjects']))
1154
            $options['ConvertObjects'] = false;
1155
        switch(intval($this->oauth_version))
1156
        {
1157
            case 1:
1158
                $oauth = array(
1159
                    'oauth_token'=>$this->access_token
1160
                );
1161
                break;
1162
1163
            case 2:
1164
                $oauth = null;
1165
                $url .= (strcspn($url, '?') < strlen($url) ? '&' : '?').'access_token='.UrlEncode($this->access_token);
1166
                break;
1167
1168
            default:
1169
                return($this->SetError($this->oauth_version.' is not a supported version of the OAuth protocol'));
1170
        }
1171
        return($this->SendAPIRequest($url, $method, $parameters, $oauth, $options, $response));
1172
    }
1173
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1174
{metadocument}
1175
        </do>
1176
    </function>
1177
{/metadocument}
1178
*/
1179
1180
/*
1181
{metadocument}
1182
    <function>
1183
        <name>Initialize</name>
1184
        <type>BOOLEAN</type>
1185
        <documentation>
1186
            <purpose>Initialize the class variables and internal state. It must
1187
                be called before calling other class functions.</purpose>
1188
            <usage>Set the <variablelink>server</variablelink> variable before
1189
                calling this function to let it initialize the class variables to
1190
                work with the specified server type. Alternatively, you can set
1191
                other class variables manually to make it work with servers that
1192
                are not yet built-in supported.</usage>
1193
            <returnvalue>This function returns <booleanvalue>1</booleanvalue> if
1194
                it was able to successfully initialize the class for the specified
1195
                server type.</returnvalue>
1196
        </documentation>
1197
        <do>
1198
{/metadocument}
1199
*/
1200
    Function Initialize()
1201
    {
1202
        switch($this->server)
1203
        {
1204
            case '';
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1205
                break;
1206
1207
            case 'Bitbucket':
1208
                $this->oauth_version = '1.0a';
1209
                $this->request_token_url = 'https://bitbucket.org/!api/1.0/oauth/request_token';
1210
                $this->dialog_url = 'https://bitbucket.org/!api/1.0/oauth/authenticate';
1211
                $this->append_state_to_redirect_uri = '';
1212
                $this->access_token_url = 'https://bitbucket.org/!api/1.0/oauth/access_token';
1213
                $this->authorization_header = true;
1214
                $this->url_parameters = true;
1215
                break;
1216
1217
            case 'Dropbox':
1218
                $this->oauth_version = '1.0';
1219
                $this->request_token_url = 'https://api.dropbox.com/1/oauth/request_token';
1220
                $this->dialog_url = 'https://www.dropbox.com/1/oauth/authorize';
1221
                $this->append_state_to_redirect_uri = '';
1222
                $this->access_token_url = 'https://api.dropbox.com/1/oauth/access_token';
1223
                $this->authorization_header = false;
1224
                $this->url_parameters = false;
1225
                break;
1226
1227
            case 'Facebook':
1228
                $this->oauth_version = '2.0';
1229
                $this->request_token_url = '';
1230
                $this->dialog_url = 'https://www.facebook.com/dialog/oauth?client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&scope={SCOPE}&state={STATE}';
1231
                $this->append_state_to_redirect_uri = '';
1232
                $this->access_token_url = 'https://graph.facebook.com/oauth/access_token';
1233
                $this->authorization_header = true;
1234
                $this->url_parameters = false;
1235
                break;
1236
1237
            case 'Fitbit':
1238
                $this->oauth_version = '1.0a';
1239
                $this->request_token_url = 'http://api.fitbit.com/oauth/request_token';
1240
                $this->dialog_url = 'http://api.fitbit.com/oauth/authorize';
1241
                $this->append_state_to_redirect_uri = '';
1242
                $this->access_token_url = 'http://api.fitbit.com/oauth/access_token';
1243
                $this->authorization_header = true;
1244
                $this->url_parameters = false;
1245
                break;
1246
1247
            case 'Flickr':
1248
                $this->oauth_version = '1.0a';
1249
                $this->request_token_url = 'http://www.flickr.com/services/oauth/request_token';
1250
                $this->dialog_url = 'http://www.flickr.com/services/oauth/authorize';
1251
                $this->append_state_to_redirect_uri = '';
1252
                $this->access_token_url = 'http://www.flickr.com/services/oauth/access_token';
1253
                $this->authorization_header = false;
1254
                $this->url_parameters = false;
1255
                break;
1256
1257
            case 'Foursquare':
1258
                $this->oauth_version = '2.0';
1259
                $this->request_token_url = '';
1260
                $this->dialog_url = 'https://foursquare.com/oauth2/authorize?client_id={CLIENT_ID}&scope={SCOPE}&response_type=code&redirect_uri={REDIRECT_URI}&state={STATE}';
1261
                $this->append_state_to_redirect_uri = '';
1262
                $this->access_token_url = 'https://foursquare.com/oauth2/access_token';
1263
                $this->authorization_header = true;
1264
                $this->url_parameters = false;
1265
                break;
1266
1267
            case 'github':
1268
                $this->oauth_version = '2.0';
1269
                $this->request_token_url = '';
1270
                $this->dialog_url = 'https://github.com/login/oauth/authorize?client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&scope={SCOPE}&state={STATE}';
1271
                $this->append_state_to_redirect_uri = '';
1272
                $this->access_token_url = 'https://github.com/login/oauth/access_token';
1273
                $this->authorization_header = true;
1274
                $this->url_parameters = false;
1275
                break;
1276
1277
            case 'Google':
1278
                $this->oauth_version = '2.0';
1279
                $this->request_token_url = '';
1280
                $this->dialog_url = 'https://accounts.google.com/o/oauth2/auth?response_type=code&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&scope={SCOPE}&state={STATE}';
1281
                $this->append_state_to_redirect_uri = '';
1282
                $this->access_token_url = 'https://accounts.google.com/o/oauth2/token';
1283
                $this->authorization_header = true;
1284
                $this->url_parameters = false;
1285
                break;
1286
1287
            case 'Instagram':
1288
                $this->oauth_version = '2.0';
1289
                $this->request_token_url = '';
1290
                $this->dialog_url ='https://api.instagram.com/oauth/authorize/?client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&scope={SCOPE}&response_type=code&state={STATE}';
1291
                $this->append_state_to_redirect_uri = '';
1292
                $this->access_token_url = 'https://api.instagram.com/oauth/access_token';
1293
                $this->authorization_header = true;
1294
                $this->url_parameters = false;
1295
                break;
1296
1297
            case 'LinkedIn':
1298
                $this->oauth_version = '1.0a';
1299
                $this->request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken?scope={SCOPE}';
1300
                $this->dialog_url = 'https://api.linkedin.com/uas/oauth/authenticate';
1301
                $this->access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken';
1302
                $this->append_state_to_redirect_uri = '';
1303
                $this->authorization_header = true;
1304
                $this->url_parameters = true;
1305
                break;
1306
1307
            case 'Microsoft':
1308
                $this->oauth_version = '2.0';
1309
                $this->request_token_url = '';
1310
                $this->dialog_url = 'https://login.live.com/oauth20_authorize.srf?client_id={CLIENT_ID}&scope={SCOPE}&response_type=code&redirect_uri={REDIRECT_URI}&state={STATE}';
1311
                $this->append_state_to_redirect_uri = '';
1312
                $this->access_token_url = 'https://login.live.com/oauth20_token.srf';
1313
                $this->authorization_header = true;
1314
                $this->url_parameters = false;
1315
                break;
1316
1317
            case 'Scoop.it':
1318
                $this->oauth_version = '1.0a';
1319
                $this->request_token_url = 'https://www.scoop.it/oauth/request';
1320
                $this->dialog_url = 'https://www.scoop.it/oauth/authorize';
1321
                $this->append_state_to_redirect_uri = '';
1322
                $this->access_token_url = 'https://www.scoop.it/oauth/access';
1323
                $this->authorization_header = false;
1324
                $this->url_parameters = false;
1325
                break;
1326
1327
            case 'Tumblr':
1328
                $this->oauth_version = '1.0a';
1329
                $this->request_token_url = 'http://www.tumblr.com/oauth/request_token';
1330
                $this->dialog_url = 'http://www.tumblr.com/oauth/authorize';
1331
                $this->append_state_to_redirect_uri = '';
1332
                $this->access_token_url = 'http://www.tumblr.com/oauth/access_token';
1333
                $this->authorization_header = true;
1334
                $this->url_parameters = false;
1335
                break;
1336
1337
            case 'Twitter':
1338
                $this->oauth_version = '1.0a';
1339
                $this->request_token_url = 'https://api.twitter.com/oauth/request_token';
1340
                $this->dialog_url = 'https://api.twitter.com/oauth/authenticate';
1341
                $this->append_state_to_redirect_uri = '';
1342
                $this->access_token_url = 'https://api.twitter.com/oauth/access_token';
1343
                $this->authorization_header = true;
1344
                $this->url_parameters = true;
1345
                break;
1346
1347
            case 'Yahoo':
1348
                $this->oauth_version = '1.0a';
1349
                $this->request_token_url = 'https://api.login.yahoo.com/oauth/v2/get_request_token';
1350
                $this->dialog_url = 'https://api.login.yahoo.com/oauth/v2/request_auth';
1351
                $this->access_token_url = 'https://api.login.yahoo.com/oauth/v2/get_token';
1352
                $this->append_state_to_redirect_uri = '';
1353
                $this->authorization_header = false;
1354
                $this->url_parameters = false;
1355
                break;
1356
1357
            default:
1358
                return($this->SetError($this->server.' is not yet a supported type of OAuth server. Please contact the author Manuel Lemos <[email protected]> to request adding built-in support to this type of OAuth server.'));
1359
        }
1360
        return(true);
1361
    }
1362
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1363
{metadocument}
1364
        </do>
1365
    </function>
1366
{/metadocument}
1367
*/
1368
1369
/*
1370
{metadocument}
1371
    <function>
1372
        <name>Process</name>
1373
        <type>BOOLEAN</type>
1374
        <documentation>
1375
            <purpose>Process the OAuth protocol interaction with the OAuth
1376
                server.</purpose>
1377
            <usage>Call this function when you need to retrieve the OAuth access
1378
                token. Check the <variablelink>access_token</variablelink> to
1379
                determine if the access token was obtained successfully.</usage>
1380
            <returnvalue>This function returns <booleanvalue>1</booleanvalue> if
1381
                the OAuth protocol was processed without errors.</returnvalue>
1382
        </documentation>
1383
        <do>
1384
{/metadocument}
1385
*/
1386
    Function Process()
0 ignored issues
show
Coding Style introduced by
Process uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
1387
    {
1388
        switch(intval($this->oauth_version))
1389
        {
1390
            case 1:
1391
                $one_a = ($this->oauth_version === '1.0a');
1392
                if($this->debug)
1393
                    $this->OutputDebug('Checking the OAuth token authorization state');
1394
                if(!$this->GetAccessToken($access_token))
1395
                    return false;
1396
                if(IsSet($access_token['authorized'])
1397
                && IsSet($access_token['value']))
1398
                {
1399
                    $expired = (IsSet($access_token['expiry']) && strcmp($access_token['expiry'], gmstrftime('%Y-%m-%d %H:%M:%S')) <= 0);
1400
                    if(!$access_token['authorized']
1401
                    || $expired)
1402
                    {
1403
                        if($this->debug)
1404
                        {
1405
                            if($expired)
1406
                                $this->OutputDebug('The OAuth token expired on '.$access_token['expiry'].'UTC');
1407
                            else
1408
                                $this->OutputDebug('The OAuth token is not yet authorized');
1409
                            $this->OutputDebug('Checking the OAuth token and verifier');
1410
                        }
1411
                        if(!$this->GetRequestToken($token, $verifier))
1412
                            return false;
1413
                        if(!IsSet($token)
1414
                        || ($one_a
1415
                        && !IsSet($verifier)))
1416
                        {
1417
                            if(!$this->GetRequestDenied($denied))
1418
                                return false;
1419
                            if(IsSet($denied)
1420
                            && $denied === $access_token['value'])
1421
                            {
1422
                                if($this->debug)
1423
                                    $this->OutputDebug('The authorization request was denied');
1424
                                $this->authorization_error = 'the request was denied';
1425
                                return true;
1426
                            }
1427
                            else
1428
                            {
1429
                                if($this->debug)
1430
                                    $this->OutputDebug('Reset the OAuth token state because token and verifier are not both set');
1431
                                $access_token = array();
1432
                            }
1433
                        }
1434
                        elseif($token !== $access_token['value'])
1435
                        {
1436
                            if($this->debug)
1437
                                $this->OutputDebug('Reset the OAuth token state because token does not match what as previously retrieved');
1438
                            $access_token = array();
1439
                        }
1440
                        else
1441
                        {
1442
                            if(!$this->GetAccessTokenURL($url))
1443
                                return false;
1444
                            $oauth = array(
1445
                                'oauth_token'=>$token,
1446
                            );
1447
                            if($one_a)
1448
                                $oauth['oauth_verifier'] = $verifier;
1449
                            $this->access_token_secret = $access_token['secret'];
1450
                            if(!$this->SendAPIRequest($url, 'GET', array(), $oauth, array('Resource'=>'OAuth access token'), $response))
1451
                                return false;
1452
                            if(strlen($this->access_token_error))
1453
                            {
1454
                                $this->authorization_error = $this->access_token_error;
1455
                                return true;
1456
                            }
1457
                            if(!IsSet($response['oauth_token'])
1458
                            || !IsSet($response['oauth_token_secret']))
1459
                            {
1460
                                $this->authorization_error= 'it was not returned the access token and secret';
1461
                                return true;
1462
                            }
1463
                            $access_token = array(
1464
                                'value'=>$response['oauth_token'],
1465
                                'secret'=>$response['oauth_token_secret'],
1466
                                'authorized'=>true
1467
                            );
1468
                            if(IsSet($response['oauth_expires_in']))
1469
                            {
1470
                                $expires = $response['oauth_expires_in'];
1471
                                if(strval($expires) !== strval(intval($expires))
1472
                                || $expires <= 0)
1473
                                    return($this->SetError('OAuth server did not return a supported type of access token expiry time'));
1474
                                $this->access_token_expiry = gmstrftime('%Y-%m-%d %H:%M:%S', time() + $expires);
1475
                                if($this->debug)
1476
                                    $this->OutputDebug('Access token expiry: '.$this->access_token_expiry.' UTC');
1477
                                $access_token['expiry'] = $this->access_token_expiry;
1478
                            }
1479
                            else
1480
                                $this->access_token_expiry = '';
1481
1482
                            if(!$this->StoreAccessToken($access_token))
1483
                                return false;
1484
                            if($this->debug)
1485
                                $this->OutputDebug('The OAuth token was authorized');
1486
                        }
1487
                    }
1488
                    elseif($this->debug)
1489
                        $this->OutputDebug('The OAuth token was already authorized');
1490
                    if(IsSet($access_token['authorized'])
1491
                    && $access_token['authorized'])
1492
                    {
1493
                        $this->access_token = $access_token['value'];
1494
                        $this->access_token_secret = $access_token['secret'];
1495
                        return true;
1496
                    }
1497
                }
1498
                else
1499
                {
1500
                    if($this->debug)
1501
                        $this->OutputDebug('The OAuth access token is not set');
1502
                    $access_token = array();
1503
                }
1504
                if(!IsSet($access_token['authorized']))
1505
                {
1506
                    if($this->debug)
1507
                        $this->OutputDebug('Requesting the unauthorized OAuth token');
1508
                    if(!$this->GetRequestTokenURL($url))
1509
                        return false;
1510
                    $url = str_replace('{SCOPE}', UrlEncode($this->scope), $url);
1511
                    if(!$this->GetRedirectURI($redirect_uri))
1512
                        return false;
1513
                    $oauth = array(
1514
                        'oauth_callback'=>$redirect_uri,
1515
                    );
1516
                    if(!$this->SendAPIRequest($url, 'GET', array(), $oauth, array('Resource'=>'OAuth request token'), $response))
1517
                        return false;
1518
                    if(strlen($this->access_token_error))
1519
                    {
1520
                        $this->authorization_error = $this->access_token_error;
1521
                        return true;
1522
                    }
1523
                    if(!IsSet($response['oauth_token'])
1524
                    || !IsSet($response['oauth_token_secret']))
1525
                    {
1526
                        $this->authorization_error = 'it was not returned the requested token';
1527
                        return true;
1528
                    }
1529
                    $access_token = array(
1530
                        'value'=>$response['oauth_token'],
1531
                        'secret'=>$response['oauth_token_secret'],
1532
                        'authorized'=>false
1533
                    );
1534
                    if(!$this->StoreAccessToken($access_token))
1535
                        return false;
1536
                }
1537
                if(!$this->GetDialogURL($url))
1538
                    return false;
1539
                $url .= '?oauth_token='.$access_token['value'];
1540
                if(!$one_a)
1541
                {
1542
                    if(!$this->GetRedirectURI($redirect_uri))
1543
                        return false;
1544
                    $url .= '&oauth_callback='.UrlEncode($redirect_uri);
1545
                }
1546
                if($this->debug)
1547
                    $this->OutputDebug('Redirecting to OAuth authorize page '.$url);
1548
                Header('HTTP/1.0 302 OAuth Redirection');
1549
                Header('Location: '.$url);
1550
                $this->exit = true;
1551
                return true;
1552
1553
            case 2:
1554
                if($this->debug)
1555
                    $this->OutputDebug('Checking if OAuth access token was already retrieved from '.$this->access_token_url);
1556
                if(!$this->GetAccessToken($access_token))
1557
                    return false;
1558
                if(IsSet($access_token['value']))
1559
                {
1560
                    if(IsSet($access_token['expiry'])
1561
                    && strcmp($this->access_token_expiry = $access_token['expiry'], gmstrftime('%Y-%m-%d %H:%M:%S')) < 0)
1562
                    {
1563
                        if($this->debug)
1564
                            $this->OutputDebug('The OAuth access token expired in '.$this->access_token_expiry);
1565
                    }
1566
                    else
1567
                    {
1568
                        $this->access_token = $access_token['value'];
1569
                        if(IsSet($access_token['type']))
1570
                            $this->access_token_type = $access_token['type'];
1571
                        if($this->debug)
1572
                            $this->OutputDebug('The OAuth access token '.$this->access_token.' is valid');
1573
                        if(strlen($this->access_token_type)
1574
                        && $this->debug)
1575
                            $this->OutputDebug('The OAuth access token is of type '.$this->access_token_type);
1576
                        return true;
1577
                    }
1578
                }
1579
                if($this->debug)
1580
                    $this->OutputDebug('Checking the authentication state in URI '.$_SERVER['REQUEST_URI']);
1581
                if(!$this->GetStoredState($stored_state))
1582
                    return false;
1583
                if(strlen($stored_state) == 0)
1584
                    return($this->SetError('it was not set the OAuth state'));
1585
                if(!$this->GetRequestState($state))
1586
                    return false;
1587
                if($state === $stored_state)
1588
                {
1589
                    if($this->debug)
1590
                        $this->OutputDebug('Checking the authentication code');
1591
                    if(!$this->GetRequestCode($code))
1592
                        return false;
1593
                    if(strlen($code) == 0)
1594
                    {
1595
                        if(!$this->GetRequestError($this->authorization_error))
1596
                            return false;
1597
                        if(IsSet($this->authorization_error))
1598
                        {
1599
                            if($this->debug)
1600
                                $this->OutputDebug('Authorization failed with error code '.$this->authorization_error);
1601
                            switch($this->authorization_error)
1602
                            {
1603
                                case 'invalid_request':
1604
                                case 'unauthorized_client':
1605
                                case 'access_denied':
1606
                                case 'unsupported_response_type':
1607
                                case 'invalid_scope':
1608
                                case 'server_error':
1609
                                case 'temporarily_unavailable':
1610
                                case 'user_denied':
1611
                                    return true;
1612
                                default:
1613
                                    return($this->SetError('it was returned an unknown OAuth error code'));
1614
                            }
1615
                        }
1616
                        return($this->SetError('it was not returned the OAuth dialog code'));
1617
                    }
1618
                    if(!$this->GetAccessTokenURL($url))
1619
                        return false;
1620
                    if(!$this->GetRedirectURI($redirect_uri))
1621
                        return false;
1622
                    $values = array(
1623
                        'code'=>$code,
1624
                        'client_id'=>$this->client_id,
1625
                        'client_secret'=>$this->client_secret,
1626
                        'redirect_uri'=>$redirect_uri,
1627
                        'grant_type'=>'authorization_code'
1628
                    );
1629
                    if(!$this->SendAPIRequest($url, 'POST', $values, null, array('Resource'=>'OAuth access token', 'ConvertObjects'=>true), $response))
1630
                        return false;
1631
                    if(strlen($this->access_token_error))
1632
                    {
1633
                        $this->authorization_error = $this->access_token_error;
1634
                        return true;
1635
                    }
1636
                    if(!IsSet($response['access_token']))
1637
                    {
1638
                        if(IsSet($response['error']))
1639
                        {
1640
                            $this->authorization_error = 'it was not possible to retrieve the access token: it was returned the error: '.$response['error'];
1641
                            return true;
1642
                        }
1643
                        return($this->SetError('OAuth server did not return the access token'));
1644
                    }
1645
                    $access_token = array(
1646
                        'value'=>$this->access_token = $response['access_token'],
1647
                        'authorized'=>true
1648
                    );
1649
                    if($this->debug)
1650
                        $this->OutputDebug('Access token: '.$this->access_token);
1651
                    if(IsSet($response['expires'])
1652
                    || IsSet($response['expires_in']))
1653
                    {
1654
                        $expires = (IsSet($response['expires']) ? $response['expires'] : $response['expires_in']);
1655
                        if(strval($expires) !== strval(intval($expires))
1656
                        || $expires <= 0)
1657
                            return($this->SetError('OAuth server did not return a supported type of access token expiry time'));
1658
                        $this->access_token_expiry = gmstrftime('%Y-%m-%d %H:%M:%S', time() + $expires);
1659
                        if($this->debug)
1660
                            $this->OutputDebug('Access token expiry: '.$this->access_token_expiry.' UTC');
1661
                        $access_token['expiry'] = $this->access_token_expiry;
1662
                    }
1663
                    else
1664
                        $this->access_token_expiry = '';
1665
                    if(IsSet($response['token_type']))
1666
                    {
1667
                        $this->access_token_type = $response['token_type'];
1668
                        if($this->debug)
1669
                            $this->OutputDebug('Access token type: '.$this->access_token_type);
1670
                        $access_token['type'] = $this->access_token_type;
1671
                    }
1672
                    else
1673
                        $this->access_token_type = '';
1674
                    if(!$this->StoreAccessToken($access_token))
1675
                        return false;
1676
                }
1677
                else
1678
                {
1679
                    if(!$this->GetDialogURL($url))
1680
                        return false;
1681
                    if(strlen($url) == 0)
1682
                        return($this->SetError('it was not set the OAuth dialog URL'));
1683
                    if(!$this->GetRedirectURI($redirect_uri))
1684
                        return false;
1685
                    if(strlen($this->append_state_to_redirect_uri))
1686
                        $redirect_uri .= (strpos($redirect_uri, '?') === false ? '?' : '&').$this->append_state_to_redirect_uri.'='.$stored_state;
1687
                    $url = str_replace(
1688
                        '{REDIRECT_URI}', UrlEncode($redirect_uri), str_replace(
1689
                        '{CLIENT_ID}', UrlEncode($this->client_id), str_replace(
1690
                        '{SCOPE}', UrlEncode($this->scope), str_replace(
1691
                        '{STATE}', UrlEncode($stored_state),
1692
                        $url))));
1693
                    if($this->debug)
1694
                        $this->OutputDebug('Redirecting to OAuth Dialog '.$url);
1695
                    Header('HTTP/1.0 302 OAuth Redirection');
1696
                    Header('Location: '.$url);
1697
                    $this->exit = true;
1698
                }
1699
                break;
1700
1701
            default:
1702
                return($this->SetError($this->oauth_version.' is not a supported version of the OAuth protocol'));
1703
        }
1704
        return(true);
1705
    }
1706
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1707
{metadocument}
1708
        </do>
1709
    </function>
1710
{/metadocument}
1711
*/
1712
1713
/*
1714
{metadocument}
1715
    <function>
1716
        <name>Finalize</name>
1717
        <type>BOOLEAN</type>
1718
        <documentation>
1719
            <purpose>Cleanup any resources that may have been used during the
1720
                OAuth protocol processing or execution of API calls.</purpose>
1721
            <usage>Always call this function as the last step after calling the
1722
                functions <functionlink>Process</functionlink> or
1723
                <functionlink>CallAPI</functionlink>.</usage>
1724
            <returnvalue>This function returns <booleanvalue>1</booleanvalue> if
1725
                the function cleaned up any resources successfully.</returnvalue>
1726
        </documentation>
1727
        <argument>
1728
            <name>success</name>
1729
            <type>BOOLEAN</type>
1730
            <documentation>
1731
                <purpose>Pass the last success state returned by the class or any
1732
                    external code processing the class function results.</purpose>
1733
            </documentation>
1734
        </argument>
1735
        <do>
1736
{/metadocument}
1737
*/
1738
    Function Finalize($success)
1739
    {
1740
        return($success);
1741
    }
1742
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1743
{metadocument}
1744
        </do>
1745
    </function>
1746
{/metadocument}
1747
*/
1748
1749
/*
1750
{metadocument}
1751
    <function>
1752
        <name>Output</name>
1753
        <type>VOID</type>
1754
        <documentation>
1755
            <purpose>Display the results of the OAuth protocol processing.</purpose>
1756
            <usage>Only call this function if you are debugging the OAuth
1757
                authorization process and you need to view what was its
1758
                results.</usage>
1759
        </documentation>
1760
        <do>
1761
{/metadocument}
1762
*/
1763
    Function Output()
1764
    {
1765
        if(strlen($this->authorization_error)
1766
        || strlen($this->access_token_error)
1767
        || strlen($this->access_token))
1768
        {
1769
?>
1770
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
1771
<html>
1772
<head>
1773
<title>OAuth client result</title>
1774
</head>
1775
<body>
1776
<h1>OAuth client result</h1>
1777
<?php
1778
            if(strlen($this->authorization_error))
1779
            {
1780
?>
1781
<p>It was not possible to authorize the application.<?php
1782
                if($this->debug)
1783
                {
1784
?>
1785
<br>Authorization error: <?php echo HtmlSpecialChars($this->authorization_error);
1786
                }
1787
?></p>
1788
<?php
1789
            }
1790
            elseif(strlen($this->access_token_error))
1791
            {
1792
?>
1793
<p>It was not possible to use the application access token.
1794
<?php
1795
                if($this->debug)
1796
                {
1797
?>
1798
<br>Error: <?php echo HtmlSpecialChars($this->access_token_error);
1799
                }
1800
?></p>
1801
<?php
1802
            }
1803
            elseif(strlen($this->access_token))
1804
            {
1805
?>
1806
<p>The application authorization was obtained successfully.
1807
<?php
1808
                if($this->debug)
1809
                {
1810
?>
1811
<br>Access token: <?php echo HtmlSpecialChars($this->access_token);
1812
                    if(IsSet($this->access_token_secret))
1813
                    {
1814
?>
1815
<br>Access token secret: <?php echo HtmlSpecialChars($this->access_token_secret);
1816
                    }
1817
                }
1818
?></p>
1819
<?php
1820
                if(strlen($this->access_token_expiry))
1821
                {
1822
?>
1823
<p>Access token expiry: <?php echo $this->access_token_expiry; ?> UTC</p>
1824
<?php
1825
                }
1826
            }
1827
?>
1828
</body>
1829
</html>
1830
<?php
1831
        }
1832
    }
1833
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1834
{metadocument}
1835
        </do>
1836
    </function>
1837
{/metadocument}
1838
*/
1839
1840
};
1841
1842
/*
1843
1844
{metadocument}
1845
</class>
1846
{/metadocument}
1847
1848
*/
1849
1850
?>
1851