/[drupal]/contributions/modules/flickr/flickr.inc
ViewVC logotype

Contents of /contributions/modules/flickr/flickr.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.31 - (show annotations) (download) (as text)
Mon Nov 24 00:15:48 2008 UTC (12 months ago) by paulbooker
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA1, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.30: +2 -2 lines
File MIME type: text/x-php
fixes bug #242983
1 <?php
2 // $Id: flickr.inc,v 1.30 2008/11/24 00:04:19 paulbooker Exp $
3
4 define('FLICKR_REST_ENDPOINT', 'http://api.flickr.com/services/rest/');
5
6 /**
7 * Return a list of all possible photo sizes with the right
8 * description and label
9 *
10 * @return An array of photo sizes
11 */
12 function flickr_photo_sizes() {
13 return array(
14 's' => array(
15 'label' => 'Square',
16 'description' => t('75x75 pixel square'),
17 ),
18 't' => array(
19 'label' => 'Thumbnail',
20 'description' => t('100 pixels on longest side'),
21 ),
22 'm' => array(
23 'label' => 'Small',
24 'description' => t('240 pixels on longest side'),
25 ),
26 '-' => array(
27 'label' => 'Medium',
28 'description' => t('500 pixels on longest side'),
29 ),
30 'b' => array(
31 'label' => 'Large',
32 'description' => t('1024 pixels on longest side'),
33 ),
34 'o' => array(
35 'label' => 'Original',
36 'description' => t('Original image')
37 ),
38 );
39 };
40
41 /**
42 * Submit a request to Flickr.
43 *
44 * @param $method
45 * string method name
46 * @param $args
47 * associative array of arguments names and values
48 * @param $cacheable
49 * boolean indicating if it's safe cache the results of this request
50 * @param $return_errors
51 * boolean indicating if the caller will handle displaying error messages
52 *
53 * @return
54 * an array with the the result of the request, or FALSE on error.
55 */
56 function flickr_request($method, $args, $cacheable = TRUE, $return_errors = FALSE) {
57 // Add in additional parameters then sort them for signing.
58 $args['api_key'] = trim(variable_get('flickr_api_key', ''));
59 $args['method'] = $method;
60 $args['format'] = 'php_serial';
61 ksort($args);
62
63 // Build an argument hash API signing (we'll also use it for the cache id)
64 $arg_hash = '';
65 foreach ($args as $k => $v) {
66 $arg_hash .= $k . $v;
67 }
68 // If we've got a secret, sign the arguments.
69 if ($secret = trim(variable_get('flickr_api_secret', ''))) {
70 $args['api_sig'] = md5($secret . $arg_hash);
71 }
72
73 // Build the URL.
74 foreach ($args as $k => $v){
75 $encoded_params[] = urlencode($k) .'='. urlencode($v);
76 }
77 $url = FLICKR_REST_ENDPOINT .'?'. implode('&', $encoded_params);
78
79 // If it's a cachable request, try to load a cached value.
80 if ($cacheable) {
81 if ($cache = cache_get("flickr_$arg_hash", 'cache')) {
82 // Check that the value is still "fresh".
83 if( $cache->expire > time() ) {
84 return unserialize($cache->data);
85 }
86 }
87 }
88
89 // If a cached value wasn't suitable, attempt to connect and fetch a result.
90 $result = drupal_http_request($url);
91 if ($result->code != 200) {
92 if ($return_errors) {
93 return array(
94 'stat' => 'error',
95 //In Drupal <= 5.1, only HTTP errors are stored in $result->code correctly, not TCP/IP errors.
96 //We can not count on this variable being correct until this module requires Drupal 5.2 or above.
97 'code' => $result->code,
98 'message' => $result->error,
99 );
100 }
101 flickr_set_error(t("Could not connect to Flickr, Error: @error", array('@error' => $result->error)));
102 return FALSE;
103 }
104
105 // Make sure it unserializes.
106 $response = unserialize($result->data);
107 if (!$response) {
108 if ($return_errors) {
109 return array(
110 'stat' => 'error',
111 'code' => '-1',
112 'message' => 'The response was corrupted, it could not be unserialized.',
113 );
114 }
115 flickr_set_error(t("Flickr's response was corrupted and could not be unserialized."));
116 return FALSE;
117 }
118
119 // Check that the request was successful.
120 if (flickr_response_has_error($response)) {
121 if ($return_errors) {
122 return $response;
123 }
124 flickr_set_error($response);
125 return FALSE;
126 }
127
128 // Save cacheable results for future use.
129 if ($cacheable) {
130 cache_set("flickr_$arg_hash", $result->data, 'cache', time() + variable_get('flickr_cache_duration', 3600));
131 }
132
133 return $response;
134 }
135
136 /**
137 * This function will try to create a html image tag referencing the Flickr
138 * photo with the desired size if that size is available for this photo.
139 *
140 * @param $photo
141 * the photo variable
142 * @param $size
143 * the desired image size
144 * @param $attribs
145 * an optional array of HTML attributes to pass to the image
146 *
147 * @return
148 * a html image tag referencing the image of the desired
149 * size if it is available.
150 */
151 function flickr_img($photo, $size = NULL, $attributes = NULL) {
152 $sizes = flickr_photo_sizes();
153 if (!isset($size)) {
154 $size = '-';
155 }
156 if (!isset($sizes[$size])) {
157 return;
158 }
159 if (!isset($attributes) || !is_array($attributes)) {
160 $attributes = array();
161 }
162 if (empty($attributes['class'])) {
163 $attributes['class'] = NULL;
164 }
165
166 // photoset's use primary instead of id to specify the image.
167 if (isset($photo['primary'])) {
168 $id = $photo['primary'];
169 $attributes['class'] = implode(' ', array($attributes['class'], 'flickr-photoset-img'));
170 }
171 else {
172 $id = $photo['id'];
173 $attributes['class'] = implode(' ', array($attributes['class'], 'flickr-photo-img'));
174 }
175
176 if ($size == 's') {
177 $attributes['height'] = $attributes['width'] = 75;
178 $img_url = flickr_photo_img($photo, $size);
179 }
180 else {
181 $image_sizes = flickr_photo_get_sizes($id);
182 if ($image_sizes) {
183 foreach ($image_sizes as $image_size) {
184 if ($image_size['label'] == $sizes[$size]['label']) {
185 break;
186 }
187 }
188 if (isset($image_size)) {
189 $img_url = $image_size['source'];
190 $attributes['height'] = $image_size['height'];
191 $attributes['width'] = $image_size['width'];
192 }
193 }
194 else {
195 $img_url = flickr_photo_img($photo, $size);
196 }
197 }
198 $title = is_array($photo['title']) ? $photo['title']['_content'] : $photo['title'];
199 return theme('image', $img_url, $title, $title, $attributes, FALSE);
200 }
201
202 /**
203 * Create the url to $photo with size $size using the correct image farm
204 * from the $photo variable
205 *
206 * @param $photo
207 * photo to which the url should point
208 * @param $size
209 * size of the photo
210 * @param $format
211 * format of the photo
212 *
213 * @return
214 * url for $photo with the correct size and format
215 */
216 function flickr_photo_img($photo, $size = NULL, $format = NULL) {
217 // early images don't have a farm setting so default to 1.
218 $farm = isset($photo['farm']) ? $photo['farm'] : 1;
219 $server = $photo['server'];
220 // photoset's use primary instead of id to specify the image.
221 $id = isset($photo['primary']) ? $photo['primary'] : $photo['id'];
222 $secret = $photo['secret'];
223
224 return "http://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}" .($size ? "_$size." : '.') . ($size == 'o' ? $format : 'jpg');
225 }
226
227 /**
228 * @param $owner
229 * owner of the photo
230 * @param $id
231 * id of the photo to reference in the url
232 *
233 * @return
234 * url for the flickr photo page showing photo with $id of $owner
235 */
236 function flickr_photo_page_url($owner, $id = NULL) {
237 $nsid = is_array($owner) ? $owner['nsid']: $owner;
238 if ($person = flickr_people_get_info($nsid)) {
239 return $person['photosurl']['_content'] . $id;
240 }
241 else {
242 return "http://flickr.com/photos/$nsid/$id";
243 }
244 }
245
246 /**
247 * @param $owner
248 * owner of the photoset
249 * @param $id
250 * id of the photoset to which the url must lead
251 *
252 * @return
253 * url for the photoset page of photoset $id of owner $owner
254 */
255 function flickr_photoset_page_url($owner, $id = NULL) {
256 $nsid = is_array($owner) ? $owner['nsid']: $owner;
257 if ($person = flickr_people_get_info($nsid)) {
258 return $person['photosurl']['_content'] .'sets/'. $id;
259 }
260 else {
261 return "http://flickr.com/photos/$nsid/sets/$id";
262 }
263 }
264
265 /**
266 * @param $photo_id
267 * id of the photo to get info about
268 *
269 * @return
270 * response from the flickr method flickr.photos.getInfo
271 * (http://www.flickr.com/services/api/flickr.photos.getInfo.html)
272 */
273 function flickr_photo_get_info($photo_id) {
274 $response = flickr_request(
275 'flickr.photos.getInfo',
276 array('photo_id' => $photo_id)
277 );
278 if ($response) {
279 return $response['photo'];
280 }
281 return FALSE;
282 }
283
284 /**
285 * @param $photo_id
286 * id of the photo to get the available sizes of
287 *
288 * @return
289 * response from the flickr method flickr.photos.getSizes
290 * (http://www.flickr.com/services/api/flickr.photos.getSizes.html)
291 */
292 function flickr_photo_get_sizes($photo_id) {
293 $response = flickr_request(
294 'flickr.photos.getSizes',
295 array('photo_id' => $photo_id)
296 );
297 if ($response) {
298 return $response['sizes']['size'];
299 }
300 return FALSE;
301 }
302
303 /**
304 * @param $photoset_id
305 * id of the photoset to get information about
306 *
307 * @return
308 * response from the flickr method flickr.photosets.getInfo
309 * (http://www.flickr.com/services/api/flickr.photosets.getInfo.html)
310 */
311 function flickr_photoset_get_info($photoset_id) {
312 $response = flickr_request(
313 'flickr.photosets.getInfo',
314 array('photoset_id' => $photoset_id)
315 );
316 if ($response) {
317 return $response['photoset'];
318 }
319 return FALSE;
320 }
321
322 /**
323 * @param $nsid
324 * nsid of the user whose photoset list you want
325 *
326 * @return
327 * response from the flickr method flickr.photosets.getList
328 * (http://www.flickr.com/services/api/flickr.photosets.getList.html)
329 */
330 function flickr_photoset_get_list($nsid) {
331 $response = flickr_request(
332 'flickr.photosets.getList',
333 array('user_id' => $nsid)
334 );
335 if ($response) {
336 return $response['photosets']['photoset'];
337 }
338 return FALSE;
339 }
340
341 /**
342 * @param $nsid The Flickr user's NSID
343 *
344 * @return
345 * array with person's info from flickr.people.getInfo
346 * (http://www.flickr.com/services/api/flickr.people.getInfo.html)
347 * or FALSE on error.
348 */
349 function flickr_people_get_info($nisd) {
350 $response = flickr_request(
351 'flickr.people.getInfo',
352 array('user_id' => $nisd)
353 );
354 if ($response) {
355 return $response['person'];
356 }
357 return FALSE;
358 }
359
360 /**
361 * Tries to match an 'identifier' onto a flickr nsid
362 *
363 * This function will first see whether $identifier is allready
364 * a nsid (format check only, no api call). If it is not and the
365 * identifier has the format of an email, an api call will be made to
366 * check whether there is an nsid for that email. If this is not the
367 * case, the $identifier is treated as a username and an api call is
368 * made to find the nsid for that username.
369 *
370 * If none of these succees, the result will be false
371 *
372 * @param $identifier
373 * identifier to find an nsid for
374 *
375 * @return
376 * valid nsid or false if none can be found
377 */
378 function flickr_user_find_by_identifier($identifier) {
379 if (flickr_is_nsid($identifier)) {
380 //identifier is an NSID
381 return $identifier;
382 }
383 if (valid_email_address($identifier) && ($user = flickr_user_find_by_email($identifier))) {
384 return $user['nsid'];
385 }
386 if ($user = flickr_user_find_by_username($identifier)) {
387 return $user['nsid'];
388 }
389
390 return FALSE;
391 }
392
393 function flickr_is_nsid($id) {
394 return preg_match('/^\d+@N\d+$/', $id);
395 }
396
397 /**
398 * Lookup an nsid for a username.
399 *
400 * @param $username
401 * username to look for
402 *
403 * @return
404 * response from the flickr method flickr.people.findByUsername
405 * (http://www.flickr.com/services/api/flickr.people.findByUsername.html)
406 */
407 function flickr_user_find_by_username($username) {
408 $response = flickr_request(
409 'flickr.people.findByUsername',
410 array('username' => $username)
411 );
412 if ($response) {
413 return $response['user'];
414 }
415 return FALSE;
416 }
417
418 /**
419 * Lookup an nsid for an email address
420 *
421 * @param $email
422 * email to look for
423 *
424 * @return
425 * response from the flickr method flickr.people.findByEmail
426 * (http://www.flickr.com/services/api/flickr.people.findByEmail.html)
427 */
428 function flickr_user_find_by_email($email) {
429 $response = flickr_request(
430 'flickr.people.findByEmail',
431 array('find_email' => $email)
432 );
433 if ($response) {
434 return $response['user'];
435 }
436 return FALSE;
437 }
438
439 /**
440 * @param $nsid
441 * nsid of the user whose tags will be returned
442 * @param $count
443 * number of tags to return
444 *
445 * @return
446 * response from the flickr method flickr.tags.getListUserPopular
447 * (http://www.flickr.com/services/api/flickr.tags.getListUserPopular.html)
448 */
449 function flickr_tags_get_list_user_popular($nsid, $count=NULL) {
450 $args = array('user_id'=>$nsid);
451 if ($count != NULL) {
452 $args['count'] = $count;
453 }
454 $response = flickr_request('flickr.tags.getListUserPopular', $args);
455 if ($response) {
456 return $response['who']['tags']['tag'];
457 }
458 return FALSE;
459 }
460
461 /**
462 * @param $nsid
463 * nsid of the user whose photoset tags will be returned
464 *
465 * @return
466 * response from the flickr method flickr.tags.getListUser
467 * (http://www.flickr.com/services/api/flickr.tags.getListUser.html)
468 */
469 function flickr_tags_get_list_user($nsid){
470 $response = flickr_request(
471 'flickr.tags.getListUser',
472 array('user_id' => $nsid)
473 );
474 if ($response) {
475 return $response['who']['tags']['tag'];
476 }
477 return FALSE;
478 }
479
480
481 function flickr_photos_search($nsid, $page = 1, $other_args = array()){
482 $args = array (
483 'user_id' => $nsid,
484 'page' => $page,
485 );
486
487 //set per_page to flickr module default if it is not specified in $other_args
488 if (!isset($other_args['per_page'])) {
489 $args['per_page'] = variable_get('flickr_photos_per_page', 20);
490 }
491
492 $response = flickr_request('flickr.photos.search', array_merge($args, $other_args));
493 if ($response) {
494 return $response['photos'];
495 }
496 return FALSE;
497 }
498
499 function flickr_tag_request_args($tags = array(), $mode = 'all') {
500 $args = array();
501 if (!empty($tags)) {
502 $args['tags'] = implode(',', $tags);
503 $args['tag_mode'] = $mode == 'all' ? $mode : 'any';
504 }
505 return $args;
506 }
507
508 /**
509 * Check if the response from the Flickr api call was an error
510 *
511 * @param $response
512 * response to check
513 *
514 * @return
515 * true if the response is an error message
516 */
517 function flickr_response_has_error($response) {
518 return !(isset($response['stat']) && $response['stat'] == 'ok');
519 }
520
521 /**
522 * Display an error message to flickr administrators and write an error to
523 * watchdog.
524 *
525 * @param $mesageOrResposne
526 * message or error response to display
527 */
528 function flickr_set_error($messageOrResponse) {
529 if (is_array($messageOrResponse)) {
530 $message = t('Flickr error @error_id: %flickr_error', array(
531 '@error_id' => $messageOrResponse['code'],
532 '%flickr_error'=> $messageOrResponse['message'],
533 ));
534 }
535 else {
536 $message = $messageOrResponse;
537 }
538
539 if (user_access('administer flickr')) {
540 drupal_set_message($message, 'error');
541 }
542 watchdog('flickr', $message, array(), WATCHDOG_WARNING);
543 }

  ViewVC Help
Powered by ViewVC 1.1.2