You are here

function js_execute_callback in JS Callback Handler 5.2

Same name and namespace in other branches
  1. 6 js.php \js_execute_callback()
  2. 7 js.php \js_execute_callback()

Loads the requested module and executes the requested callback.

Return value

The callback function's return value or one of the JS_* constants.

1 call to js_execute_callback()
js.php in ./js.php
Callback page that serves custom JavaScript requests on a Drupal installation.

File

./js.php, line 41
Callback page that serves custom JavaScript requests on a Drupal installation.

Code

function js_execute_callback() {
  global $locale;
  $args = explode('/', $_GET['q']);

  // Strip first argument 'js'.
  array_shift($args);

  // Determine module to load.
  $module = check_plain(array_shift($args));
  if (!$module || !drupal_load('module', $module)) {
    return JS_ACCESS_DENIED;
  }

  // Get info hook function name.
  $function = $module . '_js';
  if (!function_exists($function)) {
    return JS_NOT_FOUND;
  }

  // Get valid callbacks.
  $valid_callbacks = $function();
  $callback = check_plain(array_shift($args));
  if (!isset($valid_callbacks[$callback]) || !function_exists($valid_callbacks[$callback]['callback'])) {
    return JS_NOT_FOUND;
  }
  $info = $valid_callbacks[$callback];
  $full_boostrap = FALSE;

  // Bootstrap to required level.
  if (!empty($info['bootstrap'])) {
    drupal_bootstrap($info['bootstrap']);
    $full_boostrap = $info['bootstrap'] == DRUPAL_BOOTSTRAP_FULL;
  }
  if (!$full_boostrap) {

    // The following mimics the behavior of _drupal_bootstrap_full().
    // @see _drupal_bootstrap_full(), common.inc
    // Load required include files.
    if (isset($info['includes']) && is_array($info['includes'])) {
      foreach ($info['includes'] as $include) {
        if (file_exists("./includes/{$include}.inc")) {
          require_once "./includes/{$include}.inc";
        }
      }
    }

    // Always load locale.inc.
    require_once "./includes/locale.inc";

    // Set the Drupal custom error handler.
    set_error_handler('error_handler');

    // Detect string handling method.
    if (function_exists('unicode_check')) {
      unicode_check();
    }

    // Undo magic quotes.
    fix_gpc_magic();

    // Load required modules.
    $modules = array(
      $module => 0,
    );
    if (isset($info['dependencies']) && is_array($info['dependencies'])) {

      // Intersect list with active modules to avoid loading uninstalled ones.
      $dependencies = array_intersect(module_list(TRUE, FALSE), $info['dependencies']);
      foreach ($dependencies as $dependency) {
        drupal_load('module', $dependency);
        $modules[$dependency] = 0;
      }
    }

    // Reset module list.
    module_list(FALSE, TRUE, FALSE, $modules);

    // Initialize the localization system.
    // @todo We actually need to query the database whether the site has any
    // localization module enabled, and load it automatically.
    $locale = locale_initialize();

    // Invoke implementations of hook_init().
    module_invoke_all('init');
  }

  // Invoke callback function.
  return call_user_func_array($info['callback'], $args);
}