summaryrefslogtreecommitdiff
path: root/plugins/af_tumblr_1280/init.php
diff options
context:
space:
mode:
authorArt4 <[email protected]>2015-02-19 23:17:43 +0100
committerArt4 <[email protected]>2015-02-19 23:17:43 +0100
commitd9c042c4c536f1afbf206aeea09b0b127b7b6418 (patch)
tree9d7ef18c6d9d4c8f8f4d1c9a5dbfceeb9fd5acf4 /plugins/af_tumblr_1280/init.php
parentcf42b79120290ee5866136a0c4656e6999f06045 (diff)
parent4ca621a36016de1fbb5447e1c1de0b607ba94a7c (diff)
Merge branch 'master' into patch-1
Conflicts: locale/de_DE/LC_MESSAGES/messages.po
Diffstat (limited to 'plugins/af_tumblr_1280/init.php')
-rw-r--r--plugins/af_tumblr_1280/init.php79
1 files changed, 79 insertions, 0 deletions
diff --git a/plugins/af_tumblr_1280/init.php b/plugins/af_tumblr_1280/init.php
new file mode 100644
index 000000000..f9938048b
--- /dev/null
+++ b/plugins/af_tumblr_1280/init.php
@@ -0,0 +1,79 @@
+<?php
+class Af_Tumblr_1280 extends Plugin {
+ private $host;
+
+ function about() {
+ return array(1.0,
+ "Replace Tumblr pictures with largest size if available",
+ "fox");
+ }
+
+ function init($host) {
+ $this->host = $host;
+
+ if (function_exists("curl_init")) {
+ $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
+ }
+ }
+
+ function hook_article_filter($article) {
+
+ $owner_uid = $article["owner_uid"];
+
+ $charset_hack = '<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+ </head>';
+
+ $doc = new DOMDocument();
+ $doc->loadHTML($charset_hack . $article["content"]);
+
+ $found = false;
+
+ if ($doc) {
+ $xpath = new DOMXpath($doc);
+
+ $images = $xpath->query('(//img[contains(@src, \'media.tumblr.com\')])');
+
+ foreach ($images as $img) {
+ $src = $img->getAttribute("src");
+
+ $test_src = preg_replace("/_\d{3}.(jpg|gif|png)/", "_1280.$1", $src);
+
+ if ($src != $test_src) {
+
+ $ch = curl_init($test_src);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 5);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_HEADER, true);
+ curl_setopt($ch, CURLOPT_NOBODY, true);
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION,
+ !ini_get("safe_mode") && !ini_get("open_basedir"));
+ curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
+
+ @$result = curl_exec($ch);
+ $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+
+ if ($result && $http_code == 200) {
+ $img->setAttribute("src", $test_src);
+ $found = true;
+ }
+ }
+ }
+
+ if ($found) {
+ $doc->removeChild($doc->firstChild); //remove doctype
+ $article["content"] = $doc->saveHTML();
+ }
+ }
+
+ return $article;
+
+ }
+
+
+ function api_version() {
+ return 2;
+ }
+
+}
+?>