/src/libraries/joomla/twitter/oauth.php

https://bitbucket.org/ke2083/transfans.co.uk-website · PHP · 133 lines · 55 code · 18 blank · 60 comment · 6 complexity · 1fbdd58916b996cb52e82da019531edc MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Twitter
  5. *
  6. * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. use Joomla\Registry\Registry;
  11. /**
  12. * Joomla Platform class for generating Twitter API access token.
  13. *
  14. * @since 3.1.4
  15. * @deprecated 4.0 Use the `joomla/twitter` package via Composer instead
  16. */
  17. class JTwitterOAuth extends JOAuth1Client
  18. {
  19. /**
  20. * @var Registry Options for the JTwitterOauth object.
  21. * @since 3.1.4
  22. */
  23. protected $options;
  24. /**
  25. * Constructor.
  26. *
  27. * @param Registry $options JTwitterOauth options object.
  28. * @param JHttp $client The HTTP client object.
  29. * @param JInput $input The input object.
  30. * @param JApplicationWeb $application The application object.
  31. *
  32. * @since 3.1.4
  33. */
  34. public function __construct(Registry $options = null, JHttp $client = null, JInput $input = null, JApplicationWeb $application = null)
  35. {
  36. $this->options = isset($options) ? $options : new Registry;
  37. $this->options->def('accessTokenURL', 'https://api.twitter.com/oauth/access_token');
  38. $this->options->def('authenticateURL', 'https://api.twitter.com/oauth/authenticate');
  39. $this->options->def('authoriseURL', 'https://api.twitter.com/oauth/authorize');
  40. $this->options->def('requestTokenURL', 'https://api.twitter.com/oauth/request_token');
  41. // Call the JOAuth1Client constructor to setup the object.
  42. parent::__construct($this->options, $client, $input, $application);
  43. }
  44. /**
  45. * Method to verify if the access token is valid by making a request.
  46. *
  47. * @return boolean Returns true if the access token is valid and false otherwise.
  48. *
  49. * @since 3.1.4
  50. */
  51. public function verifyCredentials()
  52. {
  53. $token = $this->getToken();
  54. // Set the parameters.
  55. $parameters = array('oauth_token' => $token['key']);
  56. // Set the API base
  57. $path = 'https://api.twitter.com/1.1/account/verify_credentials.json';
  58. // Send the request.
  59. $response = $this->oauthRequest($path, 'GET', $parameters);
  60. // Verify response
  61. if ($response->code == 200)
  62. {
  63. return true;
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
  70. /**
  71. * Ends the session of the authenticating user, returning a null cookie.
  72. *
  73. * @return array The decoded JSON response
  74. *
  75. * @since 3.1.4
  76. */
  77. public function endSession()
  78. {
  79. $token = $this->getToken();
  80. // Set parameters.
  81. $parameters = array('oauth_token' => $token['key']);
  82. // Set the API base
  83. $path = 'https://api.twitter.com/1.1/account/end_session.json';
  84. // Send the request.
  85. $response = $this->oauthRequest($path, 'POST', $parameters);
  86. return json_decode($response->body);
  87. }
  88. /**
  89. * Method to validate a response.
  90. *
  91. * @param string $url The request URL.
  92. * @param JHttpResponse $response The response to validate.
  93. *
  94. * @return void
  95. *
  96. * @since 3.1.4
  97. * @throws DomainException
  98. */
  99. public function validateResponse($url, $response)
  100. {
  101. if (strpos($url, 'verify_credentials') === false && $response->code != 200)
  102. {
  103. $error = json_decode($response->body);
  104. if (property_exists($error, 'error'))
  105. {
  106. throw new DomainException($error->error);
  107. }
  108. else
  109. {
  110. $error = $error->errors;
  111. throw new DomainException($error[0]->message, $error[0]->code);
  112. }
  113. }
  114. }
  115. }