View source
<?php
define('FLICKR_REST_ENDPOINT', 'https://api.flickr.com/services/rest/');
require_once drupal_get_path('module', 'flickr') . '/flickr.api.inc';
function flickr_request($method, $args, $cacheable = TRUE, $return_errors = FALSE) {
$args['api_key'] = trim(variable_get('flickr_api_key', ''));
$args['method'] = $method;
$args['format'] = 'php_serial';
ksort($args);
$arg_hash = '';
foreach ($args as $k => $v) {
$arg_hash .= $k . $v;
}
if ($secret = trim(variable_get('flickr_api_secret', ''))) {
$args['api_sig'] = md5($secret . $arg_hash);
}
foreach ($args as $k => $v) {
$encoded_params[] = urlencode($k) . '=' . urlencode($v);
}
$url = FLICKR_REST_ENDPOINT . '?' . implode('&', $encoded_params);
if ($cacheable) {
if ($cache = cache_get("flickr_{$arg_hash}", 'cache')) {
if ($cache->expire > time()) {
return unserialize($cache->data);
}
}
}
$result = drupal_http_request($url);
if ($result->code != 200) {
if ($return_errors) {
return array(
'stat' => 'error',
'code' => $result->code,
'message' => $result->error,
);
}
flickr_set_error(t("Could not connect to Flickr, Error: @error", array(
'@error' => $result->error,
)));
return FALSE;
}
$response = unserialize($result->data);
if (!$response) {
if ($return_errors) {
return array(
'stat' => 'error',
'code' => '-1',
'message' => 'The response was corrupted, it could not be unserialized.',
);
}
flickr_set_error(t("Flickr's response was corrupted and could not be unserialized."));
return FALSE;
}
if (flickr_response_has_error($response)) {
if ($return_errors) {
return $response;
}
flickr_set_error($response);
return FALSE;
}
if ($cacheable) {
cache_set("flickr_{$arg_hash}", $result->data, 'cache', time() + variable_get('flickr_cache_duration', 3600));
}
return $response;
}
function flickr_img($photo, $size = NULL, $attributes = NULL) {
$sizes = flickr_photo_sizes();
if (!isset($size)) {
$size = '-';
}
if (!isset($sizes[$size])) {
return;
}
if (!isset($attributes) || !is_array($attributes)) {
$attributes = array();
}
if (empty($attributes['class'])) {
$attributes['class'] = NULL;
}
if (isset($photo['primary'])) {
$id = $photo['primary'];
$attributes['class'] = implode(' ', array(
$attributes['class'],
'flickr-photoset-img',
));
}
else {
$id = $photo['id'];
$attributes['class'] = implode(' ', array(
$attributes['class'],
'flickr-photo-img',
));
}
if ($size == 's') {
$attributes['height'] = $attributes['width'] = 75;
$img_url = flickr_photo_img($photo, $size);
}
else {
$image_sizes = flickr_photo_get_sizes($id);
if ($image_sizes) {
foreach ($image_sizes as $image_size) {
if ($image_size['label'] == $sizes[$size]['label']) {
break;
}
}
if (isset($image_size)) {
$img_url = $image_size['source'];
$attributes['height'] = $image_size['height'];
$attributes['width'] = $image_size['width'];
}
}
else {
$img_url = flickr_photo_img($photo, $size);
}
}
$info = !is_array($photo['title']) ? flickr_photo_get_info($photo['id']) : '';
$title = is_array($photo['title']) ? str_replace('"', "'", htmlspecialchars_decode(strip_tags($photo['description']['_content']))) : str_replace('"', "'", htmlspecialchars_decode(strip_tags($info['description']['_content'])));
if (empty($title) == 'title') {
$title = is_array($photo['title']) ? str_replace('"', "'", htmlspecialchars_decode(strip_tags($photo['title']['_content']))) : $photo['title'];
}
return theme('image', $img_url, $title, $title, $attributes, FALSE);
}
function flickr_photo_img($photo, $size = NULL, $format = NULL) {
$farm = isset($photo['farm']) ? $photo['farm'] : 1;
$server = $photo['server'];
$id = isset($photo['primary']) ? $photo['primary'] : $photo['id'];
$secret = $photo['secret'];
return "https://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}" . ($size ? "_{$size}." : '.') . ($size == 'o' ? $format : 'jpg');
}
function flickr_photo_sizes() {
return array(
's' => array(
'label' => 'Square',
'description' => t('75 px square'),
),
't' => array(
'label' => 'Thumbnail',
'description' => t('100 px on longest side'),
),
'q' => array(
'label' => 'Large Square',
'description' => t('150 px square'),
),
'm' => array(
'label' => 'Small',
'description' => t('240 px on longest side'),
),
'n' => array(
'label' => 'Small 320',
'description' => t('320 px on longest side'),
),
'-' => array(
'label' => 'Medium',
'description' => t('500 px on longest side'),
),
'z' => array(
'label' => 'Medium 640',
'description' => t('640 px on longest side'),
),
'c' => array(
'label' => 'Medium 800',
'description' => t('800 px on longest side'),
),
'b' => array(
'label' => 'Large',
'description' => t('1024 px on longest side'),
),
'h' => array(
'label' => 'Large 1600',
'description' => t('1600 px on longest side'),
),
'k' => array(
'label' => 'Large 2048',
'description' => t('2048 px on longest side'),
),
'o' => array(
'label' => 'Original',
'description' => t('Original image'),
),
);
}
function flickr_photo_page_url($owner, $id = NULL) {
$nsid = is_array($owner) ? $owner['nsid'] : $owner;
if ($person = flickr_people_get_info($nsid)) {
return $person['photosurl']['_content'] . $id;
}
else {
return "https://flickr.com/photos/{$nsid}/{$id}";
}
}
function flickr_get_info_by_url($url, $reset = FALSE) {
static $urls;
if ($urls[$url] && !$reset) {
return $urls[$url];
}
if (empty($url)) {
$urls[$url] = t('Please enter a valid Flickr URL.');
return $urls[$url];
}
if (!valid_url($url)) {
$urls[$url] = t('Please enter a valid Flickr URL.');
return $urls[$url];
}
if (strpos($url, 'http') !== 0) {
$url = 'https://' . $url;
}
$parts = parse_url($url);
if (strpos($parts['host'], 'flickr') === FALSE) {
$urls[$url] = t('Please enter a valid Flickr URL.');
return $urls[$url];
}
$parts = explode('/', trim($parts['path'], '/'));
if (count($parts) < 3 || $parts[2] == 'sets' && count($parts) < 4) {
$urls[$url] = t('Unable to retrieve Flickr image or set. Please check the URL.');
return $urls[$url];
}
if ($parts[2] == 'sets') {
$set_id = $parts[3];
$info = flickr_photoset_get_info($set_id);
$urls[$url] = $info ? $info : t('Unable to retrieve Flickr image or set. Please check the URL.');
return $urls[$url];
}
else {
$img_id = $parts[2];
$info = flickr_photo_get_info($img_id);
$urls[$url] = $info ? $info : t('Unable to retrieve Flickr image or set. Please check the URL.');
return $urls[$url];
}
}
function flickr_photoset_page_url($owner, $id = NULL) {
$nsid = is_array($owner) ? $owner['nsid'] : $owner;
if ($person = flickr_people_get_info($nsid)) {
return $person['photosurl']['_content'] . 'sets/' . $id;
}
else {
return "https://flickr.com/photos/{$nsid}/sets/{$id}";
}
}
function flickr_user_find_by_identifier($identifier) {
if (flickr_is_nsid($identifier)) {
return $identifier;
}
if (valid_email_address($identifier) && ($user = flickr_user_find_by_email($identifier))) {
return $user['nsid'];
}
if ($user = flickr_user_find_by_username($identifier)) {
return $user['nsid'];
}
return FALSE;
}
function flickr_is_nsid($id) {
return preg_match('/^\\d+@N\\d+$/', $id);
}
function flickr_tag_request_args($tags = array(), $mode = 'all') {
$args = array();
if (!empty($tags)) {
$args['tags'] = implode(',', $tags);
$args['tag_mode'] = $mode == 'all' ? $mode : 'any';
}
return $args;
}
function flickr_response_has_error($response) {
return !(isset($response['stat']) && $response['stat'] == 'ok');
}
function flickr_set_error($error_message) {
if (is_array($error_message)) {
$message = t('Flickr error @error_id: %flickr_error', array(
'@error_id' => $error_message['code'],
'%flickr_error' => $error_message['message'],
));
}
else {
$message = $error_message;
}
if (user_access('administer flickr')) {
drupal_set_message($message, 'error');
}
watchdog('flickr', $message, array(), WATCHDOG_WARNING);
}