From 0bf3e81dafb3880bbe5c012b31de138b8da12632 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 29 Mar 2012 12:18:50 +0400 Subject: add patch by gmargo to improve favicon detection by content --- include/functions.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/include/functions.php b/include/functions.php index c19915bb7..6bc3ca4f9 100644 --- a/include/functions.php +++ b/include/functions.php @@ -374,7 +374,33 @@ $favicon_url = get_favicon_url($site_url); if ($favicon_url) { - $contents = fetch_file_contents($favicon_url, "image"); + // Limiting to "image" type misses those served with text/plain + $contents = fetch_file_contents($favicon_url); // , "image"); + + if ($contents) { + // Crude image type matching. + // Patterns gleaned from the file(1) source code. + if (preg_match('/^\x00\x00\x01\x00/', $contents)) { + // 0 string \000\000\001\000 MS Windows icon resource + //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource"); + } + elseif (preg_match('/^GIF8/', $contents)) { + // 0 string GIF8 GIF image data + //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image"); + } + elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) { + // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data + //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image"); + } + elseif (preg_match('/^\xff\xd8/', $contents)) { + // 0 beshort 0xffd8 JPEG image data + //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image"); + } + else { + //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type"); + $contents = ""; + } + } if ($contents) { $fp = @fopen($icon_file, "w"); -- cgit v1.2.3