PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 3.4
Easy Basic Authentication – Add basic auth to site or admin area v3.4
trunk 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.7 1.7.1 1.8 1.8.1 1.9 All 50 releases
← All changes | class/easy-basic-authentication-class.php +12 -97 trunk3.4 View file →
@@ -1,11 +1,10 @@
1 -<?php
1 +<?php
2 2
3 3 require_once (dirname(__FILE__).'/easy-basic-authentication-log-class.php');
4 4 require_once (dirname(__FILE__).'/easy-basic-authentication-emailalert-class.php');
5 5 require_once (dirname(__FILE__).'/easy-basic-authentication-form-class.php');
6 6 require_once (dirname(__FILE__).'/easy-basic-authentication-notice-class.php');
7 -require_once (dirname(__FILE__).'/easy-basic-authentication-compatcheck-class.php');
8 7
9 8 class easy_basic_authentication_class {
10 9
11 10 private $log;
@@ -10,9 +9,8 @@
10 9
11 10 private $log;
12 11 private $email;
13 12 private $form;
14 - private $compatcheck;
15 13
16 14 public function __construct()
17 15 {
18 16 $this->log = new easy_basic_authentication_log_class();
@@ -18,10 +16,8 @@
18 16 $this->log = new easy_basic_authentication_log_class();
19 17 $this->email = new easy_basic_authentication_emailalert_class();
20 18 $this->form = new easy_basic_authentication_form_class();
21 19 $notice = new easy_basic_authentication_notice_class();
22 - $this->compatcheck = new easy_basic_authentication_compatcheck_class();
23 - $this->compatcheck->register_hooks();
24 20
25 21 if(get_option( 'basic_auth_plugin_admin_enable' )) {
26 22 if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
27 23 add_action( 'init', array($this,'basic_auth_root') );
@@ -30,9 +26,9 @@
30 26
31 27 if(get_option( 'basic_auth_plugin_enable' ) && get_option( 'basic_auth_plugin_admin_enable' )){
32 28 add_action( 'init', array($this,'basic_auth_root') );
33 29 }
34 -
30 +
35 31 add_action( 'admin_menu', array($this,'basic_auth_plugin_menu' ));
36 32 add_action( 'admin_init', array($this->form,'basic_auth_plugin_settings_init' ));
37 33
38 34 add_action('admin_init', function () {
@@ -38,9 +34,9 @@
38 34 add_action('admin_init', function () {
39 35 $post_data = $_POST;
40 36 $this->form->basic_auth_plugin_save_settings($post_data);
41 37 });
42 -
38 +
43 39 }
44 40
45 41 public function basic_auth_root()
46 42 {
@@ -45,20 +41,16 @@
45 41 public function basic_auth_root()
46 42 {
47 43 $user = get_option('basic_auth_plugin_username');
48 44 $pass = get_option('basic_auth_plugin_password');
49 -
45 +
50 46 if ($this->whiteListChecker()) {
51 47 return;
52 48 }
53 49
54 - if ($this->urlWhiteListChecker()) {
55 - return;
56 - }
57 -
58 50 if (!isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['HTTP_AUTHORIZATION'])) {
59 51 if (stripos($_SERVER['HTTP_AUTHORIZATION'], 'basic') === 0) {
60 - list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)), 2);
52 + list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
61 53 }
62 54 }
63 55
64 56 if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
@@ -66,87 +58,14 @@
66 58 !wp_check_password(wp_unslash($_SERVER['PHP_AUTH_PW']), $pass)) {
67 59 $this->do_exit(true);
68 60 }
69 61 }
70 -
71 - public function urlWhiteListChecker() {
72 - if (empty($_SERVER['HTTP_HOST']) || empty($_SERVER['REQUEST_URI'])) {
73 - return false;
74 - }
75 62
76 - $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
77 - $currentUrl = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
78 -
79 - $whitelist = $this->getUrlWhiteList();
80 -
81 - foreach ($whitelist as $entry) {
82 - if ($this->isUrlAllowed($currentUrl, $entry)) {
83 - return true;
84 - }
85 - }
86 -
87 - return false;
88 - }
89 -
90 - private function isUrlAllowed($currentUrl, $entry) {
91 - $currentUrl = rtrim($currentUrl, '/');
92 - $entry = rtrim($entry, '/');
93 -
94 - if (strpos($entry, '/') === 0) {
95 - $path = wp_parse_url($currentUrl, PHP_URL_PATH);
96 - return stripos($path, $entry) === 0;
97 - }
98 -
99 - if (!preg_match('#^https?://#i', $entry)) {
100 - $scheme = wp_parse_url($currentUrl, PHP_URL_SCHEME) ?: 'https';
101 - $entry = $scheme . '://' . $entry;
102 - }
103 -
104 - return stripos($currentUrl, $entry) === 0;
105 - }
106 -
107 63 public function whiteListChecker() {
108 - if (!isset($_SERVER['REMOTE_ADDR'])) {
109 - return false;
64 + if (isset($_SERVER['REMOTE_ADDR']) && in_array($_SERVER['REMOTE_ADDR'], $this->getWhiteList())) {
65 + return true;
110 66 }
111 -
112 - $ip = $_SERVER['REMOTE_ADDR'];
113 - $whitelist = $this->getWhiteList();
114 -
115 - foreach ($whitelist as $entry) {
116 - if ($this->isIpAllowed($ip, $entry)) {
117 - return true;
118 - }
119 - }
120 - return false;
121 67 }
122 -
123 - private function isIpAllowed($ip, $entry) {
124 - if (filter_var($entry, FILTER_VALIDATE_IP)) {
125 - return $ip === $entry;
126 - } elseif (strpos($entry, '/') !== false) {
127 - return $this->isIpInCidr($ip, $entry);
128 - } elseif (strpos($entry, '-') !== false) {
129 - return $this->isIpInRange($ip, $entry);
130 - }
131 - return false;
132 - }
133 -
134 - private function isIpInCidr($ip, $cidr) {
135 - list($subnet, $mask) = explode('/', $cidr);
136 - $ipLong = ip2long($ip);
137 - $subnetLong = ip2long($subnet);
138 - $maskLong = -1 << (32 - $mask);
139 - return ($ipLong & $maskLong) === ($subnetLong & $maskLong);
140 - }
141 -
142 - private function isIpInRange($ip, $range) {
143 - list($start, $end) = array_map('trim', explode('-', $range));
144 - $ipLong = ip2long($ip);
145 - $startLong = ip2long($start);
146 - $endLong = ip2long($end);
147 - return ($ipLong >= $startLong && $ipLong <= $endLong);
148 - }
149 68
150 69 public function do_exit($admin_area = false) {
151 70
152 71 $this->basic_auth_action_failed_access();
@@ -152,9 +71,9 @@
152 71 $this->basic_auth_action_failed_access();
153 72 do_action('basic_auth_before_401');
154 73
155 74 if($admin_area) {
156 - do_action('basic_auth_before_401_admin_area');
75 + do_action('basic_auth_before_401_admin_area');
157 76 header( 'WWW-Authenticate: Basic realm="My Website"' );
158 77 header( 'HTTP/1.0 401 Unauthorized' );
159 78 exit;
160 79 } else {
@@ -167,16 +86,12 @@
167 86 public function getWhiteList() {
168 87 return get_option( 'basic_auth_plugin_whitelist' )?explode(',',get_option( 'basic_auth_plugin_whitelist' )):[];
169 88 }
170 89
171 - public function getUrlWhiteList() {
172 - return get_option( 'basic_auth_plugin_urlwhitelist' )?explode(',',get_option( 'basic_auth_plugin_urlwhitelist' )):[];
173 - }
174 -
175 90 public function basic_auth_plugin_menu() {
176 91 add_menu_page(
177 - __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
178 - __('Easy Basic A.', 'easy-basic-authentication'),
92 + __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
93 + __('Easy Basic A.', 'easy-basic-authentication'),
179 94 'manage_options',
180 95 'basic-auth-plugin',
181 96 array($this->form, 'basic_auth_plugin_settings_page'),
182 97 'dashicons-lock'
@@ -183,10 +98,10 @@
183 98 );
184 99 if($this->log->is_enabled()) {
185 100 $this->log->getMenu();
186 101 }
187 - }
188 -
102 + }
103 +
189 104 public function basic_auth_action_failed_access() {
190 105 if($this->log->is_enabled()) {
191 106 $this->log->update_status($_SERVER);
192 107 }