]> git.wh0rd.org - tt-rss.git/blame - plugins/af_zz_imgproxy/init.php
af_zz_imgproxy: use inline disposition, misc updates
[tt-rss.git] / plugins / af_zz_imgproxy / init.php
CommitLineData
c4ebf01e
AD
1<?php
2class Af_Zz_ImgProxy extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
c93d43c6 7 "Load insecure images via built-in proxy",
c4ebf01e
AD
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 $host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
19 $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
20 $host->add_hook($host::HOOK_RENDER_ARTICLE_API, $this);
21 }
22
23 function hook_render_article($article) {
24 return $this->hook_render_article_cdm($article);
25 }
26
27 function hook_render_article_api($headline) {
28 return $this->hook_render_article_cdm($headline["headline"], true);
29 }
30
c93d43c6
AD
31 public function imgproxy() {
32 $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
33 $kind = (int) $_REQUEST["kind"]; // 1 = video
c4ebf01e 34
c93d43c6
AD
35 $extension = $kind == 1 ? '.mp4' : '.png';
36 $local_filename = CACHE_DIR . "/images/" . sha1($url) . $extension;
c4ebf01e 37
38b3998b 38 //if ($_REQUEST["debug"] == "1") { print $local_filename; die; }
c4ebf01e 39
38b3998b 40 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
c4ebf01e 41
c93d43c6 42 if (file_exists($local_filename)) {
38b3998b
AD
43 $mimetype = mime_content_type($local_filename);
44 header("Content-type: $mimetype");
45
46 $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
47 header("Last-Modified: $stamp", true);
48
c93d43c6 49 readfile($local_filename);
c4ebf01e 50 } else {
c93d43c6
AD
51 $data = fetch_file_contents(array("url" => $url));
52 if ($data) {
38b3998b
AD
53 if (file_put_contents($local_filename, $data)) {
54 $mimetype = mime_content_type($local_filename);
55 header("Content-type: $mimetype");
56 }
57
c93d43c6
AD
58 print $data;
59 }
c4ebf01e 60 }
c4ebf01e
AD
61 }
62
c93d43c6 63 function rewrite_url_if_needed($url, $kind = 0) {
c4ebf01e
AD
64 $scheme = parse_url($url, PHP_URL_SCHEME);
65
c93d43c6
AD
66 if ($scheme != 'https' && $scheme != "" && strpos($url, "data:") !== 0) {
67 $url = "backend.php?op=pluginhandler&plugin=af_zz_imgproxy&method=imgproxy&kind=$kind&url=" .
c4ebf01e 68 htmlspecialchars($url);
c4ebf01e
AD
69 }
70
71 return $url;
72 }
73
74 function hook_render_article_cdm($article, $api_mode = false) {
75
76 $need_saving = false;
77
78 $doc = new DOMDocument();
79 if (@$doc->loadHTML($article["content"])) {
80 $xpath = new DOMXPath($doc);
81 $imgs = $xpath->query("//img[@src]");
82
83 foreach ($imgs as $img) {
84 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"));
85
86 if ($new_src != $img->getAttribute("src")) {
87 $img->setAttribute("src", $new_src);
88
89 $need_saving = true;
90 }
91 }
92
93 $vids = $xpath->query("//video");
94
95 foreach ($vids as $vid) {
96 if ($vid->hasAttribute("poster")) {
97 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"));
98
99 if ($new_src != $vid->getAttribute("poster")) {
100 $vid->setAttribute("poster", $new_src);
101
102 $need_saving = true;
103 }
104 }
105
106 $vsrcs = $xpath->query("source", $vid);
107
108 foreach ($vsrcs as $vsrc) {
c93d43c6 109 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1);
c4ebf01e
AD
110
111 if ($new_src != $vsrc->getAttribute("src")) {
112 $vid->setAttribute("src", $new_src);
113
114 $need_saving = true;
115 }
116 }
117 }
118 }
119
120 if ($need_saving) $article["content"] = $doc->saveXML();
121
122 return $article;
123 }
124
125 function api_version() {
126 return 2;
127 }
128}