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