You are here

function _botcha_url in BOTCHA Spam Prevention 7.2

Same name and namespace in other branches
  1. 6 botcha.botcha.inc \_botcha_url()
  2. 6.2 botcha.inc \_botcha_url()
  3. 6.3 botcha.module \_botcha_url()
  4. 7 botcha.botcha.inc \_botcha_url()
  5. 7.3 botcha.module \_botcha_url()

Helper function - build a url (allows full URI in $url)

1 call to _botcha_url()
BotchaRecipeObscureUrl::getJsValue in controller/botcha_recipe.controller.inc

File

./botcha.inc, line 13
General BOTCHA functionality and helper functions.

Code

function _botcha_url($url, $options = array()) {
  global $base_url;
  $path = '';
  $query = '';
  $abs_base = $base_url . '/';
  $absolute = 0 === strpos($url, $abs_base);

  // Figure out if absolute url is given to keep it that way.
  $base = $absolute ? $abs_base_url : base_path();
  $url = 0 === strpos($url, $base) ? substr($url, strlen($base)) : ltrim($url, '/');

  // convert to local variables:
  // $scheme $host $port $user $pass $path $query $fragment
  extract(parse_url(urldecode($url)));

  // For non-clean URLs we need to convert query to array
  if (!empty($query) && !is_array($query)) {
    $params = explode('&', $query);
    $query = array();
    foreach ($params as $param) {
      $param = explode('=', $param, 2);
      $query[$param[0]] = isset($param[1]) ? rawurldecode($param[1]) : '';
    }
  }
  foreach ($options as $key => $value) {
    ${$key} = is_array($value) && is_array(${$key}) ? array_merge(${$key}, $value) : $value;
  }

  //  $result = url($path, array('query' => $query, 'fragment' => $fragment, 'absolute' => $absolute));
  // Unfortunately, url() messes up when $path has language prefix already.
  // Here we reproduce a part of url() to do the job right
  $fragment = !empty($fragment) ? '#' . $fragment : '';
  if (is_array($query)) {
    $query = drupal_http_build_query($query);
  }
  $prefix = !empty($prefix) ? $prefix : '';
  $prefix = empty($path) ? rtrim($prefix, '/') : $prefix;
  $path = drupal_encode_path($prefix . $path);
  if (variable_get('clean_url', '0')) {

    // With Clean URLs.
    $result = !empty($query) ? $base . $path . '?' . $query . $fragment : $base . $path . $fragment;
  }
  else {

    // Without Clean URLs.
    $variables = array();
    if (!empty($path)) {
      $variables[] = 'q=' . $path;
    }
    if (!empty($query)) {
      $variables[] = $query;
    }
    $query = join('&', $variables);
    if (!empty($query)) {
      static $script;
      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' : '';
      }
      $result = $base . $script . '?' . $query . $fragment;
    }
    else {
      $result = $base . $fragment;
    }
  }
  return $result;
}