Source for file album.php

Documentation is available at album.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Google
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Google Picasa data class for the Joomla Platform.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Google
  17.  * @since       12.3
  18.  */
  19. {
  20.     /**
  21.      * @var    SimpleXMLElement  The album's XML
  22.      * @since  12.3
  23.      */
  24.     protected $xml;
  25.  
  26.     /**
  27.      * Constructor.
  28.      *
  29.      * @param   SimpleXMLElement  $xml      XML from Google
  30.      * @param   JRegistry         $options  Google options object
  31.      * @param   JGoogleAuth       $auth     Google data http client object
  32.      *
  33.      * @since   12.3
  34.      */
  35.     public function __construct(SimpleXMLElement $xmlJRegistry $options nullJGoogleAuth $auth null)
  36.     {
  37.         $this->xml = $xml;
  38.  
  39.         parent::__construct($options$auth);
  40.  
  41.         if (isset($this->auth&& !$this->auth->getOption('scope'))
  42.         {
  43.             $this->auth->setOption('scope''https://picasaweb.google.com/data/');
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * Method to delete a Picasa album
  49.      *
  50.      * @param   mixed  $match  Check for most up to date album
  51.      *
  52.      * @return  boolean  Success or failure.
  53.      *
  54.      * @since   12.3
  55.      * @throws UnexpectedValueException
  56.      */
  57.     public function delete($match '*')
  58.     {
  59.         if ($this->isAuthenticated())
  60.         {
  61.             $url $this->getLink();
  62.  
  63.             if ($match === true)
  64.             {
  65.                 $match $this->xml->xpath('./@gd:etag');
  66.                 $match $match[0];
  67.             }
  68.  
  69.             try
  70.             {
  71.                 $jdata $this->query($urlnullarray('GData-Version' => 2'If-Match' => $match)'delete');
  72.             }
  73.             catch (Exception $e)
  74.             {
  75.                 if (strpos($e->getMessage()'Error code 412 received requesting data: Mismatch: etags'=== 0)
  76.                 {
  77.                     throw new RuntimeException("Etag match failed: `$match`.");
  78.                 }
  79.                 throw $e;
  80.             }
  81.  
  82.             if ($jdata->body != '')
  83.             {
  84.                 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
  85.             }
  86.             $this->xml = null;
  87.  
  88.             return true;
  89.         }
  90.         else
  91.         {
  92.             return false;
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Method to get the album link
  98.      *
  99.      * @param   string  $type  Type of link to return
  100.      *
  101.      * @return  string  Link or false on failure
  102.      *
  103.      * @since   12.3
  104.      */
  105.     public function getLink($type 'edit')
  106.     {
  107.         $links $this->xml->link;
  108.  
  109.         foreach ($links as $link)
  110.         {
  111.             if ($link->attributes()->rel == $type)
  112.             {
  113.                 return (string) $link->attributes()->href;
  114.             }
  115.         }
  116.         return false;
  117.     }
  118.  
  119.     /**
  120.      * Method to get the title of the album
  121.      *
  122.      * @return  string  Album title
  123.      *
  124.      * @since   12.3
  125.      */
  126.     public function getTitle()
  127.     {
  128.         return (string) $this->xml->children()->title;
  129.     }
  130.  
  131.     /**
  132.      * Method to get the summary of the album
  133.      *
  134.      * @return  string  Album summary
  135.      *
  136.      * @since   12.3
  137.      */
  138.     public function getSummary()
  139.     {
  140.         return (string) $this->xml->children()->summary;
  141.     }
  142.  
  143.     /**
  144.      * Method to get the location of the album
  145.      *
  146.      * @return  string  Album location
  147.      *
  148.      * @since   12.3
  149.      */
  150.     public function getLocation()
  151.     {
  152.         return (string) $this->xml->children('gphoto'true)->location;
  153.     }
  154.  
  155.     /**
  156.      * Method to get the access level of the album
  157.      *
  158.      * @return  string  Album access level
  159.      *
  160.      * @since   12.3
  161.      */
  162.     public function getAccess()
  163.     {
  164.         return (string) $this->xml->children('gphoto'true)->access;
  165.     }
  166.  
  167.     /**
  168.      * Method to get the time of the album
  169.      *
  170.      * @return  double  Album time
  171.      *
  172.      * @since   12.3
  173.      */
  174.     public function getTime()
  175.     {
  176.         return (double) $this->xml->children('gphoto'true)->timestamp 1000;
  177.     }
  178.  
  179.     /**
  180.      * Method to set the title of the album
  181.      *
  182.      * @param   string  $title  New album title
  183.      *
  184.      * @return  JGoogleDataPicasaAlbum  The object for method chaining
  185.      *
  186.      * @since   12.3
  187.      */
  188.     public function setTitle($title)
  189.     {
  190.         $this->xml->children()->title $title;
  191.  
  192.         return $this;
  193.     }
  194.  
  195.     /**
  196.      * Method to set the summary of the album
  197.      *
  198.      * @param   string  $summary  New album summary
  199.      *
  200.      * @return  JGoogleDataPicasaAlbum  The object for method chaining
  201.      *
  202.      * @since   12.3
  203.      */
  204.     public function setSummary($summary)
  205.     {
  206.         $this->xml->children()->summary $summary;
  207.  
  208.         return $this;
  209.     }
  210.  
  211.     /**
  212.      * Method to set the location of the album
  213.      *
  214.      * @param   string  $location  New album location
  215.      *
  216.      * @return  JGoogleDataPicasaAlbum  The object for method chaining
  217.      *
  218.      * @since   12.3
  219.      */
  220.     public function setLocation($location)
  221.     {
  222.         $this->xml->children('gphoto'true)->location $location;
  223.  
  224.         return $this;
  225.     }
  226.  
  227.     /**
  228.      * Method to set the access level of the album
  229.      *
  230.      * @param   string  $access  New album access
  231.      *
  232.      * @return  JGoogleDataPicasaAlbum  The object for method chaining
  233.      *
  234.      * @since   12.3
  235.      */
  236.     public function setAccess($access)
  237.     {
  238.         $this->xml->children('gphoto'true)->access $access;
  239.  
  240.         return $this;
  241.     }
  242.  
  243.     /**
  244.      * Method to set the time of the album
  245.      *
  246.      * @param   int  $time  New album time
  247.      *
  248.      * @return  JGoogleDataPicasaAlbum  The object for method chaining
  249.      *
  250.      * @since   12.3
  251.      */
  252.     public function setTime($time)
  253.     {
  254.         $this->xml->children('gphoto'true)->timestamp $time 1000;
  255.  
  256.         return $this;
  257.     }
  258.  
  259.     /**
  260.      * Method to modify a Picasa Album
  261.      *
  262.      * @param   string  $match  Optional eTag matching parameter
  263.      *
  264.      * @return  mixed  Data from Google.
  265.      *
  266.      * @since   12.3
  267.      */
  268.     public function save($match '*')
  269.     {
  270.         if ($this->isAuthenticated())
  271.         {
  272.             $url $this->getLink();
  273.  
  274.             if ($match === true)
  275.             {
  276.                 $match $this->xml->xpath('./@gd:etag');
  277.                 $match $match[0];
  278.             }
  279.  
  280.             try
  281.             {
  282.                 $headers array('GData-Version' => 2'Content-type' => 'application/atom+xml''If-Match' => $match);
  283.                 $jdata $this->query($url$this->xml->asXML()$headers'put');
  284.             }
  285.             catch (Exception $e)
  286.             {
  287.                 if (strpos($e->getMessage()'Error code 412 received requesting data: Mismatch: etags'=== 0)
  288.                 {
  289.                     throw new RuntimeException("Etag match failed: `$match`.");
  290.                 }
  291.                 throw $e;
  292.             }
  293.  
  294.             $this->xml = $this->safeXML($jdata->body);
  295.  
  296.             return $this;
  297.         }
  298.         else
  299.         {
  300.             return false;
  301.         }
  302.     }
  303.  
  304.     /**
  305.      * Refresh Picasa Album
  306.      *
  307.      * @return  mixed  Data from Google
  308.      *
  309.      * @since   12.3
  310.      * @throws UnexpectedValueException
  311.      */
  312.     public function refresh()
  313.     {
  314.         if ($this->isAuthenticated())
  315.         {
  316.             $url $this->getLink();
  317.             $jdata $this->query($urlnullarray('GData-Version' => 2));
  318.             $this->xml = $this->safeXML($jdata->body);
  319.  
  320.             return $this;
  321.         }
  322.         else
  323.         {
  324.             return false;
  325.         }
  326.     }
  327.  
  328.     /**
  329.      * Method to retrieve a list of Picasa Photos
  330.      *
  331.      * @return  mixed  Data from Google
  332.      *
  333.      * @since   12.3
  334.      * @throws UnexpectedValueException
  335.      */
  336.     public function listPhotos()
  337.     {
  338.         if ($this->isAuthenticated())
  339.         {
  340.             $url $this->getLink('http://schemas.google.com/g/2005#feed');
  341.             $jdata $this->query($urlnullarray('GData-Version' => 2));
  342.             $xml $this->safeXML($jdata->body);
  343.  
  344.             if (isset($xml->children()->entry))
  345.             {
  346.                 $items array();
  347.  
  348.                 foreach ($xml->children()->entry as $item)
  349.                 {
  350.                     $items[new JGoogleDataPicasaPhoto($item$this->options$this->auth);
  351.                 }
  352.                 return $items;
  353.             }
  354.             else
  355.             {
  356.                 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
  357.             }
  358.         }
  359.         else
  360.         {
  361.             return false;
  362.         }
  363.     }
  364.  
  365.     /**
  366.      * Add photo
  367.      *
  368.      * @param   string  $file     Path of file to upload
  369.      * @param   string  $title    Title to give to file (defaults to filename)
  370.      * @param   string  $summary  Description of the file
  371.      *
  372.      * @return  mixed  Data from Google
  373.      *
  374.      * @since   12.3
  375.      * @throws  RuntimeException
  376.      */
  377.     public function upload($file$title ''$summary '')
  378.     {
  379.         if ($this->isAuthenticated())
  380.         {
  381.             jimport('joomla.filesystem.file');
  382.             $title $title != '' $title JFile::getName($file);
  383.  
  384.             if (!($type $this->getMIME($file)))
  385.             {
  386.                 throw new RuntimeException("Inappropriate file type.");
  387.             }
  388.             if (!($data JFile::read($file)))
  389.             {
  390.                 throw new RuntimeException("Cannot access file: `$file`");
  391.             }
  392.  
  393.             $xml new SimpleXMLElement('<entry></entry>');
  394.             $xml->addAttribute('xmlns''http://www.w3.org/2005/Atom');
  395.             $xml->addChild('title'$title);
  396.             $xml->addChild('summary'$summary);
  397.             $cat $xml->addChild('category''');
  398.             $cat->addAttribute('scheme''http://schemas.google.com/g/2005#kind');
  399.             $cat->addAttribute('term''http://schemas.google.com/photos/2007#photo');
  400.  
  401.             $post "Media multipart posting\n";
  402.             $post .= "--END_OF_PART\n";
  403.             $post .= "Content-Type: application/atom+xml\n\n";
  404.             $post .= $xml->asXML("\n";
  405.             $post .= "--END_OF_PART\n";
  406.             $post .= "Content-Type: {$type}\n\n";
  407.             $post .= $data;
  408.  
  409.             $jdata $this->query($this->getLink()$postarray('GData-Version' => 2'Content-Type: multipart/related')'post');
  410.  
  411.             return new JGoogleDataPicasaPhoto($this->safeXML($jdata->body)$this->options$this->auth);
  412.         }
  413.         else
  414.         {
  415.             return false;
  416.         }
  417.     }
  418.  
  419.     /**
  420.      * Add photo
  421.      *
  422.      * @param   string  $file  Filename
  423.      *
  424.      * @return  mixed  Data from Google
  425.      *
  426.      * @since   12.3
  427.      * @throws UnexpectedValueException
  428.      */
  429.     protected function getMIME($file)
  430.     {
  431.         switch (strtolower(JFile::getExt($file)))
  432.         {
  433.             case 'bmp':
  434.             case 'bm':
  435.             return 'image/bmp';
  436.             case 'gif':
  437.             return 'image/gif';
  438.             case 'jpg':
  439.             case 'jpeg':
  440.             case 'jpe':
  441.             case 'jif':
  442.             case 'jfif':
  443.             case 'jfi':
  444.             return 'image/jpeg';
  445.             case 'png':
  446.             return 'image/png';
  447.             case '3gp':
  448.             return 'video/3gpp';
  449.             case 'avi':
  450.             return 'video/avi';
  451.             case 'mov':
  452.             case 'moov':
  453.             case 'qt':
  454.             return 'video/quicktime';
  455.             case 'mp4':
  456.             case 'm4a':
  457.             case 'm4p':
  458.             case 'm4b':
  459.             case 'm4r':
  460.             case 'm4v':
  461.             return 'video/mp4';
  462.             case 'mpg':
  463.             case 'mpeg':
  464.             case 'mp1':
  465.             case 'mp2':
  466.             case 'mp3':
  467.             case 'm1v':
  468.             case 'm1a':
  469.             case 'm2a':
  470.             case 'mpa':
  471.             case 'mpv':
  472.             return 'video/mpeg';
  473.             case 'asf':
  474.             return 'video/x-ms-asf';
  475.             case 'wmv':
  476.             return 'video/x-ms-wmv';
  477.             default:
  478.             return false;
  479.         }
  480.     }
  481. }

Documentation generated on Tue, 19 Nov 2013 14:53:40 +0100 by phpDocumentor 1.4.3