You are here

function _mimemail_url in Mime Mail 6

Same name and namespace in other branches
  1. 5 mimemail.inc \_mimemail_url()
  2. 7 mimemail.inc \_mimemail_url()

Helper function to format URLs.

Parameters

$url: A string containing an URL.

$embed_file: TRUE if the file should be embedded in the message. Defaults to FALSE.

Return value

A string containing an absolute URL or mailto.

2 calls to _mimemail_url()
_mimemail_expand_links in ./mimemail.inc
Callback for preg_replace_callback()
_mimemail_file in ./mimemail.inc
Helper function to extract local files.

File

./mimemail.inc, line 596
Common mail functions for sending e-mail. Originally written by Gerhard.

Code

function _mimemail_url($url, $embed_file = FALSE) {
  global $base_url;
  $url = urldecode($url);

  // If the URL is absolute or a mailto, return it as-is.
  if (strpos($url, '://') !== FALSE || preg_match('!(mailto|callto|tel)\\:!', $url)) {
    $url = str_replace(' ', '%20', $url);
    return $url;
  }
  elseif (variable_get('mimemail_linkonly', 0) && preg_match('!\\.(png|gif|jpg|jpeg)$!i', $url)) {
    $url = $base_url . $url;
    $url = str_replace(' ', '%20', $url);
    return $url;
  }
  $url = preg_replace('!^' . base_path() . '!', '', $url, 1);

  // If we're processing to embed the file, we're done here so return.
  if ($embed_file) {
    return $url;
  }
  if (!preg_match('!^\\?q=*!', $url)) {
    $strip_clean = TRUE;
  }
  $url = str_replace('?q=', '', $url);
  @(list($url, $fragment) = explode('#', $url, 2));
  @(list($path, $query) = explode('?', $url, 2));

  // If we're dealing with an intra-document reference, return it.
  if (empty($path)) {
    return '#' . $fragment;
  }

  // Get a list of enabled languages.
  $languages = language_list('enabled');
  $languages = $languages[1];

  // Default language settings.
  $prefix = '';
  $language = language_default();

  // Check for language prefix.
  $args = explode('/', $path);
  foreach (array_keys($languages) as $lang) {
    if ($args[0] == $lang) {
      $prefix = array_shift($args) . '/';
      $language = $languages[$lang];
      $path = implode('/', $args);
      break;
    }
  }
  $options = array(
    'query' => $query,
    'fragment' => $fragment,
    'absolute' => TRUE,
    'language' => $language,
    'prefix' => $prefix,
  );
  $url = url($path, $options);

  // If url() added a ?q= where there should not be one, remove it.
  if ($strip_clean) {
    $url = preg_replace('!\\?q=!', '', $url);
  }
  $url = str_replace('+', '%2B', $url);
  return $url;
}