]> git.wh0rd.org - tt-rss.git/blob - plugins/af_tumblr_1280/init.php
minor css fixes (mostly for zoom mode)
[tt-rss.git] / plugins / af_tumblr_1280 / init.php
1 <?php
2 class Af_Tumblr_1280 extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
7 "Replace Tumblr pictures and videos with largest size if available (requires CURL)",
8 "fox");
9 }
10
11 function flags() {
12 return array("needs_curl" => true);
13 }
14
15 function init($host) {
16 $this->host = $host;
17
18 if (function_exists("curl_init")) {
19 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
20 }
21 }
22
23 function hook_article_filter($article) {
24
25 if (!function_exists("curl_init") || ini_get("open_basedir"))
26 return $article;
27
28 $charset_hack = '<head>
29 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
30 </head>';
31
32 $doc = new DOMDocument();
33 $doc->loadHTML($charset_hack . $article["content"]);
34
35 $found = false;
36
37 if ($doc) {
38 $xpath = new DOMXpath($doc);
39
40 $images = $xpath->query('(//img[contains(@src, \'media.tumblr.com\')])');
41
42 foreach ($images as $img) {
43 $src = $img->getAttribute("src");
44
45 $test_src = preg_replace("/_\d{3}.(jpg|gif|png)/", "_1280.$1", $src);
46
47 if ($src != $test_src) {
48
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);
56
57 @$result = curl_exec($ch);
58 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
59
60 if ($result && $http_code == 200) {
61 $img->setAttribute("src", $test_src);
62 $found = true;
63 }
64 }
65 }
66
67 $video_sources = $xpath->query('//video/source[contains(@src, \'.tumblr.com/video_file\')]');
68
69 foreach ($video_sources as $source) {
70 $src = $source->getAttribute("src");
71
72 $new_src = preg_replace("/\/\d{3}$/", "", $src);
73
74 if ($src != $new_src) {
75 $source->setAttribute("src", $new_src);
76 $found = true;
77 }
78 }
79
80 if ($found) {
81 $doc->removeChild($doc->firstChild); //remove doctype
82 $article["content"] = $doc->saveHTML();
83 }
84 }
85
86 return $article;
87
88 }
89
90
91 function api_version() {
92 return 2;
93 }
94
95 }