PluginProbe
Easy Basic Authentication – Add basic auth to site or admin area / 2.5.2
Easy Basic Authentication – Add basic auth to site or admin area v2.5.2
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 2.5.2, at class/easy-basic-authentication-class.php

112 lines 3.9 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_admin') );
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 $user = get_option( 'basic_auth_plugin_username' );
43 $pass = get_option( 'basic_auth_plugin_password' );
44
45 if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) ||
46 $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) {
47
48 $this->do_exit(true);
49 }
50 }
51
52 public function basic_auth_admin() {
53 $user = get_option( 'basic_auth_plugin_username' );
54 $pass = get_option( 'basic_auth_plugin_password' );
55
56 if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) ||
57 $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) {
58
59 $this->do_exit(true);
60 }
61 }
62
63 public function do_exit($admin_area = false) {
64
65 if (in_array($_SERVER['REMOTE_ADDR'], $this->getWhiteList())) {
66 return;
67 }
68 $this->basic_auth_action_failed_access();
69 do_action('basic_auth_before_401');
70
71 if($admin_area) {
72 do_action('basic_auth_before_401_admin_area');
73
74 header( 'WWW-Authenticate: Basic realm="My Website"' );
75 header( 'HTTP/1.0 401 Unauthorized' );
76 exit;
77 } else {
78 header( 'HTTP/1.1 401 Unauthorized' );
79 header( 'WWW-Authenticate: Basic realm="Admin Area"' );
80 exit;
81 }
82 }
83
84 public function getWhiteList() {
85 return get_option( 'basic_auth_plugin_whitelist' )?explode(',',get_option( 'basic_auth_plugin_whitelist' )):[];
86 }
87
88 public function basic_auth_plugin_menu() {
89 add_menu_page(
90 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
91 __('Easy Basic A.', 'easy-basic-authentication'),
92 'manage_options',
93 'basic-auth-plugin',
94 array($this->form, 'basic_auth_plugin_settings_page'),
95 'dashicons-lock'
96 );
97 if($this->log->is_enabled()) {
98 $this->log->getMenu();
99 }
100 }
101
102 public function basic_auth_action_failed_access() {
103 if($this->log->is_enabled()) {
104 $this->log->update_status($_SERVER);
105 }
106 if($this->email->is_enabled()) {
107 $this->email->sendAlert($_SERVER);
108 }
109 }
110
111 }
112