You are here

function Net_URL::__construct in Flickr API 5

PHP5 Constructor

Parses the given url and stores the various parts Defaults are used in certain cases

Parameters

string $url Optional URL:

bool $useBrackets Whether to use square brackets when: multiple querystrings with the same name exist

1 call to Net_URL::__construct()
Net_URL::Net_URL in phpFlickr/PEAR/Net/URL.php
PHP4 Constructor

File

phpFlickr/PEAR/Net/URL.php, line 121

Class

Net_URL

Code

function __construct($url = null, $useBrackets = true) {
  $HTTP_SERVER_VARS = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
  $this->useBrackets = $useBrackets;
  $this->url = $url;
  $this->user = '';
  $this->pass = '';
  $this->host = '';
  $this->port = 80;
  $this->path = '';
  $this->querystring = array();
  $this->anchor = '';

  // Only use defaults if not an absolute URL given
  if (!preg_match('/^[a-z0-9]+:\\/\\//i', $url)) {
    $this->protocol = @$HTTP_SERVER_VARS['HTTPS'] == 'on' ? 'https' : 'http';

    /**
     * Figure out host/port
     */
    if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) and preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) {
      $host = $matches[1];
      if (!empty($matches[3])) {
        $port = $matches[3];
      }
      else {
        $port = $this
          ->getStandardPort($this->protocol);
      }
    }
    $this->user = '';
    $this->pass = '';
    $this->host = !empty($host) ? $host : (isset($HTTP_SERVER_VARS['SERVER_NAME']) ? $HTTP_SERVER_VARS['SERVER_NAME'] : 'localhost');
    $this->port = !empty($port) ? $port : (isset($HTTP_SERVER_VARS['SERVER_PORT']) ? $HTTP_SERVER_VARS['SERVER_PORT'] : $this
      ->getStandardPort($this->protocol));
    $this->path = !empty($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : '/';
    $this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this
      ->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
    $this->anchor = '';
  }

  // Parse the url and store the various parts
  if (!empty($url)) {
    $urlinfo = parse_url($url);

    // Default querystring
    $this->querystring = array();
    foreach ($urlinfo as $key => $value) {
      switch ($key) {
        case 'scheme':
          $this->protocol = $value;
          $this->port = $this
            ->getStandardPort($value);
          break;
        case 'user':
        case 'pass':
        case 'host':
        case 'port':
          $this->{$key} = $value;
          break;
        case 'path':
          if ($value[0] == '/') {
            $this->path = $value;
          }
          else {
            $path = dirname($this->path) == DIRECTORY_SEPARATOR ? '' : dirname($this->path);
            $this->path = sprintf('%s/%s', $path, $value);
          }
          break;
        case 'query':
          $this->querystring = $this
            ->_parseRawQueryString($value);
          break;
        case 'fragment':
          $this->anchor = $value;
          break;
      }
    }
  }
}