(file) Return to flickrssp.class.php CVS log (file) (dir) Up to [RizwankCVS] / geekymedia_web / photo / inc

  1 rizwank 1.1 <?php
  2             /* ---------------------------------------------------------------------------
  3             Title: FlickrSSP 1.0
  4             Author: Brian Sweeting (http://www.sweeting.net/)
  5             
  6             FlickrSSP allows you to use Flickr (http://www.flickr.com) and SlideshowPro 
  7             (http://www.slideshowpro.net) to display all of your Flickr photosets on 
  8             your own website instead of just your recent photos via the RSS feed.
  9             --------------------------------------------------------------------------- */
 10             
 11             class FlickrSSP {
 12             
 13                var $flickr_api_url;
 14                var $config = array();
 15                var $error = array();
 16                
 17                
 18                function flickrssp($config) {
 19                         
 20                   // Set configuration parameters
 21                   $this->config['user_id'] = $config['user_id'];
 22 rizwank 1.1       $this->config['api_key'] = $config['api_key'];
 23                   $this->config['url'] = $config['url'];
 24                   $this->config['xml'] = $config['xml'];
 25                   $this->config['cache_period'] = $config['cache_period'];
 26                   
 27                   // Set the url to Flickr's REST api
 28                   $this->flickr_api_url = 'http://www.flickr.com/services/rest/';
 29                   
 30                   // Get the xml and return it
 31                   $this->getXml();
 32                   
 33                   // If there were errors display them
 34                   if(sizeof($this->error)>0) {
 35                      $this->displayErrors();
 36                   }
 37                   
 38                   return;
 39                }
 40                
 41                function getXml() {
 42                   
 43 rizwank 1.1       //Check to see if xml file exists, if not attempt to create it
 44                   if(file_exists($this->config['xml'])) {
 45                      // Return the content of the XML file if no update is needed
 46                      if(!((time() - filemtime($this->config['xml'])) >= $this->config['cache_period'])) {
 47                         $this->displayXml();
 48                      }
 49                      else { 
 50                         $this->updateXml();
 51                      }
 52                   }
 53                   else {
 54                      $this->updateXml();
 55                   }
 56                   
 57                   return;
 58                   
 59                }
 60                
 61                function updateXml() {
 62                   
 63                   include_once('snoopy.class.php');
 64 rizwank 1.1       include_once('xml.inc.php');
 65                   
 66                   $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
 67                   $xml .= '<gallery>'."\n";
 68             
 69                   // Step 1 - Get the photosets
 70                   $url = $this->flickr_api_url.'?method=flickr.photosets.getList&api_key='.$this->config['api_key'].'&user_id='.$this->config['user_id'];
 71                   $snoopy = new Snoopy;
 72                   $snoopy->fetch($url);
 73                   $photosets = XML_unserialize($snoopy->results);
 74                   if(isset($photosets[rsp][photosets][photoset])) {
 75                       $photosets = $photosets[rsp][photosets][photoset];
 76                    }
 77                    else {
 78                       $this->error[] = 'Flickr Error: '.$photosets['rsp']['err attr']['msg'].' (Error Code = '.$photosets['rsp']['err attr']['code'].')';
 79                       $this->displayErrors();
 80                    }
 81             
 82                   // Step 2 - Get the info of each photoset and in the meantime
 83                   // lets create the final XML.
 84                   while(count($photosets) > 0) {
 85 rizwank 1.1     
 86                       $psa = array_shift($photosets);
 87                       $ps = array_shift($photosets);
 88                       // Create XML tag of the album
 89                       $xml .= "\t".'<album title="'.$ps[title].'" description="'.$ps[description].'" lgPath="" tn="http://photos'.$psa[server].'.flickr.com/'.$psa[primary].'_'.$psa[secret].'_s.jpg">'."\n";
 90                 
 91                       // Get photos of the current album
 92                       $url = $this->flickr_api_url.'?method=flickr.photosets.getPhotos&photoset_id='.$psa['id'].'&api_key='.$this->config['api_key'].'&user_id='.$this->config['user_id'];
 93                       //print $url; 
 94                       //echo $url;
 95                       $snoopy->fetch($url);
 96                       $photos = XML_unserialize($snoopy->results);
 97                       if(isset($photos[rsp][photoset][photo])) {
 98                          $photos = $photos[rsp][photoset][photo];
 99                       }
100                       else {
101                          $this->error[] = 'Flickr Error: '.$photosets['rsp']['err attr']['msg'].' (Error Code = '.$photosets['rsp']['err attr']['code'].')';
102                          $this->displayErrors();
103                       }
104             
105                       while(count($photos) > 0) {
106 rizwank 1.1               $ph = array_shift($photos);
107                           array_shift($photos);
108                           
109                           // Create XML tag of the image
110                           $xml .= "\t\t".'<img src="http://photos'.$ph[server].'.flickr.com/'.$ph[id].'_'.$ph[secret].'.jpg" link="'.$this->config['url'].$ph[id].'" tn="http://photos'.$ph[server].'.flickr.com/'.$ph[id].'_'.$ph[secret].'_t.jpg" caption="'.$ph[title].'" />'."\n";
111                       }
112                 
113                       $xml .= "\t".'</album>'."\n";
114             
115                   }
116                   $xml .= '</gallery>'."\n";
117                   
118                   // Now we save the XML file
119                   if($w = @fopen($this->config['xml'],"wb")) {
120                       fwrite($w,$xml);
121                       fclose($w);
122                   }
123                   else {
124                       $this->error[] = 'Could not save the XML file';
125                   }
126                   
127 rizwank 1.1       //print_r($this->error);
128                   // Get the xml and return it
129                   $this->displayXml();
130                   
131                }
132                
133                function displayXml() {
134                   
135                   if(!sizeof($this->error)>0) {
136                      header("Content-type: text/plain");
137                   
138                      if($f = fopen($this->config['xml'],'r')) {
139                         $xml = fread($f, filesize($this->config['xml']));
140                         fclose($f);
141                         print_r($xml);
142                         return;
143                      }
144                      else {
145                         $this->error[] = 'Could not open the cache of the XML file';
146                      }
147                   }
148 rizwank 1.1    }
149                
150                function displayErrors() {
151                   
152                   header("Content-type: text/html");
153                   
154                   // Loop through the error array and display errors
155                   for($i=0;$i<sizeof($this->error);$i++) {
156                      echo $this->error[$i]."<br />\n";
157                   }
158                   
159                   // Kill the execution of the script
160                   exit;
161                   
162                }
163             }
164             ?>

Rizwan Kassim
Powered by
ViewCVS 0.9.2