Skip to content

Instantly share code, notes, and snippets.

@themattharris
Created July 4, 2009 10:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themattharris/140526 to your computer and use it in GitHub Desktop.
Save themattharris/140526 to your computer and use it in GitHub Desktop.
Provides a local caching system for Flickr images to ensure they are viewable when clients have Flickr blocked.
<?php
/*
Flickr Caching Script for WordPress
Description: Provides a local caching system for Flickr images to ensure they
are viewable when clients have Flickr blocked.
Version: 1.0
Author: Matt Harris
Author URI: http://themattharris.com
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Copyright (c) 2009 Matt Harris
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
Usage
1. Place the file in your theme directory, e.g. wp-content/themes/mytheme/flickr.php
2. If necessary change the require path
3. On the page where you want flickr images put
<?php $encoded = base64_encode( 'http://path/to/flickrimage.jpg'); ?>
<img src="<?php echo get_bloginfo('template_directory') . '/flickr.php?hash=' . $encoded ?>">
4. To keep things healthy create a couple of cron jobs to:
a. Clean out images which haven't been accessed for 5 weeks.
http://yourdomain.com/wp-content/themes/mytheme/flickr.php?cleanup
*/
// if your wordpress content folder is not in the same location as wp-load you
// will need to alter this.
require( dirname(
dirname(
dirname(
dirname(__FILE__) // your theme directory
) // themes
) // wp-content
) // site root
. '/wp-load.php' );
$path = WP_CONTENT_DIR . '/uploads/flickr/';
function validImage( $url, &$head=FALSE ) {
# Flickr returns a 200 OK even if the image doesn't exist.
# Instead we look for the content-disposition header as it is only
# included when an actual image is found.
$head = wp_remote_head( $url );
return isset( $head['headers']['content-disposition'] );
}
# retrieve the image from Flickr. Note we only do this if the url is on Flickr.
function getImage( $url, $filename ) {
$pattern = "/https?:\/\/farm\d+.static.flickr.com\/.*/i";
if ( preg_match( $pattern, $url ) > 0 ) {
$handle = fopen( $filename, 'w');
fwrite( $handle, wp_remote_fopen( $url ) );
fclose( $handle );
} else {
print $url . " disallowed.";
die();
}
}
if ( isset( $_GET['refresh'] ) ) {
$files = scandir( $path );
foreach ($files as $file) {
if ( is_file( $path . $file ) ) {
$url = base64_decode( $file );
if ( validImage( $url, $head ) )
{
# The last_modified date of the file is the datestamp set on Flickr of
# when the file was created.
# is the last_modified more recent than when we last checked the file?
$last_mod = strtotime( $head['headers']['last-modified'] );
$file_mod = filemtime( $path . $file );
if ( $file_mod < $last_mod ) {
getImage( $url, $path . $file );
}
}
}
}
}
if ( isset( $_GET['cleanup'] ) ) {
$files = scandir( $path );
foreach ($files as $file) {
$expires = time() - ( 60 * 60 * 24 * 7 * 5 ); // 5 weeks ago
if ( is_file( $path . $file ) and ( filemtime( $path . $file ) < $expires) ) {
# delete the file
@unlink( $path . $file );
}
}
}
if ( isset( $_GET['hash'] ) ) {
# expects the url in base64_encoded format
$hash = strip_tags( stripslashes( $_GET['hash'] ) );
$image = $path . $hash;
$url = base64_decode( $hash );
# we always do a stat on the image
if ( validImage( $url, $head ) )
{
# check if we have an up-to-date copy of the image
if ( file_exists($image) ) {
# The last_modified date of the file is the datestamp set on Flickr of
# when the file was created.
# is the last_modified more recent than when we last checked the file?
$last_mod = strtotime( $head['headers']['last-modified'] );
$file_mod = filemtime( $path . $file );
# if the file is out of date, remove it - it will be recached
if ( $file_mod < $last_mod ) {
@unlink( $path . $file );
} else {
# update the marker (note this only works if the user running the script
# can write to the file
@touch( $path . $file );
}
}
if ( ! file_exists($image) ) {
getImage( $url, $image );
}
header("Content-Disposition: filename=\"$hash\"");
header("Content-Length: " . filesize($image));
header("Content-Type: $ContentType");
readfile($image);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment