Location: PHPKode > scripts > PicasaWeb class > picasaweb-class/picasaweb.class.php
<?php

/**
 * picasaweb.class.php
 *
 * @version $Id$
 * @copyright 2009 3S System Software Support
 */

/**
 *
 * retrieve and display pucassaweb albums
 */
class picasaweb{
	public $aAlbums;
	public $album_cols;
	public $tnSize = 1;
	public $dateFormat = "%d.%m.%Y %H:%M:%S";
	public $dispAlbumUrl = "dispalbum.php";
	public $aPhotos;
	public $dispPhotoUrl = "dispphoto.php";
	public $aPhoto;

	private $username;
	private $password;
	private $auth;
	private $aAuth;
	private $loc;

	function login($u, $p){
		$this->username = $u;
		$this->password = $p;
		$data = array('accountType'=>'GOOGLE',
					  'Email'=>$u,
					  'Passwd'=>$p,
					  'service'=>'lh2',
					  'source'=>'3S-picasaweb.class-1.0'
					  );
		$data = http_build_query($data);
		$resp = $this->do_post_request('https://www.google.com/accounts/ClientLogin', $data);
		parse_str(str_replace("\n", '&', $resp), $aResp);
		$this->auth = $aResp['Auth'];
		$this->aAuth = array("Authorization: GoogleLogin auth=$this->auth");
	}

	/*
	   retrieves an Array of Albums by this user
	*/
	function getAlbums(){
		$data = $this->do_get_request("http://picasaweb.google.com/data/feed/api/user/default", $this->aAuth);
		$oXml = new SimpleXMLElement($data);
		$aNs = $oXml->getNamespaces(TRUE);
		foreach($oXml->entry as $oEntry){
			$oGphoto = $oEntry->children($aNs['gphoto']);
			$oMedia = $oEntry->children($aNs['media']);
			$aAtt = $oMedia->group->thumbnail->attributes();
			$aEntry = array(
				'title'		=>(string)$oEntry->title,
				'id'		=>(string)$oGphoto->id,
				'published'	=>(string)$this->convert_date($oEntry->published),
				'nphotos'	=>(int)$oGphoto->numphotos,
				'thumb'		=>(string)$aAtt['url']
			);
			$this->aAlbums[]=$aEntry;
		}
	}

	/* Displays the list of albums as a table with 3 columns
	   The heading can be spcified
	*/
	function dispAlbums($t1="Title", $t2="Published", $t3="Photos"){
		if (count($this->aAlbums)==0) {
			$this->getAlbums();
		}
		$s = "<table id=\"albumList\">";
		$s .= "<tr><th>$t1</th><th>$t2</th><th>$t3</th></td>";
		foreach($this->aAlbums as $aAlbum){
			$s .= "<tr>";
			$s .= "<td>" . $this->genAlbumLink($aAlbum['title'], $aAlbum['id']) . "</td><td>$aAlbum[published]</td><td>$aAlbum[nphotos]</td>";
			$s .= "</tr>";
		}
		$s .= "</table>";
		return $s;
	}

	function dispAlbumsTn($nCols=5){
		if (count($this->aAlbums)==0) {
			$this->getAlbums();
		}
		$s = "<table id=\"albumTn\"><tr>";
		$col = 0;
		foreach($this->aAlbums as $aAlbum){
			if ($col >= $nCols) {
				$s .= "</tr><tr>";
				$col = 0;
			}
			$s .= "<td>" . $this->genAlbumLink("<img src=\"$aAlbum[thumb]\">", $aAlbum['id']) . "<br>$aAlbum[title]<br>$aAlbum[published]<br>$aAlbum[nphotos] Photos</td>";
			$col++;
		}
		while($col < $nCols){
			$s .= "<td></td>";
			$col++;
		} // while
		$s .= "</tr></table>";
		return $s;
	}

	function genAlbumLink($content, $id){
		$s = "<a href=\"$this->dispAlbumUrl?id=$id\">$content</a>";
		return $s;
	}

