PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 3.6.0
Easy Basic Authentication – Add basic auth to site or admin area v3.6.0
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
easy-basic-authentication / class / easy-basic-authentication-class.php

easy-basic-authentication-class.php in Easy Basic Authentication – Add basic auth to site or admin area 3.6.0, at class/easy-basic-authentication-class.php

151 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once (dirname(__FILE__).'/easy-basic-authentication-log-class.php');
4 require_once (dirname(__FILE__).'/easy-basic-authentication-emailalert-class.php');
5 require_once (dirname(__FILE__).'/easy-basic-authentication-form-class.php');
6 require_once (dirname(__FILE__).'/easy-basic-authentication-notice-class.php');
7
8 class easy_basic_authentication_class {
9
10 private $log;
11 private $email;
12 private $form;
13
14 public function __construct()
15 {
16 $this->log = new easy_basic_authentication_log_class();
17 $this->email = new easy_basic_authentication_emailalert_class();
18 $this->form = new easy_basic_authentication_form_class();
19 $notice = new easy_basic_authentication_notice_class();
20
21 if(get_option( 'basic_auth_plugin_admin_enable' )) {
22 if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
23 add_action( 'init', array($this,'basic_auth_root') );
24 }
25 }
26
27 if(get_option( 'basic_auth_plugin_enable' ) && get_option( 'basic_auth_plugin_admin_enable' )){
28 add_action( 'init', array($this,'basic_auth_root') );
29 }
30
31 add_action( 'admin_menu', array($this,'basic_auth_plugin_menu' ));
32 add_action( 'admin_init', array($this->form,'basic_auth_plugin_settings_init' ));
33
34 add_action('admin_init', function () {
35 $post_data = $_POST;
36 $this->form->basic_auth_plugin_save_settings($post_data);
37 });
38
39 }
40
41 public function basic_auth_root()
42 {
43 $user = get_option('basic_auth_plugin_username');
44 $pass = get_option('basic_auth_plugin_password');
45
46 if ($this->whiteListChecker()) {
47 return;
48 }
49
50 if (!isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['HTTP_AUTHORIZATION'])) {
51 if (stripos($_SERVER['HTTP_AUTHORIZATION'], 'basic') === 0) {
52 list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
53 }
54 }
55
56 if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
57 $_SERVER['PHP_AUTH_USER'] !== $user ||
58 !wp_check_password(wp_unslash($_SERVER['PHP_AUTH_PW']), $pass)) {
59 $this->do_exit(true);
60 }
61 }
62
63 public function whiteListChecker() {
64 if (!isset($_SERVER['REMOTE_ADDR'])) {
65 return false;
66 }
67
68 $ip = $_SERVER['REMOTE_ADDR'];
69 $whitelist = $this->getWhiteList();
70
71 foreach ($whitelist as $entry) {
72 if ($this->isIpAllowed($ip, $entry)) {
73 return true;
74 }
75 }
76 return false;
77 }
78
79 private function isIpAllowed($ip, $entry) {
80 if (filter_var($entry, FILTER_VALIDATE_IP)) {
81 return $ip === $entry;
82 } elseif (strpos($entry, '/') !== false) {
83 return $this->isIpInCidr($ip, $entry);
84 } elseif (strpos($entry, '-') !== false) {
85 return $this->isIpInRange($ip, $entry);
86 }
87 return false;
88 }
89
90 private function isIpInCidr($ip, $cidr) {
91 list($subnet, $mask) = explode('/', $cidr);
92 $ipLong = ip2long($ip);
93 $subnetLong = ip2long($subnet);
94 $maskLong = -1 << (32 - $mask);
95 return ($ipLong & $maskLong) === ($subnetLong & $maskLong);
96 }
97
98 private function isIpInRange($ip, $range) {
99 list($start, $end) = array_map('trim', explode('-', $range));
100 $ipLong = ip2long($ip);
101 $startLong = ip2long($start);
102 $endLong = ip2long($end);
103 return ($ipLong >= $startLong && $ipLong <= $endLong);
104 }
105
106 public function do_exit($admin_area = false) {
107
108 $this->basic_auth_action_failed_access();
109 do_action('basic_auth_before_401');
110
111 if($admin_area) {
112 do_action('basic_auth_before_401_admin_area');
113 header( 'WWW-Authenticate: Basic realm="My Website"' );
114 header( 'HTTP/1.0 401 Unauthorized' );
115 exit;
116 } else {
117 header( 'HTTP/1.1 401 Unauthorized' );
118 header( 'WWW-Authenticate: Basic realm="Admin Area"' );
119 exit;
120 }
121 }
122
123 public function getWhiteList() {
124 return get_option( 'basic_auth_plugin_whitelist' )?explode(',',get_option( 'basic_auth_plugin_whitelist' )):[];
125 }
126
127 public function basic_auth_plugin_menu() {
128 add_menu_page(
129 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
130 __('Easy Basic A.', 'easy-basic-authentication'),
131 'manage_options',
132 'basic-auth-plugin',
133 array($this->form, 'basic_auth_plugin_settings_page'),
134 'dashicons-lock'
135 );
136 if($this->log->is_enabled()) {
137 $this->log->getMenu();
138 }
139 }
140
141 public function basic_auth_action_failed_access() {
142 if($this->log->is_enabled()) {
143 $this->log->update_status($_SERVER);
144 }
145 if($this->email->is_enabled()) {
146 $this->email->sendAlert($_SERVER);
147 }
148 }
149
150 }
151