init.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. class Af_Zz_ImgProxy extends Plugin {
  3. /* @var PluginHost $host */
  4. private $host;
  5. function about() {
  6. return array(1.0,
  7. "Load insecure images via built-in proxy",
  8. "fox");
  9. }
  10. private $ssl_known_whitelist = "imgur.com gfycat.com i.reddituploads.com pbs.twimg.com i.redd.it i.sli.mg media.tumblr.com";
  11. function is_public_method($method) {
  12. return $method === "imgproxy";
  13. }
  14. function init($host) {
  15. $this->host = $host;
  16. $host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
  17. $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
  18. $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
  19. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  20. }
  21. function hook_enclosure_entry($enc) {
  22. if (preg_match("/image/", $enc["content_type"])) {
  23. $proxy_all = $this->host->get($this, "proxy_all");
  24. $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $proxy_all);
  25. }
  26. return $enc;
  27. }
  28. function hook_render_article($article) {
  29. return $this->hook_render_article_cdm($article);
  30. }
  31. public function imgproxy() {
  32. $url = rewrite_relative_url(get_self_url_prefix(), $_REQUEST["url"]);
  33. // called without user context, let's just redirect to original URL
  34. if (!$_SESSION["uid"]) {
  35. header("Location: $url");
  36. return;
  37. }
  38. $local_filename = CACHE_DIR . "/images/" . sha1($url);
  39. if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
  40. header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
  41. if (file_exists($local_filename)) {
  42. send_local_file($local_filename);
  43. } else {
  44. $data = fetch_file_contents(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]);
  45. if ($data) {
  46. $disable_cache = $this->host->get($this, "disable_cache");
  47. if (!$disable_cache && strlen($data) > MIN_CACHE_FILE_SIZE) {
  48. if (file_put_contents($local_filename, $data)) {
  49. $mimetype = mime_content_type($local_filename);
  50. header("Content-type: $mimetype");
  51. }
  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(get_self_url_prefix(), 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 (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) {
  94. $parts['scheme'] = 'https';
  95. $url = build_url($parts);
  96. if ($all_remote && $is_remote) {
  97. break;
  98. } else {
  99. return $url;
  100. }
  101. }
  102. }
  103. return get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
  104. urlencode($url);
  105. }
  106. }
  107. return $url;
  108. }
  109. /**
  110. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  111. */
  112. function hook_render_article_cdm($article, $api_mode = false) {
  113. $need_saving = false;
  114. $proxy_all = $this->host->get($this, "proxy_all");
  115. $doc = new DOMDocument();
  116. if (@$doc->loadHTML($article["content"])) {
  117. $xpath = new DOMXPath($doc);
  118. $imgs = $xpath->query("//img[@src]");
  119. foreach ($imgs as $img) {
  120. $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $proxy_all);
  121. if ($new_src != $img->getAttribute("src")) {
  122. $img->setAttribute("src", $new_src);
  123. $img->removeAttribute("srcset");
  124. $need_saving = true;
  125. }
  126. }
  127. $vids = $xpath->query("//video");
  128. foreach ($vids as $vid) {
  129. if ($vid->hasAttribute("poster")) {
  130. $new_src = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $proxy_all);
  131. if ($new_src != $vid->getAttribute("poster")) {
  132. $vid->setAttribute("poster", $new_src);
  133. $need_saving = true;
  134. }
  135. }
  136. $vsrcs = $xpath->query("source", $vid);
  137. foreach ($vsrcs as $vsrc) {
  138. $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $proxy_all);
  139. if ($new_src != $vsrc->getAttribute("src")) {
  140. $vid->setAttribute("src", $new_src);
  141. $need_saving = true;
  142. }
  143. }
  144. }
  145. }
  146. if ($need_saving) $article["content"] = $doc->saveHTML();
  147. return $article;
  148. }
  149. function hook_prefs_tab($args) {
  150. if ($args != "prefFeeds") return;
  151. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Image proxy settings (af_zz_imgproxy)')."\">";
  152. print "<form dojoType=\"dijit.form.Form\">";
  153. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  154. evt.preventDefault();
  155. if (this.validate()) {
  156. console.log(dojo.objectToQuery(this.getValues()));
  157. new Ajax.Request('backend.php', {
  158. parameters: dojo.objectToQuery(this.getValues()),
  159. onComplete: function(transport) {
  160. notify_info(transport.responseText);
  161. }
  162. });
  163. //this.reset();
  164. }
  165. </script>";
  166. print_hidden("op", "pluginhandler");
  167. print_hidden("method", "save");
  168. print_hidden("plugin", "af_zz_imgproxy");
  169. $proxy_all = $this->host->get($this, "proxy_all");
  170. print_checkbox("proxy_all", $proxy_all);
  171. print "&nbsp;<label for=\"proxy_all\">" . __("Enable proxy for all remote images.") . "</label><br/>";
  172. $disable_cache = $this->host->get($this, "disable_cache");
  173. print_checkbox("disable_cache", $disable_cache);
  174. print "&nbsp;<label for=\"disable_cache\">" . __("Don't cache files locally.") . "</label>";
  175. print "<p>"; print_button("submit", __("Save"));
  176. print "</form>";
  177. print "</div>";
  178. }
  179. function save() {
  180. $proxy_all = checkbox_to_sql_bool($_POST["proxy_all"]);
  181. $disable_cache = checkbox_to_sql_bool($_POST["disable_cache"]);
  182. $this->host->set($this, "proxy_all", $proxy_all, false);
  183. $this->host->set($this, "disable_cache", $disable_cache);
  184. echo __("Configuration saved");
  185. }
  186. function api_version() {
  187. return 2;
  188. }
  189. }