You are here

function janrain_capture_widget_update_remote_screens in Janrain Registration 7.4

Same name and namespace in other branches
  1. 7.2 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 25
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;
  }
  $screenSourceDirs = variable_get('janrain_capture_screens_folder');
  foreach ($screenSourceDirs as $country_id => $screenSourceDir) {
    $screen_directory = $cacheDir . '/' . $country_id . '/';
    $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, $screen_directory, $name, $ext);

        //print($screenDest);exit;
        $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;
        }

        //print($screenDest);

        //print($screen_directory);

        //exit;
        if (file_prepare_directory($screen_directory, FILE_CREATE_DIRECTORY)) {

          // use unmanaged since remote source is always authoritative
          $success = file_unmanaged_save_data($response->data, $screenDest, FILE_EXISTS_REPLACE);
        }
        else {
          watchdog('janrain_capture', t('The directory could not be created.'), array(), WATCHDOG_ERROR);
          continue;
        }
        if (FALSE === $success) {

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