]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_tumblr_1280/init.php
2 class Af_Tumblr_1280
extends Plugin
{
7 "Replace Tumblr pictures with largest size if available (requires CURL)",
12 return array("needs_curl" => true);
15 function init($host) {
18 if (function_exists("curl_init")) {
19 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
23 function hook_article_filter($article) {
25 if (!function_exists("curl_init") ||
ini_get("open_basedir"))
28 $charset_hack = '<head>
29 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
32 $doc = new DOMDocument();
33 $doc->loadHTML($charset_hack . $article["content"]);
38 $xpath = new DOMXpath($doc);
40 $images = $xpath->query('(//img[contains(@src, \'media.tumblr.com\')])');
42 foreach ($images as $img) {
43 $src = $img->getAttribute("src");
45 $test_src = preg_replace("/_\d{3}.(jpg|gif|png)/", "_1280.$1", $src);
47 if ($src != $test_src) {
49 $ch = curl_init($test_src);
50 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
51 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
52 curl_setopt($ch, CURLOPT_HEADER
, true);
53 curl_setopt($ch, CURLOPT_NOBODY
, true);
54 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
55 curl_setopt($ch, CURLOPT_USERAGENT
, SELF_USER_AGENT
);
57 @$result = curl_exec($ch);
58 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE
);
60 if ($result && $http_code == 200) {
61 $img->setAttribute("src", $test_src);
68 $doc->removeChild($doc->firstChild
); //remove doctype
69 $article["content"] = $doc->saveHTML();
78 function api_version() {