Example #1
0
 /**
  * Constructs an Album object.  
  * When called, this method will fill out each private member	
  * of the Album object based on XML returned from Picasa's Atom feed.  It will also create
  * a Picasa_Image object for each image in the Album by passing XML for each image into 
  * the Picasa_Image constructor.	
  *
  * @param string $url   The URL of the Picasa query to retrieve the XML from.  See
  *                      http://code.google.com/apis/picasaweb/gdata.html for information on
  *                      how to format the URL.  If null, it is assumed that $albums param
  *                      has been supplied.
  * @param SimpleXMLElement $albums  XML for constructing the object.  If null, it is assumed that the 
  *                                  URL to the Atom feed has been supplied.  If both are null, a
  *                                  {@link Picasa_Exception} is thrown.
  * @param array $contextArray       An array that can be passed to stream_context_create() to generate
  *                                  a PHP context.  See 
  *                                  {@link http://us2.php.net/manual/en/function.stream-context-create.php}
  * @param boolean $useCache  You can decide not to cache a specific request by passing false here.  You may
  *                           want to do this, for instance, if you're requesting a private feed.
  * @throws {@link Picasa_Exception} If the XML suppled through either parameter does not contain valid XML.
  */
 public function __construct($url = null, SimpleXMLElement $albums = null, $contextArray = null, $useCache = true)
 {
     if ($url != null) {
         $xmldata = false;
         $context = null;
         Picasa_Logger::getLogger()->logIfEnabled('Request string: ' . $url);
         if ($contextArray !== null) {
             $context = stream_context_create($contextArray);
         }
         if ($useCache === true) {
             $xmldata = Picasa_Cache::getCache()->getIfCached($url);
         }
         if ($xmldata === false) {
             Picasa_Logger::getLogger()->logIfEnabled("Not using cached entry for " . $url);
             $xmldata = @file_get_contents($url, false, $context);
             if ($useCache === true && $xmldata !== false) {
                 Picasa_Logger::getLogger()->logIfEnabled("Refreshing cache entry for key " . $url);
                 Picasa_Cache::getCache()->setInCache($url, $xmldata);
             }
         }
         if ($xmldata === false) {
             throw Picasa::getExceptionFromInvalidQuery($url, $contextArray);
         }
         try {
             // Load the XML file into a SimpleXMLElement
             $albums = new SimpleXMLElement($xmldata);
         } catch (Exception $e) {
             throw new Picasa_Exception($e->getMessage(), null, $url);
         }
         // I'm not sure why there's a difference, but the icon is given in different ways
         // depending on if the document is just for an Album or if it's part of a larger document
         $this->icon = $albums->icon;
     }
     // Whether or not the contextArray is null, it should be set.
     $this->contextArray = $contextArray;
     if ($albums != null) {
         $namespaces = $albums->getNamespaces(true);
         $this->picasaAuthor = new Picasa_Author($albums->author);
         $this->editLink = null;
         $link = $albums->link[0];
         $i = 0;
         while ($link != null) {
             $attributes = $albums->link[$i]->attributes();
             if (strcmp($attributes["rel"], "edit") == 0) {
                 $this->editLink = $attributes["href"];
                 break;
             } else {
                 if (strcmp($attributes["rel"], "alternate") == 0) {
                     $this->weblink = $attributes["href"];
                 }
             }
             $i++;
             $link = $albums->link[$i];
         }
         if (array_key_exists("gphoto", $namespaces)) {
             $gphoto_ns = $albums->children($namespaces["gphoto"]);
             $this->location = $gphoto_ns->location;
             $this->idnum = $gphoto_ns->id;
             $this->numphotos = $gphoto_ns->numphotos;
             $this->photosRemaining = $gphoto_ns->numphotosremaining;
             $this->bytesUsed = $gphoto_ns->bytesUsed;
             $this->commentingEnabled = $gphoto_ns->commentingEnabled;
             $this->numComments = $gphoto_ns->commentCount;
             $this->timestamp = $gphoto_ns->timestamp;
             // The picasaAuthor field must be set before this line is executed
             if ($this->picasaAuthor->getUser() == null || strcmp($this->picasaAuthor->getUser(), "") == 0) {
                 $this->picasaAuthor->setUser($gphoto_ns->user);
             }
         }
         if (array_key_exists("media", $namespaces)) {
             $media_ns = $albums->children($namespaces["media"]);
             // As stated above, this is to account for the different placement of icon
             if ($url === null) {
                 $thumbAtt = $media_ns->group->thumbnail->attributes();
                 $this->icon = $thumbAtt["url"];
             }
         }
         if (array_key_exists("georss", $namespaces)) {
             $georss_ns = $albums->children($namespaces["georss"]);
             $gml_ns = $georss_ns->children($namespaces["gml"]);
             $this->gmlPosition = @$gml_ns->Point->pos;
         }
         $this->id = $albums->id;
         $this->title = $albums->title;
         $this->updated = $albums->updated;
         $this->published = $albums->published;
         $this->summary = $albums->summary;
         $this->rights = $albums->rights;
         $this->author = $albums->author->name;
         $this->subtitle = $albums->subtitle;
         $this->comments = null;
         $this->images = array();
         $i = 0;
         //Create a new Image object for each Image element
         foreach ($albums->entry as $images) {
             $this->images[$i] = new Picasa_Image(null, $images);
             $i++;
         }
     }
 }
 /**
  * Constructs an Image object.
  *
  * @param string $url   URL of the query for the data that should be returned based on Picasa's API documentation.
  *                      See {@link http://code.google.com/apis/picasaweb/gdata.html} for instructions on how to formulate
  *                      the URL.  The URL parameter can be left blank, which will still produce a populated Image
  *                      object as long as the $albums parameter contains XML from the Picasa Atom feed for an image.
  * @param SimpleXMLElement $albums  XML describing a Picasa image.  This can be left blank as long as a URL is specified in the
  *                                  url parameter that returns valid XML for a Picasa image.  If both are null, a
  *                                  {@link Picasa_Exception} is thrown.
  * @param array $contextArray       An array that can be passed to stream_context_create() to generate
  *                                  a PHP context.  See
  *                                  {@link http://us2.php.net/manual/en/function.stream-context-create.php}
  * @param boolean $useCache  You can decide not to cache a specific request by passing false here.  You may
  *                           want to do this, for instance, if you're requesting a private feed.
  * @throws {@link Picasa_Exception} If the XML suppled through either parameter does not contain valid XML.
  */
 public function __construct($url = null, SimpleXMLElement $albums = null, $contextArray = null, $useCache = true)
 {
     if ($url != null) {
         Picasa_Logger::getLogger()->logIfEnabled('Request string: ' . $url);
         $context = null;
         $xmldata = false;
         if ($contextArray != null) {
             $context = stream_context_create($contextArray);
         }
         if ($useCache === true) {
             $xmldata = Picasa_Cache::getCache()->getIfCached($url);
         }
         if ($xmldata === false) {
             Picasa_Logger::getLogger()->logIfEnabled('Cached copy not available, requesting image freshly for URL: ' . $url);
             $xmldata = @file_get_contents($url, false, $context);
             if ($useCache === true && $xmldata !== false) {
                 Picasa_Logger::getLogger()->logIfEnabled('Saving account to cache.');
                 Picasa_Cache::getCache()->setInCache($url, $xmldata);
             }
         } else {
             Picasa_Logger::getLogger()->logIfEnabled('Image retreived from cache.');
         }
         if ($xmldata == false) {
             throw Picasa::getExceptionFromInvalidQuery($url, $contextArray);
         }
         try {
             // Load the XML file into a SimpleXMLElement
             $albums = new SimpleXMLElement($xmldata);
         } catch (Exception $e) {
             throw new Picasa_Exception($e->getMessage(), null, $url);
         }
     }
     $this->contextArray = $contextArray;
     if ($albums != null) {
         foreach ($albums->link as $plink) {
             if ($plink['rel'] == 'alternate') {
                 $this->weblink = $plink['href'];
             }
         }
         $namespaces = $albums->getNamespaces(true);
         if (array_key_exists("media", $namespaces)) {
             $media_ns = $albums->children($namespaces["media"]);
             $this->description = $media_ns->group->description;
             $this->keywords = $media_ns->group->keywords;
             $this->thumbUrlMap = array();
             $this->thumbHeightMap = array();
             $this->previous = null;
             $this->next = null;
             $i = 0;
             $this->thumbnails = array();
             $thumb = $media_ns->group->thumbnail[$i];
             while ($thumb != null) {
                 $thumbAtt = $thumb->attributes();
                 $width = "" . $thumbAtt['width'];
                 $height = $thumbAtt['height'];
                 $url = $thumbAtt['url'];
                 // These fields are deprecated but included for backwards compatibility
                 $this->thumbUrlMap[$width] = $url;
                 $this->thumbHeightMap[$width] = $height;
                 $this->thumbnails[$i] = new Picasa_Thumbnail($url, $thumbAtt['width'], $thumbAtt['height']);
                 $i++;
                 $thumb = $media_ns->group->thumbnail[$i];
             }
             /* This is to support the previous implementation.  It may seem inefficiant to loop
              * through twice, but typically there will be 1-3 iterations, so it is inconsequential. */
             $thumb = $media_ns->group->thumbnail[0];
             if ($thumb != null) {
                 $thumbAtt = $media_ns->group->thumbnail[0]->attributes();
                 $this->smallThumb = $thumbAtt['url'];
             } else {
                 $this->smallThumb = null;
             }
             $thumb = $media_ns->group->thumbnail[1];
             if ($thumb != null) {
                 $thumbAtt = $thumb->attributes();
                 $this->mediumThumb = $thumbAtt['url'];
             } else {
                 $this->mediumThumb = null;
             }
             $thumb = $media_ns->group->thumbnail[2];
             if ($thumb != null) {
                 $thumbAtt = $thumb->attributes();
                 $this->largeThumb = $thumbAtt['url'];
             } else {
                 $this->largeThumb = null;
             }
             $this->contentUrlMap = array();
             $this->contentHeightMap = array();
             $i = 0;
             $thumb = $media_ns->group->content[$i];
             while ($thumb != null) {
                 $thumbAtt = $thumb->attributes();
                 $width = "" . $thumbAtt['width'];
                 $height = $thumbAtt['height'];
                 $url = $thumbAtt['url'];
                 $this->contentUrlMap[$width] = $url;
                 $this->contentHeightMap[$width] = $height;
                 $i++;
                 $thumb = $media_ns->group->content[$i];
             }
             $this->content = $thumbAtt['url'];
             $this->imageType = $thumbAtt['type'];
             // Pull and parse the tags
             if ($media_ns->group->keywords != null || strcmp($media_ns->group->keywords, "") != 0) {
                 // Make an array for to hold all of a photo's tags
                 $this->tags = array();
                 /* Tags are stored as a comma-delimited list.
                  * Tokenize the list on comma and strip it to get just the tag.
                  */
                 $tok = strtok($media_ns->group->keywords, ",");
                 $i = 0;
                 while ($tok != false) {
                     // Set the tag in the array
                     $this->tags[$i] = trim($tok);
                     $tok = strtok(",");
                     $i++;
                 }
             } else {
                 $this->tags = null;
             }
         } else {
             $this->description = null;
             $this->keywords = null;
             $this->smallThumb = null;
             $this->mediumThumb = null;
             $this->largeThumb = null;
             $this->content = null;
         }
         if (array_key_exists("gphoto", $namespaces)) {
             $gphoto_ns = $albums->children($namespaces["gphoto"]);
             $this->idnum = $gphoto_ns->id;
             $this->width = $gphoto_ns->width;
             $this->height = $gphoto_ns->height;
             $this->albumid = $gphoto_ns->albumid;
             $this->albumTitle = $gphoto_ns->albumtitle;
             $this->albumDescription = $gphoto_ns->albumdesc;
             $this->version = $gphoto_ns->version;
             $this->timestamp = $gphoto_ns->timestamp;
             $this->commentingEnabled = $gphoto_ns->commentingEnabled;
             if (strcmp($gphoto_ns->commentCount, "") == 0 || $gphoto_ns->commentCount === null) {
                 $this->commentCount = null;
             } else {
                 $this->commentCount = intval($gphoto_ns->commentCount);
             }
         } else {
             $this->idnum = null;
             $this->width = null;
             $this->height = null;
             $this->albumid = null;
             $this->commentCount = null;
             $this->version = null;
             $this->timestamp = null;
         }
         if (array_key_exists("exif", $namespaces)) {
             $exif_ns = $albums->children($namespaces["exif"]);
             $this->flash = $exif_ns->tags->flash;
             $this->fstop = $exif_ns->tags->fstop;
             $this->cameraMake = $exif_ns->tags->make;
             $this->cameraModel = $exif_ns->tags->model;
             $this->exposure = $exif_ns->tags->exposure;
             $this->focalLength = $exif_ns->tags->focallength;
             $this->iso = $exif_ns->tags->iso;
         } else {
             $this->flash = null;
             $this->fstop = null;
             $this->cameraMake = null;
             $this->cameraModel = null;
             $this->exposure = null;
             $this->focalLength = null;
             $this->iso = null;
         }
         if (array_key_exists("georss", $namespaces)) {
             $georss_ns = $albums->children($namespaces["georss"]);
             $gml_ns = @$georss_ns->children($namespaces["gml"]);
             if ($gml_ns !== null || $gml_ns !== false) {
                 $this->gmlPosition = @$gml_ns->Point->pos;
             } else {
                 $this->gmlPosition = null;
             }
         } else {
             $this->gmlPosition = null;
         }
         // Set the basic attributes
         $this->id = $albums->id;
         $this->title = $albums->title;
         $this->updated = $albums->updated;
         if ($albums->author != null && strcmp($albums->author, "") != 0) {
             $this->author = new Picasa_Author($albums->author);
         } else {
             $this->author = new Picasa_Author();
         }
         // The user is not a field in the XML for an image like it is for an album.  Thus, we parse.
         if ($this->author->getUser() == null || strcmp($this->author->getUser(), "") == 0) {
             $startUser = strpos($this->id, '/user/') + 6;
             $endUser = strpos($this->id, '/', $startUser);
             $this->author->setUser(substr($this->id, $startUser, $endUser - $startUser));
         }
         // If there are comments, retrieve them and put them into the comments field of the object
         if ($this->commentCount === null) {
             $this->comments = null;
         } else {
             if ($this->commentCount === 0) {
                 $this->comments = array();
             } else {
                 $this->comments = array();
                 $i = 0;
                 // Grab each comment and make it into an object
                 foreach ($albums->entry as $comment) {
                     $this->comments[$i] = new Picasa_Comment($comment);
                     $i++;
                 }
             }
         }
     }
 }