]> git.wh0rd.org - tt-rss.git/blob - plugins/af_tumblr_1280/init.php
update phpmd ruleset to use (subset) of cleancode
[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 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 if ($found) {
68 $doc->removeChild($doc->firstChild); //remove doctype
69 $article["content"] = $doc->saveHTML();
70 }
71 }
72
73 return $article;
74
75 }
76
77
78 function api_version() {
79 return 2;
80 }
81
82 }