]> git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
df8f34dbe2c8c27bf4df343671bc1b75202e24ed
[tt-rss.git] / plugins / af_zz_imgproxy / init.php
1 <?php
2 class Af_Zz_ImgProxy extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
7 "Load insecure images via built-in proxy",
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
31 public function imgproxy() {
32 $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
33 $kind = (int) $_REQUEST["kind"]; // 1 = video
34
35 $extension = $kind == 1 ? '.mp4' : '.png';
36 $local_filename = CACHE_DIR . "/images/" . sha1($url) . $extension;
37
38 if ($_REQUEST["debug"] == "1") { print $local_filename; die; }
39
40 header("Content-Disposition: attachment; filename=\"".basename($local_filename)."\"");
41
42 if (file_exists($local_filename)) {
43 readfile($local_filename);
44 } else {
45 $data = fetch_file_contents(array("url" => $url));
46 if ($data) {
47 file_put_contents($local_filename, $data);
48 print $data;
49 }
50 }
51 }
52
53 function rewrite_url_if_needed($url, $kind = 0) {
54 $scheme = parse_url($url, PHP_URL_SCHEME);
55
56 if ($scheme != 'https' && $scheme != "" && strpos($url, "data:") !== 0) {
57 $url = "backend.php?op=pluginhandler&plugin=af_zz_imgproxy&method=imgproxy&kind=$kind&url=" .
58 htmlspecialchars($url);
59 }
60
61 return $url;
62 }
63
64 function hook_render_article_cdm($article, $api_mode = false) {
65
66 $need_saving = false;
67
68 $doc = new DOMDocument();
69 if (@$doc->loadHTML($article["content"])) {
70 $xpath = new DOMXPath($doc);
71 $imgs = $xpath->query("//img[@src]");
72
73 foreach ($imgs as $img) {
74 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"));
75
76 if ($new_src != $img->getAttribute("src")) {
77 $img->setAttribute("src", $new_src);
78
79 $need_saving = true;
80 }
81 }
82
83 $vids = $xpath->query("//video");
84
85 foreach ($vids as $vid) {
86 if ($vid->hasAttribute("poster")) {
87 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"));
88
89 if ($new_src != $vid->getAttribute("poster")) {
90 $vid->setAttribute("poster", $new_src);
91
92 $need_saving = true;
93 }
94 }
95
96 $vsrcs = $xpath->query("source", $vid);
97
98 foreach ($vsrcs as $vsrc) {
99 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1);
100
101 if ($new_src != $vsrc->getAttribute("src")) {
102 $vid->setAttribute("src", $new_src);
103
104 $need_saving = true;
105 }
106 }
107 }
108 }
109
110 if ($need_saving) $article["content"] = $doc->saveXML();
111
112 return $article;
113 }
114
115 function api_version() {
116 return 2;
117 }
118 }