-
Notifications
You must be signed in to change notification settings - Fork 1
/
Admin.php
217 lines (191 loc) · 5.25 KB
/
Admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Admin Interface for Lockdown WP Admin
*
* @package lockdown
* @codeCoverageIgnore
*/
class Lockdown_Admin {
/**
* Main Instance Storage
*
* @var Lockdown_Manager
*/
protected $instance;
/**
* Message Storage
*
* @var array
*/
protected $messages = array();
/**
* Valid HTTP Auth Settings
*
* @var arry
*/
protected $valid_http_auth = array( 'none', 'wp_creds', 'private' );
/**
* Admin Constructor
*
* @param Lockdown_Manager
*/
public function __construct( Lockdown_Manager $instance ) {
$this->instance = $instance;
// Add the action to setup the menu.
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
}
/**
* Adds the admin menu
*
* @access private
*/
public function add_admin_menu() {
add_menu_page(
__( 'Lockdown WP', 'lockdown-wp-admin' ),
__( 'Lockdown WP', 'lockdown-wp-admin' ),
'manage_options',
'lockdown-wp-admin',
array( $this, 'display_settings_page' )
);
add_submenu_page(
'lockdown-wp-admin',
__( 'Private Users', 'lockdown-wp-admin' ),
__( 'Private Users', 'lockdown-wp-admin' ),
'manage_options',
'lockdown-private-users',
array( $this, 'display_settings_users_page' )
);
}
/**
* The callback for the admin area
*
* You need the 'manage_options' capability to get here.
*/
public function display_settings_page() {
// Update the options
$this->settings_page_update();
// The UI
require_once( LD_PLUGIN_DIR . '/views/settings.php' );
}
/**
* The callback for ther private users management.
*
* You need the 'manage_options' capability to get here.
*/
public function display_settings_users_page() {
// Update the users options
$this->users_page_update();
// The UI
$private_users = $this->instance->application->getPrivateUsers();
require_once( LD_PLUGIN_DIR . '/views/private-users.php' );
}
/**
* Update the options
*
* @access private
*/
public function settings_page_update() {
if ( ! isset( $_GET['page'] ) || 'lockdown-wp-admin' !== $_GET['page'] || empty( $_POST['did_update'] ) ) {
return;
}
// Nonce
$nonce = $_POST['_wpnonce'];
if ( ! wp_verify_nonce( $nonce, 'lockdown-wp-admin' ) ) {
wp_die( __( 'Security error, please try again.', 'lockdown-wp-admin' ) );
}
// ---------------------------------------------------
// They're updating.
// ---------------------------------------------------
$use_http_auth = 'none';
if ( ! empty( $_POST['http_auth'] ) && in_array( $_POST['http_auth'], $this->valid_http_auth ) ) {
$use_http_auth = $_POST['http_auth'];
}
$this->instance->application->setHttpAuth( $use_http_auth );
$this->instance->application->setHideWpAdmin( ( ! empty( $_POST['hide_wp_admin'] ) && 'yes' === $_POST['hide_wp_admin'] ) );
if ( isset( $_POST['login_base'] ) ) {
$base = sanitize_title_with_dashes( $_POST['login_base'] );
$base = str_replace( '/', '', $base );
$disallowed = array(
'user',
'wp-admin',
'wp-content',
'wp-includes',
'wp-feed.php',
'index',
'feed',
'rss',
'robots',
'robots.txt',
'wp-login.php',
'wp-login',
'wp-config',
'blog',
'sitemap',
'sitemap.xml',
);
if ( in_array( $base, $disallowed ) ) {
return $this->add_message( __( 'That login base is not permitted.', 'lockdown-wp-admin' ), 'error' );
} else {
$this->instance->application->setLoginBase( sanitize_title_with_dashes( $base ) );
}
}
$this->add_message( __( 'Settings saved.', 'lockdown-wp-admin' ) );
}
/**
* Update the users
*
* @access private
*/
public function users_page_update() {
if ( ! isset( $_GET['page'] ) || 'lockdown-private-users' !== $_GET['page'] || empty( $_REQUEST['_wpnonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'lockdown-wp-admin' ) ) {
wp_die( __( 'Security error, please try again.', 'lockdown-wp-admin' ) );
}
// Add a user
if ( ! empty( $_POST['private_username'] ) && ! empty( $_POST['private_password'] ) ) {
try {
$this->instance->application->addPrivateUser( $_POST['private_username'], $_POST['private_password'] );
} catch ( Exception $e ) {
return $this->add_message( $e->getMessage(), 'error' );
}
return $this->add_message( __( 'User added.', 'lockdown-wp-admin' ) );
}
// Deleting a user (have to use isset since 'delete' could be 0)
if ( isset( $_GET['delete'] ) ) {
$users = $this->instance->application->getPrivateUsers();
$to_delete = (int) $_GET['delete'];
if ( ! empty( $users ) ) {
foreach ( $users as $key => $val ) {
if ( $key === $to_delete ) {
if ( $to_delete === $this->instance->application->current_user ) {
// They can't delete themself.
return $this->add_message( __( 'You cannot delete yourself.', 'lockdown-wp-admin' ) );
} else {
unset( $users[ $key ] );
}
}
}
}
$this->instance->application->setPrivateUsers( $users );
$this->add_message( __( 'User deleted.', 'lockdown-wp-admin' ) );
}
}
/**
* Add a message to display
*
* @param string $message
*/
public function add_message( $message, $type = 'normal' ) {
$this->messages[] = compact( 'message', 'type' );
}
/**
* Retrive messages
*
* @return array
*/
public function get_messages() {
return $this->messages;
}
}