<?php
    
/////////////////////////////////////////////
    //script to output a cool flickr photostream
    //by Sebastian Schaetz, chairman (at) soa minus world dot de
    /////////////////////////////////////////////

    //what we do is:
    //    we fetch the desired data from flickr (getData)
    //    we form a nice output with the data (outputImages)
    //        * this output is actually creates as javascript for easy insertion into websites

    /////////////////////////////////////////////
    //    CONFIGURATION SECTION
    /////////////////////////////////////////////

    
    //the flickr api key
    
$flickr_api_key "";
    
//the photoset id
    
$flickr_photoset_id "";
    
//number of images
    
$flickr_img_per_page "3";
    
    
/////////////////////////////////////////////
    /////////////////////////////////////////////

    /////////////////////////////////////////////
    //this function fetches the xml file from flickr, 
    //processes it into a cute little array and returns that array
    //basic code is quick and dirty taken from http://www.phpfreaks.com/tutorials/44/0.php
    /////////////////////////////////////////////
    
function getData($flickr_api_key$flickr_photoset_id$flickr_img_per_page)
    {
        
//the api url
        
$flick_api_url "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=" 
                                
$flickr_api_key "&photoset_id=" 
                                
$flickr_photoset_id "&per_page=" 
                                
$flickr_img_per_page "&page=1";

        
//create xml parser object
        
$parser xml_parser_create();

        
//set some options
        
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
        
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);

        
//read XML file into $d
        
$d implode("",file($flick_api_url));

        
//parse XML input $d into two arrays:
        
xml_parse_into_struct($parser,$d,$d_ar,$i_ar) or die();

        
$data null;

        
//get our images into an array
        
for ($i 2$i<$flickr_img_per_page 2$i++)
        {
            if(
array_key_exists($i$d_ar))
            {
                if(
array_key_exists("attributes"$d_ar[$i]))
                {
                    if(
array_key_exists("id"$d_ar[$i]["attributes"]) && array_key_exists("secret"$d_ar[$i]["attributes"]))
                    {
                        
$data[$i]["id"] = $d_ar[$i]["attributes"]["id"];
                        
$data[$i]["secret"] = $d_ar[$i]["attributes"]["secret"];
                        
$data[$i]["server"] = $d_ar[$i]["attributes"]["server"];
                        
$data[$i]["farm"] = $d_ar[$i]["attributes"]["farm"];
                        
$data[$i]["title"] = $d_ar[$i]["attributes"]["title"];
                    }
                }   
            }   
        }

        
xml_parser_free($parser); 
        if (
$data == null)
            return 
null;
        
        return 
$data;
    }

    
/////////////////////////////////////////////
    //this function takes the data and outputs javascript code for every image
    /////////////////////////////////////////////
    
function outputImages($data)
    {
        
//output header stuff
        
?>
                var b_txt = "";
                b_txt+= '                        <br />';
                b_txt+= '                        <div class="sidebarheader">Photostream:</div>';
                b_txt+= '                        <div class="sidebarcontent">';
                b_txt+= '                           <br/>';
        <?
        
        
//output actual data
        
foreach($data as $photo)
        {
            
$tmp "http://farm" 
                                    
$photo["farm"] . ".static.flickr.com/" 
                                    
$photo["server"] . "/" 
                                    
$photo["id"] . "_" 
                                    
$photo["secret"];
            
$url $tmp "_s.jpg";
            
$bigurl $tmp ".jpg";
            
            
?>

                    b_txt+= '                            <table class="img_shadow" border="0" cellspacing="0" cellpadding="0">';
                    b_txt+= '                              <tr>';
                    b_txt+= '                                <td class="img_shadow-topLeft"></td>';
                    b_txt+= '                                <td class="img_shadow-top"></td>';
                    b_txt+= '                                <td class="img_shadow-topRight"></td>';
                    b_txt+= '                               </tr>';
                    b_txt+= '                               <tr>';
                    b_txt+= '                                 <td class="img_shadow-left"></td>';
                    b_txt+= '                                 <td class="img_shadow-center">';
                    b_txt+= '                                   <img src="<?php echo $url?>" alt="click for mor information"  onclick="new Effect.toggle($(\'<?php echo $photo["id"]; ?>\'),\'blind\')">';
                    b_txt+= '                                   <div id="<?php echo $photo["id"]; ?>" class="img_container" style="display: none;">';
                    b_txt+= '                                        <div class="img_content"><?php echo $photo["title"];?><br/>';
                    b_txt+= '                                        <a href="<?php echo $bigurl;?>" target="_blank">[view larger]</a></div>';
                    b_txt+= '                                   </div>';
                    b_txt+= '                                 </td>';
                    b_txt+= '                                 <td class="img_shadow-right"></td>';
                    b_txt+= '                               </tr>';
                    b_txt+= '                               <tr>';
                    b_txt+= '                                 <td class="img_shadow-bottomLeft"></td>';
                    b_txt+= '                                 <td class="img_shadow-bottom"></td>';
                    b_txt+= '                                 <td class="img_shadow-bottomRight"></td>';
                    b_txt+= '                               </tr>';
                    b_txt+= '                            </table>';

            <?php
        
}
        
        
//output footer stuff
        
?>
                b_txt+= '                       </div>';
                document.write(b_txt);
        <?
    
}
    
    
/////////////////////////////////////////////
    //here we execute the stuff
    /////////////////////////////////////////////
    
    //call function to get data
    
$data getData($flickr_api_key$flickr_photoset_id$flickr_img_per_page);
    
    
//if we got data echo javascript
    
if ($data != null)
        echo 
outputImages($data);
        
    
//that's it
?>