You are here

flickrapi.module in Flickr API 5

Same filename and directory in other branches
  1. 6 flickrapi.module
  2. 7.2 flickrapi.module
  3. 7 flickrapi.module

File

flickrapi.module
View source
<?php

// $Id:

/**
* Implementation of hook_menu
*/
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;
}

/**
 * Implementation of hook_settings
 */
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) {

  // clean up the data ...
  $form['flickrapi_api_key'] = trim($form['flickrapi_api_key']);
  $form['flickrapi_api_secret'] = trim($form['flickrapi_api_secret']);

  // ... and save the settings
  system_settings_form_submit($form_id, $form);
}

/**
 * returns the phpFlickr object.
 * If we need to change anything, such as making the cache configuration,
 * then it'll all be done here.
 */
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;
}

/**
 * Tries to match an 'identifier' onto a flickr nsid
 *
 * This function will first see whether $identifier is allready
 * a nsid (format check only, no api call).  If it is not and the
 * identifier has the format of an email, an api call will be made to
 * check whether there is an nsid for that email.  If this is not the
 * case, the $identifier is treated as a username and an api call is
 * made to find the nsid for that username.
 *
 * If none of these succees, the result will be false
 *
 * @param $identifier
 *   identifier to find an nsid for
 *
 * @return
 *   valid nsid or false if none can be found
 */
function flickrapi_get_user_nsid($identifier) {
  if (flickrapi_is_nsid($identifier)) {

    //identifier is an NSID
    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);
}

Functions

Namesort descending Description
flickrapi_admin_settings Implementation of hook_settings
flickrapi_admin_settings_submit
flickrapi_admin_settings_validate
flickrapi_get_user_nsid Tries to match an 'identifier' onto a flickr nsid
flickrapi_is_nsid
flickrapi_menu Implementation of hook_menu
flickrapi_phpFlickr returns the phpFlickr object. If we need to change anything, such as making the cache configuration, then it'll all be done here.