]> git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
add af_zz_imgproxy (initial)
[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 (no caching)",
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 vidproxy() {
32 $url = $_REQUEST["url"];
33
34 if (preg_match("/\.(mp4|webm|gifv)/", $url, $matches)) {
35 $type = $matches[1];
36 $embed_url = $url;
37
38 if ($type == "gifv") {
39 $type = "mp4";
40 $embed_url = str_replace(".gifv", ".mp4", $embed_url);
41 }
42
43 header("Content-type: text/html");
44
45 $embed_url = htmlspecialchars("backend.php?op=pluginhandler&plugin=af_zz_imgproxy&method=imgproxy&url=" .
46 urlencode($embed_url));
47
48 print "<video class=\"\" autoplay=\"true\" controls=\"true\" loop=\"true\">";
49 print "<source src=\"$embed_url\" type=\"video/$type\">";
50 print "</video>";
51 } else {
52 header("Location: " . htmlspecialchars($url));
53 }
54 }*/
55
56 public function imgproxy() {
57 $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
58
59 if (function_exists("getimagesize")) {
60 $is = @getimagesize($url);
61 header("Content-type: " . $is["mime"]);
62 }
63
64 print fetch_file_contents(array("url" => $url));
65 }
66
67 function rewrite_url_if_needed($url) {
68 $scheme = parse_url($url, PHP_URL_SCHEME);
69
70 if ($scheme != 'https' && $scheme != "") {
71 $url = "backend.php?op=pluginhandler&plugin=af_zz_imgproxy&method=imgproxy&url=" .
72 htmlspecialchars($url);
73
74 }
75
76 return $url;
77 }
78
79 function hook_render_article_cdm($article, $api_mode = false) {
80
81 $need_saving = false;
82
83 $doc = new DOMDocument();
84 if (@$doc->loadHTML($article["content"])) {
85 $xpath = new DOMXPath($doc);
86 $imgs = $xpath->query("//img[@src]");
87
88 foreach ($imgs as $img) {
89 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"));
90
91 if ($new_src != $img->getAttribute("src")) {
92 $img->setAttribute("src", $new_src);
93
94 $need_saving = true;
95 }
96 }
97
98 $vids = $xpath->query("//video");
99
100 foreach ($vids as $vid) {
101 if ($vid->hasAttribute("poster")) {
102 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"));
103
104 if ($new_src != $vid->getAttribute("poster")) {
105 $vid->setAttribute("poster", $new_src);
106
107 $need_saving = true;
108 }
109 }
110
111 $vsrcs = $xpath->query("source", $vid);
112
113 foreach ($vsrcs as $vsrc) {
114 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"));
115
116 if ($new_src != $vsrc->getAttribute("src")) {
117 $vid->setAttribute("src", $new_src);
118
119 $need_saving = true;
120 }
121 }
122 }
123 }
124
125 if ($need_saving) $article["content"] = $doc->saveXML();
126
127 return $article;
128 }
129
130 function api_version() {
131 return 2;
132 }
133 }