You are here

function _mimemail_url in Mime Mail 7

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

Helper function to format URLs.

Parameters

string $url: The file path.

bool $to_embed: (optional) Whether the URL is used to embed the file. Defaults to NULL.

Return value

string A processed URL.

4 calls to _mimemail_url()
MimeMailUnitTestCase::testUrl in tests/mimemail.test
MimeMailWebTestCase::testUrl in tests/mimemail.test
_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 457
Common mail functions for sending e-mail. Originally written by Gerhard.

Code

function _mimemail_url($url, $to_embed = NULL) {
  $url = urldecode($url);
  $to_link = variable_get('mimemail_linkonly', 0);
  $is_image = preg_match('!\\.(png|gif|jpg|jpeg)!i', $url);
  $is_absolute = file_uri_scheme($url) != FALSE || preg_match('!(mailto|callto|tel)\\:!', $url);
  if (!$to_embed) {
    if ($is_absolute) {
      return str_replace(' ', '%20', $url);
    }
  }
  else {
    $url = preg_replace('!^' . base_path() . '!', '', $url, 1);
    if ($is_image) {

      // Remove security token from URL, this allows for styled image embedding.
      // @see https://drupal.org/drupal-7.20-release-notes
      $url = preg_replace('/\\?itok=.*$/', '', $url);
      if ($to_link) {

        // Exclude images from embedding if needed.
        $url = file_create_url($url);
        $url = str_replace(' ', '%20', $url);
      }
    }
    return $url;
  }
  $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.
  $path = trim($path, '/');
  $args = explode('/', $path);
  foreach ($languages as $lang) {
    if (!empty($args) && $args[0] == $lang->prefix) {
      $prefix = array_shift($args);
      $language = $lang;
      $path = implode('/', $args);
      break;
    }
  }
  $options = array(
    'query' => $query ? drupal_get_query_array($query) : array(),
    '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 (preg_match('!^\\?q=*!', $url)) {
    $url = preg_replace('!\\?q=!', '', $url);
  }
  $url = str_replace('+', '%2B', $url);
  return $url;
}