You are here

function js_execute_request in JS Callback Handler 7.2

Loads the requested module and executes the requested callback.

1 call to js_execute_request()
js.php in ./js.php
An optimized page execution used to serve JavaScript AJAX requests using a minimally bootstrapped Drupal installation.

File

./js.module, line 604
JavaScript callback handler module.

Code

function js_execute_request() {

  // Provide a global JS variable that will be used through out the request.
  global $_js;
  global $conf;

  // Immediately start capturing any output.
  ob_start();

  // Override error and exception handlers to capture output.
  if (empty($conf['js_silence_php_errors'])) {
    set_error_handler('_js_error_handler');
    set_exception_handler('_js_exception_handler');
    register_shutdown_function('_js_fatal_error_handler');
  }

  // Initialize the cache system and our custom handler.
  _js_cache_initialize();

  // Immediately clone the request method so it cannot be altered any further.
  static $method;
  if (!isset($method)) {
    $method = $_SERVER['REQUEST_METHOD'];
  }

  // Extract any parameters matching the unique "js_" prefixed names from the
  // referenced global request data and then unset it so it is not processed
  // again.
  $_js['module'] = FALSE;
  $_js['callback'] = FALSE;
  $_js['token'] = FALSE;
  $_js['theme'] = FALSE;
  $global_method = '_' . strtoupper($method);
  foreach ($_js as $key => $value) {
    if (isset($GLOBALS[$global_method]["js_{$key}"])) {
      $_js[$key] = check_plain($GLOBALS[$global_method]["js_{$key}"]);
      unset($GLOBALS[$global_method]["js_{$key}"]);
    }
  }

  // Prevent Devel from hi-jacking the output.
  $GLOBALS['devel_shutdown'] = FALSE;

  // Retrieve arguments for the current request.
  $_js['args'] = explode('/', $_GET['q']);

  // Determine the JS "endpoint".
  $endpoint = variable_get('js_endpoint', 'js');

  // Determine if there is a language prefix in the path.
  $_js['lang'] = FALSE;
  if (!empty($_js['args'][0]) && !empty($_js['args'][1]) && $_js['args'][1] === $endpoint) {
    $_js['lang'] = check_plain(array_shift($_js['args']));
  }

  // Remove the endpoint argument.
  if (!empty($_js['args'][0]) && $_js['args'][0] === $endpoint) {
    array_shift($_js['args']);
  }

  // Load common functions used for all requests.
  module_load_include('inc', 'js', 'includes/common');
  $info = NULL;

  // Set the default request result to JS_MENU_NOT_FOUND. The responsibility
  // of changing the results falls to the request handler.
  $request_result = JS_MENU_NOT_FOUND;

  // If a request does not provide a module or callback, we cannot retrieve a
  // valid callback info to validate against. Treat this request as a normal
  // GET request in the browser, but only return the contents of the page. This
  // is useful for certain tasks like populating modal content.
  if (!$_js['module'] || !$_js['callback']) {
    module_load_include('inc', 'js', 'includes/get');
    $request_result = js_get_page();
  }
  else {

    // Only continue if a valid callback is found. Otherwise it will will return
    // the JS_MENU_NOT_FOUND integer.
    $info = js_get_callback($_js['module'], $_js['callback']);
    if (!$info) {
      drupal_set_message(t('The requested callback "%callback" defined by the "%module" module could not be loaded. Please check your configuration and try again.', array(
        '%callback' => $_js['callback'],
        '%module' => $_js['module'],
      )), 'error', FALSE);
    }
    elseif (!in_array($method, $info['methods'])) {
      $request_result = JS_MENU_METHOD_NOT_ALLOWED;
    }
    else {

      // Set the delivery callback found in the info.
      js_delivery_callback($info['delivery callback']);

      // Enforce token validation if the token variable in the callback info is
      // not explicitly set to a boolean equaling FALSE.
      $token_valid = FALSE;
      $validate_token = $info['token'] !== FALSE;

      // If a token should be validated, Drupal requires a minimum
      // DRUPAL_BOOTSTRAP_SESSION level. The current SESSION user must also not
      // be anonymous as the token would be the same for anonymous users. This
      // is a security requirement.
      if ($validate_token) {
        js_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
        drupal_load('module', 'user');
        $token_valid = !user_is_anonymous() && drupal_valid_token($_js['token'], 'js-' . $_js['module'] . '-' . $_js['callback']);
      }

      // Set the proper request result and display a message if a token should
      // be validated and it didn't.
      if ($validate_token && !$token_valid) {
        $request_result = JS_MENU_ACCESS_DENIED;
        drupal_set_message(t('Cannot complete request. The token provided was either missing or invalid. Please refresh this page or try logging out and back in again.'), 'error', FALSE);
      }
      else {
        module_load_include('inc', 'js', 'includes/callback');
        $request_result = js_callback_execute($info);
      }
    }
  }

  // Deliver the result.
  js_deliver($request_result, $info);
}