1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Google
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 use Joomla\Registry\Registry;
13
14 /**
15 * Google Picasa data class for the Joomla Platform.
16 *
17 * @since 12.3
18 * @deprecated 4.0 Use the `joomla/google` package via Composer instead
19 */
20 class JGoogleDataPicasaPhoto extends JGoogleData
21 {
22 /**
23 * @var SimpleXMLElement The photo's XML
24 * @since 12.3
25 */
26 protected $xml;
27
28 /**
29 * Constructor.
30 *
31 * @param SimpleXMLElement $xml XML from Google
32 * @param Registry $options Google options object
33 * @param JGoogleAuth $auth Google data http client object
34 *
35 * @since 12.3
36 */
37 public function __construct(SimpleXMLElement $xml, Registry $options = null, JGoogleAuth $auth = null)
38 {
39 $this->xml = $xml;
40
41 parent::__construct($options, $auth);
42
43 if (isset($this->auth) && !$this->auth->getOption('scope'))
44 {
45 $this->auth->setOption('scope', 'https://picasaweb.google.com/data/');
46 }
47 }
48
49 /**
50 * Method to delete a Picasa photo
51 *
52 * @param mixed $match Check for most up to date photo
53 *
54 * @return boolean Success or failure.
55 *
56 * @since 12.3
57 * @throws Exception
58 * @throws RuntimeException
59 * @throws UnexpectedValueException
60 */
61 public function delete($match = '*')
62 {
63 if ($this->isAuthenticated())
64 {
65 $url = $this->getLink();
66
67 if ($match === true)
68 {
69 $match = $this->xml->xpath('./@gd:etag');
70 $match = $match[0];
71 }
72
73 try
74 {
75 $jdata = $this->query($url, null, array('GData-Version' => 2, 'If-Match' => $match), 'delete');
76 }
77 catch (Exception $e)
78 {
79 if (strpos($e->getMessage(), 'Error code 412 received requesting data: Mismatch: etags') === 0)
80 {
81 throw new RuntimeException("Etag match failed: `$match`.", $e->getCode(), $e);
82 }
83
84 throw $e;
85 }
86
87 if ($jdata->body != '')
88 {
89 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
90 }
91
92 $this->xml = null;
93
94 return true;
95 }
96 else
97 {
98 return false;
99 }
100 }
101
102 /**
103 * Method to get the photo link
104 *
105 * @param string $type Type of link to return
106 *
107 * @return string Link or false on failure
108 *
109 * @since 12.3
110 */
111 public function getLink($type = 'edit')
112 {
113 $links = $this->xml->link;
114
115 foreach ($links as $link)
116 {
117 if ($link->attributes()->rel == $type)
118 {
119 return (string) $link->attributes()->href;
120 }
121 }
122
123 return false;
124 }
125
126 /**
127 * Method to get the photo's URL
128 *
129 * @return string Link
130 *
131 * @since 12.3
132 */
133 public function getUrl()
134 {
135 return (string) $this->xml->children()->content->attributes()->src;
136 }
137
138 /**
139 * Method to get the photo's thumbnails
140 *
141 * @return array An array of thumbnails
142 *
143 * @since 12.3
144 */
145 public function getThumbnails()
146 {
147 $thumbs = array();
148
149 foreach ($this->xml->children('media', true)->group->thumbnail as $item)
150 {
151 $url = (string) $item->attributes()->url;
152 $width = (int) $item->attributes()->width;
153 $height = (int) $item->attributes()->height;
154 $thumbs[$width] = array('url' => $url, 'w' => $width, 'h' => $height);
155 }
156
157 return $thumbs;
158 }
159
160 /**
161 * Method to get the title of the photo
162 *
163 * @return string Photo title
164 *
165 * @since 12.3
166 */
167 public function getTitle()
168 {
169 return (string) $this->xml->children()->title;
170 }
171
172 /**
173 * Method to get the summary of the photo
174 *
175 * @return string Photo description
176 *
177 * @since 12.3
178 */
179 public function getSummary()
180 {
181 return (string) $this->xml->children()->summary;
182 }
183
184 /**
185 * Method to get the access level of the photo
186 *
187 * @return string Photo access level
188 *
189 * @since 12.3
190 */
191 public function getAccess()
192 {
193 return (string) $this->xml->children('gphoto', true)->access;
194 }
195
196 /**
197 * Method to get the time of the photo
198 *
199 * @return double Photo time
200 *
201 * @since 12.3
202 */
203 public function getTime()
204 {
205 return (double) $this->xml->children('gphoto', true)->timestamp / 1000;
206 }
207
208 /**
209 * Method to get the size of the photo
210 *
211 * @return int Photo size
212 *
213 * @since 12.3
214 */
215 public function getSize()
216 {
217 return (int) $this->xml->children('gphoto', true)->size;
218 }
219
220 /**
221 * Method to get the height of the photo
222 *
223 * @return int Photo height
224 *
225 * @since 12.3
226 */
227 public function getHeight()
228 {
229 return (int) $this->xml->children('gphoto', true)->height;
230 }
231
232 /**
233 * Method to get the width of the photo
234 *
235 * @return int Photo width
236 *
237 * @since 12.3
238 */
239 public function getWidth()
240 {
241 return (int) $this->xml->children('gphoto', true)->width;
242 }
243
244 /**
245 * Method to set the title of the photo
246 *
247 * @param string $title New photo title
248 *
249 * @return JGoogleDataPicasaPhoto The object for method chaining
250 *
251 * @since 12.3
252 */
253 public function setTitle($title)
254 {
255 $this->xml->children()->title = $title;
256
257 return $this;
258 }
259
260 /**
261 * Method to set the summary of the photo
262 *
263 * @param string $summary New photo description
264 *
265 * @return JGoogleDataPicasaPhoto The object for method chaining
266 *
267 * @since 12.3
268 */
269 public function setSummary($summary)
270 {
271 $this->xml->children()->summary = $summary;
272
273 return $this;
274 }
275
276 /**
277 * Method to set the access level of the photo
278 *
279 * @param string $access New photo access level
280 *
281 * @return JGoogleDataPicasaPhoto The object for method chaining
282 *
283 * @since 12.3
284 */
285 public function setAccess($access)
286 {
287 $this->xml->children('gphoto', true)->access = $access;
288
289 return $this;
290 }
291
292 /**
293 * Method to set the time of the photo
294 *
295 * @param int $time New photo time
296 *
297 * @return JGoogleDataPicasaPhoto The object for method chaining
298 *
299 * @since 12.3
300 */
301 public function setTime($time)
302 {
303 $this->xml->children('gphoto', true)->timestamp = $time * 1000;
304
305 return $this;
306 }
307
308 /**
309 * Method to modify a Picasa Photo
310 *
311 * @param string $match Optional eTag matching parameter
312 *
313 * @return mixed Data from Google.
314 *
315 * @since 12.3
316 */
317 public function save($match = '*')
318 {
319 if ($this->isAuthenticated())
320 {
321 $url = $this->getLink();
322
323 if ($match === true)
324 {
325 $match = $this->xml->xpath('./@gd:etag');
326 $match = $match[0];
327 }
328
329 try
330 {
331 $headers = array('GData-Version' => 2, 'Content-type' => 'application/atom+xml', 'If-Match' => $match);
332 $jdata = $this->query($url, $this->xml->asXml(), $headers, 'put');
333 }
334 catch (Exception $e)
335 {
336 if (strpos($e->getMessage(), 'Error code 412 received requesting data: Mismatch: etags') === 0)
337 {
338 throw new RuntimeException("Etag match failed: `$match`.", $e->getCode(), $e);
339 }
340
341 throw $e;
342 }
343
344 $this->xml = $this->safeXml($jdata->body);
345
346 return $this;
347 }
348 else
349 {
350 return false;
351 }
352 }
353
354 /**
355 * Refresh photo data
356 *
357 * @return mixed Data from Google
358 *
359 * @since 12.3
360 */
361 public function refresh()
362 {
363 if ($this->isAuthenticated())
364 {
365 $url = $this->getLink();
366 $jdata = $this->query($url, null, array('GData-Version' => 2));
367 $this->xml = $this->safeXml($jdata->body);
368
369 return $this;
370 }
371 else
372 {
373 return false;
374 }
375 }
376 }
377