BaseUrlChecker.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model;
  7. use Magento\Store\Model\ScopeInterface;
  8. /**
  9. * Verifies that the requested URL matches to base URL of store.
  10. */
  11. class BaseUrlChecker
  12. {
  13. /**
  14. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  15. */
  16. private $scopeConfig;
  17. /**
  18. * BaseUrlChecker constructor.
  19. *
  20. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  21. */
  22. public function __construct(
  23. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  24. ) {
  25. $this->scopeConfig = $scopeConfig;
  26. }
  27. /**
  28. * Performs verification.
  29. *
  30. * @param array $uri
  31. * @param \Magento\Framework\App\Request\Http $request
  32. * @return bool
  33. */
  34. public function execute($uri, $request)
  35. {
  36. $requestUri = $request->getRequestUri() ? $request->getRequestUri() : '/';
  37. $isValidSchema = !isset($uri['scheme']) || $uri['scheme'] === $request->getScheme();
  38. $isValidHost = !isset($uri['host']) || $uri['host'] === $request->getHttpHost();
  39. $isValidPath = !isset($uri['path']) || strpos($requestUri, $uri['path']) !== false;
  40. return $isValidSchema && $isValidHost && $isValidPath;
  41. }
  42. /**
  43. * Checks whether base URL verification is enabled or not.
  44. *
  45. * @return bool
  46. */
  47. public function isEnabled()
  48. {
  49. return $this->scopeConfig->isSetFlag(
  50. 'web/url/redirect_to_base',
  51. ScopeInterface::SCOPE_STORE
  52. );
  53. }
  54. /**
  55. * Checks whether frontend is completely secure or not.
  56. *
  57. * @return bool
  58. */
  59. public function isFrontendSecure()
  60. {
  61. $baseUrl = $this->scopeConfig->getValue(
  62. 'web/unsecure/base_url',
  63. ScopeInterface::SCOPE_STORE
  64. );
  65. $baseUrlParts = explode('://', $baseUrl);
  66. $baseUrlProtocol = array_shift($baseUrlParts);
  67. $isSecure = $this->scopeConfig->isSetFlag(
  68. 'web/secure/use_in_frontend',
  69. ScopeInterface::SCOPE_STORE
  70. );
  71. return $isSecure && $baseUrlProtocol == 'https';
  72. }
  73. }