2014-05-26 02:30:46 +04:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
class TwitterBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const NAME = 'Twitter Bridge';
|
|
|
|
const URI = 'https://twitter.com/';
|
2020-06-05 11:17:53 +03:00
|
|
|
const API_URI = 'https://api.twitter.com';
|
2020-06-08 12:18:24 +03:00
|
|
|
const GUEST_TOKEN_USES = 100;
|
2021-12-18 12:54:18 +03:00
|
|
|
const GUEST_TOKEN_EXPIRY = 10800; // 3hrs
|
2017-02-11 18:16:56 +03:00
|
|
|
const CACHE_TIMEOUT = 300; // 5min
|
|
|
|
const DESCRIPTION = 'returns tweets';
|
2022-01-16 21:42:28 +03:00
|
|
|
const MAINTAINER = 'arnd-s';
|
2017-02-11 18:16:56 +03:00
|
|
|
const PARAMETERS = [
|
|
|
|
'global' => [
|
|
|
|
'nopic' => [
|
|
|
|
'name' => 'Hide profile pictures',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Activate to hide profile pictures in content'
|
2017-04-22 16:36:25 +03:00
|
|
|
],
|
|
|
|
'noimg' => [
|
|
|
|
'name' => 'Hide images in tweets',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Activate to hide images in tweets'
|
2018-12-12 18:56:36 +03:00
|
|
|
],
|
|
|
|
'noimgscaling' => [
|
|
|
|
'name' => 'Disable image scaling',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Activate to disable image scaling in tweets (keeps original image)'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2017-02-11 18:16:56 +03:00
|
|
|
],
|
|
|
|
'By keyword or hashtag' => [
|
|
|
|
'q' => [
|
|
|
|
'name' => 'Keyword or #hashtag',
|
|
|
|
'required' => true,
|
2022-04-04 20:50:59 +03:00
|
|
|
'exampleValue' => 'rss-bridge OR rssbridge',
|
2019-06-11 22:51:10 +03:00
|
|
|
'title' => <<<EOD
|
|
|
|
* To search for multiple words (must contain all of these words), put a space between them.
|
|
|
|
|
|
|
|
Example: `rss-bridge release`.
|
|
|
|
|
|
|
|
* To search for multiple words (contains any of these words), put "OR" between them.
|
|
|
|
|
|
|
|
Example: `rss-bridge OR rssbridge`.
|
|
|
|
|
|
|
|
* To search for an exact phrase (including whitespace), put double-quotes around them.
|
|
|
|
|
|
|
|
Example: `"rss-bridge release"`
|
|
|
|
|
|
|
|
* If you want to search for anything **but** a specific word, put a hyphen before it.
|
|
|
|
|
|
|
|
Example: `rss-bridge -release` (ignores "release")
|
|
|
|
|
|
|
|
* Of course, this also works for hashtags.
|
|
|
|
|
|
|
|
Example: `#rss-bridge OR #rssbridge`
|
|
|
|
|
|
|
|
* And you can combine them in any shape or form you like.
|
|
|
|
|
|
|
|
Example: `#rss-bridge OR #rssbridge -release`
|
|
|
|
EOD
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2017-02-11 18:16:56 +03:00
|
|
|
],
|
|
|
|
'By username' => [
|
|
|
|
'u' => [
|
|
|
|
'name' => 'username',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'sebsauvage',
|
|
|
|
'title' => 'Insert a user name'
|
|
|
|
],
|
|
|
|
'norep' => [
|
|
|
|
'name' => 'Without replies',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Only return initial tweets'
|
2017-03-17 20:41:35 +03:00
|
|
|
],
|
|
|
|
'noretweet' => [
|
|
|
|
'name' => 'Without retweets',
|
|
|
|
'required' => false,
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Hide retweets'
|
2021-01-10 11:50:06 +03:00
|
|
|
],
|
|
|
|
'nopinned' => [
|
|
|
|
'name' => 'Without pinned tweet',
|
|
|
|
'required' => false,
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'title' => 'Hide pinned tweet'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2017-09-24 17:59:45 +03:00
|
|
|
],
|
|
|
|
'By list' => [
|
|
|
|
'user' => [
|
|
|
|
'name' => 'User',
|
|
|
|
'required' => true,
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => 'Scobleizer',
|
2017-09-24 17:59:45 +03:00
|
|
|
'title' => 'Insert a user name'
|
|
|
|
],
|
|
|
|
'list' => [
|
|
|
|
'name' => 'List',
|
|
|
|
'required' => true,
|
2022-03-24 13:59:34 +03:00
|
|
|
'exampleValue' => 'Tech-News',
|
2017-09-24 17:59:45 +03:00
|
|
|
'title' => 'Insert the list name'
|
|
|
|
],
|
|
|
|
'filter' => [
|
|
|
|
'name' => 'Filter',
|
|
|
|
'exampleValue' => '#rss-bridge',
|
|
|
|
'required' => false,
|
|
|
|
'title' => 'Specify term to search for'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2020-11-10 09:12:02 +03:00
|
|
|
],
|
|
|
|
'By list ID' => [
|
|
|
|
'listid' => [
|
|
|
|
'name' => 'List ID',
|
|
|
|
'exampleValue' => '31748',
|
|
|
|
'required' => true,
|
|
|
|
'title' => 'Insert the list id'
|
|
|
|
],
|
|
|
|
'filter' => [
|
|
|
|
'name' => 'Filter',
|
|
|
|
'exampleValue' => '#rss-bridge',
|
|
|
|
'required' => false,
|
|
|
|
'title' => 'Specify term to search for'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
]
|
2017-02-11 18:16:56 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
private $apiKey = null;
|
|
|
|
private $guestToken = null;
|
|
|
|
private $authHeader = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
public function detectParameters($url)
|
|
|
|
{
|
|
|
|
$params = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
// By keyword or hashtag (search)
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/search.*(\?|&)q=([^\/&?\n]+)/';
|
|
|
|
if (preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['q'] = urldecode($matches[4]);
|
|
|
|
return $params;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
// By hashtag
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/hashtag\/([^\/?\n]+)/';
|
|
|
|
if (preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['q'] = urldecode($matches[3]);
|
|
|
|
return $params;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
// By list
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/([^\/?\n]+)\/lists\/([^\/?\n]+)/';
|
|
|
|
if (preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['user'] = urldecode($matches[3]);
|
|
|
|
$params['list'] = urldecode($matches[4]);
|
|
|
|
return $params;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
// By username
|
|
|
|
$regex = '/^(https?:\/\/)?(www\.)?twitter\.com\/([^\/?\n]+)/';
|
|
|
|
if (preg_match($regex, $url, $matches) > 0) {
|
|
|
|
$params['u'] = urldecode($matches[3]);
|
|
|
|
return $params;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-11-26 20:05:41 +03:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
2017-07-29 20:28:00 +03:00
|
|
|
switch ($this->queriedContext) {
|
2017-02-11 18:16:56 +03:00
|
|
|
case 'By keyword or hashtag':
|
|
|
|
$specific = 'search ';
|
|
|
|
$param = 'q';
|
|
|
|
break;
|
|
|
|
case 'By username':
|
|
|
|
$specific = '@';
|
|
|
|
$param = 'u';
|
|
|
|
break;
|
2017-09-24 17:59:45 +03:00
|
|
|
case 'By list':
|
2018-01-12 15:07:40 +03:00
|
|
|
return $this->getInput('list') . ' - Twitter list by ' . $this->getInput('user');
|
2020-11-10 09:12:02 +03:00
|
|
|
case 'By list ID':
|
|
|
|
return 'Twitter List #' . $this->getInput('listid');
|
2017-02-15 00:20:55 +03:00
|
|
|
default:
|
|
|
|
return parent::getName();
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
return 'Twitter ' . $specific . $this->getInput($param);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
2017-07-29 20:28:00 +03:00
|
|
|
switch ($this->queriedContext) {
|
2017-02-11 18:16:56 +03:00
|
|
|
case 'By keyword or hashtag':
|
|
|
|
return self::URI
|
|
|
|
. 'search?q='
|
|
|
|
. urlencode($this->getInput('q'))
|
|
|
|
. '&f=tweets';
|
|
|
|
case 'By username':
|
|
|
|
return self::URI
|
2017-07-03 20:52:56 +03:00
|
|
|
. urlencode($this->getInput('u'));
|
|
|
|
// Always return without replies!
|
|
|
|
// . ($this->getInput('norep') ? '' : '/with_replies');
|
2017-09-24 17:59:45 +03:00
|
|
|
case 'By list':
|
|
|
|
return self::URI
|
|
|
|
. urlencode($this->getInput('user'))
|
|
|
|
. '/lists/'
|
|
|
|
. str_replace(' ', '-', strtolower($this->getInput('list')));
|
2020-11-10 09:12:02 +03:00
|
|
|
case 'By list ID':
|
|
|
|
return self::URI
|
|
|
|
. 'i/lists/'
|
|
|
|
. urlencode($this->getInput('listid'));
|
2017-02-15 00:36:33 +03:00
|
|
|
default:
|
|
|
|
return parent::getURI();
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
// $data will contain an array of all found tweets (unfiltered)
|
|
|
|
$data = null;
|
|
|
|
// Contains user data (when in by username context)
|
|
|
|
$user = null;
|
|
|
|
// Array of all found tweets
|
|
|
|
$tweets = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
// Get authentication information
|
|
|
|
$this->getApiKey();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
// Try to get all tweets
|
2020-06-05 11:17:53 +03:00
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'By username':
|
2022-04-04 20:50:59 +03:00
|
|
|
$user = $this->makeApiCall('/1.1/users/show.json', ['screen_name' => $this->getInput('u')]);
|
|
|
|
if (!$user) {
|
|
|
|
returnServerError('Requested username can\'t be found.');
|
2021-04-12 21:08:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$params = [
|
|
|
|
'user_id' => $user->id_str,
|
|
|
|
'tweet_mode' => 'extended'
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$data = $this->makeApiCall('/1.1/statuses/user_timeline.json', $params);
|
|
|
|
break;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
case 'By keyword or hashtag':
|
|
|
|
$params = [
|
|
|
|
'q' => urlencode($this->getInput('q')),
|
|
|
|
'tweet_mode' => 'extended',
|
|
|
|
'tweet_search_mode' => 'live',
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$data = $this->makeApiCall('/1.1/search/tweets.json', $params)->statuses;
|
|
|
|
break;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
case 'By list':
|
2022-04-04 20:50:59 +03:00
|
|
|
$params = [
|
|
|
|
'slug' => strtolower($this->getInput('list')),
|
|
|
|
'owner_screen_name' => strtolower($this->getInput('user')),
|
|
|
|
'tweet_mode' => 'extended',
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$data = $this->makeApiCall('/1.1/lists/statuses.json', $params);
|
|
|
|
break;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-11-10 09:12:02 +03:00
|
|
|
case 'By list ID':
|
2022-04-04 20:50:59 +03:00
|
|
|
$params = [
|
|
|
|
'list_id' => $this->getInput('listid'),
|
|
|
|
'tweet_mode' => 'extended',
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$data = $this->makeApiCall('/1.1/lists/statuses.json', $params);
|
|
|
|
break;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
default:
|
|
|
|
returnServerError('Invalid query context !');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
if (!$data) {
|
2017-07-29 20:28:00 +03:00
|
|
|
switch ($this->queriedContext) {
|
2016-08-28 01:01:42 +03:00
|
|
|
case 'By keyword or hashtag':
|
2022-09-09 21:18:50 +03:00
|
|
|
returnServerError('twitter: No results for this query.');
|
2022-06-24 19:29:35 +03:00
|
|
|
// fall-through
|
2016-08-28 01:01:42 +03:00
|
|
|
case 'By username':
|
2016-09-26 00:22:33 +03:00
|
|
|
returnServerError('Requested username can\'t be found.');
|
2022-06-24 19:29:35 +03:00
|
|
|
// fall-through
|
2017-09-24 17:59:45 +03:00
|
|
|
case 'By list':
|
|
|
|
returnServerError('Requested username or list can\'t be found');
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2016-08-28 01:01:42 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
// Filter out unwanted tweets
|
|
|
|
foreach ($data as $tweet) {
|
|
|
|
// Filter out retweets to remove possible duplicates of original tweet
|
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'By keyword or hashtag':
|
|
|
|
if (isset($tweet->retweeted_status) && substr($tweet->full_text, 0, 4) === 'RT @') {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
break;
|
2020-09-11 09:44:28 +03:00
|
|
|
}
|
2022-04-04 20:50:59 +03:00
|
|
|
$tweets[] = $tweet;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$hidePictures = $this->getInput('nopic');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-01-10 11:50:06 +03:00
|
|
|
$hidePinned = $this->getInput('nopinned');
|
|
|
|
if ($hidePinned) {
|
|
|
|
$pinnedTweetId = null;
|
2022-04-04 20:50:59 +03:00
|
|
|
if ($user && $user->pinned_tweet_ids_str) {
|
|
|
|
$pinnedTweetId = $user->pinned_tweet_ids_str;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2021-04-12 21:08:38 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-04-12 21:08:38 +03:00
|
|
|
foreach ($tweets as $tweet) {
|
2022-04-04 20:50:59 +03:00
|
|
|
// Skip own Retweets...
|
|
|
|
if (isset($tweet->retweeted_status) && $tweet->retweeted_status->user->id_str === $tweet->user->id_str) {
|
2020-09-11 09:44:28 +03:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2021-01-10 11:50:06 +03:00
|
|
|
// Skip pinned tweet
|
|
|
|
if ($hidePinned && $tweet->id_str === $pinnedTweetId) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'By username':
|
|
|
|
if ($this->getInput('norep') && isset($tweet->in_reply_to_status_id)) {
|
|
|
|
continue 2;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2022-04-04 20:50:59 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-08-22 19:55:59 +03:00
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$realtweet = $tweet;
|
|
|
|
if (isset($tweet->retweeted_status)) {
|
|
|
|
// Tweet is a Retweet, so set author based on original tweet and set realtweet for reference to the right content
|
|
|
|
$realtweet = $tweet->retweeted_status;
|
2020-08-20 08:00:27 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$item['username'] = $realtweet->user->screen_name;
|
|
|
|
$item['fullname'] = $realtweet->user->name;
|
|
|
|
$item['avatar'] = $realtweet->user->profile_image_url_https;
|
|
|
|
$item['timestamp'] = $realtweet->created_at;
|
|
|
|
$item['id'] = $realtweet->id_str;
|
|
|
|
$item['uri'] = self::URI . $item['username'] . '/status/' . $item['id'];
|
|
|
|
$item['author'] = (isset($tweet->retweeted_status) ? 'RT: ' : '')
|
|
|
|
. $item['fullname']
|
|
|
|
. ' (@'
|
|
|
|
. $item['username'] . ')';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-08-21 15:55:11 +03:00
|
|
|
// Convert plain text URLs into HTML hyperlinks
|
2022-04-04 20:50:59 +03:00
|
|
|
$fulltext = $realtweet->full_text;
|
|
|
|
$cleanedTweet = $fulltext;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-08-21 15:55:11 +03:00
|
|
|
$foundUrls = false;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
if (substr($cleanedTweet, 0, 4) === 'RT @') {
|
|
|
|
$cleanedTweet = substr($cleanedTweet, 3);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
if (isset($realtweet->entities->media)) {
|
|
|
|
foreach ($realtweet->entities->media as $media) {
|
2020-08-21 15:55:11 +03:00
|
|
|
$cleanedTweet = str_replace(
|
|
|
|
$media->url,
|
|
|
|
'<a href="' . $media->expanded_url . '">' . $media->display_url . '</a>',
|
|
|
|
$cleanedTweet
|
|
|
|
);
|
|
|
|
$foundUrls = true;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2020-08-21 15:55:11 +03:00
|
|
|
}
|
2022-04-04 20:50:59 +03:00
|
|
|
if (isset($realtweet->entities->urls)) {
|
|
|
|
foreach ($realtweet->entities->urls as $url) {
|
2020-08-21 15:55:11 +03:00
|
|
|
$cleanedTweet = str_replace(
|
|
|
|
$url->url,
|
|
|
|
'<a href="' . $url->expanded_url . '">' . $url->display_url . '</a>',
|
|
|
|
$cleanedTweet
|
|
|
|
);
|
|
|
|
$foundUrls = true;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2020-08-21 15:55:11 +03:00
|
|
|
}
|
|
|
|
if ($foundUrls === false) {
|
|
|
|
// fallback to regex'es
|
|
|
|
$reg_ex = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
|
2022-04-04 20:50:59 +03:00
|
|
|
if (preg_match($reg_ex, $realtweet->full_text, $url)) {
|
2020-08-21 15:55:11 +03:00
|
|
|
$cleanedTweet = preg_replace(
|
|
|
|
$reg_ex,
|
|
|
|
"<a href='{$url[0]}' target='_blank'>{$url[0]}</a> ",
|
|
|
|
$cleanedTweet
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 11:17:53 +03:00
|
|
|
// generate the title
|
2020-08-21 15:55:11 +03:00
|
|
|
$item['title'] = strip_tags($cleanedTweet);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
// Add avatar
|
2016-08-10 11:44:23 +03:00
|
|
|
$picture_html = '';
|
2017-07-29 20:28:00 +03:00
|
|
|
if (!$hidePictures) {
|
2016-08-10 11:44:23 +03:00
|
|
|
$picture_html = <<<EOD
|
2017-02-11 18:16:56 +03:00
|
|
|
<a href="https://twitter.com/{$item['username']}">
|
|
|
|
<img
|
2017-04-22 16:36:25 +03:00
|
|
|
style="align:top; width:75px; border:1px solid black;"
|
2017-02-11 18:16:56 +03:00
|
|
|
alt="{$item['username']}"
|
|
|
|
src="{$item['avatar']}"
|
|
|
|
title="{$item['fullname']}" />
|
|
|
|
</a>
|
2016-08-10 11:44:23 +03:00
|
|
|
EOD;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
// Get images
|
2020-06-10 23:39:36 +03:00
|
|
|
$media_html = '';
|
2022-04-04 20:50:59 +03:00
|
|
|
if (isset($realtweet->extended_entities->media) && !$this->getInput('noimg')) {
|
|
|
|
foreach ($realtweet->extended_entities->media as $media) {
|
2020-06-10 23:39:36 +03:00
|
|
|
switch ($media->type) {
|
|
|
|
case 'photo':
|
|
|
|
$image = $media->media_url_https . '?name=orig';
|
|
|
|
$display_image = $media->media_url_https;
|
|
|
|
// add enclosures
|
|
|
|
$item['enclosures'][] = $image;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-10 23:39:36 +03:00
|
|
|
$media_html .= <<<EOD
|
2020-06-05 11:17:53 +03:00
|
|
|
<a href="{$image}">
|
2017-04-22 16:36:25 +03:00
|
|
|
<img
|
|
|
|
style="align:top; max-width:558px; border:1px solid black;"
|
2020-06-08 12:18:24 +03:00
|
|
|
referrerpolicy="no-referrer"
|
2020-06-05 11:17:53 +03:00
|
|
|
src="{$display_image}" />
|
2017-04-22 16:36:25 +03:00
|
|
|
</a>
|
|
|
|
EOD;
|
2020-06-10 23:39:36 +03:00
|
|
|
break;
|
|
|
|
case 'video':
|
|
|
|
case 'animated_gif':
|
|
|
|
if (isset($media->video_info)) {
|
|
|
|
$link = $media->expanded_url;
|
|
|
|
$poster = $media->media_url_https;
|
|
|
|
$video = null;
|
|
|
|
$maxBitrate = -1;
|
|
|
|
foreach ($media->video_info->variants as $variant) {
|
|
|
|
$bitRate = $variant->bitrate ?? -100;
|
|
|
|
if ($bitRate > $maxBitrate) {
|
|
|
|
$maxBitrate = $bitRate;
|
|
|
|
$video = $variant->url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_null($video)) {
|
|
|
|
// add enclosures
|
|
|
|
$item['enclosures'][] = $video;
|
|
|
|
$item['enclosures'][] = $poster;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-10 23:39:36 +03:00
|
|
|
$media_html .= <<<EOD
|
|
|
|
<a href="{$link}">Video</a>
|
|
|
|
<video
|
|
|
|
style="align:top; max-width:558px; border:1px solid black;"
|
|
|
|
referrerpolicy="no-referrer"
|
|
|
|
src="{$video}" poster="{$poster}" />
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Debug::log('Missing support for media type: ' . $media->type);
|
|
|
|
}
|
2019-06-09 18:24:40 +03:00
|
|
|
}
|
2017-04-22 16:36:25 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'By list':
|
2020-11-10 09:12:02 +03:00
|
|
|
case 'By list ID':
|
2020-06-05 11:17:53 +03:00
|
|
|
// Check if filter applies to list (using raw content)
|
|
|
|
if ($this->getInput('filter')) {
|
|
|
|
if (stripos($cleanedTweet, $this->getInput('filter')) === false) {
|
|
|
|
continue 2; // switch + for-loop!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-07-26 09:26:39 +03:00
|
|
|
case 'By username':
|
2021-01-10 11:19:38 +03:00
|
|
|
if ($this->getInput('noretweet') && strtolower($item['username']) != strtolower($this->getInput('u'))) {
|
2020-07-26 09:26:39 +03:00
|
|
|
continue 2; // switch + for-loop!
|
|
|
|
}
|
|
|
|
break;
|
2020-06-05 11:17:53 +03:00
|
|
|
default:
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-08-22 19:55:59 +03:00
|
|
|
$item['content'] = <<<EOD
|
2016-08-09 23:05:42 +03:00
|
|
|
<div style="display: inline-block; vertical-align: top;">
|
2016-08-10 11:44:23 +03:00
|
|
|
{$picture_html}
|
2016-08-09 23:05:42 +03:00
|
|
|
</div>
|
|
|
|
<div style="display: inline-block; vertical-align: top;">
|
|
|
|
<blockquote>{$cleanedTweet}</blockquote>
|
|
|
|
</div>
|
2017-04-22 16:36:25 +03:00
|
|
|
<div style="display: block; vertical-align: top;">
|
2020-06-10 23:39:36 +03:00
|
|
|
<blockquote>{$media_html}</blockquote>
|
2017-04-22 16:36:25 +03:00
|
|
|
</div>
|
|
|
|
EOD;
|
|
|
|
|
2014-05-26 02:30:46 +04:00
|
|
|
// put out
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
usort($this->items, ['TwitterBridge', 'compareTweetId']);
|
2017-04-22 16:36:25 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
private static function compareTweetId($tweet1, $tweet2)
|
|
|
|
{
|
|
|
|
return (intval($tweet1['id']) < intval($tweet2['id']) ? 1 : -1);
|
2017-06-19 00:18:41 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
//The aim of this function is to get an API key and a guest token
|
|
|
|
//This function takes 2 requests, and therefore is cached
|
2022-04-04 20:50:59 +03:00
|
|
|
private function getApiKey($forceNew = 0)
|
|
|
|
{
|
2022-07-06 13:14:04 +03:00
|
|
|
$cacheFactory = new CacheFactory();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-06 13:14:04 +03:00
|
|
|
$r_cache = $cacheFactory->create();
|
2022-08-02 16:03:54 +03:00
|
|
|
$scope = 'TwitterBridge';
|
|
|
|
$r_cache->setScope($scope);
|
2020-06-23 16:14:50 +03:00
|
|
|
$r_cache->setKey(['refresh']);
|
|
|
|
$data = $r_cache->loadData();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-23 16:14:50 +03:00
|
|
|
$refresh = null;
|
|
|
|
if ($data === null) {
|
|
|
|
$refresh = time();
|
|
|
|
$r_cache->saveData($refresh);
|
|
|
|
} else {
|
|
|
|
$refresh = $data;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-06 13:14:04 +03:00
|
|
|
$cacheFactory = new CacheFactory();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-06 13:14:04 +03:00
|
|
|
$cache = $cacheFactory->create();
|
2022-08-02 16:03:54 +03:00
|
|
|
$cache->setScope($scope);
|
2020-06-05 11:17:53 +03:00
|
|
|
$cache->setKey(['api_key']);
|
|
|
|
$data = $cache->loadData();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-08 12:18:24 +03:00
|
|
|
$apiKey = null;
|
2022-04-04 20:50:59 +03:00
|
|
|
if ($forceNew || $data === null || (time() - $refresh) > self::GUEST_TOKEN_EXPIRY) {
|
2020-06-05 11:17:53 +03:00
|
|
|
$twitterPage = getContents('https://twitter.com');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-10-05 10:07:39 +03:00
|
|
|
$jsLink = false;
|
|
|
|
$jsMainRegexArray = [
|
|
|
|
'/(https:\/\/abs\.twimg\.com\/responsive-web\/web\/main\.[^\.]+\.js)/m',
|
|
|
|
'/(https:\/\/abs\.twimg\.com\/responsive-web\/web_legacy\/main\.[^\.]+\.js)/m',
|
|
|
|
'/(https:\/\/abs\.twimg\.com\/responsive-web\/client-web\/main\.[^\.]+\.js)/m',
|
|
|
|
'/(https:\/\/abs\.twimg\.com\/responsive-web\/client-web-legacy\/main\.[^\.]+\.js)/m',
|
|
|
|
];
|
|
|
|
foreach ($jsMainRegexArray as $jsMainRegex) {
|
|
|
|
if (preg_match_all($jsMainRegex, $twitterPage, $jsMainMatches, PREG_SET_ORDER, 0)) {
|
|
|
|
$jsLink = $jsMainMatches[0][0];
|
|
|
|
break;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2020-10-05 10:07:39 +03:00
|
|
|
}
|
|
|
|
if (!$jsLink) {
|
2020-08-06 08:22:17 +03:00
|
|
|
returnServerError('Could not locate main.js link');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-05 11:17:53 +03:00
|
|
|
$jsContent = getContents($jsLink);
|
|
|
|
$apiKeyRegex = '/([a-zA-Z0-9]{59}%[a-zA-Z0-9]{44})/m';
|
|
|
|
preg_match_all($apiKeyRegex, $jsContent, $apiKeyMatches, PREG_SET_ORDER, 0);
|
|
|
|
$apiKey = $apiKeyMatches[0][0];
|
2020-06-08 12:18:24 +03:00
|
|
|
$cache->saveData($apiKey);
|
|
|
|
} else {
|
|
|
|
$apiKey = $data;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-08 12:18:24 +03:00
|
|
|
$cacheFac2 = new CacheFactory();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-07-06 13:14:04 +03:00
|
|
|
$gt_cache = $cacheFactory->create();
|
2022-08-02 16:03:54 +03:00
|
|
|
$gt_cache->setScope($scope);
|
2020-06-08 12:18:24 +03:00
|
|
|
$gt_cache->setKey(['guest_token']);
|
|
|
|
$guestTokenUses = $gt_cache->loadData();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-08 12:18:24 +03:00
|
|
|
$guestToken = null;
|
2022-04-04 20:50:59 +03:00
|
|
|
if (
|
|
|
|
$forceNew || $guestTokenUses === null || !is_array($guestTokenUses) || count($guestTokenUses) != 2
|
2020-06-23 16:14:50 +03:00
|
|
|
|| $guestTokenUses[0] <= 0 || (time() - $refresh) > self::GUEST_TOKEN_EXPIRY
|
|
|
|
) {
|
2022-01-16 05:59:02 +03:00
|
|
|
$guestToken = $this->getGuestToken($apiKey);
|
2022-01-06 08:59:37 +03:00
|
|
|
if ($guestToken === null) {
|
|
|
|
if ($guestTokenUses === null) {
|
|
|
|
returnServerError('Could not parse guest token');
|
|
|
|
} else {
|
|
|
|
$guestToken = $guestTokenUses[1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$gt_cache->saveData([self::GUEST_TOKEN_USES, $guestToken]);
|
|
|
|
$r_cache->saveData(time());
|
|
|
|
}
|
2020-06-08 12:18:24 +03:00
|
|
|
} else {
|
|
|
|
$guestTokenUses[0] -= 1;
|
|
|
|
$gt_cache->saveData($guestTokenUses);
|
|
|
|
$guestToken = $guestTokenUses[1];
|
2019-06-09 18:24:40 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$this->apiKey = $apiKey;
|
|
|
|
$this->guestToken = $guestToken;
|
|
|
|
$this->authHeaders = [
|
|
|
|
'authorization: Bearer ' . $apiKey,
|
|
|
|
'x-guest-token: ' . $guestToken,
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
return [$apiKey, $guestToken];
|
2017-04-22 16:36:25 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2020-06-08 12:18:24 +03:00
|
|
|
// Get a guest token. This is different to an API key,
|
|
|
|
// and it seems to change more regularly than the API key.
|
2022-01-16 05:59:02 +03:00
|
|
|
private function getGuestToken($apiKey)
|
|
|
|
{
|
|
|
|
$headers = [
|
|
|
|
'authorization: Bearer ' . $apiKey,
|
|
|
|
];
|
|
|
|
$opts = [
|
|
|
|
CURLOPT_POST => 1,
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-01-16 05:59:02 +03:00
|
|
|
try {
|
|
|
|
$pageContent = getContents('https://api.twitter.com/1.1/guest/activate.json', $headers, $opts, true);
|
|
|
|
$guestToken = json_decode($pageContent['content'])->guest_token;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$guestToken = null;
|
|
|
|
}
|
2020-06-08 12:18:24 +03:00
|
|
|
return $guestToken;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
/**
|
|
|
|
* Tries to make an API call to twitter.
|
|
|
|
* @param $api string API entry point
|
|
|
|
* @param $params array additional URI parmaeters
|
|
|
|
* @return object json data
|
|
|
|
*/
|
|
|
|
private function makeApiCall($api, $params)
|
|
|
|
{
|
|
|
|
$uri = self::API_URI . $api . '?' . http_build_query($params);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$retries = 1;
|
|
|
|
$retry = 0;
|
|
|
|
do {
|
|
|
|
$retry = 0;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
try {
|
|
|
|
$result = getContents($uri, $this->authHeaders, [], true);
|
2022-05-08 04:58:57 +03:00
|
|
|
} catch (HttpException $e) {
|
|
|
|
switch ($e->getCode()) {
|
2022-04-04 20:50:59 +03:00
|
|
|
case 401:
|
2022-06-24 19:29:35 +03:00
|
|
|
// fall-through
|
2022-04-04 20:50:59 +03:00
|
|
|
case 403:
|
|
|
|
if ($retries) {
|
|
|
|
$retries--;
|
|
|
|
$retry = 1;
|
|
|
|
$this->getApiKey(1);
|
|
|
|
continue 2;
|
|
|
|
}
|
2022-06-24 19:29:35 +03:00
|
|
|
// fall-through
|
2022-04-04 20:50:59 +03:00
|
|
|
default:
|
2022-09-09 21:18:50 +03:00
|
|
|
throw $e;
|
2022-04-04 20:50:59 +03:00
|
|
|
}
|
2019-07-26 11:36:59 +03:00
|
|
|
}
|
2022-04-04 20:50:59 +03:00
|
|
|
} while ($retry);
|
2021-04-12 21:08:38 +03:00
|
|
|
|
2022-04-04 20:50:59 +03:00
|
|
|
$data = json_decode($result['content']);
|
|
|
|
|
|
|
|
return $data;
|
2021-04-12 21:08:38 +03:00
|
|
|
}
|
2014-05-26 02:30:46 +04:00
|
|
|
}
|