1 | <?php |
---|
2 | # ***** BEGIN LICENSE BLOCK ***** |
---|
3 | # This file is part of Pixearch |
---|
4 | # Copyright (c) 2008 Hadrien Lanneau http://www.alti.info/ |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # Pixearch is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation; either version 2 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # Pixearch is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with Pixearch; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
20 | # |
---|
21 | # ***** END LICENSE BLOCK ***** |
---|
22 | # |
---|
23 | |
---|
24 | /** |
---|
25 | * PictWebService |
---|
26 | */ |
---|
27 | abstract class Pixearch |
---|
28 | { |
---|
29 | /** |
---|
30 | * Webservice URI |
---|
31 | * |
---|
32 | * @var string |
---|
33 | **/ |
---|
34 | protected $wsuri; |
---|
35 | |
---|
36 | /** |
---|
37 | * Api key |
---|
38 | * |
---|
39 | * @var string |
---|
40 | **/ |
---|
41 | protected $apikey; |
---|
42 | |
---|
43 | /** |
---|
44 | * Number of search pages |
---|
45 | * |
---|
46 | * @var integer |
---|
47 | **/ |
---|
48 | public $nbElements = 0; |
---|
49 | |
---|
50 | /** |
---|
51 | * Prefix for cached files |
---|
52 | * |
---|
53 | * @var string |
---|
54 | **/ |
---|
55 | protected $_cachePrefix = ''; |
---|
56 | |
---|
57 | /** |
---|
58 | * Singleton |
---|
59 | * |
---|
60 | * @var Pixearch object |
---|
61 | **/ |
---|
62 | protected static $singleton = null; |
---|
63 | |
---|
64 | /** |
---|
65 | * Open a connection to Flickr |
---|
66 | * |
---|
67 | * @param {string} $apikey API key |
---|
68 | * @return void |
---|
69 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
70 | **/ |
---|
71 | public function __construct($apikey = null) |
---|
72 | { |
---|
73 | if (is_null(self::$singleton)) |
---|
74 | { |
---|
75 | $this->apikey = $apikey; |
---|
76 | |
---|
77 | self::$singleton = $this; |
---|
78 | } |
---|
79 | else |
---|
80 | { |
---|
81 | $this->apikey = self::$singleton->_getApiKey(); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Get API Key |
---|
87 | * |
---|
88 | * @return string |
---|
89 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
90 | **/ |
---|
91 | public function _getApiKey() |
---|
92 | { |
---|
93 | return $this->apikey; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Cache last search results to easily retrieve selected photo |
---|
98 | * |
---|
99 | * @return Boolean |
---|
100 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
101 | **/ |
---|
102 | protected function _setCache( |
---|
103 | $type = 'search', $xml = null) |
---|
104 | { |
---|
105 | if (!is_string($type)) |
---|
106 | { |
---|
107 | throw new Exception('Type must be a string'); |
---|
108 | } |
---|
109 | $cacheFolder = dirname(__FILE__) . '/../cache'; |
---|
110 | |
---|
111 | if (!file_exists($cacheFolder)) |
---|
112 | { |
---|
113 | mkdir($cacheFolder); |
---|
114 | } |
---|
115 | |
---|
116 | return file_put_contents( |
---|
117 | $cacheFolder . '/' . $this->_cachePrefix . $type, |
---|
118 | serialize($xml) |
---|
119 | ); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Get last cached result |
---|
124 | * |
---|
125 | * @return simple_xml Object |
---|
126 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
127 | **/ |
---|
128 | protected function _getCache($type = 'search') |
---|
129 | { |
---|
130 | if (!is_string($type)) |
---|
131 | { |
---|
132 | throw new Exception('Type must be a string'); |
---|
133 | } |
---|
134 | $cacheFile = dirname(__FILE__) . '/../cache/' . |
---|
135 | $this->_cachePrefix . $type; |
---|
136 | |
---|
137 | return unserialize( |
---|
138 | @file_get_contents( |
---|
139 | $cacheFile |
---|
140 | ) |
---|
141 | ); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | class PxSFlickr extends Pixearch |
---|
146 | { |
---|
147 | /** |
---|
148 | * Flickr webservice URI |
---|
149 | * |
---|
150 | * @var string |
---|
151 | **/ |
---|
152 | protected $wsuri = 'http://api.flickr.com/services/rest/'; |
---|
153 | |
---|
154 | /** |
---|
155 | * Search pictures from keyword |
---|
156 | * |
---|
157 | * @param {string} $keyword Keywords to search |
---|
158 | * @param {integer} $offset Page number |
---|
159 | * @param {integer} $limit Number of items per page |
---|
160 | * @return ArrayObject of PxSFlickrPicture objects |
---|
161 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
162 | **/ |
---|
163 | public function search( |
---|
164 | $keyword = '', |
---|
165 | $offset = 1, |
---|
166 | $limit = 20) |
---|
167 | { |
---|
168 | if (!is_string($keyword)) |
---|
169 | { |
---|
170 | throw new Exception('Keyword must be a string'); |
---|
171 | } |
---|
172 | if (trim($keyword) == '') |
---|
173 | { |
---|
174 | return new ArrayObject(); |
---|
175 | } |
---|
176 | |
---|
177 | // do request |
---|
178 | $picturesRaw = $this->callMethod( |
---|
179 | 'flickr.photos.search', |
---|
180 | array( |
---|
181 | 'text' => urlencode($keyword), |
---|
182 | 'per_page' => $limit, |
---|
183 | 'page' => $offset, |
---|
184 | 'privacy_filter' => 1, |
---|
185 | 'extras' => 'owner_name', |
---|
186 | 'sort' => 'relevance' |
---|
187 | ) |
---|
188 | ); |
---|
189 | return $this->_createPictures( |
---|
190 | $picturesRaw |
---|
191 | ); |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * Call a method |
---|
196 | * |
---|
197 | * @return xml |
---|
198 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
199 | **/ |
---|
200 | public function callMethod( |
---|
201 | $method = null, |
---|
202 | $params = array()) |
---|
203 | { |
---|
204 | $paramsstring = ''; |
---|
205 | foreach ($params as $k => $p) |
---|
206 | { |
---|
207 | $paramsstring .= '&' . $k . '=' . $p; |
---|
208 | } |
---|
209 | |
---|
210 | $xml = HttpClient::quickGet( |
---|
211 | $this->wsuri . '?method=' . $method . |
---|
212 | '&api_key=' . $this->apikey . $paramsstring |
---|
213 | ); |
---|
214 | if (!$xml) |
---|
215 | { |
---|
216 | throw new Exception('Connexion lost'); |
---|
217 | } |
---|
218 | |
---|
219 | return simplexml_load_string( |
---|
220 | $xml |
---|
221 | ); |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * Transform an xml from Flickr to an object that we can use |
---|
226 | * |
---|
227 | * @param {string} $xml XML response from Flickr webservice |
---|
228 | * @return ArrayObject of PxSFlickrPicture objects |
---|
229 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
230 | **/ |
---|
231 | private function _createPictures($xml = null) |
---|
232 | { |
---|
233 | if (!$xml) |
---|
234 | { |
---|
235 | throw new Exception('Invalid XML'); |
---|
236 | } |
---|
237 | if (isset($xml->err['msg'])) |
---|
238 | { |
---|
239 | throw new Exception($xml->err['msg']); |
---|
240 | } |
---|
241 | if (!is_object($xml)) |
---|
242 | { |
---|
243 | throw new Exception('Connexion lost'); |
---|
244 | } |
---|
245 | |
---|
246 | $this->nbElements = intval( |
---|
247 | $xml->photos['total'] |
---|
248 | ); |
---|
249 | |
---|
250 | $pictures = new ArrayObject(); |
---|
251 | |
---|
252 | foreach ($xml->photos->photo as $p) |
---|
253 | { |
---|
254 | $pictures[] = new PxSFlickrPicture( |
---|
255 | $p['id'], |
---|
256 | $p['title'], |
---|
257 | $p['farm'], |
---|
258 | $p['server'], |
---|
259 | $p['secret'], |
---|
260 | $p['owner'], |
---|
261 | $p['ownername'] |
---|
262 | ); |
---|
263 | } |
---|
264 | |
---|
265 | return $pictures; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * Photobucket webservice |
---|
271 | */ |
---|
272 | class PxSPhotoBucket extends Pixearch |
---|
273 | { |
---|
274 | /** |
---|
275 | * PhotoBucket webservice URI |
---|
276 | * |
---|
277 | * @var string |
---|
278 | **/ |
---|
279 | protected $wsuri = 'http://api.photobucket.com/'; |
---|
280 | |
---|
281 | /** |
---|
282 | * Signature key |
---|
283 | * |
---|
284 | * @var string |
---|
285 | **/ |
---|
286 | protected $signature; |
---|
287 | |
---|
288 | /** |
---|
289 | * Open a connection to Flickr |
---|
290 | * |
---|
291 | * @param {string} $apikey API key |
---|
292 | * @return void |
---|
293 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
294 | **/ |
---|
295 | public function __construct( |
---|
296 | $apikey = null, |
---|
297 | $signature = null) |
---|
298 | { |
---|
299 | if (is_null(self::$singleton)) |
---|
300 | { |
---|
301 | $this->apikey = $apikey; |
---|
302 | |
---|
303 | $this->signature = $signature; |
---|
304 | |
---|
305 | self::$singleton = $this; |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | $this->apikey = self::$singleton->apikey; |
---|
310 | $this->signature = self::$singleton->signature; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | /** |
---|
315 | * Search pictures from keyword |
---|
316 | * |
---|
317 | * @param {string} $keyword Keywords to search |
---|
318 | * @param {integer} $offset Page number |
---|
319 | * @param {integer} $limit Number of items per page |
---|
320 | * @return ArrayObject of PxSPhotoBucketPicture objects |
---|
321 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
322 | **/ |
---|
323 | public function search( |
---|
324 | $keyword = '', |
---|
325 | $offset = 1, |
---|
326 | $limit = 20) |
---|
327 | { |
---|
328 | if (!is_string($keyword)) |
---|
329 | { |
---|
330 | throw new Exception('Keyword must be a string'); |
---|
331 | } |
---|
332 | if (trim($keyword) == '') |
---|
333 | { |
---|
334 | return new ArrayObject(); |
---|
335 | } |
---|
336 | |
---|
337 | // do request |
---|
338 | $xml = $this->callMethod( |
---|
339 | 'search/' . urlencode($keyword) . '/image', |
---|
340 | array( |
---|
341 | 'format' => 'xml', |
---|
342 | 'perpage' => $limit, |
---|
343 | 'page' => $offset |
---|
344 | ) |
---|
345 | ); |
---|
346 | |
---|
347 | return $this->_createPictures( |
---|
348 | $xml |
---|
349 | ); |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | * Call a method |
---|
354 | * |
---|
355 | * @return simple_xml object |
---|
356 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
357 | **/ |
---|
358 | public function callMethod( |
---|
359 | $method = '', |
---|
360 | $params = array() |
---|
361 | ) |
---|
362 | { |
---|
363 | $xml = HttpClient::quickGet( |
---|
364 | $this->_constructRequest( |
---|
365 | $method, |
---|
366 | $params |
---|
367 | ) |
---|
368 | ); |
---|
369 | if (!xml) |
---|
370 | { |
---|
371 | throw new Exception('Connexion lost'); |
---|
372 | } |
---|
373 | return simplexml_load_string( |
---|
374 | $xml |
---|
375 | ); |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | * Construct an url with oauth signature |
---|
380 | * |
---|
381 | * @return string |
---|
382 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
383 | **/ |
---|
384 | private function _constructRequest($method, $params = '') |
---|
385 | { |
---|
386 | include_once(dirname(__FILE__) . '/OAuth.php'); |
---|
387 | $token = new OAuthToken( |
---|
388 | $this->apikey, |
---|
389 | $this->signaturekey |
---|
390 | ); |
---|
391 | $consumer = new OAuthConsumer( |
---|
392 | $this->apikey, |
---|
393 | $this->signature |
---|
394 | ); |
---|
395 | $request = OAuthRequest::from_consumer_and_token( |
---|
396 | $consumer, |
---|
397 | null, |
---|
398 | 'GET', |
---|
399 | $this->wsuri . $method, |
---|
400 | $params |
---|
401 | ); |
---|
402 | $request->set_parameter( |
---|
403 | 'oauth_signature_method', |
---|
404 | 'HMAC-SHA1' |
---|
405 | ); |
---|
406 | $request->sign_request( |
---|
407 | new OAuthSignatureMethod_HMAC_SHA1(), |
---|
408 | $consumer, |
---|
409 | null |
---|
410 | ); |
---|
411 | |
---|
412 | return $request->to_url(); |
---|
413 | } |
---|
414 | |
---|
415 | /** |
---|
416 | * Transform an xml from PhotoBucket to an object that we can use |
---|
417 | * |
---|
418 | * @param {string} $xml XML response from PhotoBucket webservice |
---|
419 | * @return ArrayObject of PxSPhotoBucketPicture objects |
---|
420 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
421 | **/ |
---|
422 | private function _createPictures($xml = null) |
---|
423 | { |
---|
424 | if (!is_object($xml)) |
---|
425 | { |
---|
426 | throw new Exception('Connexion lost'); |
---|
427 | } |
---|
428 | if (isset($xml->err['msg'])) |
---|
429 | { |
---|
430 | throw new Exception($xml->err['msg']); |
---|
431 | } |
---|
432 | |
---|
433 | $this->nbElements = intval( |
---|
434 | $xml->content->result['totalresults'] |
---|
435 | ); |
---|
436 | |
---|
437 | $pictures = new ArrayObject(); |
---|
438 | |
---|
439 | foreach ($xml->content->result->primary->media as $p) |
---|
440 | { |
---|
441 | $pictures[] = new PxSPhotoBucketPicture( |
---|
442 | strval($p->url), |
---|
443 | strval($p->title), |
---|
444 | strval($p->thumb), |
---|
445 | strval($p->browseurl), |
---|
446 | strval($p['username']), |
---|
447 | strval($p->albumurl) |
---|
448 | ); |
---|
449 | } |
---|
450 | |
---|
451 | return $pictures; |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | /** |
---|
456 | * DeviantArt |
---|
457 | */ |
---|
458 | class PxSDeviantArt extends Pixearch |
---|
459 | { |
---|
460 | /** |
---|
461 | * Webservices uri |
---|
462 | * |
---|
463 | * @var string |
---|
464 | **/ |
---|
465 | protected $wsuri = 'http://backend.deviantart.com/rss.xml?type=deviation&q='; |
---|
466 | |
---|
467 | /** |
---|
468 | * Prefix for cached files |
---|
469 | * |
---|
470 | * @var string |
---|
471 | **/ |
---|
472 | protected $_cachePrefix = 'dA_'; |
---|
473 | |
---|
474 | function __construct() |
---|
475 | { |
---|
476 | |
---|
477 | } |
---|
478 | |
---|
479 | /** |
---|
480 | * Search keyword |
---|
481 | * |
---|
482 | * @return PxSDeviantArtPictures Objects |
---|
483 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
484 | **/ |
---|
485 | public function search( |
---|
486 | $keyword = '', |
---|
487 | $offset = 1, |
---|
488 | $limit = 20) |
---|
489 | { |
---|
490 | if (!is_string($keyword)) |
---|
491 | { |
---|
492 | throw new Exception('Keyword must be a string'); |
---|
493 | } |
---|
494 | if (trim($keyword) == '') |
---|
495 | { |
---|
496 | return new ArrayObject(); |
---|
497 | } |
---|
498 | |
---|
499 | $rss = $this->callMethod( |
---|
500 | 'search', |
---|
501 | array( |
---|
502 | 'type' => 'deviation', |
---|
503 | 'offset' => $offset, |
---|
504 | 'limit' => $limit, |
---|
505 | 'keyword' => $keyword |
---|
506 | ) |
---|
507 | ); |
---|
508 | |
---|
509 | if (!is_object($rss)) |
---|
510 | { |
---|
511 | throw new Exception('Connexion lost'); |
---|
512 | } |
---|
513 | |
---|
514 | $pictures = new ArrayObject(); |
---|
515 | foreach ($rss->channel->item as $i) |
---|
516 | { |
---|
517 | $pictures[] = new PxSDeviantArtPicture( |
---|
518 | strval($i->guid), // ID |
---|
519 | strval($i->media_title), // title |
---|
520 | strval($i->media_thumbnail[1]['url']), // thumb |
---|
521 | strval($i->media_thumbnail[0]['url']), // medium |
---|
522 | strval($i->media_content[0]['url']), // original |
---|
523 | strval($i->media_content[1]['url']), // browserurl |
---|
524 | strval($i->media_credit[0]), // ownername |
---|
525 | strval($i->media_copyright['url']) // owneruri |
---|
526 | ); |
---|
527 | } |
---|
528 | |
---|
529 | if ($pictures->count() == $limit) |
---|
530 | { |
---|
531 | $this->nbElements = floatval(($limit + 1) * $offset); |
---|
532 | } |
---|
533 | |
---|
534 | |
---|
535 | $this->_setCache( |
---|
536 | 'search', |
---|
537 | $pictures |
---|
538 | ); |
---|
539 | |
---|
540 | return $pictures; |
---|
541 | } |
---|
542 | |
---|
543 | /** |
---|
544 | * Call a method |
---|
545 | * |
---|
546 | * @return simplexml_load_string |
---|
547 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
548 | **/ |
---|
549 | public function callMethod( |
---|
550 | $method = null, |
---|
551 | $params = array()) |
---|
552 | { |
---|
553 | switch ($method) |
---|
554 | { |
---|
555 | case 'search': |
---|
556 | $paramsstring = ''; |
---|
557 | foreach ($params as $k => $p) |
---|
558 | { |
---|
559 | if ($k == 'keyword') |
---|
560 | { |
---|
561 | $keyword = $p; |
---|
562 | } |
---|
563 | else |
---|
564 | { |
---|
565 | $paramsstring .= '&' . $k . '=' . $p; |
---|
566 | } |
---|
567 | } |
---|
568 | |
---|
569 | return simplexml_load_string( |
---|
570 | str_replace( |
---|
571 | array( |
---|
572 | 'media:title', |
---|
573 | 'media:thumbnail', |
---|
574 | 'media:content', |
---|
575 | 'media:credit', |
---|
576 | 'media:copyright' |
---|
577 | ), |
---|
578 | array( |
---|
579 | 'media_title', |
---|
580 | 'media_thumbnail', |
---|
581 | 'media_content', |
---|
582 | 'media_credit', |
---|
583 | 'media_copyright' |
---|
584 | ), |
---|
585 | HttpClient::quickGet( |
---|
586 | $this->wsuri . 'boost%3Apopular%20' . |
---|
587 | $keyword . |
---|
588 | $paramsstring |
---|
589 | ) |
---|
590 | ) |
---|
591 | ); |
---|
592 | break; |
---|
593 | case 'getFromId': |
---|
594 | if (!isset($params['id'])) |
---|
595 | { |
---|
596 | throw new Exception('ID is needed'); |
---|
597 | } |
---|
598 | $lastSelected = $this->_getCache( |
---|
599 | 'selected' |
---|
600 | ); |
---|
601 | if (urldecode($lastSelected->id) == $params['id']) |
---|
602 | { |
---|
603 | return $lastSelected; |
---|
604 | } |
---|
605 | $lastSearch = $this->_getCache( |
---|
606 | 'search' |
---|
607 | ); |
---|
608 | foreach ($lastSearch as $p) |
---|
609 | { |
---|
610 | if (urldecode($p->id) == $params['id']) |
---|
611 | { |
---|
612 | $this->_setCache( |
---|
613 | 'selected', |
---|
614 | $p |
---|
615 | ); |
---|
616 | return $p; |
---|
617 | } |
---|
618 | } |
---|
619 | throw new Exception('Bad image'); |
---|
620 | break; |
---|
621 | default: |
---|
622 | throw new Exception('Unknown method'); |
---|
623 | } |
---|
624 | } |
---|
625 | } |
---|
626 | |
---|
627 | /** |
---|
628 | * PxSPicasa |
---|
629 | */ |
---|
630 | class PxSPicasa extends Pixearch |
---|
631 | { |
---|
632 | /** |
---|
633 | * Picasa webservice URI |
---|
634 | * |
---|
635 | * @var string |
---|
636 | **/ |
---|
637 | protected $wsuri = |
---|
638 | 'http://picasaweb.google.com/data/feed/api/all?kind=photo'; |
---|
639 | |
---|
640 | /** |
---|
641 | * Prefix for cached files |
---|
642 | * |
---|
643 | * @var string |
---|
644 | **/ |
---|
645 | protected $_cachePrefix = 'Pcs_'; |
---|
646 | |
---|
647 | function __construct() |
---|
648 | { |
---|
649 | |
---|
650 | } |
---|
651 | |
---|
652 | /** |
---|
653 | * Search pictures from keyword |
---|
654 | * |
---|
655 | * @param {string} $keyword Keywords to search |
---|
656 | * @param {integer} $offset Page number |
---|
657 | * @param {integer} $limit Number of items per page |
---|
658 | * @return ArrayObject of PxSPicasaPicture objects |
---|
659 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
660 | **/ |
---|
661 | public function search( |
---|
662 | $keyword = '', |
---|
663 | $offset = 1, |
---|
664 | $limit = 20) |
---|
665 | { |
---|
666 | if (!is_string($keyword)) |
---|
667 | { |
---|
668 | throw new Exception('Keyword must be a string'); |
---|
669 | } |
---|
670 | if (trim($keyword) == '') |
---|
671 | { |
---|
672 | return new ArrayObject(); |
---|
673 | } |
---|
674 | |
---|
675 | // do request |
---|
676 | $rss = $this->callMethod( |
---|
677 | 'search', |
---|
678 | array( |
---|
679 | 'q' => urlencode($keyword), |
---|
680 | 'max-results' => $limit, |
---|
681 | 'start-index' => $offset, |
---|
682 | 'thumbsize' => 64, |
---|
683 | 'imgmax' => 640 |
---|
684 | |
---|
685 | ) |
---|
686 | ); |
---|
687 | |
---|
688 | if (!is_object($rss)) |
---|
689 | { |
---|
690 | throw new Exception('Connexion lost'); |
---|
691 | } |
---|
692 | |
---|
693 | $pictures = new ArrayObject(); |
---|
694 | foreach ($rss->entry as $p) |
---|
695 | { |
---|
696 | $pictures[] = new PxSPicasaPicture( |
---|
697 | strval($p->id), // ID |
---|
698 | strval($p->title), // title |
---|
699 | strval($p->media_group->media_thumbnail['url']), // thumb |
---|
700 | strval($p->media_group->media_thumbnail['url']), // medium |
---|
701 | strval($p->media_group->media_content['url']), // original |
---|
702 | strval($p->link[1]['href']), // browserurl |
---|
703 | strval($p->author->name), // ownername |
---|
704 | strval($p->author->uri) // owneruri |
---|
705 | ); |
---|
706 | } |
---|
707 | |
---|
708 | $this->nbElements = intval($rss->openSearch_totalResults); |
---|
709 | |
---|
710 | return $pictures; |
---|
711 | } |
---|
712 | |
---|
713 | /** |
---|
714 | * Call a method |
---|
715 | * |
---|
716 | * @return xml |
---|
717 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
718 | **/ |
---|
719 | public function callMethod( |
---|
720 | $method = null, |
---|
721 | $params = array()) |
---|
722 | { |
---|
723 | switch ($method) |
---|
724 | { |
---|
725 | case 'search': |
---|
726 | $paramsstring = ''; |
---|
727 | foreach ($params as $k => $p) |
---|
728 | { |
---|
729 | $paramsstring .= '&' . $k . '=' . urlencode($p); |
---|
730 | } |
---|
731 | return simplexml_load_string( |
---|
732 | str_replace( |
---|
733 | array( |
---|
734 | 'openSearch:totalResults', |
---|
735 | 'media:group', |
---|
736 | 'media:content', |
---|
737 | 'media:thumbnail' |
---|
738 | ), |
---|
739 | array( |
---|
740 | 'openSearch_totalResults', |
---|
741 | 'media_group', |
---|
742 | 'media_content', |
---|
743 | 'media_thumbnail' |
---|
744 | ), |
---|
745 | HttpClient::quickGet( |
---|
746 | $this->wsuri . |
---|
747 | $paramsstring |
---|
748 | ) |
---|
749 | ) |
---|
750 | ); |
---|
751 | break; |
---|
752 | case 'getFromId': |
---|
753 | if (!isset($params['id'])) |
---|
754 | { |
---|
755 | throw new Exception('ID is needed'); |
---|
756 | } |
---|
757 | return simplexml_load_string( |
---|
758 | str_replace( |
---|
759 | array( |
---|
760 | 'media:title', |
---|
761 | 'media:thumbnail', |
---|
762 | 'media:group', |
---|
763 | 'media:content', |
---|
764 | 'media:credit', |
---|
765 | 'media:copyright' |
---|
766 | ), |
---|
767 | array( |
---|
768 | 'media_title', |
---|
769 | 'media_thumbnail', |
---|
770 | 'media_group', |
---|
771 | 'media_content', |
---|
772 | 'media_credit', |
---|
773 | 'media_copyright' |
---|
774 | ), |
---|
775 | HttpClient::quickGet( |
---|
776 | $params['id'] |
---|
777 | ) |
---|
778 | ) |
---|
779 | ); |
---|
780 | break; |
---|
781 | default: |
---|
782 | throw new Exception('Unknown method'); |
---|
783 | } |
---|
784 | } |
---|
785 | |
---|
786 | /** |
---|
787 | * Transform an xml from Flickr to an object that we can use |
---|
788 | * |
---|
789 | * @param {string} $xml XML response from Flickr webservice |
---|
790 | * @return ArrayObject of PxSFlickrPicture objects |
---|
791 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
792 | **/ |
---|
793 | private function _createPictures($xml = null) |
---|
794 | { |
---|
795 | if (!$xml) |
---|
796 | { |
---|
797 | throw new Exception('Invalid XML'); |
---|
798 | } |
---|
799 | if (isset($xml->err['msg'])) |
---|
800 | { |
---|
801 | throw new Exception($xml->err['msg']); |
---|
802 | } |
---|
803 | if (!is_object($xml)) |
---|
804 | { |
---|
805 | throw new Exception('Connexion lost'); |
---|
806 | } |
---|
807 | |
---|
808 | $this->nbElements = intval( |
---|
809 | $xml->photos['total'] |
---|
810 | ); |
---|
811 | |
---|
812 | $pictures = new ArrayObject(); |
---|
813 | |
---|
814 | foreach ($xml->photos->photo as $p) |
---|
815 | { |
---|
816 | $pictures[] = new PxSFlickrPicture( |
---|
817 | $p['id'], |
---|
818 | $p['title'], |
---|
819 | $p['farm'], |
---|
820 | $p['server'], |
---|
821 | $p['secret'], |
---|
822 | $p['owner'], |
---|
823 | $p['ownername'] |
---|
824 | ); |
---|
825 | } |
---|
826 | |
---|
827 | return $pictures; |
---|
828 | } |
---|
829 | } |
---|
830 | |
---|
831 | |
---|
832 | /** |
---|
833 | * |
---|
834 | */ |
---|
835 | abstract class PixearchPicture |
---|
836 | { |
---|
837 | /** |
---|
838 | * Picture title |
---|
839 | * |
---|
840 | * @var string |
---|
841 | **/ |
---|
842 | public $title; |
---|
843 | |
---|
844 | /** |
---|
845 | * Pictures' owner name |
---|
846 | * |
---|
847 | * @var string |
---|
848 | **/ |
---|
849 | public $ownerName; |
---|
850 | |
---|
851 | /** |
---|
852 | * Pictures' owner url |
---|
853 | * |
---|
854 | * @var string |
---|
855 | **/ |
---|
856 | public $ownerUri; |
---|
857 | |
---|
858 | /** |
---|
859 | * Differents images sizes urls |
---|
860 | * |
---|
861 | * @var ArrayObject |
---|
862 | **/ |
---|
863 | public $sizes; |
---|
864 | |
---|
865 | /** |
---|
866 | * URI to the photo original page |
---|
867 | * |
---|
868 | * @var string |
---|
869 | **/ |
---|
870 | public $page; |
---|
871 | |
---|
872 | /** |
---|
873 | * Default size |
---|
874 | * |
---|
875 | * @var string |
---|
876 | **/ |
---|
877 | public $defaultSize = 'small'; |
---|
878 | |
---|
879 | /** |
---|
880 | * thumb size |
---|
881 | * |
---|
882 | * @var string |
---|
883 | **/ |
---|
884 | public $thumbSize = 'thumb'; |
---|
885 | |
---|
886 | /** |
---|
887 | * coverflow size |
---|
888 | * |
---|
889 | * @var string |
---|
890 | **/ |
---|
891 | public $coverflowSize = 'medium'; |
---|
892 | } |
---|
893 | |
---|
894 | |
---|
895 | /** |
---|
896 | * FlickR Image |
---|
897 | */ |
---|
898 | class PxSFlickrPicture extends PixearchPicture |
---|
899 | { |
---|
900 | public $defaultSize = 'small'; |
---|
901 | |
---|
902 | public $thumbSize = 'square'; |
---|
903 | |
---|
904 | public $coverflowSize = 'small'; |
---|
905 | |
---|
906 | /** |
---|
907 | * Construct a PxSFlickrPicture object |
---|
908 | * |
---|
909 | * @param {string} $id Photo id |
---|
910 | * @param {string} $title Photo title |
---|
911 | * @param {string} $farm Flickr farm id |
---|
912 | * @param {string} $server Flickr server id |
---|
913 | * @param {string} $secret Flickr secret |
---|
914 | * @param {string} $nsid Photo owner id |
---|
915 | * @param {string} $owner Photo owner name |
---|
916 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
917 | **/ |
---|
918 | function __construct( |
---|
919 | $id = null, |
---|
920 | $title = null, |
---|
921 | $farm = null, |
---|
922 | $server = null, |
---|
923 | $secret = null, |
---|
924 | $nsid = null, |
---|
925 | $owner = null) |
---|
926 | { |
---|
927 | if (!$farm) |
---|
928 | { |
---|
929 | if ($id) |
---|
930 | { |
---|
931 | $flickr = new PxSFlickr(); |
---|
932 | |
---|
933 | // Infos |
---|
934 | $pictureInfos = $flickr->callMethod( |
---|
935 | 'flickr.photos.getInfo', |
---|
936 | array( |
---|
937 | 'photo_id' => $id |
---|
938 | ) |
---|
939 | ); |
---|
940 | |
---|
941 | if (isset($pictureInfos->err['msg'])) |
---|
942 | { |
---|
943 | throw new Exception($pictureInfos->err['msg']); |
---|
944 | } |
---|
945 | |
---|
946 | $title = strval($pictureInfos->photo->title); |
---|
947 | $farm = strval($pictureInfos->photo['farm']); |
---|
948 | $server = strval($pictureInfos->photo['server']); |
---|
949 | $secret = strval($pictureInfos->photo['secret']); |
---|
950 | $nsid = strval($pictureInfos->photo->owner['nsid']); |
---|
951 | } |
---|
952 | else |
---|
953 | { |
---|
954 | throw new Exception( |
---|
955 | 'Need more params' |
---|
956 | ); |
---|
957 | } |
---|
958 | } |
---|
959 | $this->id = $id; |
---|
960 | $this->title = $title; |
---|
961 | $this->sizes = new ArrayObject(); |
---|
962 | $this->sizes['square'] = 'http://farm' . |
---|
963 | $farm . |
---|
964 | '.static.flickr.com/' . |
---|
965 | $server . |
---|
966 | '/' . |
---|
967 | $id . |
---|
968 | '_' . |
---|
969 | $secret . |
---|
970 | '_s.jpg'; |
---|
971 | $this->sizes['thumbnail'] = 'http://farm' . |
---|
972 | $farm . |
---|
973 | '.static.flickr.com/' . |
---|
974 | $server . |
---|
975 | '/' . |
---|
976 | $id . |
---|
977 | '_' . |
---|
978 | $secret . |
---|
979 | '_t.jpg'; |
---|
980 | $this->sizes['small'] = 'http://farm' . |
---|
981 | $farm . |
---|
982 | '.static.flickr.com/' . |
---|
983 | $server . |
---|
984 | '/' . |
---|
985 | $id . |
---|
986 | '_' . |
---|
987 | $secret . |
---|
988 | '_m.jpg'; |
---|
989 | $this->sizes['medium'] = 'http://farm' . |
---|
990 | $farm . |
---|
991 | '.static.flickr.com/' . |
---|
992 | $server . |
---|
993 | '/' . |
---|
994 | $id . |
---|
995 | '_' . |
---|
996 | $secret . |
---|
997 | '.jpg'; |
---|
998 | $this->sizes['original'] = 'http://farm' . |
---|
999 | $farm . |
---|
1000 | '.static.flickr.com/' . |
---|
1001 | $server . |
---|
1002 | '/' . |
---|
1003 | $id . |
---|
1004 | '_' . |
---|
1005 | $secret . |
---|
1006 | '_b.jpg'; |
---|
1007 | $this->page = 'http://www.flickr.com/photos/' . $nsid . |
---|
1008 | '/' . $this->id; |
---|
1009 | $this->ownerUri = 'http://www.flickr.com/photos/' . $nsid; |
---|
1010 | $this->ownerName = $owner; |
---|
1011 | } |
---|
1012 | } |
---|
1013 | |
---|
1014 | /** |
---|
1015 | * |
---|
1016 | */ |
---|
1017 | class PxSPhotoBucketPicture extends PixearchPicture |
---|
1018 | { |
---|
1019 | public $defaultSize = 'thumbnail'; |
---|
1020 | |
---|
1021 | public $thumbSize = 'thumbnail'; |
---|
1022 | |
---|
1023 | public $coverflowSize = 'original'; |
---|
1024 | |
---|
1025 | /** |
---|
1026 | * Construct a new PxSPhotoBucketPicture |
---|
1027 | * |
---|
1028 | * @return void |
---|
1029 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
1030 | **/ |
---|
1031 | public function __construct( |
---|
1032 | $id = null, |
---|
1033 | $title = null, |
---|
1034 | $thumb = null, |
---|
1035 | $browseurl = null, |
---|
1036 | $owner = null, |
---|
1037 | $ownerUri = null |
---|
1038 | ) |
---|
1039 | { |
---|
1040 | if (is_null($title)) |
---|
1041 | { |
---|
1042 | // Get from photobucket |
---|
1043 | // TODO |
---|
1044 | $photobucket = new PxSPhotoBucket(); |
---|
1045 | $xml = $photobucket->callMethod( |
---|
1046 | 'media/' . urlencode($id), |
---|
1047 | array( |
---|
1048 | 'format' => 'xml' |
---|
1049 | ) |
---|
1050 | ); |
---|
1051 | |
---|
1052 | $title = strval($xml->content->media->title); |
---|
1053 | $thumb = strval($xml->content->media->thumb); |
---|
1054 | $browseurl = strval($xml->content->media->browseurl); |
---|
1055 | $owner = strval($xml->content->media['username']); |
---|
1056 | $ownerUri = strval($xml->content->media->browseurl); |
---|
1057 | } |
---|
1058 | |
---|
1059 | $this->id = urlencode($id); |
---|
1060 | $this->title = $title; |
---|
1061 | $this->sizes = new ArrayObject(); |
---|
1062 | $this->sizes['thumbnail'] = $thumb; |
---|
1063 | $this->sizes['original'] = $id; |
---|
1064 | $this->page = $browseurl; |
---|
1065 | $this->ownerUri = $ownerUri; |
---|
1066 | $this->ownerName = $owner; |
---|
1067 | } |
---|
1068 | } |
---|
1069 | |
---|
1070 | /** |
---|
1071 | * PxSDeviantArtPicture |
---|
1072 | */ |
---|
1073 | class PxSDeviantArtPicture extends PixearchPicture |
---|
1074 | { |
---|
1075 | public $defaultSize = 'thumbnail'; |
---|
1076 | |
---|
1077 | public $thumbSize = 'thumbnail'; |
---|
1078 | |
---|
1079 | public $coverflowSize = 'medium'; |
---|
1080 | |
---|
1081 | /** |
---|
1082 | * Construct a new PxSDeviantArtPicture |
---|
1083 | * |
---|
1084 | * @return void |
---|
1085 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
1086 | **/ |
---|
1087 | public function __construct( |
---|
1088 | $id = null, |
---|
1089 | $title = null, |
---|
1090 | $thumb = null, |
---|
1091 | $medium = null, |
---|
1092 | $original = null, |
---|
1093 | $browseurl = null, |
---|
1094 | $ownerName = null, |
---|
1095 | $ownerUri = null |
---|
1096 | ) |
---|
1097 | { |
---|
1098 | if (is_null($title)) |
---|
1099 | { |
---|
1100 | // Get from deviantart |
---|
1101 | $deviantArt = new PxSDeviantArt(); |
---|
1102 | $p = $deviantArt->callMethod( |
---|
1103 | 'getFromId', |
---|
1104 | array( |
---|
1105 | 'id' => $id |
---|
1106 | ) |
---|
1107 | ); |
---|
1108 | $this->id = $p->id; |
---|
1109 | $this->title = $p->title; |
---|
1110 | $this->sizes = $p->sizes; |
---|
1111 | $this->page = $p->page; |
---|
1112 | $this->ownerUri = $p->ownerUri; |
---|
1113 | $this->ownerName = $p->ownerName; |
---|
1114 | } |
---|
1115 | else |
---|
1116 | { |
---|
1117 | $this->id = urlencode($id); |
---|
1118 | $this->title = $title; |
---|
1119 | $this->sizes = new ArrayObject(); |
---|
1120 | $this->sizes['thumbnail'] = $thumb; |
---|
1121 | $this->sizes['medium'] = $medium; |
---|
1122 | $this->sizes['original'] = $original; |
---|
1123 | $this->page = $id; |
---|
1124 | $this->ownerUri = $ownerUri . '/gallery/'; |
---|
1125 | $this->ownerName = $ownerName; |
---|
1126 | } |
---|
1127 | } |
---|
1128 | } |
---|
1129 | |
---|
1130 | /** |
---|
1131 | * PxSPicasaPicture |
---|
1132 | */ |
---|
1133 | class PxSPicasaPicture extends PixearchPicture |
---|
1134 | { |
---|
1135 | public $defaultSize = 'thumbnail'; |
---|
1136 | |
---|
1137 | public $thumbSize = 'thumbnail'; |
---|
1138 | |
---|
1139 | public $coverflowSize = 'original'; |
---|
1140 | |
---|
1141 | /** |
---|
1142 | * Construct a new PxSDeviantArtPicture |
---|
1143 | * |
---|
1144 | * @return void |
---|
1145 | * @author Hadrien Lanneau (contact at hadrien dot eu) |
---|
1146 | **/ |
---|
1147 | public function __construct( |
---|
1148 | $id = null, |
---|
1149 | $title = null, |
---|
1150 | $thumb = null, |
---|
1151 | $medium = null, |
---|
1152 | $original = null, |
---|
1153 | $browseurl = null, |
---|
1154 | $ownerName = null, |
---|
1155 | $ownerUri = null |
---|
1156 | ) |
---|
1157 | { |
---|
1158 | if (is_null($title)) |
---|
1159 | { |
---|
1160 | // Get from deviantart |
---|
1161 | $deviantArt = new PxSPicasa(); |
---|
1162 | $rss = $deviantArt->callMethod( |
---|
1163 | 'getFromId', |
---|
1164 | array( |
---|
1165 | 'id' => $id |
---|
1166 | ) |
---|
1167 | ); |
---|
1168 | if (!is_object($rss)) |
---|
1169 | { |
---|
1170 | throw new Exception('Wrong ID'); |
---|
1171 | } |
---|
1172 | |
---|
1173 | $this->id = urlencode($id); |
---|
1174 | $this->title = strval($rss->title); |
---|
1175 | $this->sizes = new ArrayObject(); |
---|
1176 | $this->sizes['square'] = strval( |
---|
1177 | $rss->media_group->media_thumbnail[0]['url'] |
---|
1178 | ); |
---|
1179 | $this->sizes['thumbnail'] = strval( |
---|
1180 | $rss->media_group->media_thumbnail[1]['url'] |
---|
1181 | ); |
---|
1182 | $this->sizes['small'] = strval( |
---|
1183 | $rss->media_group->media_thumbnail[2]['url'] |
---|
1184 | ); |
---|
1185 | $this->sizes['medium'] = str_replace( |
---|
1186 | array( |
---|
1187 | 's' . strval( |
---|
1188 | $rss->media_group->media_thumbnail[0]['width'] |
---|
1189 | ), |
---|
1190 | 's' . strval( |
---|
1191 | $rss->media_group->media_thumbnail[0]['height'] |
---|
1192 | ) |
---|
1193 | ), |
---|
1194 | array( |
---|
1195 | 's512', |
---|
1196 | 's512' |
---|
1197 | ), |
---|
1198 | strval( |
---|
1199 | $rss->media_group->media_thumbnail[0]['url'] |
---|
1200 | ) |
---|
1201 | ); |
---|
1202 | $this->sizes['original'] = str_replace( |
---|
1203 | array( |
---|
1204 | 's' . strval( |
---|
1205 | $rss->media_group->media_thumbnail[0]['width'] |
---|
1206 | ), |
---|
1207 | 's' . strval( |
---|
1208 | $rss->media_group->media_thumbnail[0]['height'] |
---|
1209 | ) |
---|
1210 | ), |
---|
1211 | array( |
---|
1212 | 's800', |
---|
1213 | 's800' |
---|
1214 | ), |
---|
1215 | strval( |
---|
1216 | $rss->media_group->media_thumbnail[0]['url'] |
---|
1217 | ) |
---|
1218 | ); |
---|
1219 | $this->page = $rss->link[1]['href']; |
---|
1220 | $this->ownerUri = ''; |
---|
1221 | $this->ownerName = strval($rss->media_credit); |
---|
1222 | } |
---|
1223 | else |
---|
1224 | { |
---|
1225 | $this->id = urlencode($id); |
---|
1226 | $this->title = $title; |
---|
1227 | $this->sizes = new ArrayObject(); |
---|
1228 | $this->sizes['thumbnail'] = $thumb; |
---|
1229 | $this->sizes['medium'] = $medium; |
---|
1230 | $this->sizes['original'] = $original; |
---|
1231 | $this->page = $id; |
---|
1232 | $this->ownerUri = $ownerUri; |
---|
1233 | $this->ownerName = $ownerName; |
---|
1234 | } |
---|
1235 | } |
---|
1236 | } |
---|