}
}
-/**
- * Class for manipulating picasa through the google data api.
- *
- * Docs for this can be found here:
- * {@link http://code.google.com/apis/picasaweb/developers_guide_protocol.html}
- *
- * @package core
- * @copyright Dan Poltawski <talktodan@gmail.com>
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class google_picasa {
- /** @var string Realm for authentication */
- const REALM = 'http://picasaweb.google.com/data/';
- /** @var string Upload url */
- const UPLOAD_LOCATION = 'https://picasaweb.google.com/data/feed/api/user/default/albumid/default';
- /** @var string photo list url */
- const ALBUM_PHOTO_LIST = 'https://picasaweb.google.com/data/feed/api/user/default/albumid/';
- /** @var string search url */
- const PHOTO_SEARCH_URL = 'https://picasaweb.google.com/data/feed/api/user/default?kind=photo&q=';
- /** @var string album list url */
- const LIST_ALBUMS_URL = 'https://picasaweb.google.com/data/feed/api/user/default';
- /** @var string manage files url */
- const MANAGE_URL = 'http://picasaweb.google.com/';
-
- /** @var google_oauth oauth curl class for making authenticated requests */
- private $googleoauth = null;
- /** @var string Last album name retrievied */
- private $lastalbumname = null;
-
- /**
- * Constructor.
- *
- * @param google_oauth $googleoauth oauth curl class for making authenticated requests
- */
- public function __construct(google_oauth $googleoauth) {
- $this->googleoauth = $googleoauth;
- $this->googleoauth->setHeader('GData-Version: 2');
- }
-
- /**
- * Sends a file object to picasaweb
- *
- * @param object $file File object
- * @return boolean True on success
- */
- public function send_file($file) {
- $this->googleoauth->setHeader("Content-Length: ". $file->get_filesize());
- $this->googleoauth->setHeader("Content-Type: ". $file->get_mimetype());
- $this->googleoauth->setHeader("Slug: ". $file->get_filename());
-
- $this->googleoauth->post(self::UPLOAD_LOCATION, $file->get_content());
-
- if ($this->googleoauth->info['http_code'] === 201) {
- return true;
- } else {
- 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->googleoauth->get(self::ALBUM_PHOTO_LIST.$albumid);
-
- return $this->get_photo_details($albumcontent);
- }
-
- /**
- * Returns the name of the album for which get_photo_details was called last time.
- *
- * @return string
- */
- public function get_last_album_name() {
- return $this->lastalbumname;
- }
-
- /**
- * 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->googleoauth->get(self::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() {
- $files = array();
- $content = $this->googleoauth->get(self::LIST_ALBUMS_URL);
-
- try {
- if (strpos($content, '<?xml') !== 0) {
- throw new moodle_exception('invalidxmlresponse');
- }
- $xml = new SimpleXMLElement($content);
- } catch (Exception $e) {
- // An error occured while trying to parse the XML, let's just return nothing. SimpleXML does not
- // return a more specific Exception, that's why the global Exception class is caught here.
- return $files;
- }
-
- 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) $album->title,
- 'date' => userdate($gphoto->timestamp),
- 'size' => (int) $gphoto->bytesUsed,
- 'path' => (string) $gphoto->id,
- 'thumbnail' => (string) $thumbnailinfo['url'],
- 'thumbnail_width' => 160, // 160 is the native maximum dimension.
- 'thumbnail_height' => 160,
- '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) {
- $files = array();
-
- try {
- if (strpos($rawxml, '<?xml') !== 0) {
- throw new moodle_exception('invalidxmlresponse');
- }
- $xml = new SimpleXMLElement($rawxml);
- } catch (Exception $e) {
- // An error occured while trying to parse the XML, let's just return nothing. SimpleXML does not
- // return a more specific Exception, that's why the global Exception class is caught here.
- return $files;
- }
- $this->lastalbumname = (string)$xml->title;
-
- 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();
-
- // Derive the nicest file name we can.
- if (!empty($mediainfo->group->description)) {
- $title = shorten_text((string)$mediainfo->group->description, 20, false, '');
- $title = clean_filename($title).'.jpg';
- } else {
- $title = (string)$mediainfo->group->title;
- }
-
- $files[] = array(
- 'title' => $title,
- 'date' => userdate($gphoto->timestamp),
- 'size' => (int) $gphoto->size,
- 'path' => $gphoto->albumid.'/'.$gphoto->id,
- 'thumbnail' => (string) $thumbnailinfo['url'],
- 'thumbnail_width' => 72, // 72 is the native maximum dimension.
- 'thumbnail_height' => 72,
- 'source' => (string) $fullinfo['url'],
- 'url' => (string) $fullinfo['url']
- );
- }
-
- return $files;
- }
-}
-
/**
* OAuth 2.0 client for Google Services
*