	/* get a list of all the photos in the album identified as $albumId
	   retriebe the url of the thumbnails of size $thSize (0=72x52, 1=144x104, 2=288x208
	*/
	function getPhotos($albumId){
		$data = $this->do_get_request("http://picasaweb.google.com/data/feed/api/user/default/albumid/$albumId", $this->aAuth);
		$oXml = new SimpleXMLElement($data);
		$aNs = $oXml->getNamespaces(TRUE);
		$this->aPhotos = array();
		foreach($oXml->entry as $oEntry){
			$oGphoto = $oEntry->children($aNs['gphoto']);
			$oMedia = $oEntry->children($aNs['media']);
			$attContent = $oMedia->group->content->attributes();
			$attThumb = $oMedia->group->thumbnail[$this->tnSize]->attributes();
			$aPhoto = array('id'			=>(string)$oGphoto->id,
							'albumid'		=>(string)$oGphoto->albumid,
							'url'			=>(string)$attContent['url'],
							'medium'		=>(string)$attContent['media'],
							'type'			=>(string)$attContent['type'],
							'width'			=>(int)$attContent['width'],
							'height'		=>(int)$attContent['height'],
							'title'			=>(string)$oMedia->group->title,
							'description'	=>(string)$oMedia->group->description,
							'thumb'			=>(string)$attThumb['url']
							);
			$this->aPhotos[] = $aPhoto;
		}

	}
	function listThumbs($nCols=5){
		$s = "<table id=\"photosTn\"><tr>";
		$col = 0;
		foreach($this->aPhotos as $i => $aPhoto){
			if ($col >= $nCols) {
				$s .= "</tr><tr>";
				$col = 0;
			}
			$s .= "<td>" . $this->genPhotoLink("<img src=\"$aPhoto[thumb]\">", $aPhoto['albumid'], $aPhoto['id']) . "<h3>$aPhoto[title]</h3>$aPhoto[description]<br>$aPhoto[width]x$aPhoto[height]</td>";
			$col++;
		}
		while($col < $nCols){
			$s .= "<td></td>";
			$col++;
		} // while
		$s .= "</tr></table>";
		return $s;
	}

	function genPhotoLink($content, $albumid, $photoid){
		$s = "<a href=\"$this->dispPhotoUrl?albumid=$albumid&amp;photoid=$photoid\">$content</a>";
		return $s;
	}

	function getPhoto($albumid, $photoid){
		$url = "http://picasaweb.google.com/data/entry/api/user/default/albumid/$albumid/photoid/$photoid";
		$data = $this->do_get_request($url, $this->aAuth);
		$oXml = new SimpleXMLElement($data);
		$aNs = $oXml->getNamespaces(TRUE);
		$oGphoto = $oXml->children($aNs['gphoto']);
		$oMedia = $oXml->children($aNs['media']);
		$attContent = $oMedia->group->content->attributes();
		$attThumb = $oMedia->group->thumbnail[$this->tnSize]->attributes();
		$this->aPhoto = array('id'			=>(string)$oGphoto->id,
							  'albumid'		=>(string)$oGphoto->albumid,
							  'url'			=>(string)$attContent['url'],
							  'medium'		=>(string)$attContent['media'],
							  'type'		=>(string)$attContent['type'],
							  'width'		=>(int)$attContent['width'],
							  'height'		=>(int)$attContent['height'],
							  'title'		=>(string)$oMedia->group->title,
							  'description'	=>(string)$oMedia->group->description,
							  'thumb'		=>(string)$attThumb['url']
							  );
	}

	function getPhotoUri(){
		return $this->aPhoto['url'];
	}

	private function do_post_request($url, $data, $opt_headers=NULL)
	{
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		if ($opt_headers) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $opt_headers);
		}
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		$response = curl_exec($ch);  /* Execute the HTTP command. */

		curl_close($ch);

		return $response;
	}

	private function do_get_request($url, $opt_headers=NULL)
	{
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		if ($opt_headers) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $opt_headers);
		}
		$response = curl_exec($ch);  /* Execute the HTTP command. */

		curl_close($ch);

		return $response;
	}

	private function convert_date($t) {
		return strftime($this->dateFormat, mktime(substr($t,11,2), substr($t,14,2), substr($t,17,2), substr($t,5,2), substr($t,8,2), substr($t,0,4)));
	}
}


?>
Return current item: PicasaWeb class