View source
<?php
function flickrapi_menu($cache) {
$items = array();
if ($cache) {
$items[] = array(
'title' => t('Flickr API Settings'),
'path' => 'admin/settings/flickrapi',
'description' => t('Configure Flickr API credentials.'),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'callback' => 'drupal_get_form',
'callback arguments' => array(
'flickrapi_admin_settings',
),
);
}
return $items;
}
function flickrapi_admin_settings() {
$form['#validate'] = array(
'flickrapi_admin_settings_validate' => 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'),
);
$form['flickrapi_api_secret'] = array(
'#type' => 'textfield',
'#title' => t('API Shared Secret'),
'#required' => FALSE,
'#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."),
);
return system_settings_form($form);
}
function flickrapi_admin_settings_validate($form_id, $form) {
$key = trim($form['flickrapi_api_key']);
$sec = trim($form['flickrapi_api_secret']);
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 ($sec && preg_match('/^[A-Fa-f\\d]{16}$/', $sec) != 1) {
form_set_error('flickrapi_api_secret', t('This does not appear to be a Flickr API secret.'));
}
}
function flickrapi_admin_settings_submit($form_id, $form) {
$form['flickrapi_api_key'] = trim($form['flickrapi_api_key']);
$form['flickrapi_api_secret'] = trim($form['flickrapi_api_secret']);
system_settings_form_submit($form_id, $form);
}
function flickrapi_phpFlickr() {
$include_file = drupal_get_path('module', 'flickrapi') . '/phpFlickr/phpFlickr.php';
if (!file_exists($include_file)) {
drupal_set_message(t("Missing phpFlickr - expected to find it at !file", array(
'!file' => $include_file,
)));
return false;
}
require_once $include_file;
$api_key = variable_get('flickrapi_api_key', '');
if (!$api_key) {
drupal_set_message(t("Flickr API key not set"), 'error');
if (user_access('Administer global flickr api settings')) {
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);
$flickr
->enableCache("fs", variable_get('flickrcachepath', '/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);
}