[Patch] Improvements to 'af_zz_imgproxy' plugin

Development-related discussion, including bundled plugins
ErinMorelli
Bear Rating Trainee
Bear Rating Trainee
Posts: 1
Joined: 24 Mar 2017, 02:02

[Patch] Improvements to 'af_zz_imgproxy' plugin

Postby ErinMorelli » 24 Mar 2017, 02:21

Hello! My proposed patch is some improvements to the 'af_zz_imgproxy' plugin.

I noticed that images that were loaded within the `format_article_enclosures`function with the plugin enabled were not properly being proxied. To address that I've added support to the plugin for the `HOOK_RENDER_ENCLOSURE` hook to rewrite the image URLs as-needed. Additionally, I've adjusted the regex in `hook_enclosure_entry` hook function as it was not catching any image URLs that had CGI params, e.g. photo.jpg?size=small

Here is the diff:

Code: Select all

diff --git a/plugins/af_zz_imgproxy/init.php b/plugins/af_zz_imgproxy/init.php
index f326b8e..af32419 100644
--- a/plugins/af_zz_imgproxy/init.php
+++ b/plugins/af_zz_imgproxy/init.php
@@ -18,20 +18,47 @@ class Af_Zz_ImgProxy extends Plugin {
       $host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
       $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
       $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
-
+      $host->add_hook($host::HOOK_RENDER_ENCLOSURE, $this);
       $host->add_hook($host::HOOK_PREFS_TAB, $this);
    }
 
    function hook_enclosure_entry($enc) {
-      if (preg_match("/image/", $enc["content_type"]) || preg_match("/\.(jpe?g|png|gif|bmp)$/i", $enc["filename"])) {
+      if (preg_match("/image/", $enc["content_type"]) || preg_match("/\.(jpe?g|png|gif|bmp)/i", $enc["filename"])) {
          $proxy_all = $this->host->get($this, "proxy_all");
-
          $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], 0, $proxy_all);
       }
-
       return $enc;
    }
 
+   function hook_render_enclosure($entry) {
+      if (preg_match("/image/", $entry["type"]) || preg_match("/\.(jpe?g|png|gif|bmp)/i", $entry["filename"])) {
+         $proxy_all = $this->host->get($this, "proxy_all");
+         $entry["url"] = $this->rewrite_url_if_needed($entry["url"], 0, $proxy_all);
+
+         if (!$hide_images) {
+            $encsize = '';
+            if ($entry['height'] > 0)
+               $encsize .= ' height="' . intval($entry['height']) . '"';
+            if ($entry['width'] > 0)
+               $encsize .= ' width="' . intval($entry['width']) . '"';
+            $rv = "<p><img
+               alt=\"".htmlspecialchars($entry["filename"])."\"
+               src=\"" .htmlspecialchars($entry["url"]) . "\"
+               " . $encsize . " /></p>";
+         } else {
+            $rv = "<p><a target=\"_blank\" rel=\"noopener noreferrer\"
+               href=\"".htmlspecialchars($entry["url"])."\"
+               >" .htmlspecialchars($entry["url"]) . "</a></p>";
+         }
+
+         if ($entry['title']) {
+            $rv.= "<div class=\"enclosure_title\">${entry['title']}</div>";
+         }
+
+         return $rv;
+      }
+   }
+
    function hook_render_article($article) {
       return $this->hook_render_article_cdm($article);
    }
@@ -230,4 +257,5 @@ class Af_Zz_ImgProxy extends Plugin {
    function api_version() {
       return 2;
    }
-}
\ No newline at end of file
+}
+


If this is good, I'd love to get developer privileges to make the merge request - my gitlab username is the same as here: ErinMorelli

Thanks!
/Erin

User avatar
fox
^ me reading your posts ^
Posts: 6318
Joined: 27 Aug 2005, 22:53
Location: Saint-Petersburg, Russia
Contact:

Re: [Patch] Improvements to 'af_zz_imgproxy' plugin

Postby fox » 24 Mar 2017, 07:44

well,

1. you're patching against outdated code
2. we're not matching against urls at all now, it goes by content type. the idea here being it's not 2010 anymore and people should know better.
3. instead of copy-pasting rendering code into plugin, it's better to figure out why doing this the correct way didn't work for you. otherwise you're not really improving anything, you're just making it worse.

feed url?


Return to “Development”

Who is online

Users browsing this forum: No registered users and 1 guest