View source
<?php
function flickrapi_menu() {
$items = array();
$items['admin/settings/flickrapi'] = array(
'title' => 'Flickr API Settings',
'description' => 'Configure Flickr API credentials.',
'access arguments' => array(
'configure flickr api',
),
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'flickrapi_admin_settings',
),
);
return $items;
}
function flickrapi_perm() {
return array(
'configure flickr api',
);
}
function flickrapi_admin_settings() {
$form = array();
$form['flickrapi_api_key'] = array(
'#type' => 'textfield',
'#title' => t('API Key'),
'#required' => TRUE,
'#default_value' => variable_get('flickrapi_api_key', ''),
'#description' => t('API Key from Flickr. !link', array(
'!link' => l(t('Get one!'), 'http://www.flickr.com/services/apps/by/me'),
)),
);
$form['flickrapi_api_secret'] = array(
'#type' => 'textfield',
'#title' => t('API Shared Secret'),
'#required' => TRUE,
'#default_value' => variable_get('flickrapi_api_secret', ''),
'#description' => t("API key's secret from Flickr."),
);
$times = array(
900,
1800,
2700,
3600,
7200,
10800,
14400,
18000,
21600,
43200,
86400,
);
$ageoptions = drupal_map_assoc($times, 'format_interval');
$form['flickrapi_cache_duration'] = array(
'#type' => 'select',
'#title' => t('Update interval'),
'#options' => $ageoptions,
'#default_value' => variable_get('flickrapi_cache_duration', 3600),
'#description' => t("The refresh interval indicating how often you want to check cached Flickr API calls are up to date."),
);
$form['flickrapi_cache_path'] = array(
'#title' => t('Flickr Cache Path'),
'#required' => TRUE,
'#description' => t('Location on server file system where results of Flickr API calls can be cached.'),
'#type' => 'textfield',
'#default_value' => variable_get('flickrapi_cache_path', '/tmp'),
);
$form['#validate'][] = 'flickrapi_admin_settings_validate';
return system_settings_form($form);
}
function flickrapi_admin_settings_validate($form, &$form_state) {
$key = trim($form_state['values']['flickrapi_api_key']);
$secret = trim($form_state['values']['flickrapi_api_secret']);
$cache_dir = $form_state['values']['flickrapi_cache_path'];
if ($key && preg_match('/^[A-Fa-f\\d]{32}$/', $key) != 1) {
form_set_error('flickrapi_api_key', t('This does not appear to be a Flickr API key.'));
}
if ($secret && preg_match('/^[A-Fa-f\\d]{16}$/', $secret) != 1) {
form_set_error('flickrapi_api_secret', t('This does not appear to be a Flickr API secret.'));
}
if (!is_dir($cache_dir) || !is_writable($cache_dir)) {
form_set_error('flickrapi_cache_path', t('Folder does not exist or is not writable.'));
}
}
function flickrapi_phpFlickr() {
module_load_include('php', 'flickrapi', 'phpFlickr/phpFlickr');
$api_key = variable_get('flickrapi_api_key', '');
$api_secret = variable_get('flickrapi_api_secret', '');
if (!$api_key) {
drupal_set_message(t("Flickr API key not set"), 'error');
if (user_access('configure flickr api')) {
drupal_set_message(t("Goto !link to configure the Flickr API settings", array(
'!link' => l('admin/settings/flickrapi', 'admin/settings/flickrapi'),
)));
}
}
$flickr = new phpFlickr($api_key, $api_secret);
$flickr
->enableCache("fs", variable_get('flickrapi_cache_path', '/tmp'));
return $flickr;
}
function flickrapi_get_user_nsid($identifier) {
if (flickrapi_is_nsid($identifier)) {
return $identifier;
}
$f = flickrapi_phpFlickr();
if (valid_email_address($identifier) && ($user = $f
->people_findByEmail($identifier))) {
return $user['nsid'];
}
if ($user = $f
->people_findByUsername($identifier)) {
return $user['nsid'];
}
return FALSE;
}
function flickrapi_is_nsid($id) {
return preg_match('/^\\d+@N\\d+$/', $id);
}