1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Twitter
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 use Joomla\Registry\Registry;
13
14 /**
15 * Twitter API object class for the Joomla Platform.
16 *
17 * @since 12.3
18 * @deprecated 4.0 Use the `joomla/twitter` package via Composer instead
19 */
20 abstract class JTwitterObject
21 {
22 /**
23 * @var Registry Options for the Twitter object.
24 * @since 12.3
25 */
26 protected $options;
27
28 /**
29 * @var JHttp The HTTP client object to use in sending HTTP requests.
30 * @since 12.3
31 */
32 protected $client;
33
34 /**
35 * @var JTwitterOAuth The OAuth client.
36 * @since 12.3
37 */
38 protected $oauth;
39
40 /**
41 * Constructor.
42 *
43 * @param Registry &$options Twitter options object.
44 * @param JHttp $client The HTTP client object.
45 * @param JTwitterOAuth $oauth The OAuth client.
46 *
47 * @since 12.3
48 */
49 public function __construct(Registry &$options = null, JHttp $client = null, JTwitterOAuth $oauth = null)
50 {
51 $this->options = isset($options) ? $options : new Registry;
52 $this->client = isset($client) ? $client : new JHttp($this->options);
53 $this->oauth = $oauth;
54 }
55
56 /**
57 * Method to check the rate limit for the requesting IP address
58 *
59 * @param string $resource A resource or a comma-separated list of resource families you want to know the current rate limit disposition for.
60 * @param string $action An action for the specified resource, if only one resource is specified.
61 *
62 * @return void
63 *
64 * @since 12.3
65 * @throws RuntimeException
66 */
67 public function checkRateLimit($resource = null, $action = null)
68 {
69 // Check the rate limit for remaining hits
70 $rate_limit = $this->getRateLimit($resource);
71
72 $property = '/' . $resource;
73
74 if (!is_null($action))
75 {
76 $property .= '/' . $action;
77 }
78
79 if ($rate_limit->resources->$resource->$property->remaining == 0)
80 {
81 // The IP has exceeded the Twitter API rate limit
82 throw new RuntimeException('This server has exceed the Twitter API rate limit for the given period. The limit will reset at '
83 . $rate_limit->resources->$resource->$property->reset
84 );
85 }
86 }
87
88 /**
89 * Method to build and return a full request URL for the request. This method will
90 * add appropriate pagination details if necessary and also prepend the API url
91 * to have a complete URL for the request.
92 *
93 * @param string $path URL to inflect
94 * @param array $parameters The parameters passed in the URL.
95 *
96 * @return string The request URL.
97 *
98 * @since 12.3
99 */
100 public function fetchUrl($path, $parameters = null)
101 {
102 if ($parameters)
103 {
104 foreach ($parameters as $key => $value)
105 {
106 if (strpos($path, '?') === false)
107 {
108 $path .= '?' . $key . '=' . $value;
109 }
110 else
111 {
112 $path .= '&' . $key . '=' . $value;
113 }
114 }
115 }
116
117 // Get a new JUri object fousing the api url and given path.
118 if (strpos($path, 'http://search.twitter.com/search.json') === false)
119 {
120 $uri = new JUri($this->options->get('api.url') . $path);
121 }
122 else
123 {
124 $uri = new JUri($path);
125 }
126
127 return (string) $uri;
128 }
129
130 /**
131 * Method to retrieve the rate limit for the requesting IP address
132 *
133 * @param string $resource A resource or a comma-separated list of resource families you want to know the current rate limit disposition for.
134 *
135 * @return array The JSON response decoded
136 *
137 * @since 12.3
138 */
139 public function getRateLimit($resource)
140 {
141 // Build the request path.
142 $path = '/application/rate_limit_status.json';
143
144 if (!is_null($resource))
145 {
146 return $this->sendRequest($path, 'GET', array('resources' => $resource));
147 }
148
149 return $this->sendRequest($path);
150 }
151
152 /**
153 * Method to send the request.
154 *
155 * @param string $path The path of the request to make
156 * @param string $method The request method.
157 * @param mixed $data Either an associative array or a string to be sent with the post request.
158 * @param array $headers An array of name-value pairs to include in the header of the request
159 *
160 * @return array The decoded JSON response
161 *
162 * @since 12.3
163 * @throws RuntimeException
164 */
165 public function sendRequest($path, $method = 'GET', $data = array(), $headers = array())
166 {
167 // Get the access token.
168 $token = $this->oauth->getToken();
169
170 // Set parameters.
171 $parameters['oauth_token'] = $token['key'];
172
173 // Send the request.
174 $response = $this->oauth->oauthRequest($this->fetchUrl($path), $method, $parameters, $data, $headers);
175
176 if (strpos($path, 'update_with_media') !== false)
177 {
178 // Check Media Rate Limit.
179 $response_headers = $response->headers;
180
181 if ($response_headers['x-mediaratelimit-remaining'] == 0)
182 {
183 // The IP has exceeded the Twitter API media rate limit
184 throw new RuntimeException('This server has exceed the Twitter API media rate limit for the given period. The limit will reset in '
185 . $response_headers['x-mediaratelimit-reset'] . 'seconds.'
186 );
187 }
188 }
189
190 if (strpos($response->body, 'redirected') !== false)
191 {
192 return $response->headers['Location'];
193 }
194
195 return json_decode($response->body);
196 }
197
198 /**
199 * Get an option from the JTwitterObject instance.
200 *
201 * @param string $key The name of the option to get.
202 *
203 * @return mixed The option value.
204 *
205 * @since 12.3
206 */
207 public function getOption($key)
208 {
209 return $this->options->get($key);
210 }
211
212 /**
213 * Set an option for the JTwitterObject instance.
214 *
215 * @param string $key The name of the option to set.
216 * @param mixed $value The option value to set.
217 *
218 * @return JTwitterObject This object for method chaining.
219 *
220 * @since 12.3
221 */
222 public function setOption($key, $value)
223 {
224 $this->options->set($key, $value);
225
226 return $this;
227 }
228 }
229