repository: New Picasa Web Albums Plugin MDL-17473

This commit is contained in:
poltawski
2008-11-30 17:37:06 +00:00
parent 283ebb7147
commit 83b510875d
4 changed files with 218 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
<?php
$string['repositoryname'] = 'Picasa Web Album';
?>
+114
View File
@@ -285,6 +285,9 @@ class google_picasa {
const REALM = 'http://picasaweb.google.com/data/';
const USER_PREF_NAME = 'google_authsub_sesskey_picasa';
const UPLOAD_LOCATION = 'http://picasaweb.google.com/data/feed/api/user/default/albumid/default';
const ALBUM_PHOTO_LIST = 'http://picasaweb.google.com/data/feed/api/user/default/albumid/';
const PHOTO_SEARCH_URL = 'http://picasaweb.google.com/data/feed/api/user/default?kind=photo&q=';
const LIST_ALBUMS_URL = 'http://picasaweb.google.com/data/feed/api/user/default';
private $google_curl = null;
@@ -332,6 +335,117 @@ class google_picasa {
return false;
}
}
/**
* Returns list of photos for file picker.
* If top level then returns list of albums, otherwise
* photos within an album.
*
* @param string $path The path to files (assumed to be albumid)
* @return mixed $files A list of files for the file picker
*/
public function get_file_list($path = ''){
if(!$path){
return $this->get_albums();
}else{
return $this->get_album_photos($path);
}
}
/**
* Returns list of photos in album specified
*
* @param int $albumid Photo album to list photos from
* @return mixed $files A list of files for the file picker
*/
public function get_album_photos($albumid){
$albumcontent = $this->google_curl->get(google_picasa::ALBUM_PHOTO_LIST.$albumid);
return $this->get_photo_details($albumcontent);
}
/**
* Does text search on the users photos and returns
* matches in format for picasa api
*
* @param string $query Search terms
* @return mixed $files A list of files for the file picker
*/
public function do_photo_search($query){
$content = $this->google_curl->get(google_picasa::PHOTO_SEARCH_URL.htmlentities($query));
return $this->get_photo_details($content);
}
/**
* Gets all the users albums and returns them as a list of folders
* for the file picker
*
* @return mixes $files Array in the format get_listing uses for folders
*/
public function get_albums(){
$content = $this->google_curl->get(google_picasa::LIST_ALBUMS_URL);
$xml = new SimpleXMLElement($content);
$files = array();
foreach($xml->entry as $album){
$gphoto = $album->children('http://schemas.google.com/photos/2007');
$mediainfo = $album->children('http://search.yahoo.com/mrss/');
//hacky...
$thumbnailinfo = $mediainfo->group->thumbnail[0]->attributes();
$files[] = array( 'title' => (string) $gphoto->name,
'date' => userdate($gphoto->timestamp),
'size' => (int) $gphoto->bytesUsed,
'path' => (string) $gphoto->id,
'thumbnail' => (string) $thumbnailinfo['url'],
'thumbnail_width' => (int) $thumbnailinfo['width'],
'thumbnail_height' => (int) $thumbnailinfo['height'],
'children' => array(),
);
}
return $files;
}
/**
* Recieves XML from a picasa list of photos and returns
* array in format for file picker.
*
* @param string $rawxml XML from picasa api
* @return mixed $files A list of files for the file picker
*/
public function get_photo_details($rawxml){
$xml = new SimpleXMLElement($rawxml);
$files = array();
foreach($xml->entry as $photo){
$gphoto = $photo->children('http://schemas.google.com/photos/2007');
$mediainfo = $photo->children('http://search.yahoo.com/mrss/');
$fullinfo = $mediainfo->group->content->attributes();
//hacky...
$thumbnailinfo = $mediainfo->group->thumbnail[0]->attributes();
$files[] = array('title' => (string) $mediainfo->group->title,
'date' => userdate($gphoto->timestamp),
'size' => (int) $gphoto->size,
'path' => $gphoto->albumid.'/'.$gphoto->id,
'thumbnail' => (string) $thumbnailinfo['url'],
'thumbnail_width' => (int) $thumbnailinfo['width'],
'thumbnail_height' => (int) $thumbnailinfo['height'],
'source' => (string) $fullinfo['url'],
);
}
return $files;
}
}
/**
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

+101
View File
@@ -0,0 +1,101 @@
<?php
/**
* Picasa Repository Plugin
*
* @author Dan Poltawski <[email protected]>
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once($CFG->libdir.'/googleapi.php');
class repository_picasa extends repository {
private $subauthtoken = '';
public function __construct($repositoryid, $context = SITEID, $options = array()) {
global $USER;
parent::__construct($repositoryid, $context, $options);
// TODO: I wish there was somewhere we could explicitly put this outside of constructor..
$googletoken = optional_param('token', false, PARAM_RAW);
if($googletoken){
$gauth = new google_authsub(false, $googletoken); // will throw exception if fails
google_picasa::set_sesskey($gauth->get_sessiontoken(), $USER->id);
}
# fixme - we are not checking login before all functions in the repo api.. eg search
# MDL-17474
$this->check_login();
}
public function check_login() {
global $USER;
$sesskey = google_picasa::get_sesskey($USER->id);
if($sesskey){
try{
$gauth = new google_authsub($sesskey);
$this->subauthtoken = $sesskey;
return true;
}catch(Exception $e){
// sesskey is not valid, delete store and re-auth
google_picasa::delete_sesskey($USER->id);
}
}
return false;
}
public function print_login($ajax = true){
global $CFG;
if($ajax){
$ret = array();
$popup_btn = new stdclass;
$popup_btn->type = 'popup';
$returnurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&repo_id='.$this->id;
$popup_btn->url = google_authsub::login_url($returnurl, google_picasa::REALM);
$ret['login'] = array($popup_btn);
return $ret;
}
}
public function get_listing($path='') {
$picasa = new google_picasa(new google_authsub($this->subauthtoken));
$ret = array();
$ret['dynload'] = true;
$ret['list'] = $picasa->get_file_list($path);
return $ret;
}
public function search($query){
$picasa = new google_picasa(new google_authsub($this->subauthtoken));
$ret = array();
$ret['list'] = $picasa->do_photo_search($query);
return $ret;
}
public function logout(){
global $USER;
$token = google_picasa::get_sesskey($USER->id);
$gauth = new google_authsub($token);
// revoke token from google
$gauth->revoke_session_token();
google_picasa::delete_sesskey($USER->id);
$this->subauthtoken = '';
return parent::logout();
}
public function get_name(){
return get_string('repositoryname', 'repository_picasa');
}
}
// Icon for this plugin retrieved from http://www.iconspedia.com/icon/picasa-2711.html
// Where the license is said documented to be Free