1
1
<?php
2
+
2
3
/**
3
4
* Copyright © Magento, Inc. All rights reserved.
4
5
* See COPYING.txt for license details.
5
6
*/
7
+
6
8
namespace Magento \Framework \App ;
7
9
8
10
use Magento \Framework \App \Filesystem \DirectoryList ;
9
- use Magento \Framework \Filesystem ;
10
11
use Magento \Framework \Event \Manager ;
12
+ use Magento \Framework \Filesystem ;
11
13
12
14
/**
13
15
* Application Maintenance Mode
@@ -17,20 +19,20 @@ class MaintenanceMode
17
19
/**
18
20
* Maintenance flag file name
19
21
*
20
- * DO NOT consolidate this file and the IP white list into one.
22
+ * DO NOT consolidate this file and the IP allow list into one.
21
23
* It is going to work much faster in 99% of cases: the isOn() will return false whenever file doesn't exist.
22
24
*/
23
- const FLAG_FILENAME = '.maintenance.flag ' ;
25
+ public const FLAG_FILENAME = '.maintenance.flag ' ;
24
26
25
27
/**
26
28
* IP-addresses file name
27
29
*/
28
- const IP_FILENAME = '.maintenance.ip ' ;
30
+ public const IP_FILENAME = '.maintenance.ip ' ;
29
31
30
32
/**
31
33
* Maintenance flag dir
32
34
*/
33
- const FLAG_DIR = DirectoryList::VAR_DIR ;
35
+ public const FLAG_DIR = DirectoryList::VAR_DIR ;
34
36
35
37
/**
36
38
* Path to store files
@@ -45,36 +47,59 @@ class MaintenanceMode
45
47
private $ eventManager ;
46
48
47
49
/**
48
- * @param \Magento\Framework\Filesystem $filesystem
50
+ * @param Filesystem $filesystem
51
+ * @param Utility\IPAddress $ipAddress
49
52
* @param Manager|null $eventManager
50
53
*/
51
- public function __construct (Filesystem $ filesystem , ?Manager $ eventManager = null )
52
- {
54
+ public function __construct (
55
+ Filesystem $ filesystem ,
56
+ private readonly Utility \IPAddress $ ipAddress ,
57
+ Manager $ eventManager = null ,
58
+ ) {
53
59
$ this ->flagDir = $ filesystem ->getDirectoryWrite (self ::FLAG_DIR );
54
60
$ this ->eventManager = $ eventManager ?: ObjectManager::getInstance ()->get (Manager::class);
55
61
}
56
62
57
63
/**
58
64
* Checks whether mode is on
59
65
*
60
- * Optionally specify an IP-address to compare against the white list
66
+ * Optionally specify an IP-address to compare against the allow list
61
67
*
62
68
* @param string $remoteAddr
69
+ *
63
70
* @return bool
64
71
*/
65
72
public function isOn ($ remoteAddr = '' )
66
73
{
67
74
if (!$ this ->flagDir ->isExist (self ::FLAG_FILENAME )) {
68
75
return false ;
69
76
}
70
- $ info = $ this ->getAddressInfo ();
71
- return !in_array ($ remoteAddr , $ info );
77
+
78
+ if ($ remoteAddr ) {
79
+ $ allowedAddresses = $ this ->getAddressInfo ();
80
+ foreach ($ allowedAddresses as $ allowed ) {
81
+ if ($ allowed === $ remoteAddr ) {
82
+ return false ;
83
+ }
84
+
85
+ if (!$ this ->ipAddress ->isValidRange ($ allowed )) {
86
+ continue ;
87
+ }
88
+
89
+ if ($ this ->ipAddress ->rangeContainsAddress ($ allowed , $ remoteAddr )) {
90
+ return false ;
91
+ }
92
+ }
93
+ }
94
+
95
+ return true ;
72
96
}
73
97
74
98
/**
75
99
* Sets maintenance mode "on" or "off"
76
100
*
77
101
* @param bool $isOn
102
+ *
78
103
* @return bool
79
104
*/
80
105
public function set ($ isOn )
@@ -84,17 +109,21 @@ public function set($isOn)
84
109
if ($ isOn ) {
85
110
return $ this ->flagDir ->touch (self ::FLAG_FILENAME );
86
111
}
112
+
87
113
if ($ this ->flagDir ->isExist (self ::FLAG_FILENAME )) {
88
114
return $ this ->flagDir ->delete (self ::FLAG_FILENAME );
89
115
}
116
+
90
117
return true ;
91
118
}
92
119
93
120
/**
94
121
* Sets list of allowed IP addresses
95
122
*
96
123
* @param string $addresses
124
+ *
97
125
* @return bool
126
+ *
98
127
* @throws \InvalidArgumentException
99
128
*/
100
129
public function setAddresses ($ addresses )
@@ -104,11 +133,14 @@ public function setAddresses($addresses)
104
133
if ($ this ->flagDir ->isExist (self ::IP_FILENAME )) {
105
134
return $ this ->flagDir ->delete (self ::IP_FILENAME );
106
135
}
136
+
107
137
return true ;
108
138
}
139
+
109
140
if (!preg_match ('/^[^\s,]+(,[^\s,]+)*$/ ' , $ addresses )) {
110
141
throw new \InvalidArgumentException ("One or more IP-addresses is expected (comma-separated) \n" );
111
142
}
143
+
112
144
$ result = $ this ->flagDir ->writeFile (self ::IP_FILENAME , $ addresses );
113
145
return false !== $ result ;
114
146
}
0 commit comments