From 7e4807530ec6199ac9754bce54f025a113edfbbe Mon Sep 17 00:00:00 2001 From: Dag <me@dvikan.no> Date: Mon, 31 Jul 2023 20:43:18 +0200 Subject: [PATCH] fix: various small fixes (#3580) --- bridges/AppleMusicBridge.php | 4 +++- bridges/AskfmBridge.php | 3 ++- bridges/FeedReducerBridge.php | 17 +++++++++-------- bridges/FourchanBridge.php | 3 ++- bridges/GettrBridge.php | 3 ++- bridges/TelegramBridge.php | 12 ++++++++++-- bridges/TikTokBridge.php | 3 +++ lib/BridgeAbstract.php | 4 ++++ 8 files changed, 35 insertions(+), 14 deletions(-) diff --git a/bridges/AppleMusicBridge.php b/bridges/AppleMusicBridge.php index 4c3e0e2f..900a7009 100644 --- a/bridges/AppleMusicBridge.php +++ b/bridges/AppleMusicBridge.php @@ -40,6 +40,8 @@ class AppleMusicBridge extends BridgeAbstract foreach ($json->results as $obj) { if ($obj->wrapperType === 'collection') { + $copyright = $obj->copyright ?? ''; + $this->items[] = [ 'title' => $obj->artistName . ' - ' . $obj->collectionName, 'uri' => $obj->collectionViewUrl, @@ -49,7 +51,7 @@ class AppleMusicBridge extends BridgeAbstract . '><img src="' . $obj->artworkUrl100 . '" /></a><br><br>' . $obj->artistName . ' - ' . $obj->collectionName . '<br>' - . $obj->copyright, + . $copyright, ]; } } diff --git a/bridges/AskfmBridge.php b/bridges/AskfmBridge.php index 0a326417..d0422890 100644 --- a/bridges/AskfmBridge.php +++ b/bridges/AskfmBridge.php @@ -37,7 +37,8 @@ class AskfmBridge extends BridgeAbstract $item['timestamp'] = strtotime($element->find('time', 0)->datetime); - $answer = trim($element->find('div.streamItem_content', 0)->innertext); + $var = $element->find('div.streamItem_content', 0); + $answer = trim($var->innertext ?? ''); // This probably should be cleaned up, especially for YouTube embeds if ($visual = $element->find('div.streamItem_visual', 0)) { diff --git a/bridges/FeedReducerBridge.php b/bridges/FeedReducerBridge.php index a37824c9..37bf9809 100644 --- a/bridges/FeedReducerBridge.php +++ b/bridges/FeedReducerBridge.php @@ -23,8 +23,9 @@ class FeedReducerBridge extends FeedExpander public function collectData() { - if (preg_match('#^http(s?)://#i', $this->getInput('url'))) { - $this->collectExpandableDatas($this->getInput('url')); + $url = $this->getInput('url'); + if (preg_match('#^http(s?)://#i', $url)) { + $this->collectExpandableDatas($url); } else { throw new Exception('URI must begin with http(s)://'); } @@ -35,7 +36,7 @@ class FeedReducerBridge extends FeedExpander $filteredItems = []; $intPercentage = (int)preg_replace('/[^0-9]/', '', $this->getInput('percentage')); - foreach ($this->items as $thisItem) { + foreach ($this->items as $item) { // The URL is included in the hash: // - so you can change the output by adding a local-part to the URL // - so items with the same URI in different feeds won't be correlated @@ -43,13 +44,13 @@ class FeedReducerBridge extends FeedExpander // $pseudoRandomInteger will be a 16 bit unsigned int mod 100. // This won't be uniformly distributed 1-100, but should be close enough. - $pseudoRandomInteger = unpack( - 'S', // unsigned 16-bit int - hash('sha256', $thisItem['uri'] . '::' . $this->getInput('url'), true) - )[1] % 100; + $data = $item['uri'] . '::' . $this->getInput('url'); + $hash = hash('sha256', $data, true); + // S = unsigned 16-bit int + $pseudoRandomInteger = unpack('S', $hash)[1] % 100; if ($pseudoRandomInteger < $intPercentage) { - $filteredItems[] = $thisItem; + $filteredItems[] = $item; } } diff --git a/bridges/FourchanBridge.php b/bridges/FourchanBridge.php index 179ae91f..f67d6026 100644 --- a/bridges/FourchanBridge.php +++ b/bridges/FourchanBridge.php @@ -45,7 +45,8 @@ class FourchanBridge extends BridgeAbstract $file = $element->find('.file', 0); if (!empty($file)) { - $item['image'] = $element->find('.file a', 0)->href; + $var = $element->find('.file a', 0); + $item['image'] = $var->href ?? ''; $item['imageThumb'] = $element->find('.file img', 0)->src; if (!isset($item['imageThumb']) and strpos($item['image'], '.swf') !== false) { $item['imageThumb'] = 'http://i.imgur.com/eO0cxf9.jpg'; diff --git a/bridges/GettrBridge.php b/bridges/GettrBridge.php index 2b019523..74804043 100644 --- a/bridges/GettrBridge.php +++ b/bridges/GettrBridge.php @@ -27,9 +27,10 @@ class GettrBridge extends BridgeAbstract public function collectData() { + $user = $this->getInput('user'); $api = sprintf( 'https://api.gettr.com/u/user/%s/posts?offset=0&max=%s&dir=fwd&incl=posts&fp=f_uo', - $this->getInput('user'), + $user, min($this->getInput('limit'), 20) ); $data = json_decode(getContents($api), false); diff --git a/bridges/TelegramBridge.php b/bridges/TelegramBridge.php index 14359009..9d73e06e 100644 --- a/bridges/TelegramBridge.php +++ b/bridges/TelegramBridge.php @@ -169,11 +169,19 @@ EOD; $stickerDiv->find('picture', 0)->style = ''; return $stickerDiv; - } elseif (preg_match(self::BACKGROUND_IMAGE_REGEX, $stickerDiv->find('i', 0)->style, $sticker)) { - return <<<EOD + } + + $var = $stickerDiv->find('i', 0); + if ($var) { + $style = $var->style; + if (preg_match(self::BACKGROUND_IMAGE_REGEX, $style, $sticker)) { + return <<<EOD <a href="{$stickerDiv->children(0)->herf}"><img src="{$sticker[1]}"></a> EOD; + } } + + return ''; } private function processPoll($messageDiv) diff --git a/bridges/TikTokBridge.php b/bridges/TikTokBridge.php index 556e5ffc..769bc625 100644 --- a/bridges/TikTokBridge.php +++ b/bridges/TikTokBridge.php @@ -34,6 +34,9 @@ class TikTokBridge extends BridgeAbstract $this->feedName = htmlspecialchars_decode($title); $var = $html->find('script[id=SIGI_STATE]', 0); + if (!$var) { + throw new \Exception('Unable to find tiktok user data for ' . $this->processUsername()); + } $SIGI_STATE_RAW = $var->innertext; $SIGI_STATE = Json::decode($SIGI_STATE_RAW, false); diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index eb9d5a3c..61eafb57 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -144,6 +144,10 @@ abstract class BridgeAbstract implements BridgeInterface } foreach ($contexts as $context) { + if (!isset(static::PARAMETERS[$context])) { + // unknown context provided by client, throw exception here? or continue? + } + foreach (static::PARAMETERS[$context] as $name => $properties) { if (isset($this->inputs[$context][$name]['value'])) { continue;