You are here

janrain_capture_screens.module in Janrain Registration 7.4

Janrain Capture Admin Screens Module

File

janrain_capture_screens/janrain_capture_screens.module
View source
<?php

/**
 * @file
 * Janrain Capture Admin Screens Module
 */

/**
 * Implements hook_menu_alter().
 */
function janrain_capture_screens_menu_alter(&$items) {
  $items['admin/config/people/janrain_capture/screens'] = array(
    'title' => t('Screens'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'janrain_capture_admin_screens_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -40,
    'file' => 'janrain_capture_screens.admin.inc',
    'file path' => drupal_get_path('module', 'janrain_capture_screens'),
  );
}

/**
 * Gets the appropriate Janrain screen file.
 *
 * @param string $screen_name
 *   Janrain screen name.
 *   @see _janrain_capture_get_screens()
 *
 * @return string|bool
 *   Janrain screen uri if file found, FALSE if not.
 */
function _janrain_capture_get_screen_file($screen_name) {

  // Serve updated screen if availible.
  $screen_machine_name = _janrain_capture_get_screen_machine_name($screen_name);
  $updated_screen = variable_get("janrain_capture_screen_{$screen_machine_name}");
  $updated_screen_remote_url = trim(variable_get("janrain_capture_screen_{$screen_machine_name}_remote_url", ''));
  $updated_screen_remote_fid = variable_get('janrain_capture_screen_' . $screen_machine_name . '_remote_fid');
  $screen_fid = $updated_screen_remote_url && $updated_screen_remote_fid ? $updated_screen_remote_fid : $updated_screen;
  if ($screen_fid) {
    if ($file = file_load($screen_fid)) {
      return $file->uri;
    }
  }

  // Serve default shipped screen if not.
  $module_path = drupal_get_path('module', 'janrain_capture');
  $file_path = "{$module_path}/janrain-capture-screens/{$screen_name}";
  return file_exists($file_path) ? $file_path : FALSE;
}

/**
 * Helper function to get the list of available Janrain capture screens.
 *
 * @return array
 *   Availible Janrain Capture screens.
 */
function _janrain_capture_get_screens() {
  $screens = array(
    'edit-profile.html',
    'forgot.html',
    'public-profile.html',
    'signin.html',
    'verify.html',
    'edit-profile.js',
    'forgot.js',
    'public-profile.js',
    'signin.js',
    'verify.js',
    'stylesheets/styles.css',
    'stylesheets/ie-styles.css',
    'stylesheets/mobile-styles.css',
  );
  return $screens;
}

/**
 * Gets a machine name for a screen.
 *
 * Replaces dots and slashes with underscores.
 *
 * @param string $screen_name
 *   Janrain capture screen name.
 *   @see _janrain_capture_get_screens()
 *
 * @return string
 *   Screen machine name which can be used as a form element key.
 */
function _janrain_capture_get_screen_machine_name($screen_name) {
  return str_replace(array(
    '.',
    '/',
  ), '_', $screen_name);
}

/**
 * Gets a santized file name for a screen and removes directories.
 *
 * Replaces slashes with underscores.
 *
 * @param string $screen_name
 *   Janrain capture screen name.
 *   @see _janrain_capture_get_screens()
 *
 * @return string
 *   Screen file name which retains the file extension.
 */
function _janrain_capture_get_screen_file_name($screen_name) {
  return str_replace(array(
    '/',
  ), '_', $screen_name);
}

/**
 * Implements hook_cron().
 *
 * Refreshes the locally cached remote screens.
 */
function janrain_capture_screens_cron() {
  $screens = _janrain_capture_get_screens();
  foreach ($screens as $screen) {
    $screen_machine_name = _janrain_capture_get_screen_machine_name($screen);
    $screen_remote_fid = variable_get('janrain_capture_screen_' . $screen_machine_name . '_remote_fid');
    $screen_remote_url = trim(variable_get('janrain_capture_screen_' . $screen_machine_name . '_remote_url', ''));
    if ($screen_remote_url && $screen_remote_fid && ($file = file_load($screen_remote_fid)) && $file->timestamp < REQUEST_TIME - 3 * 3600) {
      janrain_capture_screens_local_screen_data_refresh($screen);
    }
  }
}

/**
 * Refreshes a locally cached janrain capture screen.
 *
 * @param string $screen
 *   The name of the screen that needs refreshing.
 * @param string $screen_content
 *   Optional parameter, which may hold the screen's content. If not set, then
 *   the content will be fetched from the remote URL.
 */
function janrain_capture_screens_local_screen_data_refresh($screen, $screen_content = NULL) {
  $screen_machine_name = _janrain_capture_get_screen_machine_name($screen);
  if (!isset($screen_content)) {
    $screen_remote_url = trim(variable_get('janrain_capture_screen_' . $screen_machine_name . '_remote_url', ''));
    if ($screen_remote_url) {
      $response = drupal_http_request($screen_remote_url);
      if ($response->code == 200) {
        $screen_content = $response->data;
      }
    }
  }

  // Load the current screen content to see if there is a change.
  $screen_remote_fid = variable_get('janrain_capture_screen_' . $screen_machine_name . '_remote_fid');
  $current_screen_content = '';
  if ($screen_remote_fid && ($file = file_load($screen_remote_fid))) {
    $current_screen_content = file_get_contents($file->uri);
  }
  if (isset($screen_content) && (!$screen_remote_fid || md5($screen_content) != md5($current_screen_content))) {
    $file = file_save_data($screen_content, 'public://janrain_capture_screens/cache/' . _janrain_capture_get_screen_file_name($screen), FILE_EXISTS_REPLACE);
    if (!$screen_remote_fid && $file) {
      variable_set('janrain_capture_screen_' . $screen_machine_name . '_remote_fid', $file->fid);
    }
  }
}

Functions

Namesort descending Description
janrain_capture_screens_cron Implements hook_cron().
janrain_capture_screens_local_screen_data_refresh Refreshes a locally cached janrain capture screen.
janrain_capture_screens_menu_alter Implements hook_menu_alter().
_janrain_capture_get_screens Helper function to get the list of available Janrain capture screens.
_janrain_capture_get_screen_file Gets the appropriate Janrain screen file.
_janrain_capture_get_screen_file_name Gets a santized file name for a screen and removes directories.
_janrain_capture_get_screen_machine_name Gets a machine name for a screen.