Skip to content

Commit

Permalink
Fix for ports being included in HTTP_HOST
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Sep 18, 2015
1 parent 8b49eca commit 15d3d8c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions system/src/Grav/Common/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@ class Uri
*/
public function __construct()
{
$base = 'http://';
$name = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
// Remove port from HTTP_HOST generated $name
$name = Utils::substrToString($name, ':');

$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80;
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';

$root_path = str_replace(' ', '%20', rtrim(substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], 'index.php')), '/'));

// set the base
if (isset($_SERVER['HTTPS'])) {
$base = (@$_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
} else {
$base = 'http://';
}

// add the sever name
$base .= $name;

// add the port of needed
if ($port != '80' && $port != '443') {
$base .= ":".$port;
}
Expand All @@ -66,7 +73,6 @@ public function __construct()
$this->base = $base;
$this->root = $base . $root_path;
$this->url = $base . $uri;

}

/**
Expand Down
15 changes: 15 additions & 0 deletions system/src/Grav/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ public static function contains($haystack, $needle)
return $needle === '' || strpos($haystack, $needle) !== false;
}

/**
* Returns the substring of a string up to a specified needle. if not found, return the whole haytack
*
* @param $haystack
* @param $needle
* @return string
*/
public static function substrToString($haystack, $needle)
{
if (static::contains($haystack, $needle)) {
return substr($haystack, 0, strpos($haystack,$needle));
}
return $haystack;
}

/**
* Merge two objects into one.
*
Expand Down

0 comments on commit 15d3d8c

Please sign in to comment.