PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/twitter/statuses.php

https://bitbucket.org/biojazzard/joomla-eboracast
PHP | 657 lines | 288 code | 89 blank | 280 comment | 46 complexity | a07db1526304aaa60f20e995482f2b57 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Twitter
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 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. /**
  11. * Twitter API Statuses class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Twitter
  15. * @since 12.3
  16. */
  17. class JTwitterStatuses extends JTwitterObject
  18. {
  19. /**
  20. * Method to get a single tweet with the given ID.
  21. *
  22. * @param integer $id The ID of the tweet to retrieve.
  23. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  24. * the status author's numerical ID.
  25. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  26. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  27. * @param boolean $my_retweet When set to either true, t or 1, any statuses returned that have been retweeted by the authenticating user will
  28. * include an additional current_user_retweet node, containing the ID of the source status for the retweet.
  29. *
  30. * @return array The decoded JSON response
  31. *
  32. * @since 12.3
  33. */
  34. public function getTweetById($id, $trim_user = null, $entities = null, $my_retweet = null)
  35. {
  36. // Check the rate limit for remaining hits
  37. $this->checkRateLimit("statuses", "show/:id");
  38. // Set the API base
  39. $path = '/statuses/show/' . $id . '.json';
  40. $data = array();
  41. // Check if trim_user is specified
  42. if (!is_null($trim_user))
  43. {
  44. $data['trim_user'] = $trim_user;
  45. }
  46. // Check if entities is specified
  47. if (!is_null($entities))
  48. {
  49. $data['include_entities'] = $entities;
  50. }
  51. // Check if my_retweet is specified
  52. if (!is_null($my_retweet))
  53. {
  54. $data['include_my_retweet'] = $my_retweet;
  55. }
  56. // Send the request.
  57. return $this->sendRequest($path, 'GET', $data);
  58. }
  59. /**
  60. * Method to retrieve the latest statuses from the specified user timeline.
  61. *
  62. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  63. * @param integer $count Specifies the number of tweets to try and retrieve, up to a maximum of 200. Retweets are always included
  64. * in the count, so it is always suggested to set $include_rts to true
  65. * @param boolean $include_rts When set to true, the timeline will contain native retweets in addition to the standard stream of tweets.
  66. * @param boolean $no_replies This parameter will prevent replies from appearing in the returned timeline. This parameter is only supported
  67. * for JSON and XML responses.
  68. * @param integer $since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
  69. * @param integer $max_id Returns results with an ID less than (that is, older than) the specified ID.
  70. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  71. * the status author's numerical ID.
  72. * @param boolean $contributor This parameter enhances the contributors element of the status response to include the screen_name of the
  73. * contributor. By default only the user_id of the contributor is included.
  74. *
  75. * @return array The decoded JSON response
  76. *
  77. * @since 12.3
  78. * @throws RuntimeException
  79. */
  80. public function getUserTimeline($user, $count = 20, $include_rts = null, $no_replies = null, $since_id = 0, $max_id = 0, $trim_user = null,
  81. $contributor = null)
  82. {
  83. // Check the rate limit for remaining hits
  84. $this->checkRateLimit('statuses', 'user_timeline');
  85. $data = array();
  86. // Determine which type of data was passed for $user
  87. if (is_numeric($user))
  88. {
  89. $data['user_id'] = $user;
  90. }
  91. elseif (is_string($user))
  92. {
  93. $data['screen_name'] = $user;
  94. }
  95. else
  96. {
  97. // We don't have a valid entry
  98. throw new RuntimeException('The specified username is not in the correct format; must use integer or string');
  99. }
  100. // Set the API base
  101. $path = '/statuses/user_timeline.json';
  102. // Set the count string
  103. $data['count'] = $count;
  104. // Check if include_rts is specified
  105. if (!is_null($include_rts))
  106. {
  107. $data['include_rts'] = $include_rts;
  108. }
  109. // Check if no_replies is specified
  110. if (!is_null($no_replies))
  111. {
  112. $data['exclude_replies'] = $no_replies;
  113. }
  114. // Check if a since_id is specified
  115. if ($since_id > 0)
  116. {
  117. $data['since_id'] = (int) $since_id;
  118. }
  119. // Check if a max_id is specified
  120. if ($max_id > 0)
  121. {
  122. $data['max_id'] = (int) $max_id;
  123. }
  124. // Check if trim_user is specified
  125. if (!is_null($trim_user))
  126. {
  127. $data['trim_user'] = $trim_user;
  128. }
  129. // Check if contributor details is specified
  130. if (!is_null($contributor))
  131. {
  132. $data['contributor_details'] = $contributor;
  133. }
  134. // Send the request.
  135. return $this->sendRequest($path, 'GET', $data);
  136. }
  137. /**
  138. * Method to post a tweet.
  139. *
  140. * @param string $status The text of the tweet.
  141. * @param integer $in_reply_to_status_id The ID of an existing status that the update is in reply to.
  142. * @param float $lat The latitude of the location this tweet refers to.
  143. * @param float $long The longitude of the location this tweet refers to.
  144. * @param string $place_id A place in the world.
  145. * @param boolean $display_coordinates Whether or not to put a pin on the exact coordinates a tweet has been sent from.
  146. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  147. * the status author's numerical ID.
  148. *
  149. * @return array The decoded JSON response
  150. *
  151. * @since 12.3
  152. */
  153. public function tweet($status, $in_reply_to_status_id = null, $lat = null, $long = null, $place_id = null, $display_coordinates = null,
  154. $trim_user = null)
  155. {
  156. // Set the API base.
  157. $path = '/statuses/update.json';
  158. // Set POST data.
  159. $data = array('status' => utf8_encode($status));
  160. // Check if in_reply_to_status_id is specified.
  161. if ($in_reply_to_status_id)
  162. {
  163. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  164. }
  165. // Check if lat is specified.
  166. if ($lat)
  167. {
  168. $data['lat'] = $lat;
  169. }
  170. // Check if long is specified.
  171. if ($long)
  172. {
  173. $data['long'] = $long;
  174. }
  175. // Check if place_id is specified.
  176. if ($place_id)
  177. {
  178. $data['place_id'] = $place_id;
  179. }
  180. // Check if display_coordinates is specified.
  181. if (!is_null($display_coordinates))
  182. {
  183. $data['display_coordinates'] = $display_coordinates;
  184. }
  185. // Check if trim_user is specified.
  186. if (!is_null($trim_user))
  187. {
  188. $data['trim_user'] = $trim_user;
  189. }
  190. // Send the request.
  191. return $this->sendRequest($path, 'POST', $data);
  192. }
  193. /**
  194. * Method to retrieve the most recent mentions for the authenticating user.
  195. *
  196. * @param integer $count Specifies the number of tweets to try and retrieve, up to a maximum of 200. Retweets are always included
  197. * in the count, so it is always suggested to set $include_rts to true
  198. * @param boolean $include_rts When set to true, the timeline will contain native retweets in addition to the standard stream of tweets.
  199. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  200. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  201. * @param integer $since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
  202. * @param integer $max_id Returns results with an ID less than (that is, older than) the specified ID.
  203. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  204. * the status author's numerical ID.
  205. * @param string $contributor This parameter enhances the contributors element of the status response to include the screen_name
  206. * of the contributor.
  207. *
  208. * @return array The decoded JSON response
  209. *
  210. * @since 12.3
  211. * @throws RuntimeException
  212. */
  213. public function getMentions($count = 20, $include_rts = null, $entities = null, $since_id = 0, $max_id = 0,
  214. $trim_user = null, $contributor = null)
  215. {
  216. // Check the rate limit for remaining hits
  217. $this->checkRateLimit('statuses', 'mentions_timeline');
  218. // Set the API base
  219. $path = '/statuses/mentions_timeline.json';
  220. // Set the count string
  221. $data['count'] = $count;
  222. // Check if include_rts is specified
  223. if (!is_null($include_rts))
  224. {
  225. $data['include_rts'] = $include_rts;
  226. }
  227. // Check if entities is specified
  228. if (!is_null($entities))
  229. {
  230. $data['include_entities'] = $entities;
  231. }
  232. // Check if a since_id is specified
  233. if ($since_id > 0)
  234. {
  235. $data['since_id'] = (int) $since_id;
  236. }
  237. // Check if a max_id is specified
  238. if ($max_id > 0)
  239. {
  240. $data['max_id'] = (int) $max_id;
  241. }
  242. // Check if trim_user is specified
  243. if (!is_null($trim_user))
  244. {
  245. $data['trim_user'] = $trim_user;
  246. }
  247. // Check if contributor is specified
  248. if (!is_null($contributor))
  249. {
  250. $data['contributor_details'] = $contributor;
  251. }
  252. // Send the request.
  253. return $this->sendRequest($path, 'GET', $data);
  254. }
  255. /**
  256. * Method to get the most recent tweets of the authenticated user that have been retweeted by others.
  257. *
  258. * @param integer $count Specifies the number of tweets to try and retrieve, up to a maximum of 200. Retweets are always included
  259. * in the count, so it is always suggested to set $include_rts to true
  260. * @param integer $since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
  261. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  262. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  263. * @param boolean $user_entities The user entities node will be disincluded when set to false.
  264. * @param integer $max_id Returns results with an ID less than (that is, older than) the specified ID.
  265. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  266. * the status author's numerical ID.
  267. *
  268. * @return array The decoded JSON response
  269. *
  270. * @since 12.3
  271. */
  272. public function getRetweetsOfMe($count = 20, $since_id = 0, $entities = null, $user_entities = null, $max_id = 0, $trim_user = null)
  273. {
  274. // Check the rate limit for remaining hits
  275. $this->checkRateLimit('statuses', 'retweets_of_me');
  276. // Set the API path
  277. $path = '/statuses/retweets_of_me.json';
  278. // Set the count string
  279. $data['count'] = $count;
  280. // Check if a since_id is specified
  281. if ($since_id > 0)
  282. {
  283. $data['since_id'] = (int) $since_id;
  284. }
  285. // Check if a max_id is specified
  286. if ($max_id > 0)
  287. {
  288. $data['max_id'] = (int) $max_id;
  289. }
  290. // Check if trim_user is specified
  291. if (!is_null($trim_user))
  292. {
  293. $data['trim_user'] = $trim_user;
  294. }
  295. // Check if entities is specified
  296. if (!is_null($entities))
  297. {
  298. $data['include_entities'] = $entities;
  299. }
  300. // Check if entities is specified
  301. if (!is_null($user_entities))
  302. {
  303. $data['include_user_entities'] = $user_entities;
  304. }
  305. // Send the request.
  306. return $this->sendRequest($path, 'GET', $data);
  307. }
  308. /**
  309. * Method to show user objects of up to 100 members who retweeted the status.
  310. *
  311. * @param integer $id The numerical ID of the desired status.
  312. * @param integer $count Specifies the number of retweets to try and retrieve, up to a maximum of 100.
  313. * @param integer $cursor Causes the list of IDs to be broken into pages of no more than 100 IDs at a time.
  314. * The number of IDs returned is not guaranteed to be 100 as suspended users are
  315. * filtered out after connections are queried. If no cursor is provided, a value of
  316. * -1 will be assumed, which is the first "page."
  317. * @param boolean $stringify_ids Set to true to return IDs as strings, false to return as integers.
  318. *
  319. * @return array The decoded JSON response
  320. *
  321. * @since 12.3
  322. */
  323. public function getRetweeters($id, $count = 20, $cursor = null, $stringify_ids = null)
  324. {
  325. // Check the rate limit for remaining hits
  326. $this->checkRateLimit('statuses', 'retweeters/ids');
  327. // Set the API path
  328. $path = '/statuses/retweeters/ids.json';
  329. // Set the status id.
  330. $data['id'] = $id;
  331. // Set the count string
  332. $data['count'] = $count;
  333. // Check if cursor is specified
  334. if (!is_null($cursor))
  335. {
  336. $data['cursor'] = $cursor;
  337. }
  338. // Check if entities is specified
  339. if (!is_null($stringify_ids))
  340. {
  341. $data['stringify_ids'] = $stringify_ids;
  342. }
  343. // Send the request.
  344. return $this->sendRequest($path, 'GET', $data);
  345. }
  346. /**
  347. * Method to get up to 100 of the first retweets of a given tweet.
  348. *
  349. * @param integer $id The numerical ID of the desired status.
  350. * @param integer $count Specifies the number of tweets to try and retrieve, up to a maximum of 200. Retweets are always included
  351. * in the count, so it is always suggested to set $include_rts to true
  352. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  353. * the status author's numerical ID.
  354. *
  355. * @return array The decoded JSON response
  356. *
  357. * @since 12.3
  358. */
  359. public function getRetweetsById($id, $count = 20, $trim_user = null)
  360. {
  361. // Check the rate limit for remaining hits
  362. $this->checkRateLimit('statuses', 'retweets/:id');
  363. // Set the API path
  364. $path = '/statuses/retweets/' . $id . '.json';
  365. // Set the count string
  366. $data['count'] = $count;
  367. // Check if trim_user is specified
  368. if (!is_null($trim_user))
  369. {
  370. $data['trim_user'] = $trim_user;
  371. }
  372. // Send the request.
  373. return $this->sendRequest($path, 'GET', $data);
  374. }
  375. /**
  376. * Method to delete the status specified by the required ID parameter.
  377. *
  378. * @param integer $id The numerical ID of the desired status.
  379. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  380. * the status author's numerical ID.
  381. *
  382. * @return array The decoded JSON response
  383. *
  384. * @since 12.3
  385. */
  386. public function deleteTweet($id, $trim_user = null)
  387. {
  388. // Set the API path
  389. $path = '/statuses/destroy/' . $id . '.json';
  390. $data = array();
  391. // Check if trim_user is specified
  392. if (!is_null($trim_user))
  393. {
  394. $data['trim_user'] = $trim_user;
  395. }
  396. // Send the request.
  397. return $this->sendRequest($path, 'POST', $data);
  398. }
  399. /**
  400. * Method to retweet a tweet.
  401. *
  402. * @param integer $id The numerical ID of the desired status.
  403. * @param boolean $trim_user When set to true, each tweet returned in a timeline will include a user object including only
  404. * the status author's numerical ID.
  405. *
  406. * @return array The decoded JSON response
  407. *
  408. * @since 12.3
  409. */
  410. public function retweet($id, $trim_user = null)
  411. {
  412. // Set the API path
  413. $path = '/statuses/retweet/' . $id . '.json';
  414. $data = array();
  415. // Check if trim_user is specified
  416. if (!is_null($trim_user))
  417. {
  418. $data['trim_user'] = $trim_user;
  419. }
  420. // Send the request.
  421. return $this->sendRequest($path, 'POST', $data);
  422. }
  423. /**
  424. * Method to post a tweet with media.
  425. *
  426. * @param string $status The text of the tweet.
  427. * @param string $media File to upload
  428. * @param integer $in_reply_to_status_id The ID of an existing status that the update is in reply to.
  429. * @param float $lat The latitude of the location this tweet refers to.
  430. * @param float $long The longitude of the location this tweet refers to.
  431. * @param string $place_id A place in the world.
  432. * @param boolean $display_coordinates Whether or not to put a pin on the exact coordinates a tweet has been sent from.
  433. * @param boolean $sensitive Set to true for content which may not be suitable for every audience.
  434. *
  435. * @return array The decoded JSON response
  436. *
  437. * @since 12.3
  438. * @throws RuntimeException
  439. */
  440. public function tweetWithMedia($status, $media, $in_reply_to_status_id = null, $lat = null, $long = null, $place_id = null,
  441. $display_coordinates = null, $sensitive = null)
  442. {
  443. // Set the API request path.
  444. $path = '/statuses/update_with_media.json';
  445. // Set POST data.
  446. $data = array(
  447. 'status' => utf8_encode($status),
  448. 'media[]' => "@{$media}"
  449. );
  450. $header = array('Content-Type' => 'multipart/form-data');
  451. // Check if in_reply_to_status_id is specified.
  452. if (!is_null($in_reply_to_status_id))
  453. {
  454. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  455. }
  456. // Check if lat is specified.
  457. if ($lat)
  458. {
  459. $data['lat'] = $lat;
  460. }
  461. // Check if long is specified.
  462. if ($long)
  463. {
  464. $data['long'] = $long;
  465. }
  466. // Check if place_id is specified.
  467. if ($place_id)
  468. {
  469. $data['place_id'] = $place_id;
  470. }
  471. // Check if display_coordinates is specified.
  472. if (!is_null($display_coordinates))
  473. {
  474. $data['display_coordinates'] = $display_coordinates;
  475. }
  476. // Check if sensitive is specified.
  477. if (!is_null($sensitive))
  478. {
  479. $data['possibly_sensitive'] = $sensitive;
  480. }
  481. // Send the request.
  482. return $this->sendRequest($path, 'POST', $data, $header);
  483. }
  484. /**
  485. * Method to get information allowing the creation of an embedded representation of a Tweet on third party sites.
  486. * Note: either the id or url parameters must be specified in a request. It is not necessary to include both.
  487. *
  488. * @param integer $id The Tweet/status ID to return embed code for.
  489. * @param string $url The URL of the Tweet/status to be embedded.
  490. * @param integer $maxwidth The maximum width in pixels that the embed should be rendered at. This value is constrained to be
  491. * between 250 and 550 pixels.
  492. * @param boolean $hide_media Specifies whether the embedded Tweet should automatically expand images which were uploaded via
  493. * POST statuses/update_with_media.
  494. * @param boolean $hide_thread Specifies whether the embedded Tweet should automatically show the original message in the case that
  495. * the embedded Tweet is a reply.
  496. * @param boolean $omit_script Specifies whether the embedded Tweet HTML should include a <script> element pointing to widgets.js. In cases where
  497. * a page already includes widgets.js, setting this value to true will prevent a redundant script element from being included.
  498. * @param string $align Specifies whether the embedded Tweet should be left aligned, right aligned, or centered in the page.
  499. * Valid values are left, right, center, and none.
  500. * @param string $related A value for the TWT related parameter, as described in Web Intents. This value will be forwarded to all
  501. * Web Intents calls.
  502. * @param string $lang Language code for the rendered embed. This will affect the text and localization of the rendered HTML.
  503. *
  504. * @return array The decoded JSON response
  505. *
  506. * @since 12.3
  507. * @throws RuntimeException
  508. */
  509. public function getOembed($id = null, $url = null, $maxwidth = null, $hide_media = null, $hide_thread = null, $omit_script = null,
  510. $align = null, $related = null, $lang = null)
  511. {
  512. // Check the rate limit for remaining hits.
  513. $this->checkRateLimit('statuses', 'oembed');
  514. // Set the API request path.
  515. $path = '/statuses/oembed.json';
  516. // Determine which of $id and $url is specified.
  517. if ($id)
  518. {
  519. $data['id'] = $id;
  520. }
  521. elseif ($url)
  522. {
  523. $data['url'] = rawurlencode($url);
  524. }
  525. else
  526. {
  527. // We don't have a valid entry.
  528. throw new RuntimeException('Either the id or url parameters must be specified in a request.');
  529. }
  530. // Check if maxwidth is specified.
  531. if ($maxwidth)
  532. {
  533. $data['maxwidth'] = $maxwidth;
  534. }
  535. // Check if hide_media is specified.
  536. if (!is_null($hide_media))
  537. {
  538. $data['hide_media'] = $hide_media;
  539. }
  540. // Check if hide_thread is specified.
  541. if (!is_null($hide_thread))
  542. {
  543. $data['hide_thread'] = $hide_thread;
  544. }
  545. // Check if omit_script is specified.
  546. if (!is_null($omit_script))
  547. {
  548. $data['omit_script'] = $omit_script;
  549. }
  550. // Check if align is specified.
  551. if ($align)
  552. {
  553. $data['align'] = $align;
  554. }
  555. // Check if related is specified.
  556. if ($related)
  557. {
  558. $data['related'] = $related;
  559. }
  560. // Check if lang is specified.
  561. if ($lang)
  562. {
  563. $data['lang'] = $lang;
  564. }
  565. // Send the request.
  566. return $this->sendRequest($path, 'GET', $data);
  567. }
  568. }