FrontNameResolver.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Backend area front name resolver. Reads front name from configuration
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Backend\App\Area;
  9. use Magento\Backend\Setup\ConfigOptionsList;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\DeploymentConfig;
  12. use Magento\Store\Model\ScopeInterface;
  13. use Magento\Store\Model\Store;
  14. /**
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class FrontNameResolver implements \Magento\Framework\App\Area\FrontNameResolverInterface
  19. {
  20. const XML_PATH_USE_CUSTOM_ADMIN_PATH = 'admin/url/use_custom_path';
  21. const XML_PATH_CUSTOM_ADMIN_PATH = 'admin/url/custom_path';
  22. const XML_PATH_USE_CUSTOM_ADMIN_URL = 'admin/url/use_custom';
  23. const XML_PATH_CUSTOM_ADMIN_URL = 'admin/url/custom';
  24. /**
  25. * Backend area code
  26. */
  27. const AREA_CODE = 'adminhtml';
  28. /**
  29. * @var array
  30. */
  31. protected $standardPorts = ['http' => '80', 'https' => '443'];
  32. /**
  33. * @var string
  34. */
  35. protected $defaultFrontName;
  36. /**
  37. * @var \Magento\Backend\App\ConfigInterface
  38. */
  39. protected $config;
  40. /**
  41. * Deployment configuration
  42. *
  43. * @var DeploymentConfig
  44. */
  45. protected $deploymentConfig;
  46. /**
  47. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  48. */
  49. private $scopeConfig;
  50. /**
  51. * @param \Magento\Backend\App\Config $config
  52. * @param DeploymentConfig $deploymentConfig
  53. * @param ScopeConfigInterface $scopeConfig
  54. */
  55. public function __construct(
  56. \Magento\Backend\App\Config $config,
  57. DeploymentConfig $deploymentConfig,
  58. ScopeConfigInterface $scopeConfig
  59. ) {
  60. $this->config = $config;
  61. $this->defaultFrontName = $deploymentConfig->get(ConfigOptionsList::CONFIG_PATH_BACKEND_FRONTNAME);
  62. $this->scopeConfig = $scopeConfig;
  63. }
  64. /**
  65. * Retrieve area front name
  66. *
  67. * @param bool $checkHost If true, verify front name is valid for this url (hostname is correct)
  68. * @return string|bool
  69. */
  70. public function getFrontName($checkHost = false)
  71. {
  72. if ($checkHost && !$this->isHostBackend()) {
  73. return false;
  74. }
  75. $isCustomPathUsed = (bool)(string)$this->config->getValue(self::XML_PATH_USE_CUSTOM_ADMIN_PATH);
  76. if ($isCustomPathUsed) {
  77. return (string)$this->config->getValue(self::XML_PATH_CUSTOM_ADMIN_PATH);
  78. }
  79. return $this->defaultFrontName;
  80. }
  81. /**
  82. * Return whether the host from request is the backend host
  83. *
  84. * @return bool
  85. */
  86. public function isHostBackend()
  87. {
  88. if ($this->scopeConfig->getValue(self::XML_PATH_USE_CUSTOM_ADMIN_URL, ScopeInterface::SCOPE_STORE)) {
  89. $backendUrl = $this->scopeConfig->getValue(self::XML_PATH_CUSTOM_ADMIN_URL, ScopeInterface::SCOPE_STORE);
  90. } else {
  91. $backendUrl = $this->scopeConfig->getValue(Store::XML_PATH_UNSECURE_BASE_URL, ScopeInterface::SCOPE_STORE);
  92. }
  93. $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
  94. return stripos($this->getHostWithPort($backendUrl), $host) !== false;
  95. }
  96. /**
  97. * Get host with port
  98. *
  99. * @param string $url
  100. * @return mixed|string
  101. */
  102. private function getHostWithPort($url)
  103. {
  104. $scheme = parse_url(trim($url), PHP_URL_SCHEME);
  105. $host = parse_url(trim($url), PHP_URL_HOST);
  106. $port = parse_url(trim($url), PHP_URL_PORT);
  107. if (!$port) {
  108. $port = isset($this->standardPorts[$scheme]) ? $this->standardPorts[$scheme] : null;
  109. }
  110. return isset($port) ? $host . ':' . $port : $host;
  111. }
  112. }