]> git.wh0rd.org - tt-rss.git/blame - plugins/af_tumblr_1280/init.php
drop support for (obsolete, removed from recent php versions) php safe_mode setting
[tt-rss.git] / plugins / af_tumblr_1280 / init.php
CommitLineData
84c6f17d
AD
1<?php
2class Af_Tumblr_1280 extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
4c467026 7 "Replace Tumblr pictures with largest size if available (requires CURL)",
84c6f17d
AD
8 "fox");
9 }
10
11 function init($host) {
12 $this->host = $host;
13
14 if (function_exists("curl_init")) {
15 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
16 }
17 }
18
19 function hook_article_filter($article) {
20
4c467026
AD
21 if (!function_exists("curl_init") || ini_get("open_basedir"))
22 return $article;
84c6f17d
AD
23
24 $charset_hack = '<head>
25 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
26 </head>';
27
28 $doc = new DOMDocument();
29 $doc->loadHTML($charset_hack . $article["content"]);
30
31 $found = false;
32
33 if ($doc) {
34 $xpath = new DOMXpath($doc);
35
36 $images = $xpath->query('(//img[contains(@src, \'media.tumblr.com\')])');
37
38 foreach ($images as $img) {
39 $src = $img->getAttribute("src");
40
41 $test_src = preg_replace("/_\d{3}.(jpg|gif|png)/", "_1280.$1", $src);
42
43 if ($src != $test_src) {
44
45 $ch = curl_init($test_src);
46 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
47 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
48 curl_setopt($ch, CURLOPT_HEADER, true);
49 curl_setopt($ch, CURLOPT_NOBODY, true);
4c467026 50 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
84c6f17d
AD
51 curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
52
53 @$result = curl_exec($ch);
54 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
55
56 if ($result && $http_code == 200) {
57 $img->setAttribute("src", $test_src);
58 $found = true;
59 }
60 }
61 }
62
63 if ($found) {
64 $doc->removeChild($doc->firstChild); //remove doctype
65 $article["content"] = $doc->saveHTML();
66 }
67 }
68
69 return $article;
70
71 }
72
73
74 function api_version() {
75 return 2;
76 }
77
78}
79?>