SecurityInfo.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Url security information
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Url;
  9. class SecurityInfo implements \Magento\Framework\Url\SecurityInfoInterface
  10. {
  11. /**
  12. * List of secure url patterns
  13. *
  14. * @var array
  15. */
  16. protected $secureUrlsList = [];
  17. /**
  18. * List of patterns excluded form secure url list
  19. */
  20. protected $excludedUrlsList = [];
  21. /**
  22. * List of already checked urls
  23. *
  24. * @var array
  25. */
  26. protected $secureUrlsCache = [];
  27. /**
  28. * @param string[] $secureUrlList
  29. * @param string[] $excludedUrlList
  30. */
  31. public function __construct($secureUrlList = [], $excludedUrlList = [])
  32. {
  33. $this->secureUrlsList = $secureUrlList;
  34. $this->excludedUrlsList = $excludedUrlList;
  35. }
  36. /**
  37. * Check whether url is secure
  38. *
  39. * @param string $url
  40. * @return bool
  41. */
  42. public function isSecure($url)
  43. {
  44. if (!isset($this->secureUrlsCache[$url])) {
  45. $this->secureUrlsCache[$url] = false;
  46. foreach ($this->excludedUrlsList as $match) {
  47. if (strpos($url, (string)$match) === 0) {
  48. return $this->secureUrlsCache[$url];
  49. }
  50. }
  51. foreach ($this->secureUrlsList as $match) {
  52. if (strpos($url, (string)$match) === 0) {
  53. $this->secureUrlsCache[$url] = true;
  54. break;
  55. }
  56. }
  57. }
  58. return $this->secureUrlsCache[$url];
  59. }
  60. }