You are here

function securepages_url in Secure Pages 6

Same name and namespace in other branches
  1. 5 securepages.module \securepages_url()

Generate a URL from a Drupal menu path. Will also pass-through existing URLs.

Parameters

$path: The Drupal path being linked to, such as "admin/content/node", or an existing URL like "http://drupal.org/". The special path '<front>' may also be given and will generate the site's base URL.

$options: An associative array of additional options, with the following keys:

  • 'query' A query string to append to the link, or an array of query key/value properties.
  • 'fragment' A fragment identifier (or named anchor) to append to the link. Do not include the '#' character.
  • 'alias' (default FALSE) Whether the given path is an alias already.
  • 'external' Whether the given path is an external URL.
  • 'language' An optional language object. Used to build the URL to link to and look up the proper alias for the link.
  • 'base_url' Only used internally, to modify the base URL when a language dependent URL requires so.
  • 'prefix' Only used internally, to modify the path when a language dependent URL requires so.
  • 'secure' Specifies if the secure or insecure url should be returned.

Return value

A string containing a URL to the given path.

When creating links in modules, consider whether l() could be a better alternative than url().

3 calls to securepages_url()
securepages_form_alter in ./securepages.module
Implementation of hook_form_alter().
securepages_goto in ./securepages.module
securepage_goto()
securepages_link_alter in ./securepages.module
Implementation of hook_link_alter().

File

./securepages.module, line 307
Provide method of creating allowing certain pages to only viewable from https pages

Code

function securepages_url($path = NULL, $options = array()) {

  // Merge in defaults.
  $options += array(
    'fragment' => '',
    'query' => '',
    'alias' => FALSE,
    'prefix' => '',
    'secure' => TRUE,
  );
  if ($options['fragment']) {
    $options['fragment'] = '#' . $options['fragment'];
  }
  if (is_array($options['query'])) {
    $options['query'] = drupal_query_string_encode($options['query']);
  }
  global $base_url;
  static $script;
  static $clean_url;
  if (!isset($script)) {

    // On some web servers, such as IIS, we can't omit "index.php". So, we
    // generate "index.php?q=foo" instead of "?q=foo" on anything that is not
    // Apache.
    $script = strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === FALSE ? 'index.php' : '';
  }

  // Cache the clean_url variable to improve performance.
  if (!isset($clean_url)) {
    $clean_url = (bool) variable_get('clean_url', '0');
  }
  if (!isset($options['base_url'])) {

    // The base_url might be rewritten from the language rewrite in domain mode.
    $options['base_url'] = securepages_baseurl($options['secure']);
  }

  // Preserve the original path before aliasing.
  $original_path = $path;

  // The special path '<front>' links to the default front page.
  if ($path == '<front>') {
    $path = '';
  }
  elseif (!empty($path) && !$options['alias'] && function_exists('drupal_get_path_alias')) {
    $path = drupal_get_path_alias($path, isset($options['language']) ? $options['language']->language : '');
  }
  $base = $options['base_url'] . '/';
  $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];
  $path = securepages_urlencode($prefix . $path);
  if ($clean_url) {

    // With Clean URLs.
    if ($options['query']) {
      return $base . $path . '?' . $options['query'] . $options['fragment'];
    }
    else {
      return $base . $path . $options['fragment'];
    }
  }
  else {

    // Without Clean URLs.
    $variables = array();
    if (!empty($path)) {
      $variables[] = 'q=' . $path;
    }
    if (!empty($options['query'])) {
      $variables[] = $options['query'];
    }
    if ($query = join('&', $variables)) {
      return $base . $script . '?' . $query . $options['fragment'];
    }
    else {
      return $base . $options['fragment'];
    }
  }
}