PluginProbe
SiteGuard WP Plugin / 1.8.0
SiteGuard WP Plugin v1.8.0
1.8.9 1.8.8 1.8.7 1.8.6 1.8.6-beta1 1.8.6-beta2 1.8.4 1.8.5 1.8.3 1.8.2 1.8.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 All 50 releases
siteguard / siteguard.php
siteguard.php
338 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: SiteGuard WP Plugin
4 Plugin URI: https://www.jp-secure.com/siteguard_wp_plugin_en/
5 Description: Adds WordPress login and admin protections, including CAPTCHA, login lock, login alerts, renamed login URLs, and SiteGuard WAF tuning support.
6 Author: JP-Secure
7 Author URI: https://www.eg-secure.co.jp/
8 Text Domain: siteguard
9 Domain Path: /languages/
10 Version: 1.8.0
11 */
12
13 /*
14 Copyright 2014 EG Secure Solutions Inc (JP-Secure Inc)
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License, version 2, as
18 published by the Free Software Foundation.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29
30 if ( ! defined( 'ABSPATH' ) ) {
31 exit;
32 }
33
34 $data = get_file_data( __FILE__, array( 'version' => 'Version' ) );
35 define( 'SITEGUARD_VERSION', $data['version'] );
36
37 define( 'SITEGUARD_PATH', plugin_dir_path( __FILE__ ) );
38 define( 'SITEGUARD_URL_PATH', plugin_dir_url( __FILE__ ) );
39
40 define( 'SITEGUARD_RENAME_MODE_HTACCESS', '0');
41 define( 'SITEGUARD_RENAME_MODE_STUB', '1');
42
43 define( 'SITEGUARD_LOGIN_NOSELECT', 4 );
44 define( 'SITEGUARD_LOGIN_SUCCESS', 0 );
45 define( 'SITEGUARD_LOGIN_FAILED', 1 );
46 define( 'SITEGUARD_LOGIN_FAIL_ONCE', 2 );
47 define( 'SITEGUARD_LOGIN_LOCKED', 3 );
48
49 define( 'SITEGUARD_LOGIN_TYPE_NOSELECT', 2 );
50 define( 'SITEGUARD_LOGIN_TYPE_NORMAL', 0 );
51 define( 'SITEGUARD_LOGIN_TYPE_XMLRPC', 1 );
52
53 require_once 'classes/siteguard-base.php';
54 require_once 'classes/siteguard-config.php';
55 require_once 'classes/siteguard-htaccess.php';
56 require_once 'classes/siteguard-admin-filter.php';
57 require_once 'classes/siteguard-rename-login.php';
58 require_once 'classes/siteguard-login-history.php';
59 require_once 'classes/siteguard-login-lock.php';
60 require_once 'classes/siteguard-login-alert.php';
61 require_once 'classes/siteguard-captcha.php';
62 require_once 'classes/siteguard-disable-xmlrpc.php';
63 require_once 'classes/siteguard-disable-pingback.php';
64 require_once 'classes/siteguard-disable-author-query.php';
65 require_once 'classes/siteguard-waf-exclude-rule.php';
66 require_once 'classes/siteguard-updates-notify.php';
67 require_once 'admin/siteguard-menu-init.php';
68
69 global $siteguard_htaccess;
70 global $siteguard_config;
71 global $siteguard_admin_filter;
72 global $siteguard_rename_login;
73 global $siteguard_loginlock;
74 global $siteguard_loginalert;
75 global $siteguard_captcha;
76 global $siteguard_login_history;
77 global $siteguard_xmlrpc;
78 global $siteguard_pingback;
79 global $siteguard_author_query;
80 global $siteguard_waf_exclude_rule;
81 global $siteguard_updates_notify;
82
83 $siteguard_htaccess = new SiteGuard_Htaccess();
84 $siteguard_config = new SiteGuard_Config();
85 $siteguard_admin_filter = new SiteGuard_AdminFilter();
86 $siteguard_rename_login = new SiteGuard_RenameLogin();
87 $siteguard_loginlock = new SiteGuard_LoginLock();
88 $siteguard_loginalert = new SiteGuard_LoginAlert();
89 $siteguard_login_history = new SiteGuard_LoginHistory();
90 $siteguard_captcha = new SiteGuard_CAPTCHA();
91 $siteguard_xmlrpc = new SiteGuard_Disable_XMLRPC();
92 $siteguard_pingback = new SiteGuard_Disable_Pingback();
93 $siteguard_author_query = new SiteGuard_Disable_Author_Query();
94 $siteguard_waf_exclude_rule = new SiteGuard_WAF_Exclude_Rule();
95 $siteguard_updates_notify = new SiteGuard_UpdatesNotify();
96
97 function siteguard_activate() {
98 global $siteguard_config, $siteguard_admin_filter, $siteguard_rename_login, $siteguard_login_history, $siteguard_captcha, $siteguard_loginlock, $siteguard_loginalert, $siteguard_xmlrpc, $siteguard_pingback, $siteguard_author_query, $siteguard_waf_exclude_rule, $siteguard_updates_notify;
99
100 load_plugin_textdomain(
101 'siteguard',
102 false,
103 dirname( plugin_basename( __FILE__ ) ) . '/languages'
104 );
105
106 $siteguard_config->set( 'show_admin_notices', '0' );
107 $siteguard_config->update();
108 $siteguard_admin_filter->init();
109 $siteguard_rename_login->init();
110 $siteguard_login_history->init();
111 $siteguard_captcha->init();
112 $siteguard_loginlock->init();
113 $siteguard_loginalert->init();
114 $siteguard_xmlrpc->init();
115 $siteguard_pingback->init();
116 $siteguard_author_query->init();
117 $siteguard_waf_exclude_rule->init();
118 $siteguard_updates_notify->init();
119 }
120 register_activation_hook( __FILE__, 'siteguard_activate' );
121
122 function siteguard_deactivate() {
123 global $siteguard_config;
124 $siteguard_config->set( 'show_admin_notices', '0' );
125 $siteguard_config->update();
126 SiteGuard_RenameLogin::feature_off();
127 SiteGuard_AdminFilter::feature_off();
128 SiteGuard_Disable_XMLRPC::feature_off();
129 SiteGuard_WAF_Exclude_Rule::feature_off();
130 SiteGuard_UpdatesNotify::feature_off();
131 }
132 register_deactivation_hook( __FILE__, 'siteguard_deactivate' );
133
134
135 class SiteGuard extends SiteGuard_Base {
136 protected $menu_init;
137 function __construct() {
138 global $siteguard_config;
139 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
140 $this->htaccess_check();
141 if ( is_admin() ) {
142 include 'admin/siteguard-menu-login-history.php';
143 $this->menu_init = new SiteGuard_Menu_Init();
144 add_action( 'init', array( $this, 'set_cookie' ) );
145 add_action( 'admin_init', array( $this, 'upgrade' ) );
146 if ( '0' === $siteguard_config->get( 'show_admin_notices' ) && '1' === $siteguard_config->get( 'renamelogin_enable' ) ) {
147 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
148 $siteguard_config->set( 'show_admin_notices', '1' );
149 $siteguard_config->update();
150 }
151 }
152 }
153 function set_cookie() {
154 SiteGuard_Menu_Login_History::set_cookie();
155 }
156 function plugins_loaded() {
157 load_plugin_textdomain(
158 'siteguard',
159 false,
160 dirname( plugin_basename( __FILE__ ) ) . '/languages'
161 );
162 }
163 function htaccess_check() {
164 global $siteguard_config;
165
166 // Only check whether the SiteGuard marker block still exists in .htaccess.
167 // The actual ".htaccess effectiveness" probe (test_htaccess) is performed
168 // only when the user toggles a feature on, to avoid loopback HTTP
169 // requests on every WordPress request.
170 if ( '1' === $siteguard_config->get( 'waf_exclude_rule_enable' ) ) {
171 if ( ! SiteGuard_Htaccess::is_exists_setting( SiteGuard_WAF_Exclude_Rule::get_mark() ) ) {
172 $siteguard_config->set( 'waf_exclude_rule_enable', '0' );
173 $siteguard_config->update();
174 }
175 }
176 if ( '1' === $siteguard_config->get( 'renamelogin_enable' ) ) {
177 if ( SITEGUARD_RENAME_MODE_HTACCESS === $siteguard_config->get( 'renamelogin_stub' ) ) {
178 if ( ! SiteGuard_Htaccess::is_exists_setting( SiteGuard_RenameLogin::get_mark() ) ) {
179 $siteguard_config->set( 'renamelogin_enable', '0' );
180 $siteguard_config->update();
181 }
182 }
183 }
184 }
185 function admin_notices() {
186 global $siteguard_rename_login;
187 echo '<div class="updated" style="background-color:#719f1d;"><p><span style="border: 4px solid #def1b8;padding: 4px 4px;color:#fff;font-weight:bold;background-color:#038bc3;">';
188 echo esc_html__( 'The login page URL has been changed.', 'siteguard' ) . '</span>';
189 printf(
190 '<span style="color:#eee;">'
191 . esc_html__( 'Please bookmark the %1$s. You can change this setting %2$s.', 'siteguard' )
192 . '</span></p></div>',
193 '<a style="color:#fff;text-decoration:underline;" href="' . esc_url( wp_login_url() ) . '">' . esc_html__( 'new login URL', 'siteguard' ) . '</a>',
194 '<a style="color:#fff;text-decoration:underline;" href="' . esc_url( menu_page_url( 'siteguard_rename_login', false ) ) . '">' . esc_html__( 'here', 'siteguard' ) . '</a>'
195 );
196 $siteguard_rename_login->send_notify();
197 }
198 function upgrade() {
199 global $siteguard_config, $siteguard_rename_login, $siteguard_admin_filter, $siteguard_loginalert, $siteguard_updates_notify, $siteguard_login_history, $siteguard_xmlrpc, $siteguard_author_query, $siteguard_waf_exclude_rule;
200 $upgrade_ok = true;
201 $old_version = $siteguard_config->get( 'version' );
202 if ( '' === $old_version ) {
203 $old_version = '0.0.0';
204 }
205 if ( $old_version === SITEGUARD_VERSION ) {
206 return;
207 }
208 if ( version_compare( $old_version, '1.0.6' ) < 0 ) {
209 if ( '1' === $siteguard_config->get( 'admin_filter_enable' ) ) {
210 if ( true !== $siteguard_admin_filter->feature_on( $this->get_ip() ) ) {
211 siteguard_error_log( 'Failed to update at admin_filter from ' . $old_version . ' to ' . SITEGUARD_VERSION . '.' );
212 $upgrade_ok = false;
213 }
214 }
215 }
216 if ( version_compare( $old_version, '1.1.1' ) < 0 ) {
217 $siteguard_loginalert->init();
218 }
219 if ( version_compare( $old_version, '1.2.0' ) < 0 ) {
220 $siteguard_updates_notify->init();
221 }
222 if ( version_compare( $old_version, '1.2.5' ) < 0 ) {
223 if ( '1' === $siteguard_config->get( 'admin_filter_enable' ) ) {
224 $siteguard_admin_filter->cvt_status_for_1_2_5( $this->get_ip() );
225 }
226 if ( '1' === $siteguard_config->get( 'renamelogin_enable' ) ) {
227 if ( true !== $siteguard_rename_login->feature_on() ) {
228 siteguard_error_log( 'Failed to update at rename_login from ' . $old_version . ' to ' . SITEGUARD_VERSION . '.' );
229 $upgrade_ok = false;
230 }
231 }
232 }
233 if ( version_compare( $old_version, '1.3.0' ) < 0 ) {
234 $siteguard_login_history->init();
235 $siteguard_xmlrpc->init();
236 }
237 if ( version_compare( $old_version, '1.5.0' ) < 0 ) {
238 $admin_filter_exclude_path = $siteguard_config->get( 'admin_filter_exclude_path' );
239 if ( false === strpos( $admin_filter_exclude_path, 'site-health.php' ) ) {
240 $admin_filter_exclude_path .= ', site-health.php';
241 $siteguard_config->set( 'admin_filter_exclude_path', $admin_filter_exclude_path );
242 $siteguard_config->update();
243 }
244 }
245 if ( version_compare( $old_version, '1.5.1' ) < 0 ) {
246 if ( '1' === $siteguard_config->get( 'admin_filter_enable' ) ) {
247 if ( true !== $siteguard_admin_filter->feature_on( $this->get_ip() ) ) {
248 siteguard_error_log( 'Failed to update at admin_filter from ' . $old_version . ' to ' . SITEGUARD_VERSION . '.' );
249 $upgrade_ok = false;
250 }
251 }
252 if ( '1' === $siteguard_config->get( 'disable_xmlrpc_enable' ) ) {
253 if ( true !== $siteguard_xmlrpc->feature_on() ) {
254 siteguard_error_log( 'Failed to update at disable_xmlrpc from ' . $old_version . ' to ' . SITEGUARD_VERSION . '.' );
255 $upgrade_ok = false;
256 }
257 }
258 }
259 if ( version_compare( $old_version, '1.6.0' ) < 0 ) {
260 $siteguard_author_query->init();
261 }
262 if ( version_compare( $old_version, '1.7.0' ) < 0 ) {
263 if ( '1' === $siteguard_config->get( 'admin_filter_enable' ) ) {
264 if ( true !== $siteguard_admin_filter->feature_on( $this->get_ip() ) ) {
265 siteguard_error_log( 'Failed to update at admin_filter from ' . $old_version . ' to ' . SITEGUARD_VERSION . '.' );
266 $upgrade_ok = false;
267 }
268 }
269 }
270 if ( version_compare( $old_version, '1.8.0' ) < 0 ) {
271 SiteGuard_Htaccess::clear_settings( $siteguard_admin_filter->get_mark() );
272 SiteGuard_Htaccess::clear_settings( $siteguard_xmlrpc->get_mark() );
273 if ( '' === $siteguard_config->get( 'rescue_enable' ) ) {
274 $siteguard_config->set( 'rescue_enable', '1' );
275 $siteguard_config->update();
276 }
277 // Remove legacy error.log left by previous versions; logging now
278 // uses PHP error_log() so the file would only sit web-exposed on Nginx.
279 $legacy_log = SITEGUARD_PATH . 'error.log';
280 if ( file_exists( $legacy_log ) ) {
281 @unlink( $legacy_log );
282 }
283 // Remove legacy plugin-directory tmp/ used for .htaccess rebuilds.
284 // `clear_settings()` / `update_settings()` now short-circuit on
285 // Nginx (no .htaccess in use), so this directory will not be
286 // recreated there. On Apache it will be regenerated as needed
287 // by make_tmp_dir(). Existing orphan tempnam files would be
288 // web-exposed on Nginx without the .htaccess inside the dir.
289 $legacy_tmp = SITEGUARD_PATH . 'tmp';
290 if ( is_dir( $legacy_tmp ) ) {
291 $entries = @scandir( $legacy_tmp );
292 if ( is_array( $entries ) ) {
293 foreach ( $entries as $entry ) {
294 if ( '.' === $entry || '..' === $entry ) {
295 continue;
296 }
297 $path = $legacy_tmp . DIRECTORY_SEPARATOR . $entry;
298 if ( is_file( $path ) ) {
299 if ( ! @unlink( $path ) ) {
300 @chmod( $path, 0644 );
301 @unlink( $path );
302 }
303 }
304 }
305 }
306 @rmdir( $legacy_tmp );
307 }
308 // Remove legacy CAPTCHA answer files (*.txt). Pre-1.8.0 stored
309 // them at WP_CONTENT_DIR/siteguard/ with an .htaccess block on
310 // .txt; on Nginx that block does not apply and the salt+hash
311 // would be readable. New answer files use .php with a stub
312 // prefix and live in the same directory.
313 $captcha_dir = path_join( WP_CONTENT_DIR, 'siteguard' );
314 if ( is_dir( $captcha_dir ) ) {
315 $entries = @scandir( $captcha_dir );
316 if ( is_array( $entries ) ) {
317 foreach ( $entries as $entry ) {
318 if ( preg_match( '/\.txt$/', $entry ) ) {
319 $path = $captcha_dir . DIRECTORY_SEPARATOR . $entry;
320 if ( is_file( $path ) ) {
321 if ( ! @unlink( $path ) ) {
322 @chmod( $path, 0644 );
323 @unlink( $path );
324 }
325 }
326 }
327 }
328 }
329 }
330 }
331 if ( $upgrade_ok && SITEGUARD_VERSION !== $old_version ) {
332 $siteguard_config->set( 'version', SITEGUARD_VERSION );
333 $siteguard_config->update();
334 }
335 }
336 }
337 $siteguard = new SiteGuard();
338