1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Linkedin
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 /**
13 * Linkedin API Social Stream class for the Joomla Platform.
14 *
15 * @since 13.1
16 */
17 class JLinkedinStream extends JLinkedinObject
18 {
19 /**
20 * Method to add a new share. Note: post must contain comment and/or (title and url).
21 *
22 * @param string $visibility One of anyone: all members or connections-only: connections only.
23 * @param string $comment Text of member's comment.
24 * @param string $title Title of shared document.
25 * @param string $url URL for shared content.
26 * @param string $image URL for image of shared content.
27 * @param string $description Description of shared content.
28 * @param boolean $twitter True to have LinkedIn pass the status message along to a member's tethered Twitter account.
29 *
30 * @return array The decoded JSON response
31 *
32 * @since 13.1
33 * @throws RuntimeException
34 */
35 public function share($visibility, $comment = null, $title = null, $url = null, $image = null, $description = null, $twitter = false)
36 {
37 $token = $this->oauth->getToken();
38
39 // Set parameters.
40 $parameters = array(
41 'oauth_token' => $token['key'],
42 );
43
44 // Set the success response code.
45 $this->oauth->setOption('success_code', 201);
46
47 // Set the API base
48 $base = '/v1/people/~/shares';
49
50 // Check if twitter is true.
51 if ($twitter)
52 {
53 $base .= '?twitter-post=true';
54 }
55
56 // Build xml.
57 $xml = '<share>
58 <visibility>
59 <code>' . $visibility . '</code>
60 </visibility>';
61
62 // Check if comment specified.
63 if ($comment)
64 {
65 $xml .= '<comment>' . $comment . '</comment>';
66 }
67
68 // Check if title and url are specified.
69 if ($title && $url)
70 {
71 $xml .= '<content>
72 <title>' . $title . '</title>
73 <submitted-url>' . $url . '</submitted-url>';
74
75 // Check if image is specified.
76 if ($image)
77 {
78 $xml .= '<submitted-image-url>' . $image . '</submitted-image-url>';
79 }
80
81 // Check if descrption id specified.
82 if ($description)
83 {
84 $xml .= '<description>' . $description . '</description>';
85 }
86
87 $xml .= '</content>';
88 }
89 elseif (!$comment)
90 {
91 throw new RuntimeException('Post must contain comment and/or (title and url).');
92 }
93
94 $xml .= '</share>';
95
96 // Build the request path.
97 $path = $this->getOption('api.url') . $base;
98
99 $header['Content-Type'] = 'text/xml';
100
101 // Send the request.
102 $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
103
104 return $response;
105 }
106
107 /**
108 * Method to reshare an existing share.
109 *
110 * @param string $visibility One of anyone: all members or connections-only: connections only.
111 * @param string $id The unique identifier for a share.
112 * @param string $comment Text of member's comment.
113 * @param boolean $twitter True to have LinkedIn pass the status message along to a member's tethered Twitter account.
114 *
115 * @return array The decoded JSON response
116 *
117 * @since 13.1
118 * @throws RuntimeException
119 */
120 public function reshare($visibility, $id, $comment = null, $twitter = false)
121 {
122 $token = $this->oauth->getToken();
123
124 // Set parameters.
125 $parameters = array(
126 'oauth_token' => $token['key'],
127 );
128
129 // Set the success response code.
130 $this->oauth->setOption('success_code', 201);
131
132 // Set the API base
133 $base = '/v1/people/~/shares';
134
135 // Check if twitter is true.
136 if ($twitter)
137 {
138 $base .= '?twitter-post=true';
139 }
140
141 // Build xml.
142 $xml = '<share>
143 <visibility>
144 <code>' . $visibility . '</code>
145 </visibility>';
146
147 // Check if comment specified.
148 if ($comment)
149 {
150 $xml .= '<comment>' . $comment . '</comment>';
151 }
152
153 $xml .= ' <attribution>
154 <share>
155 <id>' . $id . '</id>
156 </share>
157 </attribution>
158 </share>';
159
160 // Build the request path.
161 $path = $this->getOption('api.url') . $base;
162
163 $header['Content-Type'] = 'text/xml';
164
165 // Send the request.
166 $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
167
168 return $response;
169 }
170
171 /**
172 * Method to get a particular member's current share.
173 *
174 * @param string $id Member id of the profile you want.
175 * @param string $url The public profile URL.
176 *
177 * @return array The decoded JSON response
178 *
179 * @since 13.1
180 */
181 public function getCurrentShare($id = null, $url = null)
182 {
183 $token = $this->oauth->getToken();
184
185 // Set parameters.
186 $parameters = array(
187 'oauth_token' => $token['key'],
188 );
189
190 // Set the API base
191 $base = '/v1/people/';
192
193 // Check if a member id is specified.
194 if ($id)
195 {
196 $base .= 'id=' . $id;
197 }
198 elseif (!$url)
199 {
200 $base .= '~';
201 }
202
203 // Check if profile url is specified.
204 if ($url)
205 {
206 $base .= 'url=' . $this->oauth->safeEncode($url);
207 }
208
209 $base .= ':(current-share)';
210
211 // Set request parameters.
212 $data['format'] = 'json';
213
214 // Build the request path.
215 $path = $this->getOption('api.url') . $base;
216
217 // Send the request.
218 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
219
220 return json_decode($response->body);
221 }
222
223 /**
224 * Method to get a particular member's current share.
225 *
226 * @param string $id Member id of the profile you want.
227 * @param string $url The public profile URL.
228 * @param boolean $self Used to return member's feed. Omitted to return aggregated network feed.
229 *
230 * @return array The decoded JSON response
231 *
232 * @since 13.1
233 */
234 public function getShareStream($id = null, $url = null, $self = true)
235 {
236 $token = $this->oauth->getToken();
237
238 // Set parameters.
239 $parameters = array(
240 'oauth_token' => $token['key'],
241 );
242
243 // Set the API base
244 $base = '/v1/people/';
245
246 // Check if a member id is specified.
247 if ($id)
248 {
249 $base .= $id;
250 }
251 elseif (!$url)
252 {
253 $base .= '~';
254 }
255
256 // Check if profile url is specified.
257 if ($url)
258 {
259 $base .= 'url=' . $this->oauth->safeEncode($url);
260 }
261
262 $base .= '/network';
263
264 // Set request parameters.
265 $data['format'] = 'json';
266 $data['type'] = 'SHAR';
267
268 // Check if self is true
269 if ($self)
270 {
271 $data['scope'] = 'self';
272 }
273
274 // Build the request path.
275 $path = $this->getOption('api.url') . $base;
276
277 // Send the request.
278 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
279
280 return json_decode($response->body);
281 }
282
283 /**
284 * Method to get the users network updates.
285 *
286 * @param string $id Member id.
287 * @param boolean $self Used to return member's feed. Omitted to return aggregated network feed.
288 * @param mixed $type String containing any valid Network Update Type from the table or an array of strings
289 * to specify more than one Network Update type.
290 * @param integer $count Number of updates to return, with a maximum of 250.
291 * @param integer $start The offset by which to start Network Update pagination.
292 * @param string $after Timestamp after which to retrieve updates.
293 * @param string $before Timestamp before which to retrieve updates.
294 * @param boolean $hidden Whether to display updates from people the member has chosen to "hide" from their update stream.
295 *
296 * @return array The decoded JSON response
297 *
298 * @since 13.1
299 */
300 public function getNetworkUpdates($id = null, $self = true, $type = null, $count = 0, $start = 0, $after = null, $before = null,
301 $hidden = false)
302 {
303 $token = $this->oauth->getToken();
304
305 // Set parameters.
306 $parameters = array(
307 'oauth_token' => $token['key'],
308 );
309
310 // Set the API base
311 $base = '/v1/people/';
312
313 // Check if a member id is specified.
314 if ($id)
315 {
316 $base .= $id;
317 }
318 else
319 {
320 $base .= '~';
321 }
322
323 $base .= '/network/updates';
324
325 // Set request parameters.
326 $data['format'] = 'json';
327
328 // Check if self is true.
329 if ($self)
330 {
331 $data['scope'] = 'self';
332 }
333
334 // Check if type is specified.
335 if ($type)
336 {
337 $data['type'] = $type;
338 }
339
340 // Check if count is specified.
341 if ($count > 0)
342 {
343 $data['count'] = $count;
344 }
345
346 // Check if start is specified.
347 if ($start > 0)
348 {
349 $data['start'] = $start;
350 }
351
352 // Check if after is specified.
353 if ($after)
354 {
355 $data['after'] = $after;
356 }
357
358 // Check if before is specified.
359 if ($before > 0)
360 {
361 $data['before'] = $before;
362 }
363
364 // Check if hidden is true.
365 if ($hidden)
366 {
367 $data['hidden'] = $hidden;
368 }
369
370 // Build the request path.
371 $path = $this->getOption('api.url') . $base;
372
373 // Send the request.
374 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
375
376 return json_decode($response->body);
377 }
378
379 /**
380 * Method to get information about the current member's network.
381 *
382 * @return array The decoded JSON response
383 *
384 * @since 13.1
385 */
386 public function getNetworkStats()
387 {
388 $token = $this->oauth->getToken();
389
390 // Set parameters.
391 $parameters = array(
392 'oauth_token' => $token['key'],
393 );
394
395 // Set the API base
396 $base = '/v1/people/~/network/network-stats';
397
398 // Set request parameters.
399 $data['format'] = 'json';
400
401 // Build the request path.
402 $path = $this->getOption('api.url') . $base;
403
404 // Send the request.
405 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
406
407 return json_decode($response->body);
408 }
409
410 /**
411 * Method to get the users network updates.
412 *
413 * @param string $body The actual content of the update. You can use HTML to include links to the user name and the content the user
414 * created. Other HTML tags are not supported. All body text should be HTML entity escaped and UTF-8 compliant.
415 *
416 * @return array The decoded JSON response
417 *
418 * @since 13.1
419 */
420 public function postNetworkUpdate($body)
421 {
422 $token = $this->oauth->getToken();
423
424 // Set parameters.
425 $parameters = array(
426 'oauth_token' => $token['key'],
427 );
428
429 // Set the success response code.
430 $this->oauth->setOption('success_code', 201);
431
432 // Set the API base
433 $base = '/v1/people/~/person-activities';
434
435 // Build the xml.
436 $xml = '<activity locale="en_US">
437 <content-type>linkedin-html</content-type>
438 <body>' . $body . '</body>
439 </activity>';
440
441 $header['Content-Type'] = 'text/xml';
442
443 // Build the request path.
444 $path = $this->getOption('api.url') . $base;
445
446 // Send the request.
447 $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
448
449 return $response;
450 }
451
452 /**
453 * Method to retrieve all comments for a given network update.
454 *
455 * @param string $key update/update-key representing an update.
456 *
457 * @return array The decoded JSON response
458 *
459 * @since 13.1
460 */
461 public function getComments($key)
462 {
463 $token = $this->oauth->getToken();
464
465 // Set parameters.
466 $parameters = array(
467 'oauth_token' => $token['key'],
468 );
469
470 // Set the API base
471 $base = '/v1/people/~/network/updates/key=' . $key . '/update-comments';
472
473 // Set request parameters.
474 $data['format'] = 'json';
475
476 // Build the request path.
477 $path = $this->getOption('api.url') . $base;
478
479 // Send the request.
480 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
481
482 return json_decode($response->body);
483 }
484
485 /**
486 * Method to post a new comment to an existing update.
487 *
488 * @param string $key update/update-key representing an update.
489 * @param string $comment Maximum length of 700 characters
490 *
491 * @return array The decoded JSON response
492 *
493 * @since 13.1
494 */
495 public function postComment($key, $comment)
496 {
497 $token = $this->oauth->getToken();
498
499 // Set parameters.
500 $parameters = array(
501 'oauth_token' => $token['key'],
502 );
503
504 // Set the success response code.
505 $this->oauth->setOption('success_code', 201);
506
507 // Set the API base
508 $base = '/v1/people/~/network/updates/key=' . $key . '/update-comments';
509
510 // Build the xml.
511 $xml = '<update-comment>
512 <comment>' . $comment . '</comment>
513 </update-comment>';
514
515 $header['Content-Type'] = 'text/xml';
516
517 // Build the request path.
518 $path = $this->getOption('api.url') . $base;
519
520 // Send the request.
521 $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
522
523 return $response;
524 }
525
526 /**
527 * Method to retrieve the complete list of people who liked an update.
528 *
529 * @param string $key update/update-key representing an update.
530 *
531 * @return array The decoded JSON response
532 *
533 * @since 13.1
534 */
535 public function getLikes($key)
536 {
537 $token = $this->oauth->getToken();
538
539 // Set parameters.
540 $parameters = array(
541 'oauth_token' => $token['key'],
542 );
543
544 // Set the API base
545 $base = '/v1/people/~/network/updates/key=' . $key . '/likes';
546
547 // Set request parameters.
548 $data['format'] = 'json';
549
550 // Build the request path.
551 $path = $this->getOption('api.url') . $base;
552
553 // Send the request.
554 $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
555
556 return json_decode($response->body);
557 }
558
559 /**
560 * Method to like or unlike an update.
561 *
562 * @param string $key Update/update-key representing an update.
563 * @param boolean $like True to like update, false otherwise.
564 *
565 * @return array The decoded JSON response
566 *
567 * @since 13.1
568 */
569 private function _likeUnlike($key, $like)
570 {
571 $token = $this->oauth->getToken();
572
573 // Set parameters.
574 $parameters = array(
575 'oauth_token' => $token['key'],
576 );
577
578 // Set the success response code.
579 $this->oauth->setOption('success_code', 204);
580
581 // Set the API base
582 $base = '/v1/people/~/network/updates/key=' . $key . '/is-liked';
583
584 // Build xml.
585 $xml = '<is-liked>' . $this->booleanToString($like) . '</is-liked>';
586
587 // Build the request path.
588 $path = $this->getOption('api.url') . $base;
589
590 $header['Content-Type'] = 'text/xml';
591
592 // Send the request.
593 $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
594
595 return $response;
596 }
597
598 /**
599 * Method used to like an update.
600 *
601 * @param string $key Update/update-key representing an update.
602 *
603 * @return array The decoded JSON response
604 *
605 * @since 13.1
606 */
607 public function like($key)
608 {
609 return $this->_likeUnlike($key, true);
610 }
611
612 /**
613 * Method used to unlike an update.
614 *
615 * @param string $key Update/update-key representing an update.
616 *
617 * @return array The decoded JSON response
618 *
619 * @since 13.1
620 */
621 public function unlike($key)
622 {
623 return $this->_likeUnlike($key, false);
624 }
625 }
626