You are here

url_alter.module in URL alter 6

File

url_alter.module
View source
<?php

/**
 * Implementation of hook_help().
 */
function url_alter_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/url-alter':
      module_load_install('url_alter');
      if (url_alter_check_status() && user_access('administer site configuration')) {
        drupal_set_message(t('One or more problems were detected with your URL alter module configuration. Check the <a href="@status">status report</a> for more information.', array(
          '@status' => url('admin/reports/status'),
        )), 'error', FALSE);
      }
      return '<p>' . t('Do not use %php tags around your PHP code. Note that executing incorrect PHP-code can severely break your Drupal site.', array(
        '%php' => '<?php ?>',
      )) . '</p>';
  }
}

/**
 * Implementation of hook_boot().
 */
function url_alter_boot() {

  // This function is blank so the module will be included with bootstrap
  // modules in module_list().
}

/**
 * Implementation of hook_perm().
 */
function url_alter_perm() {
  return array(
    'administer custom_url_rewrite functions',
  );
}

/**
 * Implementation of hook_menu().
 */
function url_alter_menu() {
  $items['admin/settings/url-alter'] = array(
    'title' => 'URL alter',
    'description' => 'Administer custom_url_rewrite function code',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'url_alter_settings_form',
    ),
    'access arguments' => array(
      'administer custom_url_rewrite functions',
    ),
    'file' => 'url_alter.admin.inc',
  );
  return $items;
}

/**
 * Define custom_url_rewrite_inbound() if it is not already defined.
 */
if (!function_exists('custom_url_rewrite_inbound')) {

  // Setting this constant lets url_alter_requirements() know this is working.
  define('URL_ALTER_CUSTOM_URL_REWRITE_INBOUND', TRUE);
  function custom_url_rewrite_inbound(&$result, $path, $path_language) {

    // Run all hook implementations of hook_url_inbound_alter().
    foreach (array_reverse(module_implements('url_inbound_alter')) as $module) {

      // These hooks must run in reverse of url_outbound_alter().
      $function = $module . '_url_inbound_alter';
      $function($result, $path, $path_language);
    }
  }
}

/**
 * Implementation of hook_url_inbound_alter().
 */
function url_alter_url_inbound_alter(&$result, $path, $path_language) {
  if (!url_alter_is_disabled() && ($code = variable_get('url_alter_inbound', ''))) {

    // We can not use drupal_eval() here since we need to be able to modify
    // the $result parameter.
    eval($code);
  }
}

/**
 * Define custom_url_rewrite_outbound() if it is not already defined.
 */
if (!function_exists('custom_url_rewrite_outbound')) {

  // Setting this constant lets url_alter_requirements() know this is working.
  define('URL_ALTER_CUSTOM_URL_REWRITE_OUTBOUND', TRUE);
  function custom_url_rewrite_outbound(&$path, &$options, $original_path) {

    // Run all hook implementations of hook_url_outbound_alter().
    foreach (module_implements('url_outbound_alter') as $module) {
      $function = $module . '_url_outbound_alter';
      $function($path, $options, $original_path);
    }
  }
}

/**
 * Implementation of hook_url_outbound_alter().
 */
function url_alter_url_outbound_alter(&$path, &$options, $original_path) {
  if (!url_alter_is_disabled() && ($code = variable_get('url_alter_outbound', ''))) {

    // We can not use drupal_eval() here since we need to be able to modify
    // the $path and $options parameters.
    eval($code);
  }
}

/**
 * Check if running the eval() url_alter code has been disabled.
 *
 * @return
 *   TRUE if the user is on the admin settings page or has disabled it via the
 *   query parameter, or FALSE otherwise.
 */
function url_alter_is_disabled() {
  static $is_disabled;
  if (!isset($is_disabled)) {
    $is_disabled = FALSE;
    if (isset($_GET['url-alter-kill'])) {

      // Always disable when the query parameter is defined.
      $is_disabled = TRUE;
    }
    elseif (isset($_GET['q']) && $_GET['q'] == 'admin/settings/url-alter') {

      // Always disable on the settings page.
      $is_disabled = TRUE;
    }
  }
  return $is_disabled;
}

/**
 * Fetch information about a function using reflection.
 *
 * @param $function
 *   The name of the function.
 * @return
 *   A ReflectionFunction object with added file, code, and location
 *   attributes, or FALSE if the function does not exist.
 */
function url_alter_get_function_info($function) {
  if (!function_exists($function) || !class_exists('ReflectionFunction')) {
    return FALSE;
  }
  $info = new ReflectionFunction($function);
  $info->file = trim(substr($info
    ->getFileName(), strlen(getcwd())), '\\/');
  $info->location = t('line @line in @file', array(
    '@file' => $info->file,
    '@line' => $info
      ->getStartLine(),
  ));
  $file_contents = file_get_contents($info
    ->getFileName());
  $file_contents = explode("\n", $file_contents);
  $info->code = array_slice($file_contents, $info
    ->getStartLine(), $info
    ->getEndLine() - $info
    ->getStartLine() - 1);
  $info->code = implode("\n", $info->code);
  return $info;
}

Functions

Namesort descending Description
url_alter_boot Implementation of hook_boot().
url_alter_get_function_info Fetch information about a function using reflection.
url_alter_help Implementation of hook_help().
url_alter_is_disabled Check if running the eval() url_alter code has been disabled.
url_alter_menu Implementation of hook_menu().
url_alter_perm Implementation of hook_perm().
url_alter_url_inbound_alter Implementation of hook_url_inbound_alter().
url_alter_url_outbound_alter Implementation of hook_url_outbound_alter().