You are here

rel_to_abs.module in Relative Path to Absolute URLs 7

Same filename and directory in other branches
  1. 6 rel_to_abs.module

Relative Paths to Absolute URLs

Filter for converting relative paths to absolute URLs.

File

rel_to_abs.module
View source
<?php

/**
 * @file
 * Relative Paths to Absolute URLs
 *
 * Filter for converting relative paths to absolute URLs.
 */

/**
 * Implements hook_filter_info().
 */
function rel_to_abs_filter_info() {
  $filters['rel_to_abs'] = array(
    'title' => t('Relative Paths to Absolute URLs'),
    'description' => t('Filter for convertion of relative paths to absolute URLs.'),
    'process callback' => 'rel_to_abs_filter_html',
    'settings callback' => 'rel_to_abs_filter_html_settings',
    'tips callback' => 'rel_to_abs_filter_html_tips',
    'cache' => TRUE,
  );
  return $filters;
}

/**
 * Calback for filter.
 */
function rel_to_abs_filter_html($text, $filter, $format) {
  $lang = language_default();
  $front = url('<front>', array(
    'absolute' => TRUE,
    'language' => $lang,
  ));
  $base_url = $front;
  $text = _absolute_url($text, $base_url);
  return $text;
}

/**
 * Absolute url callback.
 *
 * @param string $txt
 *   Text to be parsed.
 * @param string $base_url
 *   Base url of the site to prefix.
 *
 * @return string
 *   Processed text.
 */
function _absolute_url($txt, $base_url) {
  $needles = array(
    'href="',
    'src="',
    'background="',
  );
  $new_txt = '';
  if (substr($base_url, -1) != '/') {
    $base_url .= '/';
  }
  $new_base_url = $base_url;
  $base_url_parts = parse_url($base_url);
  foreach ($needles as $needle) {
    while ($pos = strpos($txt, $needle)) {
      $pos += strlen($needle);
      if (substr($txt, $pos, 7) != 'http://' && substr($txt, $pos, 8) != 'https://' && substr($txt, $pos, 6) != 'ftp://' && substr($txt, $pos, 7) != 'mailto:' && substr($txt, $pos, 2) != '//' && substr($txt, $pos, 1) != '#') {
        $new_txt .= substr($txt, 0, $pos) . $new_base_url;
      }
      else {
        $new_txt .= substr($txt, 0, $pos);
      }
      $txt = substr($txt, $pos);
    }
    $txt = $new_txt . $txt;
    $new_txt = '';
  }
  return $txt;
}

/**
 * Implements hook_filter_html_settings().
 */
function rel_to_abs_filter_html_settings($format) {
}

/**
 * Implements hook_filter_html_tips().
 */
function rel_to_abs_filter_html_tips($filter, $format, $long = FALSE) {
}

Functions

Namesort descending Description
rel_to_abs_filter_html Calback for filter.
rel_to_abs_filter_html_settings Implements hook_filter_html_settings().
rel_to_abs_filter_html_tips Implements hook_filter_html_tips().
rel_to_abs_filter_info Implements hook_filter_info().
_absolute_url Absolute url callback.