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

99 lines 3.4 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
7 class easy_basic_authentication_class {
8
9 private $log;
10 private $email;
11 private $form;
12
13 public function __construct()
14 {
15 $this->log = new easy_basic_authentication_log_class();
16 $this->email = new easy_basic_authentication_emailalert_class();
17 $this->form = new easy_basic_authentication_form_class();
18
19 if(get_option( 'basic_auth_plugin_admin_enable' )) {
20 if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
21 add_action( 'init', array($this,'basic_auth_admin') );
22 }
23 }
24
25 if(get_option( 'basic_auth_plugin_enable' ) && get_option( 'basic_auth_plugin_admin_enable' )){
26 add_action( 'init', array($this,'basic_auth_root') );
27 }
28
29 add_action( 'admin_menu', array($this,'basic_auth_plugin_menu' ));
30 add_action( 'admin_init', array($this->form,'basic_auth_plugin_settings_init' ));
31
32 add_action('admin_init', function () {
33 $post_data = $_POST;
34 $this->form->basic_auth_plugin_save_settings($post_data);
35 });
36
37 }
38
39 public function basic_auth_root() {
40 $user = get_option( 'basic_auth_plugin_username' );
41 $pass = get_option( 'basic_auth_plugin_password' );
42
43 if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) ||
44 $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) {
45
46 $this->basic_auth_action_failed_access();
47
48 do_action('basic_auth_before_401');
49
50 header( 'WWW-Authenticate: Basic realm="My Website"' );
51 header( 'HTTP/1.0 401 Unauthorized' );
52 echo 'Autenticazione richiesta';
53 exit;
54 }
55 }
56
57 public function basic_auth_admin() {
58 $user = get_option( 'basic_auth_plugin_username' );
59 $pass = get_option( 'basic_auth_plugin_password' );
60
61 if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) ||
62 $_SERVER['PHP_AUTH_USER'] != $user || !wp_check_password( $_SERVER['PHP_AUTH_PW'], $pass ) ) {
63
64 $this->basic_auth_action_failed_access();
65
66 do_action('basic_auth_before_401');
67
68 header( 'HTTP/1.1 401 Unauthorized' );
69 header( 'WWW-Authenticate: Basic realm="Admin Area"' );
70 exit;
71 }
72 }
73
74
75 public function basic_auth_plugin_menu() {
76 add_menu_page(
77 __('Configurations for Easy Basic Authentication', 'easy-basic-authentication'),
78 __('Easy Basic A.', 'easy-basic-authentication'),
79 'manage_options',
80 'basic-auth-plugin',
81 array($this->form, 'basic_auth_plugin_settings_page'),
82 'dashicons-lock'
83 );
84 if($this->log->is_enabled()) {
85 $this->log->getMenu();
86 }
87 }
88
89 public function basic_auth_action_failed_access() {
90 if($this->log->is_enabled()) {
91 $this->log->update_status($_SERVER);
92 }
93 if($this->email->is_enabled()) {
94 $this->email->sendAlert($_SERVER);
95 }
96 }
97
98 }
99