init.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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(get_self_url_prefix(), $_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. send_local_file($local_filename);
  42. } else {
  43. $data = fetch_file_contents(array("url" => $url));
  44. if ($data) {
  45. $disable_cache = $this->host->get($this, "disable_cache");
  46. if (!$disable_cache && strlen($data) > MIN_CACHE_FILE_SIZE) {
  47. if (file_put_contents($local_filename, $data)) {
  48. $mimetype = mime_content_type($local_filename);
  49. header("Content-type: $mimetype");
  50. }
  51. }
  52. print $data;
  53. } else {
  54. global $fetch_last_error;
  55. global $fetch_last_error_code;
  56. global $fetch_last_error_content;
  57. if (function_exists("imagecreate") && !isset($_REQUEST["text"])) {
  58. $img = imagecreate(450, 75);
  59. /*$bg =*/ imagecolorallocate($img, 255, 255, 255);
  60. $textcolor = imagecolorallocate($img, 255, 0, 0);
  61. imagerectangle($img, 0, 0, 450-1, 75-1, $textcolor);
  62. imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor);
  63. imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor);
  64. imagestring($img, 5, 5, 55, "HTTP Code: $fetch_last_error_code", $textcolor);
  65. header("Content-type: image/png");
  66. print imagepng($img);
  67. imagedestroy($img);
  68. } else {
  69. header("Content-type: text/html");
  70. http_response_code(400);
  71. print "<h1>Proxy request failed.</h1>";
  72. print "<p>Fetch error $fetch_last_error ($fetch_last_error_code)</p>";
  73. print "<p>URL: $url</p>";
  74. print "<textarea cols='80' rows='25'>" . htmlspecialchars($fetch_last_error_content) . "</textarea>";
  75. }
  76. }
  77. }
  78. }
  79. function rewrite_url_if_needed($url, $all_remote = false) {
  80. $scheme = parse_url($url, PHP_URL_SCHEME);
  81. if ($all_remote) {
  82. $host = parse_url($url, PHP_URL_HOST);
  83. $self_host = parse_url(get_self_url_prefix(), PHP_URL_HOST);
  84. $is_remote = $host != $self_host;
  85. } else {
  86. $is_remote = false;
  87. }
  88. if (($scheme != 'https' && $scheme != "") || $is_remote) {
  89. if (strpos($url, "data:") !== 0) {
  90. $parts = parse_url($url);
  91. foreach (explode(" " , $this->ssl_known_whitelist) as $host) {
  92. if (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) {
  93. $parts['scheme'] = 'https';
  94. $url = build_url($parts);
  95. if ($all_remote && $is_remote) {
  96. break;
  97. } else {
  98. return $url;
  99. }
  100. }
  101. }
  102. return get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
  103. urlencode($url);
  104. }
  105. }
  106. return $url;
  107. }
  108. /**
  109. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  110. */
  111. function hook_render_article_cdm($article, $api_mode = false) {
  112. $need_saving = false;
  113. $proxy_all = $this->host->get($this, "proxy_all");
  114. $doc = new DOMDocument();
  115. if (@$doc->loadHTML($article["content"])) {
  116. $xpath = new DOMXPath($doc);
  117. $imgs = $xpath->query("//img[@src]");
  118. foreach ($imgs as $img) {
  119. $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all);
  120. if ($new_src != $img->getAttribute("src")) {
  121. $img->setAttribute("src", $new_src);
  122. $img->removeAttribute("srcset");
  123. $need_saving = true;
  124. }
  125. }
  126. $vids = $xpath->query("//video");
  127. foreach ($vids as $vid) {
  128. if ($vid->hasAttribute("poster")) {
  129. $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all);
  130. if ($new_src != $vid->getAttribute("poster")) {
  131. $vid->setAttribute("poster", $new_src);
  132. $need_saving = true;
  133. }
  134. }
  135. $vsrcs = $xpath->query("source", $vid);
  136. foreach ($vsrcs as $vsrc) {
  137. $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all);
  138. if ($new_src != $vsrc->getAttribute("src")) {
  139. $vid->setAttribute("src", $new_src);
  140. $need_saving = true;
  141. }
  142. }
  143. }
  144. }
  145. if ($need_saving) $article["content"] = $doc->saveHTML();
  146. return $article;
  147. }
  148. function hook_prefs_tab($args) {
  149. if ($args != "prefFeeds") return;
  150. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
  151. print "<form dojoType=\"dijit.form.Form\">";
  152. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  153. evt.preventDefault();
  154. if (this.validate()) {
  155. console.log(dojo.objectToQuery(this.getValues()));
  156. new Ajax.Request('backend.php', {
  157. parameters: dojo.objectToQuery(this.getValues()),
  158. onComplete: function(transport) {
  159. notify_info(transport.responseText);
  160. }
  161. });
  162. //this.reset();
  163. }
  164. </script>";
  165. print_hidden("op", "pluginhandler");
  166. print_hidden("method", "save");
  167. print_hidden("plugin", "af_zz_imgproxy");
  168. $proxy_all = $this->host->get($this, "proxy_all");
  169. print_checkbox("proxy_all", $proxy_all);
  170. print "&nbsp;<label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label><br/>";
  171. $disable_cache = $this->host->get($this, "disable_cache");
  172. print_checkbox("disable_cache", $disable_cache);
  173. print "&nbsp;<label for=\"disable_cache\">" . __("Don't cache files locally.") . "</label>";
  174. print "<p>"; print_button("submit", __("Save"));
  175. print "</form>";
  176. print "</div>";
  177. }
  178. function save() {
  179. $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]) == "true";
  180. $disable_cache = checkbox_to_sql_bool($_POST["disable_cache"]) == "true";
  181. $this->host->set($this, "proxy_all", $proxy_all, false);
  182. $this->host->set($this, "disable_cache", $disable_cache);
  183. echo __("Configuration saved");
  184. }
  185. function api_version() {
  186. return 2;
  187. }
  188. }