| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | require_once 'Flickr/API.php'; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | class Metalbum_Flickr extends Metalbum { |
|---|
| 30 | var $api; |
|---|
| 31 | var $username; |
|---|
| 32 | var $nsid; |
|---|
| 33 | var $nb_items; |
|---|
| 34 | |
|---|
| 35 | function Metalbum_Flickr($username, $params) { |
|---|
| 36 | $this->api =& new Flickr_API($params); |
|---|
| 37 | if(!$this->_setUsername($username)) |
|---|
| 38 | $this->status = 'error'; |
|---|
| 39 | else |
|---|
| 40 | $this->status = 'ok'; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | function _setUsername($username) { |
|---|
| 44 | $response = $this->api->callMethod('flickr.people.findByUsername', array('username' => $username)); |
|---|
| 45 | if ($response) { |
|---|
| 46 | $this->username = $username; |
|---|
| 47 | $user_node = $response->getNodeAt('user'); |
|---|
| 48 | $this->nsid = $user_node->getAttribute('nsid'); |
|---|
| 49 | return $this->nsid; |
|---|
| 50 | } else { |
|---|
| 51 | $this->_setError(__FUNCTION__, $this->api->getErrorCode(), $this->api->getErrorMessage()); |
|---|
| 52 | return false; |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | function _buildUrl($type, $server, $secret, $id) { |
|---|
| 57 | switch($type) { |
|---|
| 58 | case 'thumb': $format='_t'; break; |
|---|
| 59 | case 'full': |
|---|
| 60 | default: $format=''; break; |
|---|
| 61 | } |
|---|
| 62 | return "http://photos{$server}.flickr.com/{$id}_{$secret}{$format}.jpg"; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | function getNbItems() { |
|---|
| 66 | if($this->nb_items) { |
|---|
| 67 | return $this->nb_items; |
|---|
| 68 | } else { |
|---|
| 69 | $response = $this->api->callMethod('flickr.people.getPublicPhotos', array('user_id' => $this->nsid)); |
|---|
| 70 | if ($response) { |
|---|
| 71 | $photos_node = $response->getNodeAt('photos'); |
|---|
| 72 | $this->nb_items = $photos_node->getAttribute('total'); |
|---|
| 73 | return $this->nb_items; |
|---|
| 74 | } else { |
|---|
| 75 | $this->_setError(__FUNCTION__, $this->api->getErrorCode(), $this->api->getErrorMessage()); |
|---|
| 76 | return false; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | function getPhotos($page=1, $per_page=15) { |
|---|
| 82 | $response = $this->api->callMethod('flickr.people.getPublicPhotos', array( |
|---|
| 83 | 'user_id' => $this->nsid, |
|---|
| 84 | 'per_page' => $per_page, |
|---|
| 85 | 'page' => $page |
|---|
| 86 | )); |
|---|
| 87 | if ($response) { |
|---|
| 88 | $photos_node = $response->getNodeAt('photos'); |
|---|
| 89 | $this->nb_items = $photos_node->getAttribute('total'); |
|---|
| 90 | $idx = 0; |
|---|
| 91 | |
|---|
| 92 | for($idx=0; $idx < $per_page*2; $idx+=2) { |
|---|
| 93 | $photo_node = $photos_node->getElement(array($idx+1)); |
|---|
| 94 | if(PEAR::isError($photo_node)) |
|---|
| 95 | break; |
|---|
| 96 | else { |
|---|
| 97 | unset($photo); |
|---|
| 98 | $id = $photo_node->getAttribute('id'); |
|---|
| 99 | $server = $photo_node->getAttribute('server'); |
|---|
| 100 | $secret = $photo_node->getAttribute('secret'); |
|---|
| 101 | $photo['id'] = $id; |
|---|
| 102 | $photo['title'] = $photo_node->getAttribute('title'); |
|---|
| 103 | $photo['url_thumb'] = $this->_buildUrl('thumb', $server, $secret, $id); |
|---|
| 104 | $photo['url_full'] = $this->_buildUrl('full', $server, $secret, $id); |
|---|
| 105 | $rval[] = $photo; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | return $rval; |
|---|
| 109 | } else { |
|---|
| 110 | $this->_setError(__FUNCTION__, $this->api->getErrorCode(), $this->api->getErrorMessage()); |
|---|
| 111 | return false; |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | function getPhotoInfo($id_photo) { |
|---|
| 117 | $response = $this->api->callMethod('flickr.photos.getInfo', array( |
|---|
| 118 | 'photo_id' => $id_photo |
|---|
| 119 | )); |
|---|
| 120 | if ($response) { |
|---|
| 121 | $photo_node = $response->getNodeAt('photo'); |
|---|
| 122 | $id = $photo_node->getAttribute('id'); |
|---|
| 123 | $secret = $photo_node->getAttribute('secret'); |
|---|
| 124 | $server = $photo_node->getAttribute('server'); |
|---|
| 125 | |
|---|
| 126 | $title_node = $photo_node->getNodeAt('title'); |
|---|
| 127 | $info['title'] = $title_node->decodeXmlEntities($title_node->content); |
|---|
| 128 | $description_node = $photo_node->getNodeAt('description'); |
|---|
| 129 | $info['description'] = $description_node->decodeXmlEntities($description_node->content); |
|---|
| 130 | $info['url_thumb'] = $this->_buildUrl('thumb', $server, $secret, $id); |
|---|
| 131 | $info['url_full'] = $this->_buildUrl('full', $server, $secret, $id); |
|---|
| 132 | return $info; |
|---|
| 133 | } else { |
|---|
| 134 | $this->_setError(__FUNCTION__, $this->api->getErrorCode(), $this->api->getErrorMessage()); |
|---|
| 135 | return false; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | function getUrlAlbum() { |
|---|
| 140 | return 'http://flickr.com/photos/'.$this->nsid.'/'; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | ?> |
|---|