<?php
/*
 * getflickr.php uses the MagpieRSS Parser to fetch and parse a
 * tag, user or group pool RSS feed from the photo sharing site www.flickr.com,
 * and display the pictures.
 *
 * fork V 0.1 renamed to WPgetflickr.php, modified to fetch a random
 * image by Edgar Acosta (http://eacosta.wordpress.com).
 * 
 * V 0.6 renamed back to getflickr.php, and fixed up to account for new flickr
 * url structure by Tim Bishop (http://www.timbishop.com).
 * 
 * V 0.5 of flickrrss.php written and released under GPL by
 * Dave Kellam (http://www.eightface.com)
 *
 * V 0.2 of getflickr.php written and released under GPL by
 * Tim Bishop, http://www.timbishop.com
 *
 * This script requires the lastest version of MagpieRSS, which
 * can be obtained from: http://magpierss.sourceforge.net
 *
 * Thanks to Kellan Elliott-McCrea, author of MagpieRSS, and
 * cip at acme.sinless.org, who wrote the first version of this
 * script.  And Tim Bishop (http://www.timbishop.com) who wrote
 * the getflickr.php script that I used as a base.
 *
 * THIS 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.
 */
 
#################################
## User configurable variables ##
#################################

# set what type of tag you're pulling from flickr
# 0 = tag; 1 = user; 2 = group pool
$tagtype 1;

# set the tag to pull from flickr (tag, user or group pool)
# example: "cats", "44124462494@N01", "circle"
$tag "15756867@N00";


# set default value for number of pictures to show
# (flickr show maximum of 10 photos in feed)
$num_items 1;

# set to true to use medium pics, otherwise it uses thumbnails
$mediumPics true;

# set location of Magpie RSS files
require_once('magpierss/rss_fetch.inc');
require_once(
'magpierss/rss_utils.inc');
# require_once('./magpierss-0.6/rss_fetch.inc');
# require_once('./magpierss-0.6/rss_utils.inc');

# use image cache & set location
$useImageCache false;
#$cachePath="http://www.thebishop.net/news/cache/";
#$fullPath="/home/tbishop61/www/news/cache/";



################################################################
## End  of user defined variables, except for some html cruft ##
################################################################

// get the feeds
if ($tagtype == 0) { define('RSS_URL','http://www.flickr.com/services/feeds/photos_public.gne?tags=' $tag '&format=rss_200'); }
elseif (
$tagtype == 1) { define('RSS_URL','http://www.flickr.com/services/feeds/photos_public.gne?id=' $tag '&format=rss_200'); }
elseif (
$tagtype == 2) { define('RSS_URL','http://www.flickr.com/groups/' $tag '/pool/feed/?format=rss_200'); }
else { Print 
"Invalid tagtype"; }

# get rss file
$rss fetch_rss(RSS_URL);

if (
$rss) {

# HTML formatting of results
#    Print '<h3><a href="http://www.flickr.com/photos/tags/' . $tag . '/">';
#    Print "Recent " .$tag." photos posted to Flickr";
#    Print "</a></h3><br />";
#    Print '<div style="width:170px;">';


    
$imgurl "";
    
# specifies number of pictures
#    $items = array_slice($rss->items, 0, $num_items);

#  Random photos by Edgar Acosta

    
$array_keys = array(array_rand($rss->items));
  

    
# builds html from array
    
foreach ( $array_keys as $key ) {
       
$item $rss->items[$key];
       if(
preg_match('<img src="([^"]*)" [^/]*/>'$item['description'],$imgUrlMatches)) {
           
$imgurl $imgUrlMatches[1];
           
# change to thumbnail pictures
           
if (!$mediumPics) {
             
$imgurl str_replace("m.jpg""t.jpg"$imgurl);
           }
           
$title $item['title'];
           
$url $item['link'];
    
          
# Changed due to flickr changing urls
           # preg_match('<http://photos[0-9]\.flickr\.com/([^.]*)_(m|s)\.jpg>', $imgurl, $flickrSlugMatches);
           # preg_match('<http://photos\d\d?\.flickr\.com/([^.]*)_(m|s)\.jpg>', $imgurl, $flickrSlugMatches);
           
preg_match('<http://static.flickr\.com/\d\d?\/([^.]*)\.jpg>'$imgurl$flickrSlugMatches);
          
$flickrSlug $flickrSlugMatches[1];
           
           
# cache images 
           
if ($useImageCache) {
                       
               
# check if file already exists in cache
               
if (!file_exists("$fullPath$flickrSlug.jpg")) {           
                  
$filedata "";
                   
$remoteimage fopen($imgurl'rb');
                  if (
$remoteimage) {
                     while(!
feof($remoteimage)) {
                         
$filedata.= fread($remoteimage,1024*8);
                         }
                  }
                
fclose($remoteimage);
                
$localimage fopen("$fullPath$flickrSlug.jpg"'wb');
                
fwrite($localimage,$filedata);
                
fclose($localimage);
                }
            
#                print "<a href=\"$url\" title=\"$title\">"
#                    ."<img src=\"$cachePath$flickrSlug.jpg\" alt=\"$title\" border=\"0\" width=\"170\" />"
#                    ."</a>\n";
         
header("Location: $cachePath$flickrSlug.jpg");
                }
            else {
#                print "<a href=\"$url\" title=\"$title\">"
#                    ."<img src=\"$imgurl\" alt=\"$title\" border=\"0\" width=\"170\" />"
#                    ."</a>\n";      
                 
header("Location: $imgurl");
         }
       }
       
       
    }
#print "</div>";


else {
    echo 
magpie_error();
 }

?>