]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgproxy/init.php
df8f34dbe2c8c27bf4df343671bc1b75202e24ed
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: attachment; filename=\"".basename($local_filename)."\"");
42 if (file_exists($local_filename)) {
43 readfile($local_filename);
45 $data = fetch_file_contents(array("url" => $url));
47 file_put_contents($local_filename, $data);
53 function rewrite_url_if_needed($url, $kind = 0) {
54 $scheme = parse_url($url, PHP_URL_SCHEME
);
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);
64 function hook_render_article_cdm($article, $api_mode = false) {
68 $doc = new DOMDocument();
69 if (@$doc->loadHTML($article["content"])) {
70 $xpath = new DOMXPath($doc);
71 $imgs = $xpath->query("//img[@src]");
73 foreach ($imgs as $img) {
74 $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"));
76 if ($new_src != $img->getAttribute("src")) {
77 $img->setAttribute("src", $new_src);
83 $vids = $xpath->query("//video");
85 foreach ($vids as $vid) {
86 if ($vid->hasAttribute("poster")) {
87 $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"));
89 if ($new_src != $vid->getAttribute("poster")) {
90 $vid->setAttribute("poster", $new_src);
96 $vsrcs = $xpath->query("source", $vid);
98 foreach ($vsrcs as $vsrc) {
99 $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1);
101 if ($new_src != $vsrc->getAttribute("src")) {
102 $vid->setAttribute("src", $new_src);
110 if ($need_saving) $article["content"] = $doc->saveXML();
115 function api_version() {