1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Facebook
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 * Facebook API object class for the Joomla Platform.
16 *
17 * @since 13.1
18 * @deprecated 4.0 Use the `joomla/facebook` package via Composer instead
19 */
20 abstract class JFacebookObject
21 {
22 /**
23 * @var Registry Options for the Facebook object.
24 * @since 13.1
25 */
26 protected $options;
27
28 /**
29 * @var JHttp The HTTP client object to use in sending HTTP requests.
30 * @since 13.1
31 */
32 protected $client;
33
34 /**
35 * @var JFacebookOAuth The OAuth client.
36 * @since 13.1
37 */
38 protected $oauth;
39
40 /**
41 * Constructor.
42 *
43 * @param Registry $options Facebook options object.
44 * @param JHttp $client The HTTP client object.
45 * @param JFacebookOAuth $oauth The OAuth client.
46 *
47 * @since 13.1
48 */
49 public function __construct(Registry $options = null, JHttp $client = null, JFacebookOAuth $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 build and return a full request URL for the request. This method will
58 * add appropriate pagination details if necessary and also prepend the API url
59 * to have a complete URL for the request.
60 *
61 * @param string $path URL to inflect.
62 * @param integer $limit The number of objects per page.
63 * @param integer $offset The object's number on the page.
64 * @param timestamp $until A unix timestamp or any date accepted by strtotime.
65 * @param timestamp $since A unix timestamp or any date accepted by strtotime.
66 *
67 * @return string The request URL.
68 *
69 * @since 13.1
70 */
71 protected function fetchUrl($path, $limit = 0, $offset = 0, $until = null, $since = null)
72 {
73 // Get a new JUri object fousing the api url and given path.
74 $uri = new JUri($this->options->get('api.url') . $path);
75
76 if ($limit > 0)
77 {
78 $uri->setVar('limit', (int) $limit);
79 }
80
81 if ($offset > 0)
82 {
83 $uri->setVar('offset', (int) $offset);
84 }
85
86 if ($until != null)
87 {
88 $uri->setVar('until', $until);
89 }
90
91 if ($since != null)
92 {
93 $uri->setVar('since', $since);
94 }
95
96 return (string) $uri;
97 }
98
99 /**
100 * Method to send the request.
101 *
102 * @param string $path The path of the request to make.
103 * @param mixed $data Either an associative array or a string to be sent with the post request.
104 * @param array $headers An array of name-value pairs to include in the header of the request
105 * @param integer $limit The number of objects per page.
106 * @param integer $offset The object's number on the page.
107 * @param string $until A unix timestamp or any date accepted by strtotime.
108 * @param string $since A unix timestamp or any date accepted by strtotime.
109 *
110 * @return mixed The request response.
111 *
112 * @since 13.1
113 * @throws DomainException
114 */
115 public function sendRequest($path, $data = '', array $headers = null, $limit = 0, $offset = 0, $until = null, $since = null)
116 {
117 // Send the request.
118 $response = $this->client->get($this->fetchUrl($path, $limit, $offset, $until, $since), $headers);
119
120 $response = json_decode($response->body);
121
122 // Validate the response.
123 if (property_exists($response, 'error'))
124 {
125 throw new RuntimeException($response->error->message);
126 }
127
128 return $response;
129 }
130
131 /**
132 * Method to get an object.
133 *
134 * @param string $object The object id.
135 *
136 * @return mixed The decoded JSON response or false if the client is not authenticated.
137 *
138 * @since 13.1
139 */
140 public function get($object)
141 {
142 if ($this->oauth != null)
143 {
144 if ($this->oauth->isAuthenticated())
145 {
146 $response = $this->oauth->query($this->fetchUrl($object));
147
148 return json_decode($response->body);
149 }
150 else
151 {
152 return false;
153 }
154 }
155
156 // Send the request.
157 return $this->sendRequest($object);
158 }
159
160 /**
161 * Method to get object's connection.
162 *
163 * @param string $object The object id.
164 * @param string $connection The object's connection name.
165 * @param string $extra_fields URL fields.
166 * @param integer $limit The number of objects per page.
167 * @param integer $offset The object's number on the page.
168 * @param string $until A unix timestamp or any date accepted by strtotime.
169 * @param string $since A unix timestamp or any date accepted by strtotime.
170 *
171 * @return mixed The decoded JSON response or false if the client is not authenticated.
172 *
173 * @since 13.1
174 */
175 public function getConnection($object, $connection = null, $extra_fields = '', $limit = 0, $offset = 0, $until = null, $since = null)
176 {
177 $path = $object . '/' . $connection . $extra_fields;
178
179 if ($this->oauth != null)
180 {
181 if ($this->oauth->isAuthenticated())
182 {
183 $response = $this->oauth->query($this->fetchUrl($path, $limit, $offset, $until, $since));
184
185 if (strcmp($response->body, ''))
186 {
187 return json_decode($response->body);
188 }
189 else
190 {
191 return $response->headers['Location'];
192 }
193 }
194 else
195 {
196 return false;
197 }
198 }
199
200 // Send the request.
201 return $this->sendRequest($path, '', null, $limit, $offset, $until, $since);
202 }
203
204 /**
205 * Method to create a connection.
206 *
207 * @param string $object The object id.
208 * @param string $connection The object's connection name.
209 * @param array $parameters The POST request parameters.
210 * @param array $headers An array of name-value pairs to include in the header of the request
211 *
212 * @return mixed The decoded JSON response or false if the client is not authenticated.
213 *
214 * @since 13.1
215 */
216 public function createConnection($object, $connection = null, $parameters = null, array $headers = null)
217 {
218 if ($this->oauth->isAuthenticated())
219 {
220 // Build the request path.
221 if ($connection != null)
222 {
223 $path = $object . '/' . $connection;
224 }
225 else
226 {
227 $path = $object;
228 }
229
230 // Send the post request.
231 $response = $this->oauth->query($this->fetchUrl($path), $parameters, $headers, 'post');
232
233 return json_decode($response->body);
234 }
235 else
236 {
237 return false;
238 }
239 }
240
241 /**
242 * Method to delete a connection.
243 *
244 * @param string $object The object id.
245 * @param string $connection The object's connection name.
246 * @param string $extra_fields URL fields.
247 *
248 * @return mixed The decoded JSON response or false if the client is not authenticated.
249 *
250 * @since 13.1
251 */
252 public function deleteConnection($object, $connection = null, $extra_fields = '')
253 {
254 if ($this->oauth->isAuthenticated())
255 {
256 // Build the request path.
257 if ($connection != null)
258 {
259 $path = $object . '/' . $connection . $extra_fields;
260 }
261 else
262 {
263 $path = $object . $extra_fields;
264 }
265
266 // Send the delete request.
267 $response = $this->oauth->query($this->fetchUrl($path), null, array(), 'delete');
268
269 return json_decode($response->body);
270 }
271 else
272 {
273 return false;
274 }
275 }
276
277 /**
278 * Method used to set the OAuth client.
279 *
280 * @param JFacebookOAuth $oauth The OAuth client object.
281 *
282 * @return JFacebookObject This object for method chaining.
283 *
284 * @since 13.1
285 */
286 public function setOAuth($oauth)
287 {
288 $this->oauth = $oauth;
289
290 return $this;
291 }
292
293 /**
294 * Method used to get the OAuth client.
295 *
296 * @return JFacebookOAuth The OAuth client
297 *
298 * @since 13.1
299 */
300 public function getOAuth()
301 {
302 return $this->oauth;
303 }
304 }
305