From 4ddcd54e8d4a11b19de43e31c81ab03d26ef105c Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sat, 13 Mar 2021 11:18:59 +0300 Subject: [PATCH] * limit progressfunction debugging to size quota exceeded notifications * af_redditimgur: reparent generated iframes outside of post table --- classes/urlhelper.php | 11 ++- plugins/af_redditimgur/init.php | 123 ++++++++++++++++++++------------ 2 files changed, 86 insertions(+), 48 deletions(-) diff --git a/classes/urlhelper.php b/classes/urlhelper.php index 55d5d1e6a..46d80a0e6 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -271,10 +271,15 @@ class UrlHelper { // holy shit closures in php // download & upload are *expected* sizes respectively, could be zero - curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function($curl_handle, $download_size, $downloaded, $upload_size, $uploaded) use( &$max_size) { - Debug::log("[curl progressfunction] $downloaded $max_size", Debug::$LOG_EXTENDED); + curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function($curl_handle, $download_size, $downloaded, $upload_size, $uploaded) use(&$max_size, $url) { + //Debug::log("[curl progressfunction] $downloaded $max_size", Debug::$LOG_EXTENDED); - return ($downloaded > $max_size) ? 1 : 0; // if max size is set, abort when exceeding it + if ($downloaded > $max_size) { + Debug::log("curl: reached max size of $max_size bytes requesting $url, aborting.", Debug::LOG_VERBOSE); + return 1; + } + + return 0; }); } diff --git a/plugins/af_redditimgur/init.php b/plugins/af_redditimgur/init.php index 32970fb99..35eb40950 100755 --- a/plugins/af_redditimgur/init.php +++ b/plugins/af_redditimgur/init.php @@ -132,7 +132,7 @@ class Af_RedditImgur extends Plugin { if (!empty($media["s"]["u"])) { $media_url = htmlspecialchars_decode($media["s"]["u"]); - Debug::log("found media_metadata (gallery): $media_url", Debug::$LOG_VERBOSE); + Debug::log("found media_metadata (gallery): $media_url", Debug::LOG_VERBOSE); if ($media_url) { $this->handle_as_image($doc, $anchor, $media_url); @@ -153,7 +153,7 @@ class Af_RedditImgur extends Plugin { else $poster_url = ""; - Debug::log("found stream fallback_url: $stream_url / poster $poster_url", Debug::$LOG_VERBOSE); + Debug::log("found stream fallback_url: $stream_url / poster $poster_url", Debug::LOG_VERBOSE); $this->handle_as_video($doc, $anchor, $stream_url, $poster_url); } @@ -172,12 +172,12 @@ class Af_RedditImgur extends Plugin { else $poster_url = ""; - Debug::log("found hosted video url: $media_url / poster $poster_url, looking up fallback url...", Debug::$LOG_VERBOSE); + Debug::log("found hosted video url: $media_url / poster $poster_url, looking up fallback url...", Debug::LOG_VERBOSE); $fallback_url = $data["media"]["reddit_video"]["fallback_url"]; if ($fallback_url) { - Debug::log("found video fallback_url: $fallback_url", Debug::$LOG_VERBOSE); + Debug::log("found video fallback_url: $fallback_url", Debug::LOG_VERBOSE); $this->handle_as_video($doc, $anchor, $fallback_url, $poster_url); $found = 1; @@ -192,7 +192,7 @@ class Af_RedditImgur extends Plugin { else $poster_url = ""; - Debug::log("found video url: $media_url / poster $poster_url", Debug::$LOG_VERBOSE); + Debug::log("found video url: $media_url / poster $poster_url", Debug::LOG_VERBOSE); $this->handle_as_video($doc, $anchor, $media_url, $poster_url); $found = 1; @@ -201,7 +201,7 @@ class Af_RedditImgur extends Plugin { if (!$found && $post_hint == "image") { $media_url = $data["url"]; - Debug::log("found image url: $media_url", Debug::$LOG_VERBOSE); + Debug::log("found image url: $media_url", Debug::LOG_VERBOSE); $this->handle_as_image($doc, $anchor, $media_url); $found = 1; @@ -215,12 +215,12 @@ class Af_RedditImgur extends Plugin { if ($media_url) { if ($post_hint == "self") { - Debug::log("found preview image url: $media_url (link: $target_url)", Debug::$LOG_VERBOSE); + Debug::log("found preview image url: $media_url (link: $target_url)", Debug::LOG_VERBOSE); $this->handle_as_image($doc, $anchor, $media_url, $target_url); $found = 1; } else { // gonna use this later if nothing is found using generic link processing - Debug::log("found fallback preview image url: $media_url (link: $target_url);", Debug::$LOG_VERBOSE); + Debug::log("found fallback preview image url: $media_url (link: $target_url);", Debug::LOG_VERBOSE); array_push($this->fallback_preview_urls, $media_url); } } @@ -247,12 +247,12 @@ class Af_RedditImgur extends Plugin { $link_flairs = []; $apply_nsfw_tags = FeedItem_Common::normalize_categories($this->host->get_array($this, "apply_nsfw_tags", [])); - // embed before reddit post layout + // embed anchor element, before reddit
post layout $anchor = $xpath->query('//body/*')->item(0); // deal with json-provided media content first if ($article["link"] && $anchor) { - Debug::log("JSON: requesting from URL: " . $article["link"] . "/.json", Debug::$LOG_VERBOSE); + Debug::log("JSON: requesting from URL: " . $article["link"] . "/.json", Debug::LOG_VERBOSE); $tmp = UrlHelper::fetch($article["link"] . "/.json"); @@ -263,7 +263,7 @@ class Af_RedditImgur extends Plugin { $json = json_decode($tmp, true); if ($json) { - Debug::log("JSON: processing media elements...", Debug::$LOG_EXTENDED); + Debug::log("JSON: processing media elements...", Debug::LOG_EXTENDED); if ($this->dump_json_data) print_r($json); @@ -281,12 +281,12 @@ class Af_RedditImgur extends Plugin { } if ($over_18) { - Debug::log("JSON: post is NSFW", Debug::$LOG_EXTENDED); + Debug::log("JSON: post is NSFW", Debug::LOG_EXTENDED); $post_is_nsfw = true; } if (isset($data["crosspost_parent_list"])) { - Debug::log("JSON: processing child crosspost_parent_list", Debug::$LOG_EXTENDED); + Debug::log("JSON: processing child crosspost_parent_list", Debug::LOG_EXTENDED); foreach ($data["crosspost_parent_list"] as $parent) { if ($this->process_post_media($parent, $doc, $xpath, $anchor)) { @@ -297,7 +297,7 @@ class Af_RedditImgur extends Plugin { } } - Debug::log("JSON: processing child data element...", Debug::$LOG_EXTENDED); + Debug::log("JSON: processing child data element...", Debug::LOG_EXTENDED); if (!$found && $this->process_post_media($data, $doc, $xpath, $anchor)) { $found = 1; @@ -307,15 +307,15 @@ class Af_RedditImgur extends Plugin { } } } else { - Debug::log("JSON: failed to parse received data.", Debug::$LOG_EXTENDED); + Debug::log("JSON: failed to parse received data.", Debug::LOG_EXTENDED); } } else { if (!$tmp) { - Debug::log("JSON: failed to fetch post:" . UrlHelper::$fetch_last_error, Debug::$LOG_EXTENDED); + Debug::log("JSON: failed to fetch post:" . UrlHelper::$fetch_last_error, Debug::LOG_EXTENDED); } } } else if (!$anchor) { - Debug::log("JSON: anchor element not found, unable to embed", Debug::$LOG_EXTENDED); + Debug::log("JSON: anchor element not found, unable to embed", Debug::LOG_EXTENDED); } if ($post_is_nsfw && count($apply_nsfw_tags) > 0) { @@ -332,7 +332,7 @@ class Af_RedditImgur extends Plugin { $article["score_modifier"] = ($article["score_modifier"] ?? 0) + ($score > $max_score ? $max_score : $score); if ($found) { - Debug::log("JSON: found media data, skipping further processing of content", Debug::$LOG_VERBOSE); + Debug::log("JSON: found media data, skipping further processing of content", Debug::LOG_VERBOSE); $this->remove_post_thumbnail($doc, $xpath); return true; } @@ -346,14 +346,14 @@ class Af_RedditImgur extends Plugin { /* skip links going back to reddit (and any other blacklisted stuff) */ if (!$found && $this->is_blacklisted($entry_href, ["reddit.com"])) { - Debug::log("BODY: domain of $entry_href is blacklisted, skipping", Debug::$LOG_EXTENDED); + Debug::log("BODY: domain of $entry_href is blacklisted, skipping", Debug::LOG_EXTENDED); continue; } - Debug::log("BODY: processing URL: " . $entry_href, Debug::$LOG_VERBOSE); + Debug::log("BODY: processing URL: " . $entry_href, Debug::LOG_VERBOSE); if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry_href, $matches)) { - Debug::log("handling as twitter: " . $matches[1] . " " . $matches[2], Debug::$LOG_VERBOSE); + Debug::log("handling as twitter: " . $matches[1] . " " . $matches[2], Debug::LOG_VERBOSE); $oembed_result = UrlHelper::fetch("https://publish.twitter.com/oembed?url=" . urlencode($entry_href)); @@ -385,7 +385,7 @@ class Af_RedditImgur extends Plugin { if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry_href, $matches)) { - Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE); + Debug::log("Handling as Gfycat", Debug::LOG_VERBOSE); $source_stream = 'https://giant.gfycat.com/' . $matches[2] . '.mp4'; $poster_url = 'https://thumbs.gfycat.com/' . $matches[2] . '-mobile.jpg'; @@ -400,14 +400,14 @@ class Af_RedditImgur extends Plugin { // imgur .gif -> .gifv if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry_href)) { - Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE); + Debug::log("Handling as imgur gif (->gifv)", Debug::LOG_VERBOSE); $entry->setAttribute("href", str_replace(".gif", ".gifv", $entry_href)); } if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry_href)) { - Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE); + Debug::log("Handling as imgur gifv", Debug::LOG_VERBOSE); $source_stream = str_replace(".gifv", ".mp4", $entry_href); @@ -430,7 +430,7 @@ class Af_RedditImgur extends Plugin { $vid_id = $matches[1]; - Debug::log("Handling as youtube: $vid_id", Debug::$LOG_VERBOSE); + Debug::log("Handling as youtube: $vid_id", Debug::LOG_VERBOSE); $iframe = $doc->createElement("iframe"); $iframe->setAttribute("class", "youtube-player"); @@ -441,9 +441,15 @@ class Af_RedditImgur extends Plugin { $iframe->setAttribute("allowfullscreen", "1"); $iframe->setAttribute("frameborder", "0"); - $br = $doc->createElement('br'); - $entry->parentNode->insertBefore($iframe, $entry); - $entry->parentNode->insertBefore($br, $entry); + //$br = $doc->createElement('br'); + //$entry->parentNode->insertBefore($iframe, $entry); + //$entry->parentNode->insertBefore($br, $entry); + + // reparent generated iframe because it doesn't scale well inside
+ if ($anchor) + $anchor->parentNode->insertBefore($iframe, $anchor); + else + $entry->parentNode->insertBefore($iframe, $entry); $found = true; } @@ -452,7 +458,7 @@ class Af_RedditImgur extends Plugin { /* mb_strpos($entry_href, "i.reddituploads.com") !== false || */ mb_strpos($this->get_content_type($entry_href), "image/") !== false)) { - Debug::log("Handling as a picture", Debug::$LOG_VERBOSE); + Debug::log("Handling as a picture", Debug::LOG_VERBOSE); $img = $doc->createElement('img'); $img->setAttribute("src", $entry_href); @@ -467,7 +473,7 @@ class Af_RedditImgur extends Plugin { // imgur via link rel="image_src" href="..." if (!$found && preg_match("/imgur/", $entry_href)) { - Debug::log("handling as imgur page/whatever", Debug::$LOG_VERBOSE); + Debug::log("handling as imgur page/whatever", Debug::LOG_VERBOSE); $content = UrlHelper::fetch(["url" => $entry_href, "http_accept" => "text/*"]); @@ -499,7 +505,7 @@ class Af_RedditImgur extends Plugin { if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry_href, $matches)) { $img_id = $matches[1]; - Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE); + Debug::log("handling as gyazo: $img_id", Debug::LOG_VERBOSE); $img = $doc->createElement('img'); $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg"); @@ -513,7 +519,7 @@ class Af_RedditImgur extends Plugin { // let's try meta properties if (!$found) { - Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE); + Debug::log("looking for meta og:image", Debug::LOG_VERBOSE); $content = UrlHelper::fetch(["url" => $entry_href, "http_accept" => "text/*"]); @@ -564,7 +570,7 @@ class Af_RedditImgur extends Plugin { } if (!$found && $anchor && count($this->fallback_preview_urls) > 0) { - Debug::log("JSON: processing fallback preview urls...", Debug::$LOG_VERBOSE); + Debug::log("JSON: processing fallback preview urls...", Debug::LOG_VERBOSE); foreach ($this->fallback_preview_urls as $media_url) { $this->handle_as_image($doc, $anchor, $media_url); @@ -671,7 +677,7 @@ class Af_RedditImgur extends Plugin { private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) { - Debug::log("handle_as_video: $source_stream", Debug::$LOG_VERBOSE); + Debug::log("handle_as_video: $source_stream", Debug::LOG_VERBOSE); $video = $doc->createElement('video'); $video->setAttribute("autoplay", "1"); @@ -705,10 +711,11 @@ class Af_RedditImgur extends Plugin { $url = clean($_POST["url"] ?? ""); $article_url = clean($_POST["article_url"] ?? ""); + $article_id = clean($_POST["article_id"] ?? ""); $this->dump_json_data = true; - if (!$url && !$article_url) { + if (!$url && !$article_url && !$article_id) { header("Content-type: text/html"); ?>