@@ -13,161 +13,161 @@ |
||
13 | 13 | */ |
14 | 14 | class IpAccess extends Object |
15 | 15 | { |
16 | - /** |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $allowedIps = array(); |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $allowedIps = array(); |
|
20 | 20 | |
21 | - /** |
|
22 | - * @config |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - private static $allowed_ips = array(); |
|
21 | + /** |
|
22 | + * @config |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + private static $allowed_ips = array(); |
|
26 | 26 | |
27 | - /** |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - private $ip = ''; |
|
27 | + /** |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + private $ip = ''; |
|
31 | 31 | |
32 | - /** |
|
33 | - * IpAccess constructor. |
|
34 | - * |
|
35 | - * @param string $ip |
|
36 | - * @param array $allowedIps |
|
37 | - */ |
|
38 | - public function __construct($ip = '', $allowedIps = array()) |
|
39 | - { |
|
40 | - parent::__construct(); |
|
41 | - $this->ip = $ip; |
|
32 | + /** |
|
33 | + * IpAccess constructor. |
|
34 | + * |
|
35 | + * @param string $ip |
|
36 | + * @param array $allowedIps |
|
37 | + */ |
|
38 | + public function __construct($ip = '', $allowedIps = array()) |
|
39 | + { |
|
40 | + parent::__construct(); |
|
41 | + $this->ip = $ip; |
|
42 | 42 | |
43 | - self::config()->allowed_ips = $allowedIps; |
|
44 | - } |
|
43 | + self::config()->allowed_ips = $allowedIps; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * @param $ip |
|
48 | - */ |
|
49 | - public function setIp($ip) |
|
50 | - { |
|
51 | - $this->ip = $ip; |
|
52 | - } |
|
46 | + /** |
|
47 | + * @param $ip |
|
48 | + */ |
|
49 | + public function setIp($ip) |
|
50 | + { |
|
51 | + $this->ip = $ip; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public function getAllowedIps() |
|
58 | - { |
|
59 | - if (!empty($this->allowedIps)) { |
|
60 | - Deprecation::notice('1.1', 'Use the "IpAccess.allowed_ips" config setting instead'); |
|
61 | - self::config()->allowed_ips = $this->allowedIps; |
|
62 | - } |
|
63 | - return (array)self::config()->allowed_ips; |
|
64 | - } |
|
54 | + /** |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public function getAllowedIps() |
|
58 | + { |
|
59 | + if (!empty($this->allowedIps)) { |
|
60 | + Deprecation::notice('1.1', 'Use the "IpAccess.allowed_ips" config setting instead'); |
|
61 | + self::config()->allowed_ips = $this->allowedIps; |
|
62 | + } |
|
63 | + return (array)self::config()->allowed_ips; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function hasAccess() |
|
70 | - { |
|
71 | - return (!(bool)Config::inst()->get('IpAccess', 'enabled') |
|
72 | - || empty($this->getAllowedIps()) |
|
73 | - || $this->matchExact() |
|
74 | - || $this->matchRange() |
|
75 | - || $this->matchCIDR() |
|
76 | - || $this->matchWildCard()); |
|
77 | - } |
|
66 | + /** |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function hasAccess() |
|
70 | + { |
|
71 | + return (!(bool)Config::inst()->get('IpAccess', 'enabled') |
|
72 | + || empty($this->getAllowedIps()) |
|
73 | + || $this->matchExact() |
|
74 | + || $this->matchRange() |
|
75 | + || $this->matchCIDR() |
|
76 | + || $this->matchWildCard()); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @param Controller $controller |
|
81 | - * @throws SS_HTTPResponse_Exception |
|
82 | - */ |
|
83 | - public function respondNoAccess(Controller $controller) |
|
84 | - { |
|
85 | - $response = null; |
|
86 | - if (class_exists('ErrorPage', true)) { |
|
87 | - $response = ErrorPage::response_for(403); |
|
88 | - } |
|
89 | - $controller->httpError(403, $response ? $response : 'The requested page could not be found.'); |
|
90 | - } |
|
79 | + /** |
|
80 | + * @param Controller $controller |
|
81 | + * @throws SS_HTTPResponse_Exception |
|
82 | + */ |
|
83 | + public function respondNoAccess(Controller $controller) |
|
84 | + { |
|
85 | + $response = null; |
|
86 | + if (class_exists('ErrorPage', true)) { |
|
87 | + $response = ErrorPage::response_for(403); |
|
88 | + } |
|
89 | + $controller->httpError(403, $response ? $response : 'The requested page could not be found.'); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @return string |
|
94 | - */ |
|
95 | - public function matchExact() |
|
96 | - { |
|
97 | - return in_array($this->ip, $this->getAllowedIps()) ? $this->ip : ''; |
|
98 | - } |
|
92 | + /** |
|
93 | + * @return string |
|
94 | + */ |
|
95 | + public function matchExact() |
|
96 | + { |
|
97 | + return in_array($this->ip, $this->getAllowedIps()) ? $this->ip : ''; |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Try to match against a ip range |
|
102 | - * |
|
103 | - * Example : 192.168.1.50-100 |
|
104 | - * |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function matchRange() |
|
108 | - { |
|
109 | - if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
110 | - return strstr($ip, '-'); |
|
111 | - }) |
|
112 | - ) { |
|
113 | - foreach ($ranges as $range) { |
|
114 | - $first = substr($range, 0, strrpos($range, '.') + 1); |
|
115 | - $last = substr(strrchr($range, '.'), 1); |
|
116 | - list ($start, $end) = explode('-', $last); |
|
117 | - for ($i = $start; $i <= $end; $i++) { |
|
118 | - if ($this->ip === $first . $i) { |
|
119 | - return $range; |
|
120 | - } |
|
121 | - } |
|
122 | - } |
|
123 | - } |
|
124 | - return ''; |
|
125 | - } |
|
100 | + /** |
|
101 | + * Try to match against a ip range |
|
102 | + * |
|
103 | + * Example : 192.168.1.50-100 |
|
104 | + * |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function matchRange() |
|
108 | + { |
|
109 | + if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
110 | + return strstr($ip, '-'); |
|
111 | + }) |
|
112 | + ) { |
|
113 | + foreach ($ranges as $range) { |
|
114 | + $first = substr($range, 0, strrpos($range, '.') + 1); |
|
115 | + $last = substr(strrchr($range, '.'), 1); |
|
116 | + list ($start, $end) = explode('-', $last); |
|
117 | + for ($i = $start; $i <= $end; $i++) { |
|
118 | + if ($this->ip === $first . $i) { |
|
119 | + return $range; |
|
120 | + } |
|
121 | + } |
|
122 | + } |
|
123 | + } |
|
124 | + return ''; |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Try to match cidr range |
|
129 | - * |
|
130 | - * Example : 192.168.1.0/24 |
|
131 | - * |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - public function matchCIDR() |
|
135 | - { |
|
136 | - if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
137 | - return strstr($ip, '/'); |
|
138 | - }) |
|
139 | - ) { |
|
140 | - foreach ($ranges as $cidr) { |
|
141 | - list ($net, $mask) = explode('/', $cidr); |
|
142 | - if ((ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($net)) { |
|
143 | - return $cidr; |
|
144 | - } |
|
145 | - } |
|
146 | - } |
|
147 | - return ''; |
|
148 | - } |
|
127 | + /** |
|
128 | + * Try to match cidr range |
|
129 | + * |
|
130 | + * Example : 192.168.1.0/24 |
|
131 | + * |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + public function matchCIDR() |
|
135 | + { |
|
136 | + if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
137 | + return strstr($ip, '/'); |
|
138 | + }) |
|
139 | + ) { |
|
140 | + foreach ($ranges as $cidr) { |
|
141 | + list ($net, $mask) = explode('/', $cidr); |
|
142 | + if ((ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($net)) { |
|
143 | + return $cidr; |
|
144 | + } |
|
145 | + } |
|
146 | + } |
|
147 | + return ''; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Try to match against a range that ends with a wildcard * |
|
152 | - * |
|
153 | - * Example : 192.168.1.* |
|
154 | - * Example : 192.168.* |
|
155 | - * |
|
156 | - * @return string |
|
157 | - */ |
|
158 | - public function matchWildCard() |
|
159 | - { |
|
160 | - if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
161 | - return substr($ip, -1) === '*'; |
|
162 | - }) |
|
163 | - ) { |
|
164 | - foreach ($ranges as $range) { |
|
165 | - if (substr($this->ip, 0, strlen(substr($range, 0, -1))) === substr($range, 0, -1)) { |
|
166 | - return $range; |
|
167 | - } |
|
168 | - } |
|
169 | - } |
|
170 | - return ''; |
|
171 | - } |
|
150 | + /** |
|
151 | + * Try to match against a range that ends with a wildcard * |
|
152 | + * |
|
153 | + * Example : 192.168.1.* |
|
154 | + * Example : 192.168.* |
|
155 | + * |
|
156 | + * @return string |
|
157 | + */ |
|
158 | + public function matchWildCard() |
|
159 | + { |
|
160 | + if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
161 | + return substr($ip, -1) === '*'; |
|
162 | + }) |
|
163 | + ) { |
|
164 | + foreach ($ranges as $range) { |
|
165 | + if (substr($this->ip, 0, strlen(substr($range, 0, -1))) === substr($range, 0, -1)) { |
|
166 | + return $range; |
|
167 | + } |
|
168 | + } |
|
169 | + } |
|
170 | + return ''; |
|
171 | + } |
|
172 | 172 | |
173 | 173 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | Deprecation::notice('1.1', 'Use the "IpAccess.allowed_ips" config setting instead'); |
61 | 61 | self::config()->allowed_ips = $this->allowedIps; |
62 | 62 | } |
63 | - return (array)self::config()->allowed_ips; |
|
63 | + return (array) self::config()->allowed_ips; |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function hasAccess() |
70 | 70 | { |
71 | - return (!(bool)Config::inst()->get('IpAccess', 'enabled') |
|
71 | + return (!(bool) Config::inst()->get('IpAccess', 'enabled') |
|
72 | 72 | || empty($this->getAllowedIps()) |
73 | 73 | || $this->matchExact() |
74 | 74 | || $this->matchRange() |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | */ |
107 | 107 | public function matchRange() |
108 | 108 | { |
109 | - if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
109 | + if ($ranges = array_filter($this->getAllowedIps(), function($ip) { |
|
110 | 110 | return strstr($ip, '-'); |
111 | 111 | }) |
112 | 112 | ) { |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public function matchCIDR() |
135 | 135 | { |
136 | - if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
136 | + if ($ranges = array_filter($this->getAllowedIps(), function($ip) { |
|
137 | 137 | return strstr($ip, '/'); |
138 | 138 | }) |
139 | 139 | ) { |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | */ |
158 | 158 | public function matchWildCard() |
159 | 159 | { |
160 | - if ($ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
160 | + if ($ranges = array_filter($this->getAllowedIps(), function($ip) { |
|
161 | 161 | return substr($ip, -1) === '*'; |
162 | 162 | }) |
163 | 163 | ) { |