You are here

function janrain_capture_widget_update_remote_screens in Janrain Registration 7.2

Same name and namespace in other branches
  1. 7.4 includes/janrain_capture.widget.inc \janrain_capture_widget_update_remote_screens()

Update the local cache of remote JTL and Event files aka "Screens".

1 call to janrain_capture_widget_update_remote_screens()
janrain_capture_cron in ./janrain_capture.module
Implements hook_cron().

File

includes/janrain_capture.widget.inc, line 18
Widget-related functions

Code

function janrain_capture_widget_update_remote_screens() {

  // wtb oop or enums
  static $allowedScreens = array(
    'signin',
    'edit-profile',
    'public-profile',
    'forgot',
    'verify',
  );
  static $allowedTypes = array(
    'js',
    'html',
  );
  $cacheDir = JANRAIN_CAPTURE_WIDGET_SCREEN_CACHE_DIR;

  // make sure cache directory is ready for files
  if (!file_prepare_directory($cacheDir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {

    // cache dir failure, notify and bail
    watchdog('janrain_capture', 'Failed to create screen cache directory: %directory', array(
      '%directory' => $cacheDir,
    ), WATCHDOG_ERROR);
    return;
  }

  // hook cron shouldn't run this but lets sanity check anyway
  if (!_janrain_capture_widget_is_remote_screens()) {
    return;
  }
  $screenSourceDir = variable_get('janrain_capture_screens_folder');
  $sourcePathTemplate = '%s%s.%s';
  $destPathTemplate = '%s/%s.%s';

  // grab each file
  foreach ($allowedScreens as &$name) {
    foreach ($allowedTypes as &$ext) {
      $screenSource = sprintf($sourcePathTemplate, $screenSourceDir, $name, $ext);
      $screenDest = sprintf($destPathTemplate, $cacheDir, $name, $ext);
      $response = drupal_http_request($screenSource);
      if ($response->code != 200) {

        // notify there was an issue, but try to get the other files
        watchdog('janrain_capture', $response->error, array(), WATCHDOG_ERROR);
        continue;
      }

      // use unmanaged since remote source is always authoritative
      $success = file_unmanaged_save_data($response->data, $screenDest, FILE_EXISTS_REPLACE);
      if (FALSE === $success) {

        // failed to save a file that was succesfully downloaded
        watchdog('janrain_capture', "Failed to write %screenDest", array(
          'screenDest' => $screenDest,
        ), WATCHDOG_ERROR);
      }
    }
  }
}