1
0
Files
www.erictaylor.me/content/files/old/gvp/images/portfolio/flickrViewer.php

422 lines
11 KiB
PHP

<?php
/*********************************************************************
FlickrViewer - an Flickr enhancement for SimpleViewer
Original Code Copyright (C) 2006 Mark Sweeting
SimpleViewer 1.8 Updates by Dustin Senos
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
Version 1.1 - 20 May 2006
Version 1.2 - 27 May 2007
Version 1.2.1 - 10 June 2007
Version 1.2.2 - 27 June 2007
Version 1.2.3 - 20 November 2007
1.2 Updates:
- Supports SimpleViewer 1.8
- Added SimpleViewer XML Options
1.2.1 Updates:
- Added curl support for some hosts such as dreamhost see:
http://sourceforge.net/forum/forum.php?thread_id=1690731&forum_id=652458
1.2.2 Updates:
- Fixed Caching Issue
- Updated XHML and CSS
- Added SWFobject to ZIP and XHTML
1.2.3 Updates
- Fixed issue with $thumbnailColumns, thanks Corey
FlickrViewer homepage:
http://www.sweeting.org/mark/flickrviewer/
For the latest news and updates about FlickrViewer, keep an eye on:
http://www.sweeting.org/mark/blog/category/flickrviewer/
http://dustinsenos.com/flickrviewer/
Dustin Senos homepage:
http://dustinsenos.com/flickrviewer/
IMPORTANT!
Before you can use this you must fill out the configuration values
below. You must get a Flickr API key, decide which of your flickr
photo sets you want to publish, and fill out a few more bits.
If you're having problems getting FLickrViewer to work, set $debug to
true to enable more verbose error messages. You should turn this off
(set to false) when you are done.
*/
$debug = false;
/********************************************************************
* START OF CONFIGURATION SECTION
********************************************************************
* What is your API key? Enter it below in the quotes
* If you don't know your API key you can get if here:
*
* http://www.flickr.com/services/api/misc.api_keys.html
*/
$apikey = '51404603@N00';
/* What is the set ID for the photos you would like to display?
* All photos must be public and added to this set.
*
* The set id can be got from the set url. For example:
*
* Photo set URL:
* http://www.flickr.com/photos/markymoo/sets/72057594052387557/
*
* So the photo set ID is
* 72057594052387557
*/
$setid = '72157602545199414';
/* What is the URL of your main photo page?
* For example, mine is:
* http://www.flickr.com/photos/markymoo/
*
* Remember to include the trailing slash!
*/
$userurl = 'http://www.flickr.com/photos/silver-dragon/';
/* FlickrViewer needs a place where it can write a temporary file.
* This is used to cache the data downloaded from Flickr so that your
* website goes faster!
*
* The web server must be able to read/write to your chosen location.
*/
$cache = "tmp/flickr.xml";
/* Depending on your hosting provider, fopen may be disabled. If so
* you can change this to true to make FlickrViewer us curl to fetch
* the info from Flickr.
*
*/
$use_curl = false;
/* Cache age - set the maximum age of the cache file in seconds.
* This needs to reflect the frequency you update your photo set, so
* you may get away with a day or two cache age.
*
* To set the cache to 12 hours (recommended):
*
* $ttl = 43200
*
* If you don't want to use caching, or if you are unable to write
* the cache file to your web server's disc, set this to 0. E.g.:
*
* $ttl = 0;
*
* Note that this is not recommended, and you would be better off
* using FlickrViewer Lite.
*/
$ttl = 43200; // 12 hours
/* Set the title that will appear just above the thumbnails.
*/
$title = "";
/* Do you want to disable the link to the flickr photo/comments page?
* Setting this value to false will remove the link, setting it to
* true will show the link. To turn the link off:
*
* $showImageLink = false;
*
* To turn the link on (recommended):
*
* $showImageLink = true;
*/
$showImageLink = false;
/* Do you want to show the image title/caption?
* To turn the caption off:
*
* $showImageCaption = false;
*
* To display the caption (recommended):
*
* $showImageCaption = true;
*/
$showImageCaption = false;
/* SimpleViewer XML Options
*
* http://www.airtightinteractive.com/simpleviewer/xml_options.html
*
*/
$maximageHeight = '640';
$maximageWidth = '640';
$textColor = '0xFFFFFF';
$frameColor = '0xffffff';
$frameWidth = '15';
$stagePadding = '40';
$thumbnailColumns = '3';
$thumbnailRows = '4';
$navPosition = 'left';
$enableRightClickOpen = 'true';
$backgroundimagePath = '';
/* END OF CONFIGURATION SECTION
********************************************************************
* Don't edit anything below this line unless you know what
* you're doing!
*******************************************************************/
$mode = isset($_GET['mode']) ? $_GET['mode'] : 'xml';
if($mode == 'img')
{
// redirect SimpleViewer to the image on flickr
$size = isset($_GET['size']) ? $_GET['size'] : 'thumb';
$image = isset($_GET['image']) ? $_GET['image']: '0';
if($size == 'thumb')
{
$url = 'http://static.flickr.com/' . $image . '_s.jpg';
}
else
{
$url = 'http://static.flickr.com/'. $image . '.jpg';
}
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
exit;
}
/* Still here? Then we're after the XML...
*
* First, see if there is a valid cache file.
*/
if($ttl && $data = @file_get_contents($cache))
{
if($data = unserialize($data))
{
// check age of cache file
if(isset($data[0]) && is_numeric($data[0]))
{
if((time() - $data[0]) < $ttl)
{
// cached data is OK, so print and quit!
header("Content-Type: text/xml");
header("Last-Modified: " . gmdate("r", $data[0]));
header("Expires: " . gmdate("r", ($data[0] + $ttl)));
header("X-Cache-Status: Hit");
print $data[1];
exit;
}
}
}
else
{
print "Couldn't unserialize the cache file.";
exit;
}
}
/* If we're here then the cache file either
* doesn't exist, it's too old, or caching is turned off.
*/
// array to hold photo data
$photos = array();
// string to hold the flickr api response xml
$xmlin = '';
// xml startElement handler
function startElement($parser, $name, $attrs)
{
global $photos, $userurl;
if ($name == "PHOTO")
{
$photos[] = array("{$attrs['SERVER']}/{$attrs['ID']}_{$attrs['SECRET']}", $attrs['TITLE'], $userurl . $attrs['ID']);
}
elseif ($name == "ERR")
{
print "Flickr API error: Code {$attrs['CODE']}: {$attrs['MSG']}";
exit;
}
}
// xml endElement handler - not needed!
function endElement($parser, $name){ }
// construct the flickr api request
$url = "http://www.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=$apikey&photoset_id=$setid";
// Fetch the data from Flickr
function fetch_data ($request_url, $use_curl) {
// Use fopen
if(!$use_curl) {
if($handle = @fopen("$request_url", "rb")) {
while (!feof($handle)) {
$response .= fread($handle, 8192);
}
fclose($handle);
}
}
// Use curl
else {
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $request_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($ch);
curl_close($ch);
}
return $response;
}
// off we go!
if($xmlin = @fetch_data($url, $use_curl))
{
// parse the flickr response
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_parse($xml_parser, $xmlin, true);
xml_parser_free($xml_parser);
}
else
{
if($debug)
{
print "Error: Couldn't open $url. Does your web server allow outgoing HTTP requests? Try setting 'use_curl' to true in flickrViewer.php";
}
else
{
print "Error: Couldn't connect to Flickr Web Service";
}
exit;
}
// Do we have any photos?
if(!count($photos))
{
print "Error: No photos found in photo set.";
exit;
}
/* Generate the XML OUT */
$xmlout = '<?xm' . 'l version="1.0" encoding="UTF-8"?>';
$xmlout .= '<!-- Last updated: ' . date("r") . ' -->';
$xmlout .= '<simpleviewerGallery
maximageHeight="' . $maximageHeight . '"
maximageWidth="' . $maximageWidth . '"
textColor="' . $textColor . '"
frameColor="' . $frameColor . '"
frameWidth="' . $frameWidth . '"
stagePadding="' . $stagePadding . '"
thumbnailColumns="' . $thumbnailColumns . '"
thumbnailRows="' . $thumbnailRows . '"
navPosition="' . $navPosition . '"
enableRightClickOpen="' . $enableRightClickOpen . '"
backgroundimagePath="' . $backgroundimagePath . '"
title="' . $title . '"
imagePath="flickrViewer.php?mode=img&amp;size=large&amp;image="
thumbPath="flickrViewer.php?mode=img&amp;size=thumb&amp;image=">
';
// loop over the photos array and build xml
foreach($photos as $photo)
{
$xmlout .= "<image><filename>{$photo[0]}</filename><caption>";
if($showImageCaption)
{
if($showImageLink)
{
$xmlout .= "<![CDATA[<a href=\"{$photo[2]}\">{$photo[1]}<br /><u>View full size...</u></a>]]>";
}
else
{
$xmlout .= "<![CDATA[{$photo[1]}]]>";
}
}
$xmlout .= "</caption></image>\n";
}
$xmlout .= "</simpleviewerGallery>";
// get the time
$now = time();
// should we cache the results?
if($ttl)
{
// cache the results so we don't need to keep generating it.
$sData = serialize(array($now, $xmlout));
if (!$handle = @fopen($cache, 'w')) {
if($debug)
{
echo "Error: Cannot open $cache for writing.\n";
if(!is_dir(dirname($cache)))
{
print "The directory " . dirname($cache) . " doesn't exist. Please correct the \$cache variable, or create the directory.\n";
}
elseif(is_file($cache) && !is_writable($cache))
{
print "The cache file exists, but I can't write to it. Please check the pemissions of this file and try again.\n";
}
else
{
print "Please check the permissions on " . dirname($cache) . " as I don't have write permission here.\n";
}
}
else
{
echo "Error: Cannot open cache file";
}
exit;
}
if (@fwrite($handle, $sData) === FALSE) {
echo "Error: Cannot write to cache file";
exit;
}
fclose($handle);
}
// now return the XML for SimpleViewer
header("Content-Type: text/xml");
header("X-Cache-Status: Miss");
header("Last-Modified: " . gmdate("r", $now));
header("Expires: " . gmdate("r", ($now + $ttl)));
print $xmlout;
exit;
?>