From 0d1336bd29ba32ef82dd0f80dde4f018e6ab32b9 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 21 Jan 2021 08:28:55 +0300 Subject: [PATCH] af_redditimgur: * draw a basic form for testurl() if no url is given * only process specific JSON media files/child elements until something is found * handle generic preview images for self posts (not link posts because link is handled afterwards) --- plugins/af_redditimgur/init.php | 83 +++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/plugins/af_redditimgur/init.php b/plugins/af_redditimgur/init.php index df353da83..b919fd836 100755 --- a/plugins/af_redditimgur/init.php +++ b/plugins/af_redditimgur/init.php @@ -132,7 +132,7 @@ class Af_RedditImgur extends Plugin { } } */ - if ($data["post_hint"] == "hosted:video") { + if (!$found && $data["post_hint"] == "hosted:video") { $media_url = $data["url"]; if (isset($data["preview"]["images"][0]["source"])) @@ -152,7 +152,7 @@ class Af_RedditImgur extends Plugin { } } - if ($data["post_hint"] == "video") { + if (!$found && $data["post_hint"] == "video") { $media_url = $data["url"]; if (isset($data["preview"]["images"][0]["source"])) @@ -166,7 +166,7 @@ class Af_RedditImgur extends Plugin { $found = 1; } - if ($data["post_hint"] == "image") { + if (!$found && $data["post_hint"] == "image") { $media_url = $data["url"]; Debug::log("found image url: $media_url", Debug::$LOG_VERBOSE); @@ -175,6 +175,23 @@ class Af_RedditImgur extends Plugin { $found = 1; } + if (!$found && $data["post_hint"] == "self" && is_array($data["preview"]["images"])) { + foreach ($data["preview"]["images"] as $img) { + if (isset($img["source"]["url"])) { + $media_url = htmlspecialchars_decode($img["source"]["url"]); + $target_url = $data["url"]; + + if ($media_url) { + 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; + } + } + } + } + return $found; } @@ -194,9 +211,11 @@ class Af_RedditImgur extends Plugin { if ($tmp && $anchor) { $json = json_decode($tmp, true); - Debug::log("JSON: processing media elements...", Debug::$LOG_EXTENDED); - if ($json) { + Debug::log("JSON: processing media elements...", Debug::$LOG_EXTENDED); + + //print_r($json); + foreach ($json as $listing) { foreach ($listing["data"]["children"] as $child) { @@ -208,7 +227,8 @@ class Af_RedditImgur extends Plugin { foreach ($data["crosspost_parent_list"] as $parent) { if ($this->process_post_media($parent, $doc, $xpath, $anchor)) { $found = 1; - continue; + + break 2; } } } @@ -217,10 +237,13 @@ class Af_RedditImgur extends Plugin { if (!$found && $this->process_post_media($data, $doc, $xpath, $anchor)) { $found = 1; - continue; + + break 2; } } } + } else { + Debug::log("JSON: failed to parse received data.", Debug::$LOG_EXTENDED); } } else { if (!$tmp) { @@ -538,12 +561,21 @@ class Af_RedditImgur extends Plugin { $thumb->parentNode->parentNode->removeChild($thumb->parentNode); } - private function handle_as_image($doc, $entry, $image_url) { + private function handle_as_image($doc, $entry, $image_url, $link_url = false) { $img = $doc->createElement("img"); $img->setAttribute("src", $image_url); $p = $doc->createElement("p"); - $p->appendChild($img); + + if ($link_url) { + $a = $doc->createElement("a"); + $a->setAttribute("href", $link_url); + + $a->appendChild($img); + $p->appendChild($a); + } else { + $p->appendChild($img); + } $entry->parentNode->insertBefore($p, $entry); } @@ -576,16 +608,40 @@ class Af_RedditImgur extends Plugin { $entry->parentNode->insertBefore($img, $entry);*/ } - // TODO: draw a form or something if url/article_url are not given function testurl() { + + $url = clean($_POST["url"]); + $article_url = clean($_POST["article_url"]); + + if (!$url && !$article_url) { + header("Content-type: text/html"); + ?> + +
+
+ + +
+
+ + +
+
+ +
+
+ saveHTML(); } + } private function get_header($url, $header, $useragent = SELF_USER_AGENT) {