Configuration Instructions.';
const MAINTAINER = 'quickwick';
const CONFIGURATION = [
'twitterv2apitoken' => [
'required' => true,
]
];
const PARAMETERS = [
'global' => [
'filter' => [
'name' => 'Filter',
'exampleValue' => 'rss-bridge',
'required' => false,
'title' => 'Specify a single term to search for'
],
'norep' => [
'name' => 'Without replies',
'type' => 'checkbox',
'title' => 'Activate to exclude reply tweets'
],
'noretweet' => [
'name' => 'Without retweets',
'required' => false,
'type' => 'checkbox',
'title' => 'Activate to exclude retweets'
],
'nopinned' => [
'name' => 'Without pinned tweet',
'required' => false,
'type' => 'checkbox',
'title' => 'Activate to exclude pinned tweets'
],
'maxresults' => [
'name' => 'Maximum results',
'required' => false,
'exampleValue' => '20',
'title' => 'Maximum number of tweets to retrieve (limit is 100)'
],
'imgonly' => [
'name' => 'Only media tweets',
'type' => 'checkbox',
'title' => 'Activate to show only tweets with media (photo/video)'
],
'nopic' => [
'name' => 'Hide profile pictures',
'type' => 'checkbox',
'title' => 'Activate to hide profile pictures in content'
],
'noimg' => [
'name' => 'Hide images in tweets',
'type' => 'checkbox',
'title' => 'Activate to hide images in tweets'
],
'noimgscaling' => [
'name' => 'Disable image scaling',
'type' => 'checkbox',
'title' => 'Activate to display original sized images (no thumbnails)'
],
'noexternallink' => [
'name' => 'Hide external link from content html',
'type' => 'checkbox',
'title' => 'Activate to hide the links from the content html field'
],
'idastitle' => [
'name' => 'Use tweet id as title',
'type' => 'checkbox',
'title' => 'Activate to use tweet id as title (instead of tweet text)'
]
],
'By username' => [
'u' => [
'name' => 'username',
'required' => true,
'exampleValue' => 'sebsauvage',
'title' => 'Insert a user name'
]
],
'By keyword or hashtag' => [
'query' => [
'name' => 'Keyword or #hashtag',
'required' => true,
'exampleValue' => 'rss-bridge OR #rss-bridge',
'title' => << $quotedUser->name @$quotedUser->username ยท
$quotedTweet->created_at
EOD;
}
// Generate media HTML block
$media_html = '';
$quoted_media_html = '';
$ext_media_html = '';
if (!$hideImages) {
if (isset($tweet->attachments->media_keys)) {
Debug::log('Generating HTML for tweet media');
$media_html = $this->createTweetMediaHTML($tweet, $includesMedia, $retweetedMedia);
}
if (isset($quotedTweet->attachments->media_keys)) {
Debug::log('Generating HTML for quoted tweet media');
$quoted_media_html = $this->createTweetMediaHTML($quotedTweet, $includesMedia, $retweetedMedia);
}
if (isset($extURL)) {
Debug::log('Generating HTML for external link media');
if ($this->getInput('noimgscaling')) {
$extMediaURL = $extMediaOrig;
} else {
$extMediaURL = $extMediaScaled;
}
$ext_media_html = <<
EOD;
}
}
// Generate the HTML for Item content
$this->item['content'] = <<
$extDisplayURL
$extTitle
$extDesc
EXTERNAL;
$this->item['content'] .= $ext_html;
}
$this->item['content'] = htmlspecialchars_decode($this->item['content'], ENT_QUOTES);
// Add current Item to Items array
$this->items[] = $this->item;
}
// Sort all tweets in array by date
usort($this->items, ['TwitterV2Bridge', 'compareTweetDate']);
}
private static function compareTweetDate($tweet1, $tweet2)
{
return (strtotime($tweet1['timestamp']) < strtotime($tweet2['timestamp']) ? 1 : -1);
}
/**
* 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, $authHeaders, $params)
{
$uri = self::API_URI . $api . '?' . http_build_query($params);
$result = getContents($uri, $authHeaders, [], false);
$data = json_decode($result);
return $data;
}
/**
* Change format of URLs in tweet text
* @param $tweetObject object current Tweet JSON
* @param $tweetText string current Tweet text
* @return string modified tweet text
*/
private function replaceTweetURLs($tweetObject, $tweetText)
{
$foundUrls = false;
// Rewrite URL links, based on URL list in tweet object
if (isset($tweetObject->entities->urls)) {
foreach ($tweetObject->entities->urls as $url) {
$tweetText = str_replace(
$url->url,
'' . $url->display_url . '',
$tweetText
);
}
$foundUrls = true;
}
// Regex fallback for rewriting URL links. Should never trigger?
if ($foundUrls === false) {
$reg_ex = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
if (preg_match($reg_ex, $tweetText, $url)) {
$tweetText = preg_replace(
$reg_ex,
"{$url[0]} ",
$tweetText
);
}
}
// Fix back-to-back URLs by adding a
$reg_ex = '/\/a>\s*
id === $tweetObject->author_id) {
$matchedUser = $retweetedUser;
Debug::log('Found author_id match in $retweetedUsers');
break;
}
}
}
if (!isset($matchedUser->username) && isset($includesUsers)) {
Debug::log('Searching for tweet author_id in $includesUsers');
foreach ($includesUsers as $includesUser) {
if ($includesUser->id === $tweetObject->author_id) {
$matchedUser = $includesUser;
Debug::log('Found author_id match in $includesUsers');
break;
}
}
}
return $matchedUser;
}
/**
* Generates HTML for embedded media
* @param $tweetObject object current Tweet JSON
* @param $includesMedia
* @param $retweetedMedia
* @return string modified tweet text
*/
private function createTweetMediaHTML($tweetObject, $includesMedia, $retweetedMedia)
{
$media_html = '';
// Match media_keys in tweet to media list from, put matches into new array
$tweetMedia = [];
// Start by checking the original list of tweet Media includes
if (isset($includesMedia)) {
Debug::log('Searching for media_key in $includesMedia');
foreach ($includesMedia as $includesMedium) {
if (
in_array(
$includesMedium->media_key,
$tweetObject->attachments->media_keys
)
) {
Debug::log('Found media_key in $includesMedia');
$tweetMedia[] = $includesMedium;
}
}
}
// If no matches found, check the retweet Media includes
if (empty($tweetMedia) && isset($retweetedMedia)) {
Debug::log('Searching for media_key in $retweetedMedia');
foreach ($retweetedMedia as $retweetedMedium) {
if (
in_array(
$retweetedMedium->media_key,
$tweetObject->attachments->media_keys
)
) {
Debug::log('Found media_key in $retweetedMedia');
$tweetMedia[] = $retweetedMedium;
}
}
}
foreach ($tweetMedia as $media) {
switch ($media->type) {
case 'photo':
if ($this->getInput('noimgscaling')) {
$image = $media->url;
$display_image = $media->url;
} else {
$image = $media->url . '?name=orig';
$display_image = $media->url;
}
// add enclosures
$this->item['enclosures'][] = $image;
$media_html .= <<