]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_tumblr_1280/init.php
add af_tumblr_1280
[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",
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
21                 $owner_uid = $article["owner_uid"];
22
23                 $charset_hack = '<head>
24                         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
25                 </head>';
26
27                 $doc = new DOMDocument();
28                 $doc->loadHTML($charset_hack . $article["content"]);
29
30                 $found = false;
31
32                 if ($doc) {
33                         $xpath = new DOMXpath($doc);
34
35                         $images = $xpath->query('(//img[contains(@src, \'media.tumblr.com\')])');
36
37                         foreach ($images as $img) {
38                                 $src = $img->getAttribute("src");
39
40                                 $test_src = preg_replace("/_\d{3}.(jpg|gif|png)/", "_1280.$1", $src);
41
42                                 if ($src != $test_src) {
43
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);
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 ?>