topical media & game development

talk show tell print

mashup-flickr-14-Complete-lib-FlickrGallery.php / php



  <?php
  
  class FlickrGallery extends FlickrAuthenticator
  {
    var nsid = '50317659@N00';
  
    var connection = 0;  
  
    var useDB = 1;
  
    var dbhost;
    var dbname;
    var dbuser;
    var dbpasswd;
  
    function FlickrGallery()
    {
      global flickrApiKey;
      global flickrApiSecret;
  
      this->FlickrAuthenticator(flickrApiKey, flickrApiSecret);
  
      global dbhost;
      global dbname;
      global dbuser;
      global dbpasswd; 
  
      this->dbhost = dbhost;
      this->dbname = dbname;
      this->dbuser = dbuser;
      this->dbpasswd = dbpasswd;
    }
  
    function findRecentPhotos(n = 20)
    {
      recentPhotos = array();
      if (this->useDB)
      {
        sql = "SELECT * FROM photo ORDER BY photo_date_upload DESC LIMIT n";
        res = mysql_query(sql, this->getConnection())
          or die("Invalid query: " . sql);
  
       while(row = mysql_fetch_assoc(res))
       {
         photo = this->buildPhotoFromRow(row);
         array_push(recentPhotos, photo);
       }
      }
      else
      {
        args = array(
          'user_id' => this->nsid,
          'sort' => 'date-posted-desc',
          'page' => 1, 
          'per_page' => n,
          'extras' => 'owner_name'
          );
  
        p = this->flickr->photos_search(args);
        if (this->flickr->getErrorCode())
        {
          echo ("Error fetching photos: " . this->flickr->getErrorMsg());
        }
  
        recentPhotos = p['photo'];
      }
  
      return recentPhotos;
    }
    
    function getPhotoInfo(id)
    {
      p = this->flickr->photos_getInfo(id);
      if (this->flickr->getErrorCode())
      {
        echo ("Error getting photo info: " . this->flickr->getErrorMsg());
      }
  
      return p;
    }
  
    function showSmartSetThumbnail(linkPage, title = "Set", n = 20, tags = "", 
      tagMode = "all", sort = "date-posted-desc")
    {
      s = "";
      url = linkPage . '?title=' . urlencode(title) . '&tags=' . urlencode(tags) 
        . "&n=n&tagMode=tagMode&sort=sort";
      // Get image to display
      photos = this->getSmartSet(1, tags, tagMode, sort);
  
      if (is_array(photos) && count(photos) > 0)
      {
        photo = photos[0];
        img = 'http://static.flickr.com/' . photo['server'] . '/' . photo['id'] 
          . '_' . photo['secret'] . '_s.jpg';
        s .= "<a href=\"url\"><img src=\"img\" /></a>";
        s .= "<p class=\"smart-set-caption\">" . title . "</p>";
      }
      return s;
    }
  
    function getSmartSet(n = 20, tags = "", tagMode = "all", 
      sort = "date-posted-desc")
    {
      ret = array();
  
      if (this->useDB)
      {
        key = this->getSmartSetKey(tags, tagMode, sort);
        sql = "SELECT COUNT(*) FROM smartset WHERE sset_key = 'key'";
        res = mysql_query(sql, this->getConnection()) 
          or die("Invalid query: " . sql);
  
        if (row = mysql_fetch_array(res))
        {
          count = row[0];
          if (count == 0)
          {
            this->populateSmartSet(tags, tagMode, sort);
          }
        }
  
        sql = "SELECT * FROM smartset, photo"
          . " WHERE sset_photo_id = photo_id"
          . " AND sset_key = 'key' LIMIT n";
        res = mysql_query(sql, this->getConnection()) 
          or die("Invalid query: " . sql);
  
        while (row = mysql_fetch_assoc(res))
        {
          photo = this->buildPhotoFromRow(row);
          array_push(ret, photo);
        }
      }
      else
      {
        args = array(
          'user_id' => this->nsid,
          'sort' => sort,
          'page' => 1, 
          'per_page' => n,
          'extras' => 'owner_name'
          );
  
        if (!empty(tags))
        {
          args['tags'] = tags;
          args['tag_mode'] = tagMode;
        }
  
        p = this->flickr->photos_search(args);
        if (this->flickr->getErrorCode())
        {
          echo ("Error fetching photos: " . this->flickr->getErrorMsg());
        }
  
        if (is_array(p['photo']) && count(p['photo']) > 0)
        {
          ret = p['photo'];
        }
      }
  
      return ret;
    }
  
    function setMeta(id, title, description)
    {
      this->flickr->photos_setMeta(id, title, description);
  
      if (this->flickr->getErrorCode())
      {
        echo ("Error setting metadata: " . this->flickr->getErrorMsg());
      }
    }
  
    function checkAuthenticatedUser()
    {
      return (this->nsid == this->auth['user']['nsid']);
    }
    
    function setTags(id, tags)
    {
      this->flickr->photos_setTags(id, tags);
  
      if (this->flickr->getErrorCode())
      {
        echo ("Error setting tags: " . this->flickr->getErrorMsg());
      }
    }
  
    function uploadPhoto(file, title = null, description = null, tags = null, 
      isPublic = null, isFriend = null, isFamily = null)
    {
      id = this->flickr->sync_upload(file, title, description, tags, isPublic,
        isFriend, isFamily);
  
      if (this->flickr->getErrorCode())
      {
        echo ("Error uploading photo: " . this->flickr->getErrorMsg());
      }
  
      return id;
    }
  
    function replacePhoto(file, photoId)
    {
      id = this->flickr->replace(file, photoId);
  
      if (this->flickr->getErrorCode())
      {
        echo ("Error replacing photo: " . this->flickr->getErrorMsg());
      }
  
      return id;
    }
  
    function getWithGeoData(bbox = NULL)
    {
      if (bbox == NULL)
      {
        bbox = '-180, -90, 180, 90';
      }
  
      geoPhotos = array();
      if (this->useDB)
      {
        bounds = explode(",", bbox);
        sql = "SELECT * FROM photo WHERE photo_accuracy > 0"
          . " AND photo_latitude >= " . bounds[1] 
          . " AND photo_latitude <= " . bounds[3] 
          . " AND photo_longitude >= " . bounds[0] 
          . " AND photo_longitude <= " . bounds[2];
  
        res = mysql_query(sql, this->getConnection()) 
          or die("Invalid query: " . sql);
  
        while(row = mysql_fetch_assoc(res))
        {
          photo = this->buildPhotoFromRow(row);
          array_push(geoPhotos, photo);
        }
      }
      else
      {
        args = array(
          'user_id' => this->nsid,
          'sort' => 'interestingness-desc',
          'bbox' => bbox,
          'extras' => 'geo',
          'per_page' => 500,
          );
  
        p = this->flickr->photos_search(args);
        if (this->flickr->getErrorCode())
        {
          echo ("Error fetching photos with GeoData: " . this->flickr->getErrorMsg());
        }
        geoPhotos = p['photo'];
      }
  
      return geoPhotos;
    }
    
    function getConnection()
    {
      if (empty(this->connection))
      {
        this->connection = mysql_connect(this->dbhost, 
          this->dbuser, this->dbpasswd);
  
        if (!this->connection)
        {
          die ("Cannot connect to database server: " . this->dbhost);
        }
  
        ret = mysql_select_db(this->dbname, this->connection);
  
        if (!ret)
        {
          die ("Cannot access database " . this->dbname . " on " . this->dbhostname);
        }
      }
  
      return this->connection;
    }
  
    function flushDBPhotos()
    {
      sql = "DELETE FROM photo";
      mysql_query(sql, this->getConnection()) or die("Invalid query: " . sql); 
    }
  
    function populateDBPhotos()
    {
      this->flushDBPhotos();
      set_time_limit(240);
  
      q = "INSERT INTO photo" 
        . " (photo_id, photo_secret, photo_server, photo_owner, photo_owner_name,"
        . " photo_is_public, photo_is_friend, photo_is_family,"
        . " photo_date_taken, photo_date_taken_granularity, photo_date_upload,"
        . " photo_title, photo_latitude, photo_longitude, photo_accuracy)"
        . " VALUES" 
        . " ('\%s', '\%s', \%d, '\%s', '\%s', \%d, \%d, \%d, '\%s', \%d, \%d, '\%s',"
        . " \%f, \%f, \%d)";
  
      keepGoing = 1;
      page = 1;
      while (keepGoing)
      {
        args = array(
          'user_id' => this->nsid,
          'sort' => 'date-posted-desc',
          'page' => page, 
          'per_page' => 500,
          'extras' => 'date_upload, date_taken, owner_name, geo'
        );
  
        p = this->flickr->photos_search(args);
        if (this->flickr->getErrorCode())
        {
          echo ("Error fetching photos: " . this->flickr->getErrorMsg());
        }
  
        if (count(p['photo']) == 0)
        {
          // no more photos
          break;
        }
  
        foreach(p['photo'] as photo)
        {
          sql = sprintf(q, photo['id'], photo['secret'], photo['server'], 
            photo['owner'], mysql_escape_string(photo['ownername']), 
            photo['ispublic'], photo['isfriend'], photo['isfamily'], 
            photo['datetaken'], photo['datetakengranularity'], 
            photo['dateupload'],
            mysql_escape_string(photo['title']), photo['latitude'], 
            photo['longitude'], photo['accuracy']);
  
          mysql_query(sql, this->getConnection()) 
            or die("Invalid query: " . sql);
        }
        page++;
      }
    }
  
    function buildPhotoFromRow(row)
    {
      photo = array();
  
      photo['id'] = row['photo_id'];
      photo['secret'] = row['photo_secret'];
      photo['server'] = row['photo_server'];
      photo['owner'] = row['photo_owner'];
      photo['ownername'] = row['photo_owner_name'];
      photo['title'] = row['photo_title'];
      photo['ispublic'] = row['photo_is_public'];
      photo['isfriend'] = row['photo_is_friend'];
      photo['isfamily'] = row['photo_is_family'];
      photo['dateupload'] = row['photo_date_upload'];
      photo['datetaken'] = row['photo_date_taken'];
      photo['datetakengranularity'] = row['photo_date_taken_granularity'];
      photo['latitude'] = row['photo_latitude'];
      photo['longitude'] = row['photo_longitude'];
      photo['accuracy'] = row['photo_accuracy'];
  
      return photo;
    }
  
    function getPhoto(id)
    {
      photo = NULL;
      if (this->useDB)
      {
        sql = "SELECT * FROM photo WHERE photo_id = '" 
          . mysql_escape_string(id) . "'";
        res = mysql_query(sql, this->getConnection()) 
          or die("Invalid query: " . sql);
        if (row = mysql_fetch_assoc(res))
        {
          photo = this->buildPhotoFromRow(row); 
        }
      }
  
      return photo;
    }
  
    function getNextPhoto(photo, key = NULL)
    {
      nextPhoto = NULL;
      sql = "";
  
      if (this->useDB)
      {
        if (empty(key))
        {
          // Get from recent photos
          date = photo['dateupload'];
          sql = "select * from photo"
            . " where photo_date_upload > date"
            . " order by photo_date_upload limit 1";
        }
        else
        {
          // Get from smart set
          // First find index for current photo
          photoId = photo['id'];
          indexSql = "select sset_index from smartset "
            . " where sset_key = '" . mysql_escape_string(key) . "'"
            . " and sset_photo_id = 'photoId'";
          res = mysql_query(indexSql, this->getConnection())
            or die("Invalid query: " . indexSql);
  
          if (row = mysql_fetch_array(res))
          {
            index = row[0];
  
            sql = "select * from smartset, photo"
              . " where sset_photo_id = photo_id"
              . " and sset_key = '" . mysql_escape_string(key) . "'"
              . " and sset_index > index"
              . " order by sset_index limit 1";
          }
        }
  
        if (!empty(sql))
        {
          res = mysql_query(sql, this->getConnection())
            or die("Invalid query: " . sql);
          if (row = mysql_fetch_assoc(res))
          {
            nextPhoto = this->buildPhotoFromRow(row);
          }
        }
      }
  
      return nextPhoto;
    }
  
    function getPreviousPhoto(photo, key = NULL)
    {
      prevPhoto = NULL;
      sql = "";
  
      if (this->useDB)
      {
        if (empty(key))
        {
          // Get from recent photos
          date = photo['dateupload'];
          sql = "select * from photo"
            . " where photo_date_upload < date"
            . " order by photo_date_upload desc limit 1";
        }
        else
        {
          // Get from smart set
          // First find index for current photo
          photoId = photo['id'];
          indexSql = "select sset_index from smartset "
            . " where sset_key = '" . mysql_escape_string(key) . "'"
            . " and sset_photo_id = 'photoId'";
          res = mysql_query(indexSql, this->getConnection())
            or die("Invalid query: " . indexSql);
          if (row = mysql_fetch_array(res))
          {
            index = row[0];
  
            sql = "select * from smartset, photo"
              . " where sset_photo_id = photo_id"
              . " and sset_key = '" . mysql_escape_string(key) . "'"
              . " and sset_index < index"
              . " order by sset_index desc limit 1";
          }
        }
  
        if (!empty(sql))
        {
          res = mysql_query(sql, this->getConnection())
            or die("Invalid query: " . sql);
          if (row = mysql_fetch_assoc(res))
          {
            prevPhoto = this->buildPhotoFromRow(row);
          }
        }
      }
  
      return prevPhoto;
    }
  
    function getSmartSetKey(tags, tagMode, sort)
    {
      s = tags . ":" . tagmode . ":" . sort;
      return md5(s);
    }
  
    function populateSmartSet(tags, tagMode, sort)
    {
      set_time_limit(240);
      key = this->getSmartSetKey(tags, tagMode, sort);
  
      keepGoing = 1;
      page = 1;
      n = 0;
      while (keepGoing)
      {
        args = array(
          'user_id' => this->nsid,
          'sort' => sort,
          'page' => page,
          'tags' => tags,
          'tag_mode' => tagMode, 
          'per_page' => 500,
          'extras' => 'date_upload, date_taken, owner_name, geo'
          );
  
        p = this->flickr->photos_search(args);
        if (this->flickr->getErrorCode())
        {
          echo ("Error fetching photos: " . this->flickr->getErrorMsg());
        }
  
        if (count(p['photo']) == 0)
        {
          // no more photos
          break;
        }
  
        q = "INSERT INTO smartset" 
          . " (sset_key, sset_photo_id, sset_index)" 
          . " VALUES" 
          . " ('\%s', '\%s', \%d)";
  
        foreach(p['photo'] as photo)
        {
          sql = sprintf(q, key, photo['id'], n);
   
          mysql_query(sql, this->getConnection()) or die("Invalid query: " . sql);
          n++;
        }
  
        page++;
      }    
    }
  
  }
  
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.