summaryrefslogtreecommitdiff
path: root/plugins/af_redditimgur
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2021-01-19 22:21:57 +0300
committerAndrew Dolgov <[email protected]>2021-01-19 22:21:57 +0300
commit1ded706f8f12f3832a1944a5bb50b0362b872b53 (patch)
tree64215f1cb0e154550791ed9fc6cf0bfa54336065 /plugins/af_redditimgur
parent2933483393b20882b700811844a5bb538bb69d6c (diff)
af_redditimgur: cleanup, rework to embed stuff from reddit-provided JSON first
Diffstat (limited to 'plugins/af_redditimgur')
-rwxr-xr-xplugins/af_redditimgur/init.php528
1 files changed, 284 insertions, 244 deletions
diff --git a/plugins/af_redditimgur/init.php b/plugins/af_redditimgur/init.php
index 6f08cd944..df353da83 100755
--- a/plugins/af_redditimgur/init.php
+++ b/plugins/af_redditimgur/init.php
@@ -96,353 +96,374 @@ class Af_RedditImgur extends Plugin {
echo __("Configuration saved");
}
- /**
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- private function inline_stuff($article, &$doc, $xpath) {
+ private function process_post_media($data, $doc, $xpath, $anchor) {
+ $found = 0;
- $entries = $xpath->query('(//a[@href]|//img[@src])');
- $img_entries = $xpath->query("(//img[@src])");
+ if (is_array($data["media_metadata"])) {
+ foreach ($data["media_metadata"] as $media) {
+ $media_url = htmlspecialchars_decode($media["s"]["u"]);
- $found = false;
+ Debug::log("found media_metadata (gallery): $media_url", Debug::$LOG_VERBOSE);
- foreach ($entries as $entry) {
- if ($entry->hasAttribute("href")) {
- $entry_href = $entry->getAttribute("href");
+ if ($media_url) {
+ $this->handle_as_image($doc, $anchor, $media_url);
+ $found = 1;
+ }
+ }
+ }
- Debug::log("processing href: " . $entry_href, Debug::$LOG_VERBOSE);
+ // v.redd.it - see below
+ /* if (is_array($data["media"])) {
+ foreach ($data["media"] as $media) {
+ if (isset($media["fallback_url"])) {
+ $stream_url = $media["fallback_url"];
- $matches = [];
+ if (isset($data["preview"]["images"][0]["source"]))
+ $poster_url = $data["preview"]["images"][0]["source"]["url"];
+ else
+ $poster_url = "";
- if (!$found && preg_match("/^https?:\/\/www\.reddit\.com\/gallery\/(.*)/", $entry_href, $matches)) {
- Debug::log("handling as a reddit gallery: " . $matches[1], Debug::$LOG_VERBOSE);
+ Debug::log("found stream fallback_url: $stream_url / poster $poster_url", Debug::$LOG_VERBOSE);
- $tmp = UrlHelper::fetch($entry_href);
+ $this->handle_as_video($doc, $anchor, $stream_url, $poster_url);
+ }
- if ($tmp) {
- $tmpdoc = new DOMDocument();
+ $found = 1;
+ }
+ } */
- if (@$tmpdoc->loadHTML($tmp)) {
- $tmpxpath = new DOMXPath($tmpdoc);
+ if ($data["post_hint"] == "hosted:video") {
+ $media_url = $data["url"];
- $links = $tmpxpath->query("//figure/a[@href]");
+ if (isset($data["preview"]["images"][0]["source"]))
+ $poster_url = htmlspecialchars_decode($data["preview"]["images"][0]["source"]["url"]);
+ else
+ $poster_url = "";
- foreach ($links as $link) {
- $link_href = $link->getAttribute("href");
+ Debug::log("found hosted video url: $media_url / poster $poster_url, looking up fallback url...", Debug::$LOG_VERBOSE);
- if (strpos($link_href, "preview.redd.it") !== false) {
- Debug::log("found URL: $link_href", Debug::$LOG_EXTENDED);
+ $fallback_url = $data["media"]["reddit_video"]["fallback_url"];
- // TODO: could there be other media types? videos?
- if (strpos($link_href, ".jpg") !== false) {
- $img = $doc->createElement("img");
- $img->setAttribute("src", $link_href);
+ if ($fallback_url) {
+ Debug::log("found video fallback_url: $fallback_url", Debug::$LOG_VERBOSE);
+ $this->handle_as_video($doc, $anchor, $fallback_url, $poster_url);
- $p = $doc->createElement("p");
- $p->appendChild($img);
+ $found = 1;
+ }
+ }
- $entry->parentNode->insertBefore($p, $entry);
+ if ($data["post_hint"] == "video") {
+ $media_url = $data["url"];
- $found = true;
- }
- }
- }
- }
- }
- }
+ if (isset($data["preview"]["images"][0]["source"]))
+ $poster_url = htmlspecialchars_decode($data["preview"]["images"][0]["source"]["url"]);
+ else
+ $poster_url = "";
- /* skip other links going to reddit other than galleries (and any other blacklisted stuff) */
- if (!$found && $this->is_blacklisted($entry_href, ["reddit.com"])) {
- Debug::log("domain is blacklisted, skipping", Debug::$LOG_VERBOSE);
- continue;
- }
+ Debug::log("found video url: $media_url / poster $poster_url", Debug::$LOG_VERBOSE);
+ $this->handle_as_video($doc, $anchor, $media_url, $poster_url);
- if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry_href, $matches)) {
- Debug::log("handling as twitter: " . $matches[1] . " " . $matches[2], Debug::$LOG_VERBOSE);
+ $found = 1;
+ }
- $oembed_result = UrlHelper::fetch("https://publish.twitter.com/oembed?url=" . urlencode($entry_href));
+ if ($data["post_hint"] == "image") {
+ $media_url = $data["url"];
- if ($oembed_result) {
- $oembed_result = json_decode($oembed_result, true);
+ Debug::log("found image url: $media_url", Debug::$LOG_VERBOSE);
+ $this->handle_as_image($doc, $anchor, $media_url);
- if ($oembed_result && isset($oembed_result["html"])) {
+ $found = 1;
+ }
- $tmp = new DOMDocument();
- if (@$tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
- $p = $doc->createElement("p");
+ return $found;
+ }
- $p->appendChild($doc->importNode(
- $tmp->getElementsByTagName("blockquote")->item(0), TRUE));
+ private function inline_stuff($article, &$doc, $xpath) {
- $br = $doc->createElement('br');
- $entry->parentNode->insertBefore($p, $entry);
- $entry->parentNode->insertBefore($br, $entry);
+ $found = false;
+ // deal with json-provided media content first
+ if ($article["link"]) {
+ Debug::log("JSON: requesting from URL: " . $article["link"] . "/.json", Debug::$LOG_VERBOSE);
+
+ $tmp = UrlHelper::fetch($article["link"] . "/.json");
+
+ // embed before reddit <table> post layout
+ $anchor = $xpath->query('//body/*')->item(0);
+
+ if ($tmp && $anchor) {
+ $json = json_decode($tmp, true);
+
+ Debug::log("JSON: processing media elements...", Debug::$LOG_EXTENDED);
+
+ if ($json) {
+ foreach ($json as $listing) {
+ foreach ($listing["data"]["children"] as $child) {
+
+ $data = $child["data"];
+
+ if (is_array($data["crosspost_parent_list"])) {
+ 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)) {
+ $found = 1;
+ continue;
+ }
+ }
+ }
+
+ Debug::log("JSON: processing child data element...", Debug::$LOG_EXTENDED);
+
+ if (!$found && $this->process_post_media($data, $doc, $xpath, $anchor)) {
$found = 1;
+ continue;
}
}
}
}
+ } else {
+ if (!$tmp) {
+ global $fetch_last_error;
+ Debug::log("JSON: failed to fetch post:" . $fetch_last_error, Debug::$LOG_EXTENDED);
+ }
- if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry_href, $matches)) {
- $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
+ if (!$anchor) {
+ Debug::log("JSON: anchor element not found, unable to embed", Debug::$LOG_EXTENDED);
}
+ }
+ }
- if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry_href, $matches)) {
+ if ($found) {
+ Debug::log("JSON: found media data, skipping further processing of content", Debug::$LOG_VERBOSE);
+ $this->remove_post_thumbnail($doc, $xpath);
+ return true;
+ }
- Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE);
+ $entries = $xpath->query('//a[@href]');
- $source_stream = 'https://giant.gfycat.com/' . $matches[2] . '.mp4';
- $poster_url = 'https://thumbs.gfycat.com/' . $matches[2] . '-mobile.jpg';
+ foreach ($entries as $entry) {
+ $entry_href = $entry->getAttribute("href");
- $content_type = $this->get_content_type($source_stream);
+ $matches = [];
- if (strpos($content_type, "video/") !== false) {
- $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
- $found = 1;
- }
- }
+ /* 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);
+ continue;
+ }
- if (!$found && preg_match("/https?:\/\/v\.redd\.it\/(.*)$/i", $entry_href, $matches)) {
+ Debug::log("BODY: processing URL: " . $entry_href, Debug::$LOG_VERBOSE);
- Debug::log("Handling as reddit inline video", 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);
- $img = $img_entries->item(0);
+ $oembed_result = UrlHelper::fetch("https://publish.twitter.com/oembed?url=" . urlencode($entry_href));
- if ($img) {
- $poster_url = $img->getAttribute("src");
- } else {
- $poster_url = false;
- }
+ if ($oembed_result) {
+ $oembed_result = json_decode($oembed_result, true);
- // Get original article URL from v.redd.it redirects
- $source_article_url = $this->get_location($matches[0]);
- Debug::log("Resolved ".$matches[0]." to ".$source_article_url, Debug::$LOG_VERBOSE);
-
- $source_stream = false;
-
- if ($source_article_url) {
- $j = json_decode(UrlHelper::fetch($source_article_url.".json"), true);
-
- if ($j) {
- foreach ($j as $listing) {
- foreach ($listing["data"]["children"] as $child) {
- if ($child["data"]["url"] == $matches[0]) {
- try {
- $source_stream = $child["data"]["media"]["reddit_video"]["fallback_url"];
- }
- catch (Exception $e) {
- }
- break 2;
- }
- }
- }
- }
- }
+ if ($oembed_result && isset($oembed_result["html"])) {
- if (!$source_stream) {
- $source_stream = "https://v.redd.it/" . $matches[1] . "/DASH_600_K";
- }
+ $tmp = new DOMDocument();
+ if (@$tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
+ $p = $doc->createElement("p");
- $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
- $found = 1;
- }
+ $p->appendChild($doc->importNode(
+ $tmp->getElementsByTagName("blockquote")->item(0), TRUE));
- if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry_href)) {
+ $br = $doc->createElement('br');
+ $entry->parentNode->insertBefore($p, $entry);
+ $entry->parentNode->insertBefore($br, $entry);
- Debug::log("Handling as Streamable", Debug::$LOG_VERBOSE);
+ $found = 1;
+ }
+ }
+ }
+ }
- $tmp = UrlHelper::fetch($entry_href);
+ if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry_href, $matches)) {
+ $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
+ }
- if ($tmp) {
- $tmpdoc = new DOMDocument();
+ if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry_href, $matches)) {
- if (@$tmpdoc->loadHTML($tmp)) {
- $tmpxpath = new DOMXPath($tmpdoc);
+ Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE);
- $source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0);
- $poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0);
+ $source_stream = 'https://giant.gfycat.com/' . $matches[2] . '.mp4';
+ $poster_url = 'https://thumbs.gfycat.com/' . $matches[2] . '-mobile.jpg';
- if ($source_node && $poster_node) {
- $source_stream = $source_node->getAttribute("src");
- $poster_url = $poster_node->getAttribute("poster");
+ $content_type = $this->get_content_type($source_stream);
- $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
- $found = 1;
- }
- }
- }
+ if (strpos($content_type, "video/") !== false) {
+ $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
+ $found = 1;
}
+ }
- // imgur .gif -> .gifv
- if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry_href)) {
- Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE);
+ // imgur .gif -> .gifv
+ if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry_href)) {
+ Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE);
- $entry->setAttribute("href",
- str_replace(".gif", ".gifv", $entry_href));
- }
+ $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);
+ if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry_href)) {
+ Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE);
- $source_stream = str_replace(".gifv", ".mp4", $entry_href);
+ $source_stream = str_replace(".gifv", ".mp4", $entry_href);
- if (strpos($source_stream, "imgur.com") !== false)
- $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
+ if (strpos($source_stream, "imgur.com") !== false)
+ $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
- $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
+ $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
- $found = true;
- }
+ $found = true;
+ }
- $matches = array();
- if (!$found && (preg_match("/youtube\.com\/v\/([\w-]+)/", $entry_href, $matches) ||
- preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry_href, $matches) ||
- preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry_href, $matches) ||
- preg_match("/\/\/youtu.be\/([\w-]+)/", $entry_href, $matches))) {
+ $matches = array();
+ if (!$found && (preg_match("/youtube\.com\/v\/([\w-]+)/", $entry_href, $matches) ||
+ preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry_href, $matches) ||
+ preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry_href, $matches) ||
+ preg_match("/\/\/youtu.be\/([\w-]+)/", $entry_href, $matches))) {
- $vid_id = $matches[1];
+ $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");
- $iframe->setAttribute("type", "text/html");
- $iframe->setAttribute("width", "640");
- $iframe->setAttribute("height", "385");
- $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
- $iframe->setAttribute("allowfullscreen", "1");
- $iframe->setAttribute("frameborder", "0");
+ $iframe = $doc->createElement("iframe");
+ $iframe->setAttribute("class", "youtube-player");
+ $iframe->setAttribute("type", "text/html");
+ $iframe->setAttribute("width", "640");
+ $iframe->setAttribute("height", "385");
+ $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
+ $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);
- $found = true;
- }
+ $found = true;
+ }
- if (!$found && (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?[$\?]/i", $entry_href) ||
- mb_strpos($entry_href, "i.reddituploads.com") !== false ||
- mb_strpos($this->get_content_type($entry_href), "image/") !== false)) {
+ if (!$found && (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?[$\?]/i", $entry_href) ||
+ /* 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);
+ $img = $doc->createElement('img');
+ $img->setAttribute("src", $entry_href);
- $br = $doc->createElement('br');
- $entry->parentNode->insertBefore($img, $entry);
- $entry->parentNode->insertBefore($br, $entry);
+ $br = $doc->createElement('br');
+ $entry->parentNode->insertBefore($img, $entry);
+ $entry->parentNode->insertBefore($br, $entry);
- $found = true;
- }
+ $found = true;
+ }
- // imgur via link rel="image_src" href="..."
- if (!$found && preg_match("/imgur/", $entry_href)) {
+ // 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/*"]);
+ $content = UrlHelper::fetch(["url" => $entry_href,
+ "http_accept" => "text/*"]);
- if ($content) {
- $cdoc = new DOMDocument();
+ if ($content) {
+ $cdoc = new DOMDocument();
- if (@$cdoc->loadHTML($content)) {
- $cxpath = new DOMXPath($cdoc);
+ if (@$cdoc->loadHTML($content)) {
+ $cxpath = new DOMXPath($cdoc);
- $rel_image = $cxpath->query("//link[@rel='image_src']")->item(0);
+ $rel_image = $cxpath->query("//link[@rel='image_src']")->item(0);
- if ($rel_image) {
+ if ($rel_image) {
- $img = $doc->createElement('img');
- $img->setAttribute("src", $rel_image->getAttribute("href"));
+ $img = $doc->createElement('img');
+ $img->setAttribute("src", $rel_image->getAttribute("href"));
- $br = $doc->createElement('br');
- $entry->parentNode->insertBefore($img, $entry);
- $entry->parentNode->insertBefore($br, $entry);
+ $br = $doc->createElement('br');
+ $entry->parentNode->insertBefore($img, $entry);
+ $entry->parentNode->insertBefore($br, $entry);
- $found = true;
- }
+ $found = true;
}
}
}
+ }
- // wtf is this even
- if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry_href, $matches)) {
- $img_id = $matches[1];
-
- Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE);
+ // wtf is this even
+ if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry_href, $matches)) {
+ $img_id = $matches[1];
- $img = $doc->createElement('img');
- $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
+ Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE);
- $br = $doc->createElement('br');
- $entry->parentNode->insertBefore($img, $entry);
- $entry->parentNode->insertBefore($br, $entry);
+ $img = $doc->createElement('img');
+ $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
- $found = true;
- }
+ $br = $doc->createElement('br');
+ $entry->parentNode->insertBefore($img, $entry);
+ $entry->parentNode->insertBefore($br, $entry);
- // let's try meta properties
- if (!$found) {
- Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE);
+ $found = true;
+ }
- $content = UrlHelper::fetch(["url" => $entry_href,
- "http_accept" => "text/*"]);
+ // let's try meta properties
+ if (!$found) {
+ Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE);
- if ($content) {
- $cdoc = new DOMDocument();
+ $content = UrlHelper::fetch(["url" => $entry_href,
+ "http_accept" => "text/*"]);
- if (@$cdoc->loadHTML($content)) {
- $cxpath = new DOMXPath($cdoc);
+ if ($content) {
+ $cdoc = new DOMDocument();
- $og_image = $cxpath->query("//meta[@property='og:image']")->item(0);
- $og_video = $cxpath->query("//meta[@property='og:video']")->item(0);
+ if (@$cdoc->loadHTML($content)) {
+ $cxpath = new DOMXPath($cdoc);
- if ($og_video) {
+ $og_image = $cxpath->query("//meta[@property='og:image']")->item(0);
+ $og_video = $cxpath->query("//meta[@property='og:video']")->item(0);
- $source_stream = $og_video->getAttribute("content");
+ if ($og_video) {
- if ($source_stream) {
+ $source_stream = $og_video->getAttribute("content");
- if ($og_image) {
- $poster_url = $og_image->getAttribute("content");
- } else {
- $poster_url = false;
- }
+ if ($source_stream) {
- $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
- $found = true;
+ if ($og_image) {
+ $poster_url = $og_image->getAttribute("content");
+ } else {
+ $poster_url = false;
}
- } else if ($og_image) {
+ $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
+ $found = true;
+ }
- $og_src = $og_image->getAttribute("content");
+ } else if ($og_image) {
- if ($og_src) {
- $img = $doc->createElement('img');
- $img->setAttribute("src", $og_src);
+ $og_src = $og_image->getAttribute("content");
- $br = $doc->createElement('br');
- $entry->parentNode->insertBefore($img, $entry);
- $entry->parentNode->insertBefore($br, $entry);
+ if ($og_src) {
+ $img = $doc->createElement('img');
+ $img->setAttribute("src", $og_src);
- $found = true;
- }
+ $br = $doc->createElement('br');
+ $entry->parentNode->insertBefore($img, $entry);
+ $entry->parentNode->insertBefore($br, $entry);
+
+ $found = true;
}
}
}
}
-
}
- // remove tiny thumbnails
- if ($entry->hasAttribute("src")) {
- if ($entry->parentNode && $entry->parentNode->parentNode) {
- $entry->parentNode->parentNode->removeChild($entry->parentNode);
- }
- }
+ if ($found)
+ $this->remove_post_thumbnail($doc, $xpath);
}
return $found;
@@ -510,6 +531,23 @@ class Af_RedditImgur extends Plugin {
return 2;
}
+ private function remove_post_thumbnail($doc, $xpath) {
+ $thumb = $xpath->query("//td/a/img[@src]")->item(0);
+
+ if ($thumb)
+ $thumb->parentNode->parentNode->removeChild($thumb->parentNode);
+ }
+
+ private function handle_as_image($doc, $entry, $image_url) {
+ $img = $doc->createElement("img");
+ $img->setAttribute("src", $image_url);
+
+ $p = $doc->createElement("p");
+ $p->appendChild($img);
+
+ $entry->parentNode->insertBefore($p, $entry);
+ }
+
private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
Debug::log("handle_as_video: $source_stream", Debug::$LOG_VERBOSE);
@@ -538,32 +576,34 @@ 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() {
header("Content-type: text/plain");
Debug::set_enabled(true);
- Debug::set_loglevel(Debug::$LOG_VERBOSE);
+ Debug::set_loglevel(Debug::$LOG_EXTENDED);
- $url = htmlspecialchars($_REQUEST["url"]);
+ $url = clean($_REQUEST["url"]);
+ $article_url = clean($_REQUEST["article_url"]);
Debug::log("URL: $url", Debug::$LOG_VERBOSE);
$doc = new DOMDocument();
- @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>");
+ @$doc->loadHTML("<html><body><table><tr><td><a href=\"$url\">[link]</a></td></tr></table></body>");
$xpath = new DOMXPath($doc);
- $found = $this->inline_stuff([], $doc, $xpath);
+ $found = $this->inline_stuff(["link" => $article_url], $doc, $xpath);
- Debug::log("Inline result: $found");
+ Debug::log("Inline result: $found", Debug::$LOG_VERBOSE);
if (!$found) {
- Debug::log("Readability result:");
+ Debug::log("Readability result:", Debug::$LOG_VERBOSE);
$article = $this->readability([], $url, $doc, $xpath);
print_r($article);
} else {
- Debug::log("Resulting HTML:");
+ Debug::log("Resulting HTML:", Debug::$LOG_VERBOSE);
print $doc->saveHTML();
}