I've spent my weekend programming a little April Fool's joke, but it doesn't work just the way I want it to yet.
I have a Drupal 6 based website on which I want to change as little as possible. The idea is that all images that are served from the /files directory are redirected to an external webserver (myserver) that flips the image upside down and then serves it to the browser.
To make the Drupal website (targetserver) redirect all requests for images to the other server, I set up a .htaccess as follows:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} !^aprilFool$
RewriteRule ^(.*)$ http://myserver/aprilFool/?url=http://targetserver/files/$1 [R=302,L]
And so far it works great! When I enable everything, the April Fool's trickery changes some of the images and they get displayed in the client's browser.
But when I disable the .htaccess @targetserver, my browser refuses to realize it was only meant to be a temporary joke and forget the edited images :(
Here is a sniplet of the Perl script on myserver/aprilFool:
my $ua = LWP::UserAgent->new;
# Identify ourselves through the useragent, to prevent endless redirect loops
$ua->agent( 'aprilFool' );
# Load remote file
my $response = $ua->get( $url );
if ( $response->is_error ) { die "Cannot retrieve document $url\n"; }
my $imageData = $response->content;
# Determine the file's mime type, need that to determine if we want to change it or not
my $ft = File::Type->new();
my $format = $ft->mime_type( $imageData );
# If the file is an image, flip it
if ( $format =~ m/^image\// ) {
my $image=Image::Magick->new;
$image->BlobToImage( $imageData );
$image->Flip();
$imageData = $image->ImageToBlob();
}
# Send HTTP headers
print "Content-type:$format\r\n";
print "\r\n";
print $imageData;
I've tried the following without success:
But after disabling the .htaccess the targetserver keeps sending 304 => 'Not Modified' until I manually flush the browser cache.
So my question is: How can I make April 1st last only one day, preferably until midnight ... How can I make the browser realize it has to reload the original image once the joke is over?
This is a funny question :-)
Just disable the redirect when it isn't april first. 302's are not cached by the browser.
RewriteCond %{TIME_DAY} ^1$
RewriteCond %{TIME_MON} ^3$
RewriteCond %{HTTP_USER_AGENT} !^aprilFool$
RewriteRule ^(.*)$ http://myserver/aprilFool/?url=http://targetserver/files/$1 [R=302,L]
You'll have to check if the values 1 and 3 are correct to match april first, but I think TIME_MON is 0-11 and TIME_DAY is 1-31
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With