mirror of
https://github.com/Mibew/mibew.git
synced 2025-03-03 18:38:31 +03:00
Add secure URL generation for assets
This commit is contained in:
parent
21c0ee9134
commit
e97f3d1fbf
@ -113,8 +113,8 @@ class Application implements RouterAwareInterface, AuthenticationManagerAwareInt
|
|||||||
}
|
}
|
||||||
$authentication_manager->setOperatorFromRequest($request);
|
$authentication_manager->setOperatorFromRequest($request);
|
||||||
|
|
||||||
// Actualize properties in AssetUrlGenerator
|
// Actualize AssetUrlGenerator
|
||||||
$this->assetUrlGenerator->fromRequest($request);
|
$this->assetUrlGenerator->setRequest($request);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to match a route, check if the client can access it and add
|
// Try to match a route, check if the client can access it and add
|
||||||
|
@ -23,10 +23,10 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
|
|
||||||
class AssetUrlGenerator implements AssetUrlGeneratorInterface
|
class AssetUrlGenerator implements AssetUrlGeneratorInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $scheme;
|
protected $scheme;
|
||||||
protected $host;
|
protected $host;
|
||||||
protected $port;
|
protected $httpPort;
|
||||||
|
protected $httpsPort;
|
||||||
protected $basePath;
|
protected $basePath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,27 +35,33 @@ class AssetUrlGenerator implements AssetUrlGeneratorInterface
|
|||||||
* @param string $host The HTTP host name without port and scheme.
|
* @param string $host The HTTP host name without port and scheme.
|
||||||
* @param string $base_path The base path.
|
* @param string $base_path The base path.
|
||||||
* @param string $scheme The scheme.
|
* @param string $scheme The scheme.
|
||||||
* @param int $port The port.
|
* @param int $http_port The port which is used for HTTP requests.
|
||||||
|
* @param int $https_port The port which is used for HTTPS requests.
|
||||||
*/
|
*/
|
||||||
public function __construct($host = 'localhost', $base_path = '', $scheme = 'http', $port = 80)
|
public function __construct($host = 'localhost', $base_path = '', $scheme = 'http', $http_port = 80, $https_port = 443)
|
||||||
{
|
{
|
||||||
$this->scheme = strtolower($scheme);
|
$this->scheme = strtolower($scheme);
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->port = $port;
|
$this->httpPort = $http_port;
|
||||||
|
$this->httpsPort = $https_port;
|
||||||
$this->basePath = $base_path;
|
$this->basePath = $base_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets all needed values according to the request.
|
* Sets all needed values from the request.
|
||||||
*
|
*
|
||||||
* @param Request $request A request to get values from.
|
* @param Request $request A request to get values from.
|
||||||
*/
|
*/
|
||||||
public function fromRequest(Request $request)
|
public function setRequest(Request $request)
|
||||||
{
|
{
|
||||||
$this->setScheme($request->getScheme());
|
$this->setScheme($request->getScheme());
|
||||||
$this->setHost($request->getHost());
|
$this->setHost($request->getHost());
|
||||||
$this->setPort($request->getPort());
|
|
||||||
$this->setBasePath($request->getBasePath());
|
$this->setBasePath($request->getBasePath());
|
||||||
|
if ($request->isSecure()) {
|
||||||
|
$this->setHttpsPort($request->getPort());
|
||||||
|
} else {
|
||||||
|
$this->setHttpPort($request->getPort());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -99,23 +105,43 @@ class AssetUrlGenerator implements AssetUrlGeneratorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the port.
|
* Gets the HTTP port.
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function getPort()
|
public function getHttpPort()
|
||||||
{
|
{
|
||||||
return $this->port;
|
return $this->httpPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the port.
|
* Sets the HTTP port.
|
||||||
*
|
*
|
||||||
* @param int $port
|
* @param int $port
|
||||||
*/
|
*/
|
||||||
public function setPort($port)
|
public function setHttpPort($port)
|
||||||
{
|
{
|
||||||
$this->port = $port;
|
$this->httpPort = $port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the HTTPS port.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getHttpsPort()
|
||||||
|
{
|
||||||
|
return $this->httpsPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the HTTPS port.
|
||||||
|
*
|
||||||
|
* @param int $port
|
||||||
|
*/
|
||||||
|
public function setHttpsPort($port)
|
||||||
|
{
|
||||||
|
$this->httpsPort = $port;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,17 +169,43 @@ class AssetUrlGenerator implements AssetUrlGeneratorInterface
|
|||||||
*/
|
*/
|
||||||
public function generate($relative_path, $reference_type = AssetUrlGeneratorInterface::ABSOLUTE_PATH)
|
public function generate($relative_path, $reference_type = AssetUrlGeneratorInterface::ABSOLUTE_PATH)
|
||||||
{
|
{
|
||||||
|
return $this->doGenerate($relative_path, $reference_type, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function generateSecure($relative_path, $reference_type = AssetUrlGeneratorInterface::ABSOLUTE_PATH)
|
||||||
|
{
|
||||||
|
return $this->doGenerate($relative_path, $reference_type, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function doGenerate($relative_path, $reference_type, $force_secure)
|
||||||
|
{
|
||||||
|
$scheme = $force_secure ? 'https' : $this->getScheme();
|
||||||
$host = '';
|
$host = '';
|
||||||
$need_port = ($this->getScheme() == 'http' && $this->getPort() != 80)
|
$port = false;
|
||||||
|| ($this->getScheme() == 'https' && $this->getPort() != 443);
|
|
||||||
|
|
||||||
if ($need_port || $reference_type === AssetUrlGeneratorInterface::ABSOLUTE_URL) {
|
// Check if a non-standard port is used.
|
||||||
$host = $this->getScheme() . '://' . $this->getHost();
|
if ($scheme == 'http' && $this->getHttpPort() != 80) {
|
||||||
|
$port = $this->getHttpPort();
|
||||||
|
} elseif ($scheme == 'https' && $this->getHttpsPort() != 443) {
|
||||||
|
$port = $this->getHttpsPort();
|
||||||
|
}
|
||||||
|
|
||||||
if ($need_port) {
|
$need_host =
|
||||||
// A non standatd port is used. It should be added to the
|
// A user wants an absolute URL
|
||||||
// resulting URL.
|
($reference_type === AssetUrlGeneratorInterface::ABSOLUTE_URL)
|
||||||
$host .= ':' . $this->getPort();
|
// A scheme deffers from one from request.
|
||||||
|
|| $scheme !== $this->getScheme()
|
||||||
|
// A non-standard port is used.
|
||||||
|
|| $port;
|
||||||
|
|
||||||
|
if ($need_host) {
|
||||||
|
$host = $scheme . '://' . $this->getHost();
|
||||||
|
|
||||||
|
if ($port) {
|
||||||
|
$host .= ':' . $port;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,4 +44,14 @@ interface AssetUrlGeneratorInterface
|
|||||||
* @return string Asset URL.
|
* @return string Asset URL.
|
||||||
*/
|
*/
|
||||||
public function generate($relative_path, $reference_type = self::ABSOLUTE_PATH);
|
public function generate($relative_path, $reference_type = self::ABSOLUTE_PATH);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates HTTPS URL for an asset with the specified relative path.
|
||||||
|
*
|
||||||
|
* @param string $relative_path Relative path of an asset.
|
||||||
|
* @param bool|string $reference_type Indicates what type of URL should be
|
||||||
|
* generated. It is equal to one of the interface constants.
|
||||||
|
* @return string Asset URL.
|
||||||
|
*/
|
||||||
|
public function generateSecure($relative_path, $reference_type = self::ABSOLUTE_PATH);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user