]>
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",
11 function init($host) {
14 if (function_exists("curl_init")) {
15 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
19 function hook_article_filter($article) {
21 $owner_uid = $article["owner_uid"];
23 $charset_hack = '<head>
24 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
27 $doc = new DOMDocument();
28 $doc->loadHTML($charset_hack . $article["content"]);
33 $xpath = new DOMXpath($doc);
35 $images = $xpath->query('(//img[contains(@src, \'media.tumblr.com\')])');
37 foreach ($images as $img) {
38 $src = $img->getAttribute("src");
40 $test_src = preg_replace("/_\d{3}.(jpg|gif|png)/", "_1280.$1", $src);
42 if ($src != $test_src) {
44 $ch = curl_init($test_src);
45 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
46 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
47 curl_setopt($ch, CURLOPT_HEADER
, true);
48 curl_setopt($ch, CURLOPT_NOBODY
, true);
49 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
,
50 !ini_get("safe_mode") && !ini_get("open_basedir"));
51 curl_setopt($ch, CURLOPT_USERAGENT
, SELF_USER_AGENT
);
53 @$result = curl_exec($ch);
54 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE
);
56 if ($result && $http_code == 200) {
57 $img->setAttribute("src", $test_src);
64 $doc->removeChild($doc->firstChild
); //remove doctype
65 $article["content"] = $doc->saveHTML();
74 function api_version() {