You are here

function janrain_capture_widget_js in Janrain Registration 7.3

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

Returns Capture widget js.

2 calls to janrain_capture_widget_js()
janrain_capture_oauth in includes/janrain_capture.endpoints.inc
Callback for the janrain_capture/oauth menu item. This serves as the redirect_uri Capture redirects the user to and performs the authentication.
janrain_capture_widget_add_scripts in includes/janrain_capture.widget.inc
Adds widget JS scripts to the page.

File

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

Code

function janrain_capture_widget_js() {
  global $base_url;
  global $base_path;
  $janrain_settings = variable_get('janrain_capture_fields2', array());
  $janrain_settings = array_merge($janrain_settings, variable_get('janrain_capture_main2', array()));
  $janrain_settings = array_merge($janrain_settings, variable_get('janrain_capture_ui2', array()));
  $janrain_settings = array_merge($janrain_settings, variable_get('janrain_capture_federate2', array()));
  $janrain_settings = array_merge($janrain_settings, variable_get('janrain_capture_backplane2', array()));

  // capture
  $settings["capture.redirectUri"] = url('janrain_capture/oauth', array(
    'absolute' => TRUE,
  ));
  $settings["capture.appId"] = $janrain_settings['capture_app_id'];
  $settings["capture.clientId"] = $janrain_settings['capture_client_id'];
  $settings["capture.responseType"] = "code";
  $settings["capture.captureServer"] = $janrain_settings['capture_address'];
  $settings["capture.registerFlow"] = $janrain_settings['registration_flow'];
  $settings["capture.recaptchaPublicKey"] = $janrain_settings['recaptcha_key'];
  $settings["capture.loadJsUrl"] = $janrain_settings['load_js'];
  $share_settings = variable_get('janrain_capture_share', array());
  if (isset($share_settings["enabled"]) && $share_settings["enabled"]) {
    $settings["packages"] = '["login","capture","share"]';
  }
  else {
    $settings["packages"] = '["login","capture"]';
  }

  // engage
  $settings["appUrl"] = $janrain_settings['engage_address'];

  // federate
  $settings["capture.federate"] = $janrain_settings['capture_sso_enabled'];
  $settings["capture.federateServer"] = 'https://' . $janrain_settings['capture_sso_address'];
  $settings["capture.federateXdReceiver"] = url(NULL, array(
    'absolute' => TRUE,
  )) . drupal_get_path('module', 'janrain_capture') . '/xdcomm.html';
  $settings["capture.federateLogoutUri"] = url('janrain_capture/simple_logout', array(
    'absolute' => TRUE,
  ));

  // backplane
  $settings["capture.backplane"] = $janrain_settings['backplane_enabled'];
  $settings["capture.backplaneBusName"] = $janrain_settings['backplane_bus_name'];
  $settings["capture.backplaneVersion"] = $janrain_settings['backplane_version'];
  if (module_exists('janrain_capture_screens')) {
    $settings["capture.stylesheets"] = "'" . file_create_url(_janrain_capture_get_screen_file('stylesheets/styles.css')) . "'";
    if ($mobile_stylesheet = _janrain_capture_get_screen_file('stylesheets/mobile-styles.css')) {
      $settings["capture.mobileStylesheets"] = "'" . file_create_url($mobile_stylesheet) . "'";
    }
    if ($ie_stylesheet = _janrain_capture_get_screen_file('stylesheets/ie-styles.css')) {
      $settings["capture.conditionalIEStylesheets"] = "'" . file_create_url($ie_stylesheet) . "'";
    }
  }
  else {
    $folder_url = variable_get('janrain_capture_screens_folder', 'file:///sites/all/themes/janrain-capture-screens/');

    // If path is local, search for user agent-specific stylesheets in the file system.
    if (strpos($folder_url, 'file:///', 0) === 0) {

      // Example of $folder_url: file:///sites/all/themes/janrain-capture-screens/
      $web_path = str_replace('file://', '', $folder_url);

      // Example of $web_path: /sites/all/themes/janrain-capture-screens/
      $fs_path = DRUPAL_ROOT . $web_path;

      // Example of $fs_path: /var/www/d7/sites/all/themes/janrain-capture-screens/

      //watchdog('janrain_capture', 'Searching for Capture screen stylesheets in: ' . $fs_path,  array(), WATCHDOG_DEBUG);
      $web_url = $base_url . $web_path;
      $is_nested = is_dir($fs_path . 'stylesheets');
      if ($is_nested) {
        $fs_path .= "stylesheets";
      }
      try {
        $dir = new DirectoryIterator($fs_path);
      } catch (UnexpectedValueException $e) {
        if ($is_nested) {
          $screens_message = 'No "stylesheets" subfolder found in the screens folder (@path). Please check the Janrain Capture module settings.';
        }
        else {
          $screens_message = 'The screens folder (@path) could not be found. Please check the Janrain Capture module settings.';
        }
        watchdog('janrain_capture', $screens_message, array(
          '@path' => $fs_path,
        ), WATCHDOG_WARNING, l(t('Janrain Capture module settings'), 'admin/config/people/janrain_capture'));
        return;
      }
      if ($is_nested) {
        $web_url .= "stylesheets/";
      }
      $css = array();
      $css_mobile = array();
      $css_ie = array();
      if ($dir) {
        foreach ($dir as $fileinfo) {
          $fn = $fileinfo
            ->getFilename();
          if (!$fileinfo
            ->isDot() && stripos($fn, '.css')) {
            switch (true) {

              // if file begins with mobile set is as a mobile style
              case stripos($fn, 'mobile') === 0:
                $css_mobile[] = "'{$web_url}{$fn}'";
                break;

              // if file begins with ie set is as an IE style
              case stripos($fn, 'ie') === 0:
                $css_ie[] = "'{$web_url}{$fn}'";
                break;

              // otherwise set it as a normal style
              default:
                $css[] = "'{$web_url}{$fn}'";
                break;
            }
          }
        }
      }
      $settings["capture.stylesheets"] = implode(', ', $css);
      $settings["capture.mobileStylesheets"] = implode(', ', $css_mobile);
      $settings["capture.conditionalIEStylesheets"] = implode(', ', $css_ie);
    }
    else {

      // Remote stylesheets
      $settings["capture.stylesheets"] = "'{$folder_url}stylesheets/styles.css'";
    }

    // Log a warning if directories are setup properly but no stylesheets were found
    if (!count($settings["capture.stylesheets"]) && !count($settings["capture.mobileStylesheets"]) && !count($settings["capture.conditionalIEStylesheets"])) {
      watchdog('janrain_capture', 'No stylesheets were found in the screens folder (@path). Please check the Janrain Capture module settings.', array(
        '@path' => $fs_path ?: $folder_url,
      ), WATCHDOG_WARNING, l(t('Janrain Capture module settings'), 'admin/config/people/janrain_capture'));
    }
  }
  $output = <<<EOD
function janrainSignOut(){
  janrain.capture.ui.endCaptureSession();
}
(function() {
  if (typeof window.janrain !== 'object') window.janrain = {};
  window.janrain.settings = {};
  window.janrain.settings.capture = {};

  // capture settings
  janrain.settings.capture.redirectUri = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.redirectUri"</span>]}';
  janrain.settings.capture.appId= '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.appId"</span>]}';
  janrain.settings.capture.clientId = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.clientId"</span>]}';
  janrain.settings.capture.responseType = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.responseType"</span>]}';
  janrain.settings.capture.captureServer = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.captureServer"</span>]}';
  janrain.settings.capture.registerFlow = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.registerFlow"</span>]}';
  janrain.settings.packages = {<span class="php-variable">$settings</span>[<span class="php-string">'packages'</span>]};

  janrain.settings.capture.setProfileCookie = true;
  janrain.settings.capture.keepProfileCookieAfterLogout = true;
  janrain.settings.capture.setProfileData = true;

  // styles
  janrain.settings.capture.stylesheets = [{<span class="php-variable">$settings</span>[<span class="php-string">"capture.stylesheets"</span>]}];

EOD;

  // mobile styles
  if (isset($settings["capture.mobileStylesheets"]) && $settings["capture.mobileStylesheets"] != '') {
    $output .= <<<EOD
  janrain.settings.capture.mobileStylesheets = [{<span class="php-variable">$settings</span>[<span class="php-string">"capture.mobileStylesheets"</span>]}];

EOD;
  }

  //IE styles
  if (isset($settings["capture.conditionalIEStylesheets"]) && $settings["capture.conditionalIEStylesheets"] != '') {
    $output .= <<<EOD
  janrain.settings.capture.conditionalIEStylesheets = [{<span class="php-variable">$settings</span>[<span class="php-string">"capture.conditionalIEStylesheets"</span>]}];

EOD;
  }

  // captcha
  if ($settings["capture.recaptchaPublicKey"] != '') {
    $output .= <<<EOD
  janrain.settings.capture.recaptchaPublicKey = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.recaptchaPublicKey"</span>]}';

EOD;
  }
  $output .= <<<EOD
  // engage settings
  janrain.settings.appUrl = '{<span class="php-variable">$settings</span>[<span class="php-string">"appUrl"</span>]}';
  janrain.settings.tokenAction = 'event';

EOD;

  // Backplane
  if ($settings["capture.backplane"]) {
    $output .= <<<EOD
  // backplane settings
  janrain.settings.capture.backplane = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.backplane"</span>]}';
  janrain.settings.capture.backplaneBusName = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.backplaneBusName"</span>]}';
  janrain.settings.capture.backplaneVersion = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.backplaneVersion"</span>]}';

EOD;
  }
  if ($settings["capture.federate"]) {
    $output .= <<<EOD
  // federate settings
  janrain.settings.capture.federate = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.federate"</span>]}';
  janrain.settings.capture.federateServer = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.federateServer"</span>]}';
  janrain.settings.capture.federateXdReceiver = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.federateXdReceiver"</span>]}';
  janrain.settings.capture.federateLogoutUri = '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.federateLogoutUri"</span>]}';

EOD;
  }
  if (!isset($_SESSION['janrain_capture_access_token'])) {
    $api = new JanrainCaptureApi();
    $api
      ->refreshAccessToken();
  }
  $access_token = "var access_token = '";
  $access_token .= isset($_SESSION['janrain_capture_access_token']) ? $_SESSION['janrain_capture_access_token'] : "";
  $access_token .= "';";
  $output .= <<<EOD
  function isReady() { janrain.ready = true; };
  if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", isReady, false);
  } else {
    window.attachEvent('onload', isReady);
  }

  var e = document.createElement('script');
  e.type = 'text/javascript';
  e.id = 'janrainAuthWidget';
  var url = document.location.protocol === 'https:' ? 'https://' : 'http://';
  url += '{<span class="php-variable">$settings</span>[<span class="php-string">"capture.loadJsUrl"</span>]}';
  e.src = url;
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(e, s);
})();
{<span class="php-variable">$access_token</span>}

EOD;
  return $output;
}