|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00032 class Zend_Service_Flickr 00033 { 00037 const URI_BASE = 'http://www.flickr.com'; 00038 00044 public $apiKey; 00045 00051 protected $_restClient = null; 00052 00053 00063 public function __construct($apiKey) 00064 { 00065 iconv_set_encoding('output_encoding', 'UTF-8'); 00066 iconv_set_encoding('input_encoding', 'UTF-8'); 00067 iconv_set_encoding('internal_encoding', 'UTF-8'); 00068 00069 $this->apiKey = (string) $apiKey; 00070 } 00071 00072 00092 public function tagSearch($query, array $options = array()) 00093 { 00094 static $method = 'flickr.photos.search'; 00095 static $defaultOptions = array('per_page' => 10, 00096 'page' => 1, 00097 'tag_mode' => 'or', 00098 'extras' => 'license, date_upload, date_taken, owner_name, icon_server'); 00099 00100 $options['tags'] = is_array($query) ? implode(',', $query) : $query; 00101 00102 $options = $this->_prepareOptions($method, $options, $defaultOptions); 00103 00104 $this->_validateTagSearch($options); 00105 00106 // now search for photos 00107 $restClient = $this->getRestClient(); 00108 $restClient->getHttpClient()->resetParameters(); 00109 $response = $restClient->restGet('/services/rest/', $options); 00110 00111 if ($response->isError()) { 00115 require_once 'Zend/Service/Exception.php'; 00116 throw new Zend_Service_Exception('An error occurred sending request. Status code: ' 00117 . $response->getStatus()); 00118 } 00119 00120 $dom = new DOMDocument(); 00121 $dom->loadXML($response->getBody()); 00122 00123 self::_checkErrors($dom); 00124 00128 require_once 'Zend/Service/Flickr/ResultSet.php'; 00129 return new Zend_Service_Flickr_ResultSet($dom, $this); 00130 } 00131 00132 00150 public function userSearch($query, array $options = null) 00151 { 00152 static $method = 'flickr.people.getPublicPhotos'; 00153 static $defaultOptions = array('per_page' => 10, 00154 'page' => 1, 00155 'extras' => 'license, date_upload, date_taken, owner_name, icon_server'); 00156 00157 00158 // can't access by username, must get ID first 00159 if (strchr($query, '@')) { 00160 // optimistically hope this is an email 00161 $options['user_id'] = $this->getIdByEmail($query); 00162 } else { 00163 // we can safely ignore this exception here 00164 $options['user_id'] = $this->getIdByUsername($query); 00165 } 00166 00167 $options = $this->_prepareOptions($method, $options, $defaultOptions); 00168 $this->_validateUserSearch($options); 00169 00170 // now search for photos 00171 $restClient = $this->getRestClient(); 00172 $restClient->getHttpClient()->resetParameters(); 00173 $response = $restClient->restGet('/services/rest/', $options); 00174 00175 if ($response->isError()) { 00179 require_once 'Zend/Service/Exception.php'; 00180 throw new Zend_Service_Exception('An error occurred sending request. Status code: ' 00181 . $response->getStatus()); 00182 } 00183 00184 $dom = new DOMDocument(); 00185 $dom->loadXML($response->getBody()); 00186 00187 self::_checkErrors($dom); 00188 00192 require_once 'Zend/Service/Flickr/ResultSet.php'; 00193 return new Zend_Service_Flickr_ResultSet($dom, $this); 00194 } 00195 00204 public function groupPoolGetPhotos($query, array $options = array()) 00205 { 00206 static $method = 'flickr.groups.pools.getPhotos'; 00207 static $defaultOptions = array('per_page' => 10, 00208 'page' => 1, 00209 'extras' => 'license, date_upload, date_taken, owner_name, icon_server'); 00210 00211 if (empty($query) || !is_string($query)) { 00215 require_once 'Zend/Service/Exception.php'; 00216 throw new Zend_Service_Exception('You must supply a group id'); 00217 } 00218 00219 $options['group_id'] = $query; 00220 00221 $options = $this->_prepareOptions($method, $options, $defaultOptions); 00222 00223 $this->_validateGroupPoolGetPhotos($options); 00224 00225 // now search for photos 00226 $restClient = $this->getRestClient(); 00227 $restClient->getHttpClient()->resetParameters(); 00228 $response = $restClient->restGet('/services/rest/', $options); 00229 00230 if ($response->isError()) { 00234 require_once 'Zend/Service/Exception.php'; 00235 throw new Zend_Service_Exception('An error occurred sending request. Status code: ' 00236 . $response->getStatus()); 00237 } 00238 00239 $dom = new DOMDocument(); 00240 $dom->loadXML($response->getBody()); 00241 00242 self::_checkErrors($dom); 00243 00247 require_once 'Zend/Service/Flickr/ResultSet.php'; 00248 return new Zend_Service_Flickr_ResultSet($dom, $this); 00249 } 00250 00251 00252 00262 public function getIdByUsername($username) 00263 { 00264 static $method = 'flickr.people.findByUsername'; 00265 00266 $options = array('api_key' => $this->apiKey, 'method' => $method, 'username' => (string) $username); 00267 00268 if (empty($username)) { 00272 require_once 'Zend/Service/Exception.php'; 00273 throw new Zend_Service_Exception('You must supply a username'); 00274 } 00275 00276 $restClient = $this->getRestClient(); 00277 $restClient->getHttpClient()->resetParameters(); 00278 $response = $restClient->restGet('/services/rest/', $options); 00279 00280 if ($response->isError()) { 00284 require_once 'Zend/Service/Exception.php'; 00285 throw new Zend_Service_Exception('An error occurred sending request. Status code: ' 00286 . $response->getStatus()); 00287 } 00288 00289 $dom = new DOMDocument(); 00290 $dom->loadXML($response->getBody()); 00291 self::_checkErrors($dom); 00292 $xpath = new DOMXPath($dom); 00293 return (string) $xpath->query('//user')->item(0)->getAttribute('id'); 00294 } 00295 00296 00306 public function getIdByEmail($email) 00307 { 00308 static $method = 'flickr.people.findByEmail'; 00309 00310 if (empty($email)) { 00314 require_once 'Zend/Service/Exception.php'; 00315 throw new Zend_Service_Exception('You must supply an e-mail address'); 00316 } 00317 00318 $options = array('api_key' => $this->apiKey, 'method' => $method, 'find_email' => (string) $email); 00319 00320 $restClient = $this->getRestClient(); 00321 $restClient->getHttpClient()->resetParameters(); 00322 $response = $restClient->restGet('/services/rest/', $options); 00323 00324 if ($response->isError()) { 00328 require_once 'Zend/Service/Exception.php'; 00329 throw new Zend_Service_Exception('An error occurred sending request. Status code: ' 00330 . $response->getStatus()); 00331 } 00332 00333 $dom = new DOMDocument(); 00334 $dom->loadXML($response->getBody()); 00335 self::_checkErrors($dom); 00336 $xpath = new DOMXPath($dom); 00337 return (string) $xpath->query('//user')->item(0)->getAttribute('id'); 00338 } 00339 00340 00348 public function getImageDetails($id) 00349 { 00350 static $method = 'flickr.photos.getSizes'; 00351 00352 if (empty($id)) { 00356 require_once 'Zend/Service/Exception.php'; 00357 throw new Zend_Service_Exception('You must supply a photo ID'); 00358 } 00359 00360 $options = array('api_key' => $this->apiKey, 'method' => $method, 'photo_id' => $id); 00361 00362 $restClient = $this->getRestClient(); 00363 $restClient->getHttpClient()->resetParameters(); 00364 $response = $restClient->restGet('/services/rest/', $options); 00365 00366 $dom = new DOMDocument(); 00367 $dom->loadXML($response->getBody()); 00368 $xpath = new DOMXPath($dom); 00369 self::_checkErrors($dom); 00370 $retval = array(); 00374 require_once 'Zend/Service/Flickr/Image.php'; 00375 foreach ($xpath->query('//size') as $size) { 00376 $label = (string) $size->getAttribute('label'); 00377 $retval[$label] = new Zend_Service_Flickr_Image($size); 00378 } 00379 00380 return $retval; 00381 } 00382 00383 00389 public function getRestClient() 00390 { 00391 if (null === $this->_restClient) { 00395 require_once 'Zend/Rest/Client.php'; 00396 $this->_restClient = new Zend_Rest_Client(self::URI_BASE); 00397 } 00398 00399 return $this->_restClient; 00400 } 00401 00402 00410 protected function _validateUserSearch(array $options) 00411 { 00412 $validOptions = array('api_key', 'method', 'user_id', 'per_page', 'page', 'extras', 'min_upload_date', 00413 'min_taken_date', 'max_upload_date', 'max_taken_date', 'safe_search'); 00414 00415 $this->_compareOptions($options, $validOptions); 00416 00420 require_once 'Zend/Validate/Between.php'; 00421 $between = new Zend_Validate_Between(1, 500, true); 00422 if (!$between->isValid($options['per_page'])) { 00426 require_once 'Zend/Service/Exception.php'; 00427 throw new Zend_Service_Exception($options['per_page'] . ' is not valid for the "per_page" option'); 00428 } 00429 00433 require_once 'Zend/Validate/Int.php'; 00434 $int = new Zend_Validate_Int(); 00435 if (!$int->isValid($options['page'])) { 00439 require_once 'Zend/Service/Exception.php'; 00440 throw new Zend_Service_Exception($options['page'] . ' is not valid for the "page" option'); 00441 } 00442 00443 // validate extras, which are delivered in csv format 00444 if ($options['extras']) { 00445 $extras = explode(',', $options['extras']); 00446 $validExtras = array('license', 'date_upload', 'date_taken', 'owner_name', 'icon_server'); 00447 foreach($extras as $extra) { 00451 //in_array(trim($extra), $validExtras); 00452 } 00453 } 00454 } 00455 00456 00464 protected function _validateTagSearch(array $options) 00465 { 00466 $validOptions = array('method', 'api_key', 'user_id', 'tags', 'tag_mode', 'text', 'min_upload_date', 00467 'max_upload_date', 'min_taken_date', 'max_taken_date', 'license', 'sort', 00468 'privacy_filter', 'bbox', 'accuracy', 'safe_search', 'content_type', 'machine_tags', 00469 'machine_tag_mode', 'group_id', 'contacts', 'woe_id', 'place_id', 'media', 'has_geo', 00470 'geo_context', 'lat', 'lon', 'radius', 'radius_units', 'is_commons', 'is_gallery', 00471 'extras', 'per_page', 'page'); 00472 00473 $this->_compareOptions($options, $validOptions); 00474 00478 require_once 'Zend/Validate/Between.php'; 00479 $between = new Zend_Validate_Between(1, 500, true); 00480 if (!$between->isValid($options['per_page'])) { 00484 require_once 'Zend/Service/Exception.php'; 00485 throw new Zend_Service_Exception($options['per_page'] . ' is not valid for the "per_page" option'); 00486 } 00487 00491 require_once 'Zend/Validate/Int.php'; 00492 $int = new Zend_Validate_Int(); 00493 if (!$int->isValid($options['page'])) { 00497 require_once 'Zend/Service/Exception.php'; 00498 throw new Zend_Service_Exception($options['page'] . ' is not valid for the "page" option'); 00499 } 00500 00501 // validate extras, which are delivered in csv format 00502 if ($options['extras']) { 00503 $extras = explode(',', $options['extras']); 00504 $validExtras = array('license', 'date_upload', 'date_taken', 'owner_name', 'icon_server'); 00505 foreach($extras as $extra) { 00509 //in_array(trim($extra), $validExtras); 00510 } 00511 } 00512 00513 } 00514 00515 00523 protected function _validateGroupPoolGetPhotos(array $options) 00524 { 00525 $validOptions = array('api_key', 'tags', 'method', 'group_id', 'per_page', 'page', 'extras', 'user_id'); 00526 00527 $this->_compareOptions($options, $validOptions); 00528 00532 require_once 'Zend/Validate/Between.php'; 00533 $between = new Zend_Validate_Between(1, 500, true); 00534 if (!$between->isValid($options['per_page'])) { 00538 require_once 'Zend/Service/Exception.php'; 00539 throw new Zend_Service_Exception($options['per_page'] . ' is not valid for the "per_page" option'); 00540 } 00541 00545 require_once 'Zend/Validate/Int.php'; 00546 $int = new Zend_Validate_Int(); 00547 00548 if (!$int->isValid($options['page'])) { 00552 require_once 'Zend/Service/Exception.php'; 00553 throw new Zend_Service_Exception($options['page'] . ' is not valid for the "page" option'); 00554 } 00555 00556 // validate extras, which are delivered in csv format 00557 if (isset($options['extras'])) { 00558 $extras = explode(',', $options['extras']); 00559 $validExtras = array('license', 'date_upload', 'date_taken', 'owner_name', 'icon_server'); 00560 foreach($extras as $extra) { 00564 //in_array(trim($extra), $validExtras); 00565 } 00566 } 00567 } 00568 00569 00577 protected static function _checkErrors(DOMDocument $dom) 00578 { 00579 if ($dom->documentElement->getAttribute('stat') === 'fail') { 00580 $xpath = new DOMXPath($dom); 00581 $err = $xpath->query('//err')->item(0); 00585 require_once 'Zend/Service/Exception.php'; 00586 throw new Zend_Service_Exception('Search failed due to error: ' . $err->getAttribute('msg') 00587 . ' (error #' . $err->getAttribute('code') . ')'); 00588 } 00589 } 00590 00591 00600 protected function _prepareOptions($method, array $options, array $defaultOptions) 00601 { 00602 $options['method'] = (string) $method; 00603 $options['api_key'] = $this->apiKey; 00604 00605 return array_merge($defaultOptions, $options); 00606 } 00607 00608 00617 protected function _compareOptions(array $options, array $validOptions) 00618 { 00619 $difference = array_diff(array_keys($options), $validOptions); 00620 if ($difference) { 00624 require_once 'Zend/Service/Exception.php'; 00625 throw new Zend_Service_Exception('The following parameters are invalid: ' . implode(',', $difference)); 00626 } 00627 } 00628 } 00629