@@ -13,162 +13,162 @@ |
||
13 | 13 | */ |
14 | 14 | class IpAccess extends Object |
15 | 15 | { |
16 | - /** |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $allowedIps = array(); |
|
20 | - |
|
21 | - /** |
|
22 | - * @config |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - private static $allowed_ips = array(); |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - private $ip = ''; |
|
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; |
|
42 | - |
|
43 | - self::config()->allowed_ips = $allowedIps; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @param $ip |
|
48 | - */ |
|
49 | - public function setIp($ip) |
|
50 | - { |
|
51 | - $this->ip = $ip; |
|
52 | - } |
|
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 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function isEnabled() |
|
70 | - { |
|
71 | - return (bool)Config::inst()->get('IpAccess', 'enabled'); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @return bool |
|
76 | - */ |
|
77 | - public function hasAccess() |
|
78 | - { |
|
79 | - if (!$this->isEnabled() || empty($this->getAllowedIps())) { |
|
80 | - return true; |
|
81 | - } |
|
82 | - |
|
83 | - return ($this->matchExact() || $this->matchRange() || $this->matchCIDR() || $this->matchWildCard()); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @param Controller $controller |
|
88 | - * @throws SS_HTTPResponse_Exception |
|
89 | - */ |
|
90 | - public function respondNoAccess(Controller $controller) |
|
91 | - { |
|
92 | - $response = null; |
|
93 | - if (class_exists('ErrorPage', true)) { |
|
94 | - $response = ErrorPage::response_for(403); |
|
95 | - } |
|
96 | - $controller->httpError(403, $response ? $response : 'The requested page could not be found.'); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - public function matchExact() |
|
103 | - { |
|
104 | - return in_array($this->ip, $this->getAllowedIps()) ? $this->ip : ''; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Try to match against a ip range |
|
109 | - * Example : 192.168.1.50-100 |
|
110 | - * |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function matchRange() |
|
114 | - { |
|
115 | - $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
116 | - return strstr($ip, '-'); |
|
117 | - }); |
|
118 | - |
|
119 | - $ipFirstPart = substr($this->ip, 0, strrpos($this->ip, '.') + 1); |
|
120 | - $ipLastpart = substr(strrchr($this->ip, '.'), 1); |
|
121 | - |
|
122 | - if (!empty($ranges)) foreach ($ranges as $range) { |
|
123 | - $rangeFirstPart = substr($range, 0, strrpos($range, '.') + 1); |
|
124 | - list ($start, $end) = explode('-', substr(strrchr($range, '.'), 1)); |
|
125 | - if ($ipFirstPart === $rangeFirstPart && $ipLastpart >= $start && $ipLastpart <= $end) { |
|
126 | - return $range; |
|
127 | - } |
|
128 | - } |
|
129 | - return ''; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Try to match cidr range |
|
134 | - * Example : 192.168.1.0/24 |
|
135 | - * |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - public function matchCIDR() |
|
139 | - { |
|
140 | - $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
141 | - return strstr($ip, '/'); |
|
142 | - }); |
|
143 | - |
|
144 | - if (!empty($ranges)) foreach ($ranges as $range) { |
|
145 | - list ($net, $mask) = explode('/', $cidr); |
|
146 | - if ((ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($net)) { |
|
147 | - return $cidr; |
|
148 | - } |
|
149 | - } |
|
150 | - return ''; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Try to match against a range that ends with a wildcard * |
|
155 | - * Example : 192.168.1.* |
|
156 | - * Example : 192.168.* |
|
157 | - * |
|
158 | - * @return string |
|
159 | - */ |
|
160 | - public function matchWildCard() |
|
161 | - { |
|
162 | - $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
163 | - return substr($ip, -1) === '*'; |
|
164 | - }); |
|
165 | - |
|
166 | - if (!empty($ranges)) foreach ($ranges as $range) { |
|
167 | - if (substr($this->ip, 0, strlen(substr($range, 0, -1))) === substr($range, 0, -1)) { |
|
168 | - return $range; |
|
169 | - } |
|
170 | - } |
|
171 | - return ''; |
|
172 | - } |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $allowedIps = array(); |
|
20 | + |
|
21 | + /** |
|
22 | + * @config |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + private static $allowed_ips = array(); |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + private $ip = ''; |
|
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; |
|
42 | + |
|
43 | + self::config()->allowed_ips = $allowedIps; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @param $ip |
|
48 | + */ |
|
49 | + public function setIp($ip) |
|
50 | + { |
|
51 | + $this->ip = $ip; |
|
52 | + } |
|
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 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function isEnabled() |
|
70 | + { |
|
71 | + return (bool)Config::inst()->get('IpAccess', 'enabled'); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @return bool |
|
76 | + */ |
|
77 | + public function hasAccess() |
|
78 | + { |
|
79 | + if (!$this->isEnabled() || empty($this->getAllowedIps())) { |
|
80 | + return true; |
|
81 | + } |
|
82 | + |
|
83 | + return ($this->matchExact() || $this->matchRange() || $this->matchCIDR() || $this->matchWildCard()); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @param Controller $controller |
|
88 | + * @throws SS_HTTPResponse_Exception |
|
89 | + */ |
|
90 | + public function respondNoAccess(Controller $controller) |
|
91 | + { |
|
92 | + $response = null; |
|
93 | + if (class_exists('ErrorPage', true)) { |
|
94 | + $response = ErrorPage::response_for(403); |
|
95 | + } |
|
96 | + $controller->httpError(403, $response ? $response : 'The requested page could not be found.'); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + public function matchExact() |
|
103 | + { |
|
104 | + return in_array($this->ip, $this->getAllowedIps()) ? $this->ip : ''; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Try to match against a ip range |
|
109 | + * Example : 192.168.1.50-100 |
|
110 | + * |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function matchRange() |
|
114 | + { |
|
115 | + $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
116 | + return strstr($ip, '-'); |
|
117 | + }); |
|
118 | + |
|
119 | + $ipFirstPart = substr($this->ip, 0, strrpos($this->ip, '.') + 1); |
|
120 | + $ipLastpart = substr(strrchr($this->ip, '.'), 1); |
|
121 | + |
|
122 | + if (!empty($ranges)) foreach ($ranges as $range) { |
|
123 | + $rangeFirstPart = substr($range, 0, strrpos($range, '.') + 1); |
|
124 | + list ($start, $end) = explode('-', substr(strrchr($range, '.'), 1)); |
|
125 | + if ($ipFirstPart === $rangeFirstPart && $ipLastpart >= $start && $ipLastpart <= $end) { |
|
126 | + return $range; |
|
127 | + } |
|
128 | + } |
|
129 | + return ''; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Try to match cidr range |
|
134 | + * Example : 192.168.1.0/24 |
|
135 | + * |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + public function matchCIDR() |
|
139 | + { |
|
140 | + $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
141 | + return strstr($ip, '/'); |
|
142 | + }); |
|
143 | + |
|
144 | + if (!empty($ranges)) foreach ($ranges as $range) { |
|
145 | + list ($net, $mask) = explode('/', $cidr); |
|
146 | + if ((ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($net)) { |
|
147 | + return $cidr; |
|
148 | + } |
|
149 | + } |
|
150 | + return ''; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Try to match against a range that ends with a wildcard * |
|
155 | + * Example : 192.168.1.* |
|
156 | + * Example : 192.168.* |
|
157 | + * |
|
158 | + * @return string |
|
159 | + */ |
|
160 | + public function matchWildCard() |
|
161 | + { |
|
162 | + $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
163 | + return substr($ip, -1) === '*'; |
|
164 | + }); |
|
165 | + |
|
166 | + if (!empty($ranges)) foreach ($ranges as $range) { |
|
167 | + if (substr($this->ip, 0, strlen(substr($range, 0, -1))) === substr($range, 0, -1)) { |
|
168 | + return $range; |
|
169 | + } |
|
170 | + } |
|
171 | + return ''; |
|
172 | + } |
|
173 | 173 | |
174 | 174 | } |
@@ -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 isEnabled() |
70 | 70 | { |
71 | - return (bool)Config::inst()->get('IpAccess', 'enabled'); |
|
71 | + return (bool) Config::inst()->get('IpAccess', 'enabled'); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | */ |
113 | 113 | public function matchRange() |
114 | 114 | { |
115 | - $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
115 | + $ranges = array_filter($this->getAllowedIps(), function($ip) { |
|
116 | 116 | return strstr($ip, '-'); |
117 | 117 | }); |
118 | 118 | |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | */ |
138 | 138 | public function matchCIDR() |
139 | 139 | { |
140 | - $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
140 | + $ranges = array_filter($this->getAllowedIps(), function($ip) { |
|
141 | 141 | return strstr($ip, '/'); |
142 | 142 | }); |
143 | 143 | |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | */ |
160 | 160 | public function matchWildCard() |
161 | 161 | { |
162 | - $ranges = array_filter($this->getAllowedIps(), function ($ip) { |
|
162 | + $ranges = array_filter($this->getAllowedIps(), function($ip) { |
|
163 | 163 | return substr($ip, -1) === '*'; |
164 | 164 | }); |
165 | 165 |
@@ -119,8 +119,10 @@ discard block |
||
119 | 119 | $ipFirstPart = substr($this->ip, 0, strrpos($this->ip, '.') + 1); |
120 | 120 | $ipLastpart = substr(strrchr($this->ip, '.'), 1); |
121 | 121 | |
122 | - if (!empty($ranges)) foreach ($ranges as $range) { |
|
122 | + if (!empty($ranges)) { |
|
123 | + foreach ($ranges as $range) { |
|
123 | 124 | $rangeFirstPart = substr($range, 0, strrpos($range, '.') + 1); |
125 | + } |
|
124 | 126 | list ($start, $end) = explode('-', substr(strrchr($range, '.'), 1)); |
125 | 127 | if ($ipFirstPart === $rangeFirstPart && $ipLastpart >= $start && $ipLastpart <= $end) { |
126 | 128 | return $range; |
@@ -141,8 +143,10 @@ discard block |
||
141 | 143 | return strstr($ip, '/'); |
142 | 144 | }); |
143 | 145 | |
144 | - if (!empty($ranges)) foreach ($ranges as $range) { |
|
146 | + if (!empty($ranges)) { |
|
147 | + foreach ($ranges as $range) { |
|
145 | 148 | list ($net, $mask) = explode('/', $cidr); |
149 | + } |
|
146 | 150 | if ((ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($net)) { |
147 | 151 | return $cidr; |
148 | 152 | } |
@@ -163,9 +167,11 @@ discard block |
||
163 | 167 | return substr($ip, -1) === '*'; |
164 | 168 | }); |
165 | 169 | |
166 | - if (!empty($ranges)) foreach ($ranges as $range) { |
|
170 | + if (!empty($ranges)) { |
|
171 | + foreach ($ranges as $range) { |
|
167 | 172 | if (substr($this->ip, 0, strlen(substr($range, 0, -1))) === substr($range, 0, -1)) { |
168 | 173 | return $range; |
174 | + } |
|
169 | 175 | } |
170 | 176 | } |
171 | 177 | return ''; |
@@ -6,67 +6,67 @@ |
||
6 | 6 | class AdminLoginForm extends MemberLoginForm |
7 | 7 | { |
8 | 8 | |
9 | - public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) |
|
10 | - { |
|
11 | - parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser); |
|
9 | + public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) |
|
10 | + { |
|
11 | + parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser); |
|
12 | 12 | |
13 | - if ($this->Actions()->fieldByName('forgotPassword')) { |
|
14 | - // replaceField won't work, since it's a dataless field |
|
15 | - $this->Actions()->removeByName('forgotPassword'); |
|
16 | - $this->Actions()->push(new LiteralField( |
|
17 | - 'forgotPassword', |
|
18 | - '<p id="ForgotPassword"><a href="AdminSecurity/lostpassword">' |
|
19 | - . _t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>' |
|
20 | - )); |
|
21 | - } |
|
13 | + if ($this->Actions()->fieldByName('forgotPassword')) { |
|
14 | + // replaceField won't work, since it's a dataless field |
|
15 | + $this->Actions()->removeByName('forgotPassword'); |
|
16 | + $this->Actions()->push(new LiteralField( |
|
17 | + 'forgotPassword', |
|
18 | + '<p id="ForgotPassword"><a href="AdminSecurity/lostpassword">' |
|
19 | + . _t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>' |
|
20 | + )); |
|
21 | + } |
|
22 | 22 | |
23 | - Requirements::customScript(<<<JS |
|
23 | + Requirements::customScript(<<<JS |
|
24 | 24 | (function() { |
25 | 25 | var el = document.getElementById("AdminLoginForm_LoginForm_Email"); |
26 | 26 | if(el && el.focus) el.focus(); |
27 | 27 | })(); |
28 | 28 | JS |
29 | - ); |
|
30 | - } |
|
29 | + ); |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * @param array $data |
|
34 | - * @return SS_HTTPResponse |
|
35 | - */ |
|
36 | - public function forgotPassword($data) |
|
37 | - { |
|
38 | - if($data['Email']) { |
|
39 | - /* @var $member Member */ |
|
40 | - if ($member = Member::get()->where("Email = '".Convert::raw2sql($data['Email'])."'")->first()) { |
|
41 | - $token = $member->generateAutologinTokenAndStoreHash(); |
|
42 | - $this->sendPasswordResetLinkEmail($member, $token); |
|
43 | - } |
|
32 | + /** |
|
33 | + * @param array $data |
|
34 | + * @return SS_HTTPResponse |
|
35 | + */ |
|
36 | + public function forgotPassword($data) |
|
37 | + { |
|
38 | + if($data['Email']) { |
|
39 | + /* @var $member Member */ |
|
40 | + if ($member = Member::get()->where("Email = '".Convert::raw2sql($data['Email'])."'")->first()) { |
|
41 | + $token = $member->generateAutologinTokenAndStoreHash(); |
|
42 | + $this->sendPasswordResetLinkEmail($member, $token); |
|
43 | + } |
|
44 | 44 | |
45 | - return $this->controller->redirect('AdminSecurity/passwordsent/' . urlencode($data['Email'])); |
|
46 | - } |
|
45 | + return $this->controller->redirect('AdminSecurity/passwordsent/' . urlencode($data['Email'])); |
|
46 | + } |
|
47 | 47 | |
48 | - $this->sessionMessage( |
|
49 | - _t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.'), |
|
50 | - 'bad' |
|
51 | - ); |
|
48 | + $this->sessionMessage( |
|
49 | + _t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.'), |
|
50 | + 'bad' |
|
51 | + ); |
|
52 | 52 | |
53 | - return $this->controller->redirect('AdminSecurity/lostpassword'); |
|
54 | - } |
|
53 | + return $this->controller->redirect('AdminSecurity/lostpassword'); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param Member $member |
|
58 | - * @param string $token |
|
59 | - */ |
|
60 | - protected function sendPasswordResetLinkEmail($member, $token) |
|
61 | - { |
|
62 | - /* @var $email Member_ForgotPasswordEmail */ |
|
63 | - $email = Member_ForgotPasswordEmail::create(); |
|
64 | - $email->populateTemplate($member); |
|
65 | - $email->populateTemplate(array( |
|
66 | - 'PasswordResetLink' => AdminSecurity::getPasswordResetLink($member, $token) |
|
67 | - )); |
|
68 | - $email->setTo($member->Email); |
|
69 | - $email->send(); |
|
70 | - } |
|
56 | + /** |
|
57 | + * @param Member $member |
|
58 | + * @param string $token |
|
59 | + */ |
|
60 | + protected function sendPasswordResetLinkEmail($member, $token) |
|
61 | + { |
|
62 | + /* @var $email Member_ForgotPasswordEmail */ |
|
63 | + $email = Member_ForgotPasswordEmail::create(); |
|
64 | + $email->populateTemplate($member); |
|
65 | + $email->populateTemplate(array( |
|
66 | + 'PasswordResetLink' => AdminSecurity::getPasswordResetLink($member, $token) |
|
67 | + )); |
|
68 | + $email->setTo($member->Email); |
|
69 | + $email->send(); |
|
70 | + } |
|
71 | 71 | |
72 | 72 | } |