Completed
Pull Request — master (#12)
by Martijn van
02:33
created
code/LimitAdminAccessExtension.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -7,14 +7,14 @@
 block discarded – undo
7 7
  */
8 8
 class LimitAdminAccessExtension extends Extension
9 9
 {
10
-    /**
11
-     * @return mixed
12
-     */
13
-    public function onBeforeInit()
14
-    {
15
-        $access = new IpAccess($this->owner->getRequest()->getIP());
16
-        if (!$access->hasAccess()) {
17
-            $access->respondNoAccess($this->owner);
18
-        }
19
-    }
10
+	/**
11
+	 * @return mixed
12
+	 */
13
+	public function onBeforeInit()
14
+	{
15
+		$access = new IpAccess($this->owner->getRequest()->getIP());
16
+		if (!$access->hasAccess()) {
17
+			$access->respondNoAccess($this->owner);
18
+		}
19
+	}
20 20
 }
Please login to merge, or discard this patch.
code/AdminLoginForm.php 2 patches
Indentation   +51 added lines, -51 removed lines patch added patch discarded remove patch
@@ -5,67 +5,67 @@
 block discarded – undo
5 5
  */
6 6
 class AdminLoginForm extends MemberLoginForm
7 7
 {
8
-    public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
9
-    {
10
-        parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser);
8
+	public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
9
+	{
10
+		parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser);
11 11
 
12
-        if ($this->Actions()->fieldByName('forgotPassword')) {
13
-            // replaceField won't work, since it's a dataless field
14
-            $this->Actions()->removeByName('forgotPassword');
15
-            $this->Actions()->push(new LiteralField(
16
-                'forgotPassword',
17
-                '<p id="ForgotPassword"><a href="AdminSecurity/lostpassword">'
18
-                ._t('Member.BUTTONLOSTPASSWORD', "I've lost my password").'</a></p>'
19
-            ));
20
-        }
12
+		if ($this->Actions()->fieldByName('forgotPassword')) {
13
+			// replaceField won't work, since it's a dataless field
14
+			$this->Actions()->removeByName('forgotPassword');
15
+			$this->Actions()->push(new LiteralField(
16
+				'forgotPassword',
17
+				'<p id="ForgotPassword"><a href="AdminSecurity/lostpassword">'
18
+				._t('Member.BUTTONLOSTPASSWORD', "I've lost my password").'</a></p>'
19
+			));
20
+		}
21 21
 
22
-        Requirements::customScript(<<<'JS'
22
+		Requirements::customScript(<<<'JS'
23 23
 			(function() {
24 24
 				var el = document.getElementById("AdminLoginForm_LoginForm_Email");
25 25
 				if(el && el.focus) el.focus();
26 26
 			})();
27 27
 JS
28
-        );
29
-    }
28
+		);
29
+	}
30 30
 
31
-    /**
32
-     * @param array $data
33
-     *
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
-            }
31
+	/**
32
+	 * @param array $data
33
+	 *
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([
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([
66
+			'PasswordResetLink' => AdminSecurity::getPasswordResetLink($member, $token),
67
+		]);
68
+		$email->setTo($member->Email);
69
+		$email->send();
70
+	}
71 71
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
             $this->Actions()->push(new LiteralField(
16 16
                 'forgotPassword',
17 17
                 '<p id="ForgotPassword"><a href="AdminSecurity/lostpassword">'
18
-                ._t('Member.BUTTONLOSTPASSWORD', "I've lost my password").'</a></p>'
18
+                ._t('Member.BUTTONLOSTPASSWORD', "I've lost my password") . '</a></p>'
19 19
             ));
20 20
         }
21 21
 
@@ -37,12 +37,12 @@  discard block
 block discarded – undo
37 37
     {
38 38
         if ($data['Email']) {
39 39
             /* @var $member Member */
40
-            if ($member = Member::get()->where("Email = '".Convert::raw2sql($data['Email'])."'")->first()) {
40
+            if ($member = Member::get()->where("Email = '" . Convert::raw2sql($data['Email']) . "'")->first()) {
41 41
                 $token = $member->generateAutologinTokenAndStoreHash();
42 42
                 $this->sendPasswordResetLinkEmail($member, $token);
43 43
             }
44 44
 
45
-            return $this->controller->redirect('AdminSecurity/passwordsent/'.urlencode($data['Email']));
45
+            return $this->controller->redirect('AdminSecurity/passwordsent/' . urlencode($data['Email']));
46 46
         }
