{"id":25,"date":"2006-01-29T23:46:31","date_gmt":"2006-01-30T07:46:31","guid":{"rendered":"http:\/\/simplepie.org\/blog\/2006\/01\/29\/bypass-hotlink-blocking-when-displaying-a-feed\/"},"modified":"2006-06-26T19:36:56","modified_gmt":"2006-06-27T03:36:56","slug":"bypass-hotlink-blocking-when-displaying-a-feed","status":"publish","type":"post","link":"https:\/\/simplepie.org\/blog\/2006\/01\/29\/bypass-hotlink-blocking-when-displaying-a-feed\/","title":{"rendered":"Bypass &#8220;hotlink blocking&#8221; when displaying a feed (Beta 1)"},"content":{"rendered":"<p class=\"subscribe\"><b>Requirements:<\/b> SimplePie 1.0 Beta 1<\/p>\n<div class=\"chunk noborder\">\n<em>This tutorial has been superceded by <a href=\"\/blog\/2006\/06\/03\/bypass-hotlink-blocking-when-displaying-a-feed-beta-2\/\">a newer version<\/a>.<\/em><\/p>\n<p>In my quest for making a feed parser that is intelligent, simple, and graceful, one thing that has always bugged me about online feed readers is that some people disable the ability to hotlink images.<\/p>\n<p>Now, I understand why they do this, because I do it too.  You don&#8217;t want a bunch of your bandwidth sucked up by people who are stealing your images for their own nefarious purposes (mwah-hah-hah!).  Rather, you&#8217;d prefer to keep the images for <em>your readers who are reading your content<\/em>.  Well, that&#8217;s exactly what a feed parser is for, right?<\/p>\n<p>Desktop aggregators like <a href=\"http:\/\/www.feeddemon.com\">Feed Demon<\/a> and <a href=\"http:\/\/ranchero.com\/netnewswire\/\">NetNewsWire<\/a> are always able to just load up the images in context with the post that they&#8217;re reading, and it all makes sense.  The only reason why online feed readers have a problem is because the browsers that run them respect the hotlinking rules&mdash;even if the reasons don&#8217;t make sense for the context (like trying to apply laws about CD&#8217;s to MP3&#8217;s&mdash;although they&#8217;re related, they&#8217;re different, and the rules need to be modified for the new medium).<\/p>\n<p>So, we&#8217;ve decided to solve the problem in our latest release.  Here&#8217;s how to bypass hotlink protection for feeds that you&#8217;re trying to read online.  We&#8217;ve added a new function called <code>display_image()<\/code> that will take the URL of the image and display it&mdash;blocked or not.<\/p>\n<p>Here&#8217;s how it&#8217;s done.  First we want to take our page (I&#8217;ll be using the demo page that is included with the SimplePie download), and make a small modification to the head of the page.  We&#8217;re going to take the source code, and add the bolded part below:<\/p>\n<pre>&lt;?php\r\n\/\/ Start counting time for loading...\r\n$starttime = explode(' ', microtime());\r\n$starttime = $starttime[1] + $starttime[0];\r\n\r\ninclude('simplepie.inc');\r\n\r\n\/\/ Parse it\r\n$feed = new SimplePie();\r\nif (!empty($_GET['feed'])) {\r\n\t$feed-&gt;feed_url($_GET['feed']);\r\n\t$feed->init();\r\n\tif (!headers_sent() && $feed-&gt;get_encoding()) {\r\n\t\theader('Content-type: text\/html; charset=' . $feed->get_encoding());\r\n\t}\r\n}\r\n<b>else if (!empty($_GET['i'])) {\r\n\t$feed->display_image($_GET['i']);\r\n}<\/b>\r\n?&gt;&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\r\n\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" xml:lang=\"en-US\" lang=\"en-US\"&gt;\r\n&lt;head&gt;\r\n&lt;title>SimplePie: Demo<\/title&gt;\r\n<\/pre>\n<p>What did we just do?  We told the page that:<\/p>\n<ol>\n<li>If we're not passing the <code>feed<\/code> parameter to the page (like <code>index.php?feed=url_goes_here<\/code>), then check for the <code>i<\/code> parameter.  (I chose <code>i<\/code> because it's short for \"image\".  You can choose anything you want.)<\/li>\n<li>If the <code>i<\/code> parameter exists, pass it to the <code>display_image()<\/code> function.<\/li>\n<\/ol>\n<p>The <code>display_image()<\/code> function is an exception to standard SimplePie processing in that it doesn't return any value.  It automatically <code>echo<\/code>'s the image content to the page.  So, if load up our page like <code>index.php?i=url_of_image<\/code>, we should see the image&mdash; and <i>only<\/i> the image&mdash;on the page.<\/p>\n<p>Now, we just need to modify the value of all the <code>&lt;img src=\"\" \/&gt;<\/code> tags on the page, so that they all get passed through our new script.  Specifically, we're looking for all off-site images, not our own local ones, so let's make sure to target those.<\/p>\n<p>The simplest way is with JavaScript, although you're free to use other languages if you prefer.  I wrote something that looks like this (I assumed an XHTML page, and I'm using an anonymous function):<\/p>\n<pre>\r\n&lt;script type=\"text\/javascript\"&gt;\r\n\/\/&lt;![CDATA[\r\n\r\n(function(){\r\n\r\n\t\/\/ Get an array of all of the &lt;img&gt; tags in the page\r\n\tvar img = document.getElementsByTagName('img');\r\n\r\n\t\/\/ Count how many there are, and store that number for faster processing\r\n\tvar imgLength = img.length;\r\n\r\n\t\/\/ Loop through all of the images.  Unfortuately, JavaScript doesn't have a foreach() function.\r\n\tfor (var x=0; x&lt;imgLength; x++) {\r\n\r\n\t\t\/\/ Check to make sure that the image we have is off-site\r\n\t\tif (img[x].src.substring(0,4) == 'http') {\r\n\r\n\t\t\t\/\/ Pass the URL as the value for the 'i' parameter.\r\n\t\t\timg[x].src = '?i=' + img[x].src;\r\n\t\t}\r\n\t}\r\n})();\r\n\r\n\/\/]]&gt;\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>If you place this script at the very bottom of your page, right above the closing <code>&lt;\/body&gt;<\/code> tag, the script will re-write your external image URL's to have them pass through our function, and they'll all show up on your feed reader page, just like they do in the desktop aggregators.<\/p>\n<p>Have fun!  This code has been implemented in the demo that comes with the <a href=\"\/downloads\/\">SimplePie 1.0 Beta download<\/a>.\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Requirements: SimplePie 1.0 Beta 1 This tutorial has been superceded by a newer version. In my quest for making a feed parser that is intelligent, simple, and graceful, one thing that has always bugged me about online feed readers is that some people disable the ability to hotlink images. Now, I understand why they do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-tips-and-tricks"],"_links":{"self":[{"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":0,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}