init.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. private $ssl_known_whitelist = "imgur.com gfycat.com i.reddituploads.com pbs.twimg.com i.redd.it i.sli.mg media.tumblr.com";
  10. function is_public_method($method) {
  11. return $method === "imgproxy";
  12. }
  13. function init($host) {
  14. $this->host = $host;
  15. $host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
  16. $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $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. if (preg_match("/image/", $enc["content_type"])) {
  22. $proxy_all = $this->host->get($this, "proxy_all");
  23. $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
  24. }
  25. return $enc;
  26. }
  27. function hook_render_article($article) {
  28. return $this->hook_render_article_cdm($article);
  29. }
  30. public function imgproxy() {
  31. $url = rewrite_relative_url(SELF_URL_PATH, $_REQUEST["url"]);
  32. // called without user context, let's just redirect to original URL
  33. if (!$_SESSION["uid"]) {
  34. header("Location: $url");
  35. return;
  36. }
  37. $local_filename = CACHE_DIR . "/images/" . sha1($url);
  38. if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
  39. header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
  40. if (file_exists($local_filename)) {
  41. $mimetype = mime_content_type($local_filename);
  42. header("Content-type: $mimetype");
  43. $stamp = gmdate("D, d M Y H:i:s", filemtime($local_filename)). " GMT";
  44. header("Last-Modified: $stamp", true);
  45. readfile($local_filename);
  46. } else {
  47. $data = fetch_file_contents(array("url" => $url));
  48. if ($data) {
  49. if (file_put_contents($local_filename, $data)) {
  50. $mimetype = mime_content_type($local_filename);
  51. header("Content-type: $mimetype");
  52. }
  53. print $data;
  54. } else {
  55. global $fetch_last_error;
  56. global $fetch_last_error_code;
  57. global $fetch_last_error_content;
  58. if (function_exists("imagecreate") && !isset($_REQUEST["text"])) {
  59. $img = imagecreate(450, 75);
  60. $bg = imagecolorallocate($img, 255, 255, 255);
  61. $textcolor = imagecolorallocate($img, 255, 0, 0);
  62. imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor);
  63. imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor);
  64. imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor);
  65. imagestring($img, 5, 5, 55, "HTTP Code: $fetch_last_error_code", $textcolor);
  66. header("Content-type: image/png");
  67. print imagepng($img);
  68. imagedestroy($img);
  69. } else {
  70. header("Content-type: text/html");
  71. http_response_code(400);
  72. print "<h1>Proxy request failed.</h1>";
  73. print "<p>Fetch error $fetch_last_error ($fetch_last_error_code)</p>";
  74. print "<p>URL: $url</p>";
  75. print "<textarea cols='80' rows='25'>" . htmlspecialchars($fetch_last_error_content) . "</textarea>";
  76. }
  77. }
  78. }
  79. }
  80. function rewrite_url_if_needed($url, $all_remote = false) {
  81. $scheme = parse_url($url, PHP_URL_SCHEME);
  82. if ($all_remote) {
  83. $host = parse_url($url, PHP_URL_HOST);
  84. $self_host = parse_url(SELF_URL_PATH, PHP_URL_HOST);
  85. $is_remote = $host != $self_host;
  86. } else {
  87. $is_remote = false;
  88. }
  89. if (($scheme != 'https' && $scheme != "") || $is_remote) {
  90. if (strpos($url, "data:") !== 0) {
  91. $parts = parse_url($url);
  92. foreach (explode(" " , $this->ssl_known_whitelist) as $host) {
  93. if (strpos($parts['host'], $host) !== FALSE) {
  94. $parts['scheme'] = 'https';
  95. return build_url($parts);
  96. }
  97. }
  98. return get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
  99. urlencode($url);
  100. }
  101. }
  102. return $url;
  103. }
  104. function hook_render_article_cdm($article, $api_mode = false) {
  105. $need_saving = false;
  106. $proxy_all = $this->host->get($this, "proxy_all");
  107. $doc = new DOMDocument();
  108. if (@$doc->loadHTML($article["content"])) {
  109. $xpath = new DOMXPath($doc);
  110. $imgs = $xpath->query("//img[@src]");
  111. foreach ($imgs as $img) {
  112. $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all);
  113. if ($new_src != $img->getAttribute("src")) {
  114. $img->setAttribute("src", $new_src);
  115. $img->removeAttribute("srcset");
  116. $need_saving = true;
  117. }
  118. }
  119. $vids = $xpath->query("//video");
  120. foreach ($vids as $vid) {
  121. if ($vid->hasAttribute("poster")) {
  122. $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all);
  123. if ($new_src != $vid->getAttribute("poster")) {
  124. $vid->setAttribute("poster", $new_src);
  125. $need_saving = true;
  126. }
  127. }
  128. $vsrcs = $xpath->query("source", $vid);
  129. foreach ($vsrcs as $vsrc) {
  130. $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all);
  131. if ($new_src != $vsrc->getAttribute("src")) {
  132. $vid->setAttribute("src", $new_src);
  133. $need_saving = true;
  134. }
  135. }
  136. }
  137. }
  138. if ($need_saving) $article["content"] = $doc->saveXML();
  139. return $article;
  140. }
  141. function hook_prefs_tab($args) {
  142. if ($args != "prefFeeds") return;
  143. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
  144. print "<form dojoType=\"dijit.form.Form\">";
  145. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  146. evt.preventDefault();
  147. if (this.validate()) {
  148. console.log(dojo.objectToQuery(this.getValues()));
  149. new Ajax.Request('backend.php', {
  150. parameters: dojo.objectToQuery(this.getValues()),
  151. onComplete: function(transport) {
  152. notify_info(transport.responseText);
  153. }
  154. });
  155. //this.reset();
  156. }
  157. </script>";
  158. print_hidden("op", "pluginhandler");
  159. print_hidden("method", "save");
  160. print_hidden("plugin", "af_zz_imgproxy");
  161. $proxy_all = $this->host->get($this, "proxy_all");
  162. print_checkbox("proxy_all", $proxy_all);
  163. print "&nbsp;<label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label>";
  164. print "<p>"; print_button("submit", __("Save"));
  165. print "</form>";
  166. print "</div>";
  167. }
  168. function save() {
  169. $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
  170. $this->host->set($this, "proxy_all", $proxy_all);
  171. echo __("Configuration saved");
  172. }
  173. function api_version() {
  174. return 2;
  175. }
  176. }