]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
2 class Af_Zz_ImgProxy
extends Plugin
{
7 "Load insecure images via built-in proxy",
12 return array("needs_curl" => true);
15 function init($host) {
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);
23 function hook_render_article($article) {
24 return $this->hook_render_article_cdm($article);
27 function hook_render_article_api($headline) {
28 return $this->hook_render_article_cdm($headline["headline"], true);
31 public function imgproxy() {
32 $url = rewrite_relative_url(SELF_URL_PATH
, $_REQUEST["url"]);
33 $kind = (int) $_REQUEST["kind"]; // 1 = video
35 $extension = $kind == 1 ?
'.mp4' : '.png';
36 $local_filename = CACHE_DIR
. "/images/" . sha1($url) . $extension;
38 //if ($_REQUEST["debug"] == "1") { print $local_filename; die; }
40 header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
42 if (file_exists($local_filename)) {
43 $mimetype = mime_content_type($local_filename);
44 header("Content-type: $mimetype");
46 $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
47 header("Last-Modified: $stamp", true);
49 readfile($local_filename);
51 $data = fetch_file_contents(array("url" => $url));
53 if (file_put_contents($local_filename, $data)) {
54 $mimetype = mime_content_type($local_filename);
55 header("Content-type: $mimetype");
63 function rewrite_url_if_needed($url, $kind = 0) {
64 $scheme = parse_url($url, PHP_URL_SCHEME
);
66 if ($scheme != 'https' && $scheme != "" && strpos($url, "data:") !== 0) {
67 $url = "backend.php?op=pluginhandler&plugin=af_zz_imgproxy&method=imgproxy&kind=$kind&url=" .
68 htmlspecialchars($url);
74 function hook_render_article_cdm($article, $api_mode = false) {
78 $doc = new DOMDocument();
79 if (@$doc->loadHTML($article["content"])) {
80 $xpath = new DOMXPath($doc);
81 $imgs = $xpath->query("//img[@src]");
83 foreach ($imgs as $img) {
84 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"));
86 if ($new_src != $img->getAttribute("src")) {
87 $img->setAttribute("src", $new_src);
93 $vids = $xpath->query("//video");
95 foreach ($vids as $vid) {
96 if ($vid->hasAttribute("poster")) {
97 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"));
99 if ($new_src != $vid->getAttribute("poster")) {
100 $vid->setAttribute("poster", $new_src);
106 $vsrcs = $xpath->query("source", $vid);
108 foreach ($vsrcs as $vsrc) {
109 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1);
111 if ($new_src != $vsrc->getAttribute("src")) {
112 $vid->setAttribute("src", $new_src);
120 if ($need_saving) $article["content"] = $doc->saveXML();
125 function api_version() {