Complex classes like oauth_client_class often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use oauth_client_class, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 = ''; |
||
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; |
||
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; |
||
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; |
||
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 = ''; |
||
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: '; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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'; |
||
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; |
||
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; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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 = ''; |
||
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; |
||
587 | |||
588 | var $oauth_user_agent = 'PHP-OAuth-API (http://www.phpclasses.org/oauth-api $Revision: 1.46 $)'; |
||
589 | var $session_started = false; |
||
590 | |||
591 | Function SetError($error) |
||
598 | |||
599 | Function SetPHPError($error, &$php_error_message) |
||
606 | |||
607 | Function OutputDebug($message) |
||
617 | |||
618 | Function GetRequestTokenURL(&$request_token_url) |
||
623 | |||
624 | Function GetDialogURL(&$redirect_url) |
||
629 | |||
630 | Function GetAccessTokenURL(&$access_token_url) |
||
635 | |||
636 | Function GetStoredState(&$state) |
||
644 | |||
645 | Function GetRequestState(&$state) |
||
651 | |||
652 | Function GetRequestCode(&$code) |
||
657 | |||
658 | Function GetRequestError(&$error) |
||
663 | |||
664 | Function GetRequestDenied(&$denied) |
||
669 | |||
670 | Function GetRequestToken(&$token, &$verifier) |
||
676 | |||
677 | Function GetRedirectURI(&$redirect_uri) |
||
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) |
||
732 | /* |
||
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) |
||
786 | /* |
||
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() |
||
833 | /* |
||
834 | {metadocument} |
||
835 | </do> |
||
836 | </function> |
||
837 | {/metadocument} |
||
838 | */ |
||
839 | |||
840 | Function Encode($value) |
||
844 | |||
845 | Function EncodeArray($array) |
||
851 | |||
852 | Function HMAC($function, $data, $key) |
||
870 | |||
871 | Function SendAPIRequest($url, $method, $parameters, $oauth, $options, &$response) |
||
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>*/*</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) |
||
1173 | /* |
||
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() |
||
1362 | /* |
||
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() |
||
1706 | /* |
||
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) |
||
1742 | /* |
||
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() |
||
1833 | /* |
||
1834 | {metadocument} |
||
1835 | </do> |
||
1836 | </function> |
||
1837 | {/metadocument} |
||
1838 | */ |
||
1839 | |||
1840 | }; |
||
1841 | |||
1851 |
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.