init.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. class Af_Zz_ImgProxy extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Load insecure images via built-in proxy",
  7. "fox");
  8. }
  9. function is_public_method($method) {
  10. return $method === "imgproxy";
  11. }
  12. function init($host) {
  13. $this->host = $host;
  14. $host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
  15. $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
  16. $host->add_hook($host::HOOK_RENDER_ARTICLE_API, $this);
  17. $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
  18. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  19. }
  20. function hook_enclosure_entry($enc) {
  21. $proxy_all = $this->host->get($this, "proxy_all");
  22. $enc["url"] = $this->rewrite_url_if_needed($enc["url"], $proxy_all);
  23. return $enc;
  24. }
  25. function hook_render_article($article) {
  26. return $this->hook_render_article_cdm($article);
  27. }
  28. function hook_render_article_api($headline) {
  29. return $this->hook_render_article_cdm($headline["headline"], true);
  30. }
  31. public function imgproxy() {
  32. $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
  33. $kind = (int) $_REQUEST["kind"]; // 1 = video
  34. // called without user context, let's just redirect to original URL
  35. if (!$_SESSION["uid"]) {
  36. header("Location: $url");
  37. return;
  38. }
  39. $extension = $kind == 1 ? '.mp4' : '.png';
  40. $local_filename = CACHE_DIR . "/images/" . sha1($url) . $extension;
  41. if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
  42. header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
  43. if (file_exists($local_filename)) {
  44. $mimetype = mime_content_type($local_filename);
  45. header("Content-type: $mimetype");
  46. $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
  47. header("Last-Modified: $stamp", true);
  48. readfile($local_filename);
  49. } else {
  50. $data = fetch_file_contents(array("url" => $url));
  51. if ($data) {
  52. if (file_put_contents($local_filename, $data)) {
  53. $mimetype = mime_content_type($local_filename);
  54. header("Content-type: $mimetype");
  55. }
  56. print $data;
  57. }
  58. }
  59. }
  60. function rewrite_url_if_needed($url, $kind, $all_remote = false) {
  61. $scheme = parse_url($url, PHP_URL_SCHEME);
  62. if ($all_remote) {
  63. $host = parse_url($url, PHP_URL_HOST);
  64. $self_host = parse_url(SELF_URL_PATH, PHP_URL_HOST);
  65. $is_remote = $host != $self_host;
  66. } else {
  67. $is_remote = false;
  68. }
  69. if (($scheme != 'https' && $scheme != "") || $is_remote) {
  70. if (strpos($url, "data:") !== 0) {
  71. $url = "public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&kind=$kind&url=" .
  72. urlencode($url);
  73. }
  74. }
  75. return $url;
  76. }
  77. function hook_render_article_cdm($article, $api_mode = false) {
  78. $need_saving = false;
  79. $proxy_all = $this->host->get($this, "proxy_all");
  80. $doc = new DOMDocument();
  81. if (@$doc->loadHTML($article["content"])) {
  82. $xpath = new DOMXPath($doc);
  83. $imgs = $xpath->query("//img[@src]");
  84. foreach ($imgs as $img) {
  85. $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), 0, $proxy_all);
  86. if ($new_src != $img->getAttribute("src")) {
  87. $img->setAttribute("src", $new_src);
  88. $need_saving = true;
  89. }
  90. }
  91. $vids = $xpath->query("//video");
  92. foreach ($vids as $vid) {
  93. if ($vid->hasAttribute("poster")) {
  94. $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), 0, $proxy_all);
  95. if ($new_src != $vid->getAttribute("poster")) {
  96. $vid->setAttribute("poster", $new_src);
  97. $need_saving = true;
  98. }
  99. }
  100. $vsrcs = $xpath->query("source", $vid);
  101. foreach ($vsrcs as $vsrc) {
  102. $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), 1, $proxy_all);
  103. if ($new_src != $vsrc->getAttribute("src")) {
  104. $vid->setAttribute("src", $new_src);
  105. $need_saving = true;
  106. }
  107. }
  108. }
  109. }
  110. if ($need_saving) $article["content"] = $doc->saveXML();
  111. return $article;
  112. }
  113. function hook_prefs_tab($args) {
  114. if ($args != "prefFeeds") return;
  115. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
  116. print "<form dojoType=\"dijit.form.Form\">";
  117. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  118. evt.preventDefault();
  119. if (this.validate()) {
  120. console.log(dojo.objectToQuery(this.getValues()));
  121. new Ajax.Request('backend.php', {
  122. parameters: dojo.objectToQuery(this.getValues()),
  123. onComplete: function(transport) {
  124. notify_info(transport.responseText);
  125. }
  126. });
  127. //this.reset();
  128. }
  129. </script>";
  130. print_hidden("op", "pluginhandler");
  131. print_hidden("method", "save");
  132. print_hidden("plugin", "af_zz_imgproxy");
  133. $proxy_all = $this->host->get($this, "proxy_all");
  134. print_checkbox("proxy_all", $proxy_all);
  135. print "&nbsp;<label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
  136. print "<p>"; print_button("submit", __("Save"));
  137. print "</form>";
  138. print "</div>";
  139. }
  140. function save() {
  141. $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
  142. $this->host->set($this, "proxy_all", $proxy_all);
  143. echo __("Configuration saved");
  144. }
  145. function api_version() {
  146. return 2;
  147. }
  148. }