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