47 47
 
48 48
         $this->sessionMessage(
Please login to merge, or discard this patch.
code/AdminSecurity.php 2 patches
Indentation   +91 added lines, -91 removed lines patch added patch discarded remove patch
@@ -6,108 +6,108 @@
 block discarded – undo
6 6
  */
7 7
 class AdminSecurity extends Security
8 8
 {
9
-    /**
10
-     * @var array
11
-     */
12
-    private static $allowed_actions = [
13
-        'passwordsent',
14
-        'ChangePasswordForm',
15
-    ];
9
+	/**
10
+	 * @var array
11
+	 */
12
+	private static $allowed_actions = [
13
+		'passwordsent',
14
+		'ChangePasswordForm',
15
+	];
16 16
 
17
-    /**
18
-     * Template thats used to render the pages.
19
-     *
20
-     * @config
21
-     *
22
-     * @var string
23
-     */
24
-    private static $template_main = 'AdminLogin';
17
+	/**
18
+	 * Template thats used to render the pages.
19
+	 *
20
+	 * @config
21
+	 *
22
+	 * @var string
23
+	 */
24
+	private static $template_main = 'AdminLogin';
25 25
 
26
-    /**
27
-     * @return void
28
-     */
29
-    public function init()
30
-    {
31
-        parent::init();
26
+	/**
27
+	 * @return void
28
+	 */
29
+	public function init()
30
+	{
31
+		parent::init();
32 32
 
33
-        $access = new IpAccess($this->getRequest()->getIP());
34
-        if (!$access->hasAccess()) {
35
-            $access->respondNoAccess($this);
36
-        }
33
+		$access = new IpAccess($this->getRequest()->getIP());
34
+		if (!$access->hasAccess()) {
35
+			$access->respondNoAccess($this);
36
+		}
37 37
 
38
-        if (Config::inst()->get('AdminLogin', 'UseTheme') !== true) {
39
-            // this prevents loading frontend css and javscript files
40
-            Object::useCustomClass('Page_Controller', 'AdminLoginPage_Controller');
41
-            Requirements::css('adminlogin/css/style.css');
42
-        }
38
+		if (Config::inst()->get('AdminLogin', 'UseTheme') !== true) {
39
+			// this prevents loading frontend css and javscript files
40
+			Object::useCustomClass('Page_Controller', 'AdminLoginPage_Controller');
41
+			Requirements::css('adminlogin/css/style.css');
42
+		}
43 43
 
44
-        Object::useCustomClass('MemberLoginForm', 'AdminLoginForm');
45
-    }
44
+		Object::useCustomClass('MemberLoginForm', 'AdminLoginForm');
45
+	}
46 46
 
47
-    /**
48
-     * @param null $action
49
-     *
50
-     * @return string
51
-     */
52
-    public function Link($action = null)
53
-    {
54
-        return "AdminSecurity/$action";
55
-    }
47
+	/**
48
+	 * @param null $action
49
+	 *
50
+	 * @return string
51
+	 */
52
+	public function Link($action = null)
53
+	{
54
+		return "AdminSecurity/$action";
55
+	}
56 56
 
57
-    /**
58
-     * @return string
59
-     */
60
-    public static function isAdminLogin()
61
-    {
62
-        return strstr(self::getBackUrl(), '/admin/');
63
-    }
57
+	/**
58
+	 * @return string
59
+	 */
60
+	public static function isAdminLogin()
61
+	{
62
+		return strstr(self::getBackUrl(), '/admin/');
63
+	}
64 64
 
65
-    /**
66
-     * @return string
67
-     */
68
-    public static function getBackUrl()
69
-    {
70
-        $request = Controller::curr()->getRequest();
71
-        if ($url = $request->requestVar('BackURL')) {
72
-            return $url;
73
-        }
65
+	/**
66
+	 * @return string
67
+	 */
68
+	public static function getBackUrl()
69
+	{
70
+		$request = Controller::curr()->getRequest();
71
+		if ($url = $request->requestVar('BackURL')) {
72
+			return $url;
73
+		}
74 74
 
75
-        return '';
76
-    }
75
+		return '';
76
+	}
77 77
 
78
-    /**
79
-     * @param SS_HTTPRequest $request
80
-     *
81
-     * @return SS_HTTPResponse|HTMLText
82
-     */
83
-    public function passwordsent($request)
84
-    {
85
-        return parent::passwordsent($request);
86
-    }
78
+	/**
79
+	 * @param SS_HTTPRequest $request
80
+	 *
81
+	 * @return SS_HTTPResponse|HTMLText
82
+	 */
83
+	public function passwordsent($request)
84
+	{
85
+		return parent::passwordsent($request);
86
+	}
87 87
 
88
-    /**
89
-     * @see Security::getPasswordResetLink()
90
-     * We overload this, so we can add the BackURL to the password resetlink
91
-     *
92
-     * @param Member $member
93
-     * @param string $autologinToken
94
-     *
95
-     * @return string
96
-     */
97
-    public static function getPasswordResetLink($member, $autologinToken)
98
-    {
99
-        $autologinToken = urldecode($autologinToken);
100
-        $selfControllerClass = __CLASS__;
101
-        $selfController = new $selfControllerClass();
88
+	/**
89
+	 * @see Security::getPasswordResetLink()
90
+	 * We overload this, so we can add the BackURL to the password resetlink
91
+	 *
92
+	 * @param Member $member
93
+	 * @param string $autologinToken
94
+	 *
95
+	 * @return string
96
+	 */
97
+	public static function getPasswordResetLink($member, $autologinToken)
98
+	{
99
+		$autologinToken = urldecode($autologinToken);
100
+		$selfControllerClass = __CLASS__;
101
+		$selfController = new $selfControllerClass();
102 102
 
103
-        return $selfController->Link('changepassword')."?m={$member->ID}&t=$autologinToken";
104
-    }
103
+		return $selfController->Link('changepassword')."?m={$member->ID}&t=$autologinToken";
104
+	}
105 105
 
106
-    /**
107
-     * @return ChangePasswordForm
108
-     */
109
-    public function ChangePasswordForm()
110
-    {
111
-        return new ChangePasswordForm($this, 'ChangePasswordForm');
112
-    }
106
+	/**
107
+	 * @return ChangePasswordForm
108
+	 */
109
+	public function ChangePasswordForm()
110
+	{
111
+		return new ChangePasswordForm($this, 'ChangePasswordForm');
112
+	}
113 113
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -100,7 +100,7 @@
 block discarded – undo
100 100
         $selfControllerClass = __CLASS__;
101 101
         $selfController = new $selfControllerClass();
102 102
 
103
-        return $selfController->Link('changepassword')."?m={$member->ID}&t=$autologinToken";
103
+        return $selfController->Link('changepassword') . "?m={$member->ID}&t=$autologinToken";
104 104
     }
105 105
 
106 106
     /**
Please login to merge, or discard this patch.
code/AdminLoginExtension.php 2 patches
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -9,20 +9,20 @@
 block discarded – undo
9 9
  */
10 10
 class AdminLoginExtension extends Extension
11 11
 {
12
-    /**
13
-     * Redirect to AdminSecurity, when we are coming from /admin/*.
14
-     *
15
-     * @return SS_HTTPResponse|void
16
-     */
17
-    public function onBeforeSecurityLogin()
18
-    {
19
-        $backUrl = $this->owner->getRequest()->getVar('BackURL');
20
-        if (strstr($backUrl, '/admin/')) {
21
-            if (Controller::curr()->class != 'AdminSecurity') {
22
-                $link = 'AdminSecurity/login'.'?BackURL='.urlencode($backUrl);
12
+	/**
13
+	 * Redirect to AdminSecurity, when we are coming from /admin/*.
14
+	 *
15
+	 * @return SS_HTTPResponse|void
16
+	 */
17
+	public function onBeforeSecurityLogin()
18
+	{
19
+		$backUrl = $this->owner->getRequest()->getVar('BackURL');
20
+		if (strstr($backUrl, '/admin/')) {
21
+			if (Controller::curr()->class != 'AdminSecurity') {
22
+				$link = 'AdminSecurity/login'.'?BackURL='.urlencode($backUrl);
23 23
 
24
-                return $this->owner->redirect($link);
25
-            }
26
-        }
27
-    }
24
+				return $this->owner->redirect($link);
25
+			}
26
+		}
27
+	}
28 28
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@
 block discarded – undo
19 19
         $backUrl = $this->owner->getRequest()->getVar('BackURL');
20 20
         if (strstr($backUrl, '/admin/')) {
21 21
             if (Controller::curr()->class != 'AdminSecurity') {
22
-                $link = 'AdminSecurity/login'.'?BackURL='.urlencode($backUrl);
22
+                $link = 'AdminSecurity/login' . '?BackURL=' . urlencode($backUrl);
23 23
 
24 24
                 return $this->owner->redirect($link);
25 25
             }
Please login to merge, or discard this patch.
code/model/IpAccess.php 2 patches
Indentation   +176 added lines, -176 removed lines patch added patch discarded remove patch
@@ -14,180 +14,180 @@
 block discarded – undo
14 14
  */
15 15
 class IpAccess extends Object
16 16
 {
17
-    /**
18
-     * @var array
19
-     */
20
-    public $allowedIps = [];
21
-
22
-    /**
23
-     * @config
24
-     *
25
-     * @var array
26
-     */
27
-    private static $allowed_ips = [];
28
-
29
-    /**
30
-     * @var string
31
-     */
32
-    private $ip = '';
33
-
34
-    /**
35
-     * IpAccess constructor.
36
-     *
37
-     * @param string $ip
38
-     * @param array  $allowedIps
39
-     */
40
-    public function __construct($ip = '', $allowedIps = [])
41
-    {
42
-        parent::__construct();
43
-        $this->ip = $ip;
44
-
45
-        self::config()->allowed_ips = $allowedIps;
46
-    }
47
-
48
-    /**
49
-     * @param $ip
50
-     */
51
-    public function setIp($ip)
52
-    {
53
-        $this->ip = $ip;
54
-    }
55
-
56
-    /**
57
-     * @return array
58
-     */
59
-    public function getAllowedIps()
60
-    {
61
-        if (!empty($this->allowedIps)) {
62
-            Deprecation::notice('1.1', 'Use the "IpAccess.allowed_ips" config setting instead');
63
-            self::config()->allowed_ips = $this->allowedIps;
64
-        }
65
-
66
-        return self::$allowed_ips ? self::$allowed_ips : (array) self::config()->allowed_ips;
67
-    }
68
-
69
-    /**
70
-     * @return bool
71
-     */
72
-    public function isEnabled()
73
-    {
74
-        return (bool) Config::inst()->get('IpAccess', 'enabled');
75
-    }
76
-
77
-    /**
78
-     * @return bool
79
-     */
80
-    public function hasAccess()
81
-    {
82
-        if (!$this->isEnabled() || !(bool) $this->getAllowedIps()) {
83
-            return true;
84
-        }
85
-
86
-        return $this->matchIp();
87
-    }
88
-
89
-    /**
90
-     * @return bool
91
-     */
92
-    public function matchIp()
93
-    {
94
-        return $this->matchExact() || $this->matchRange() || $this->matchCIDR() || $this->matchWildCard();
95
-    }
96
-
97
-    /**
98
-     * @param Controller $controller
99
-     *
100
-     * @throws SS_HTTPResponse_Exception
101
-     */
102
-    public function respondNoAccess(Controller $controller)
103
-    {
104
-        $response = null;
105
-        if (class_exists('ErrorPage', true)) {
106
-            $response = ErrorPage::response_for(403);
107
-        }
108
-        $controller->httpError(403, $response ? $response : 'The requested page could not be found.');
109
-    }
110
-
111
-    /**
112
-     * @return string
113
-     */
114
-    public function matchExact()
115
-    {
116
-        return in_array($this->ip, $this->getAllowedIps()) ? $this->ip : '';
117
-    }
118
-
119
-    /**
120
-     * Try to match against a ip range
121
-     * Example : 192.168.1.50-100.
122
-     *
123
-     * @return string
124
-     */
125
-    public function matchRange()
126
-    {
127
-        $ranges = array_filter($this->getAllowedIps(), function ($ip) {
128
-            return strstr($ip, '-');
129
-        });
130
-
131
-        $ip = $this->ip;
132
-
133
-        $matches = array_filter($ranges, function ($range) use ($ip) {
134
-            $ipFirstPart = substr($ip, 0, strrpos($ip, '.') + 1);
135
-            $ipLastPart = substr(strrchr($ip, '.'), 1);
136
-            $rangeFirstPart = substr($range, 0, strrpos($range, '.') + 1);
137
-
138
-            list($start, $end) = explode('-', substr(strrchr($range, '.'), 1));
139
-
140
-            return $ipFirstPart === $rangeFirstPart && $ipLastPart >= $start && $ipLastPart <= $end;
141
-        });
142
-
143
-        return array_shift($matches);
144
-    }
145
-
146
-    /**
147
-     * Try to match cidr range
148
-     * Example : 192.168.1.0/24.
149
-     *
150
-     * @return string
151
-     */
152
-    public function matchCIDR()
153
-    {
154
-        $ranges = array_filter($this->getAllowedIps(), function ($ip) {
155
-            return strstr($ip, '/');
156
-        });
157
-
158
-        if (!empty($ranges)) {
159
-            foreach ($ranges as $range) {
160
-                list($net, $mask) = explode('/', $range);
161
-                if ((ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($net)) {
162
-                    return $range;
163
-                }
164
-            }
165
-        }
166
-
167
-        return '';
168
-    }
169
-
170
-    /**
171
-     * Try to match against a range that ends with a wildcard *
172
-     * Example : 192.168.1.*
173
-     * Example : 192.168.*.
174
-     *
175
-     * @return string
176
-     */
177
-    public function matchWildCard()
178
-    {
179
-        $ranges = array_filter($this->getAllowedIps(), function ($ip) {
180
-            return substr($ip, -1) === '*';
181
-        });
182
-
183
-        if (!empty($ranges)) {
184
-            foreach ($ranges as $range) {
185
-                if (substr($this->ip, 0, strlen(substr($range, 0, -1))) === substr($range, 0, -1)) {
186
-                    return $range;
187
-                }
188
-            }
189
-        }
190
-
191
-        return '';
192
-    }
17
+	/**
18
+	 * @var array
19
+	 */
20
+	public $allowedIps = [];
21
+
22
+	/**
23
+	 * @config
24
+	 *
25
+	 * @var array
26
+	 */
27
+	private static $allowed_ips = [];
28
+
29
+	/**
30
+	 * @var string
31
+	 */
32
+	private $ip = '';
33
+
34
+	/**
35
+	 * IpAccess constructor.
36
+	 *
37
+	 * @param string $ip
38
+	 * @param array  $allowedIps
39
+	 */
40
+	public function __construct($ip = '', $allowedIps = [])
41
+	{
42
+		parent::__construct();
43
+		$this->ip = $ip;
44
+
45
+		self::config()->allowed_ips = $allowedIps;
46
+	}
47
+
48
+	/**
49
+	 * @param $ip
50
+	 */
51
+	public function setIp($ip)
52
+	{
53
+		$this->ip = $ip;
54
+	}
55
+
56
+	/**
57
+	 * @return array
58
+	 */
59
+	public function getAllowedIps()
60
+	{
61
+		if (!empty($this->allowedIps)) {
62
+			Deprecation::notice('1.1', 'Use the "IpAccess.allowed_ips" config setting instead');
63
+			self::config()->allowed_ips = $this->allowedIps;
64
+		}
65
+
66
+		return self::$allowed_ips ? self::$allowed_ips : (array) self::config()->allowed_ips;
67
+	}
68
+
69
+	/**
70
+	 * @return bool
71
+	 */
72
+	public function isEnabled()
73
+	{
74
+		return (bool) Config::inst()->get('IpAccess', 'enabled');
75
+	}
76
+
77
+	/**
78
+	 * @return bool
79
+	 */
80
+	public function hasAccess()
81
+	{
82
+		if (!$this->isEnabled() || !(bool) $this->getAllowedIps()) {
83
+			return true;
84
+		}
85
+
86
+		return $this->matchIp();
87
+	}
88
+
89
+	/**
90
+	 * @return bool
91
+	 */
92
+	public function matchIp()
93
+	{
94
+		return $this->matchExact() || $this->matchRange() || $this->matchCIDR() || $this->matchWildCard();
95
+	}
96
+
97
+	/**
98
+	 * @param Controller $controller
99
+	 *
100
+	 * @throws SS_HTTPResponse_Exception
101
+	 */
102
+	public function respondNoAccess(Controller $controller)
103
+	{
104
+		$response = null;
105
+		if (class_exists('ErrorPage', true)) {
106
+			$response = ErrorPage::response_for(403);
107
+		}
108
+		$controller->httpError(403, $response ? $response : 'The requested page could not be found.');
109
+	}
110
+
111
+	/**
112
+	 * @return string
113
+	 */
114
+	public function matchExact()
115
+	{
116
+		return in_array($this->ip, $this->getAllowedIps()) ? $this->ip : '';
117
+	}
118
+
119
+	/**
120
+	 * Try to match against a ip range
121
+	 * Example : 192.168.1.50-100.
122
+	 *
123
+	 * @return string
124
+	 */
125
+	public function matchRange()
126
+	{
127
+		$ranges = array_filter($this->getAllowedIps(), function ($ip) {
128
+			return strstr($ip, '-');
129
+		});
130
+
131
+		$ip = $this->ip;
132
+
133
+		$matches = array_filter($ranges, function ($range) use ($ip) {
134
+			$ipFirstPart = substr($ip, 0, strrpos($ip, '.') + 1);
135
+			$ipLastPart = substr(strrchr($ip, '.'), 1);
136
+			$rangeFirstPart = substr($range, 0, strrpos($range, '.') + 1);
137
+
138
+			list($start, $end) = explode('-', substr(strrchr($range, '.'), 1));
139
+
140
+			return $ipFirstPart === $rangeFirstPart && $ipLastPart >= $start && $ipLastPart <= $end;
141
+		});
142
+
143
+		return array_shift($matches);
144
+	}
145
+
146
+	/**
147
+	 * Try to match cidr range
148
+	 * Example : 192.168.1.0/24.
149
+	 *
150
+	 * @return string
151
+	 */
152
+	public function matchCIDR()
153
+	{
154
+		$ranges = array_filter($this->getAllowedIps(), function ($ip) {
155
+			return strstr($ip, '/');
156
+		});
157
+
158
+		if (!empty($ranges)) {
159
+			foreach ($ranges as $range) {
160
+				list($net, $mask) = explode('/', $range);
161
+				if ((ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($net)) {
162
+					return $range;
163
+				}
164
+			}
165
+		}
166
+
167
+		return '';
168
+	}
169
+
170
+	/**
171
+	 * Try to match against a range that ends with a wildcard *
172
+	 * Example : 192.168.1.*
173
+	 * Example : 192.168.*.
174
+	 *
175
+	 * @return string
176
+	 */
177
+	public function matchWildCard()
178
+	{
179
+		$ranges = array_filter($this->getAllowedIps(), function ($ip) {
180
+			return substr($ip, -1) === '*';
181
+		});
182
+
183
+		if (!empty($ranges)) {
184
+			foreach ($ranges as $range) {
185
+				if (substr($this->ip, 0, strlen(substr($range, 0, -1))) === substr($range, 0, -1)) {
186
+					return $range;
187
+				}
188
+			}
189
+		}
190
+
191
+		return '';
192
+	}
193 193
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -124,13 +124,13 @@  discard block
 block discarded – undo
124 124
      */
125 125
     public function matchRange()
126 126
     {
127
-        $ranges = array_filter($this->getAllowedIps(), function ($ip) {
127
+        $ranges = array_filter($this->getAllowedIps(), function($ip) {
128 128
             return strstr($ip, '-');
129 129
         });
130 130
 
131 131
         $ip = $this->ip;
132 132
 
133
-        $matches = array_filter($ranges, function ($range) use ($ip) {
133
+        $matches = array_filter($ranges, function($range) use ($ip) {
134 134
             $ipFirstPart = substr($ip, 0, strrpos($ip, '.') + 1);
135 135
             $ipLastPart = substr(strrchr($ip, '.'), 1);
136 136
             $rangeFirstPart = substr($range, 0, strrpos($range, '.') + 1);
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
      */
152 152
     public function matchCIDR()
153 153
     {
154
-        $ranges = array_filter($this->getAllowedIps(), function ($ip) {
154
+        $ranges = array_filter($this->getAllowedIps(), function($ip) {
155 155
             return strstr($ip, '/');
156 156
         });
157 157
 
@@ -176,7 +176,7 @@  discard block
 block discarded – undo
176 176
      */
177 177
     public function matchWildCard()
178 178
     {
179
-        $ranges = array_filter($this->getAllowedIps(), function ($ip) {
179
+        $ranges = array_filter($this->getAllowedIps(), function($ip) {
180 180
             return substr($ip, -1) === '*';
181 181
         });
182 182
 
Please login to merge, or discard this patch.