MaintenanceMode.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. /**
  10. * Application Maintenance Mode
  11. */
  12. class MaintenanceMode
  13. {
  14. /**
  15. * Maintenance flag file name
  16. *
  17. * DO NOT consolidate this file and the IP white list into one.
  18. * It is going to work much faster in 99% of cases: the isOn() will return false whenever file doesn't exist.
  19. */
  20. const FLAG_FILENAME = '.maintenance.flag';
  21. /**
  22. * IP-addresses file name
  23. */
  24. const IP_FILENAME = '.maintenance.ip';
  25. /**
  26. * Maintenance flag dir
  27. */
  28. const FLAG_DIR = DirectoryList::VAR_DIR;
  29. /**
  30. * Path to store files
  31. *
  32. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  33. */
  34. protected $flagDir;
  35. /**
  36. * Constructor
  37. *
  38. * @param \Magento\Framework\Filesystem $filesystem
  39. */
  40. public function __construct(Filesystem $filesystem)
  41. {
  42. $this->flagDir = $filesystem->getDirectoryWrite(self::FLAG_DIR);
  43. }
  44. /**
  45. * Checks whether mode is on
  46. *
  47. * Optionally specify an IP-address to compare against the white list
  48. *
  49. * @param string $remoteAddr
  50. * @return bool
  51. */
  52. public function isOn($remoteAddr = '')
  53. {
  54. if (!$this->flagDir->isExist(self::FLAG_FILENAME)) {
  55. return false;
  56. }
  57. $info = $this->getAddressInfo();
  58. return !in_array($remoteAddr, $info);
  59. }
  60. /**
  61. * Sets maintenance mode "on" or "off"
  62. *
  63. * @param bool $isOn
  64. * @return bool
  65. */
  66. public function set($isOn)
  67. {
  68. if ($isOn) {
  69. return $this->flagDir->touch(self::FLAG_FILENAME);
  70. }
  71. if ($this->flagDir->isExist(self::FLAG_FILENAME)) {
  72. return $this->flagDir->delete(self::FLAG_FILENAME);
  73. }
  74. return true;
  75. }
  76. /**
  77. * Sets list of allowed IP addresses
  78. *
  79. * @param string $addresses
  80. * @return bool
  81. * @throws \InvalidArgumentException
  82. */
  83. public function setAddresses($addresses)
  84. {
  85. $addresses = (string)$addresses;
  86. if (empty($addresses)) {
  87. if ($this->flagDir->isExist(self::IP_FILENAME)) {
  88. return $this->flagDir->delete(self::IP_FILENAME);
  89. }
  90. return true;
  91. }
  92. if (!preg_match('/^[^\s,]+(,[^\s,]+)*$/', $addresses)) {
  93. throw new \InvalidArgumentException("One or more IP-addresses is expected (comma-separated)\n");
  94. }
  95. $result = $this->flagDir->writeFile(self::IP_FILENAME, $addresses);
  96. return false !== $result ? true : false;
  97. }
  98. /**
  99. * Get list of IP addresses effective for maintenance mode
  100. *
  101. * @return string[]
  102. */
  103. public function getAddressInfo()
  104. {
  105. if ($this->flagDir->isExist(self::IP_FILENAME)) {
  106. $temp = $this->flagDir->readFile(self::IP_FILENAME);
  107. return explode(',', trim($temp));
  108. } else {
  109. return [];
  110. }
  111. }
  112. }