ninjafirewall
Last commit date
images
1 month ago
languages
11 months ago
lib
1 month ago
static
1 month ago
.htaccess
7 years ago
.htninja.sample
2 years ago
LICENSE.TXT
13 years ago
index.html
12 years ago
ninjafirewall.php
1 month ago
readme.txt
1 month ago
uninstall.php
1 year ago
ninjafirewall.php
1237 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: NinjaFirewall (WP Edition) |
| 4 | Plugin URI: https://nintechnet.com/ |
| 5 | Description: A true Web Application Firewall to protect and secure WordPress. |
| 6 | Version: 4.8.8 |
| 7 | Author: The Ninja Technologies Network |
| 8 | Author URI: https://nintechnet.com/ |
| 9 | License: GPLv3 or later |
| 10 | Network: true |
| 11 | Text Domain: ninjafirewall |
| 12 | Domain Path: /languages |
| 13 | */ |
| 14 | define('NFW_ENGINE_VERSION', '4.8.8'); |
| 15 | /* |
| 16 | +=====================================================================+ |
| 17 | | _ _ _ _ _____ _ _ _ | |
| 18 | | | \ | (_)_ __ (_) __ _| ___(_)_ __ _____ ____ _| | | | |
| 19 | | | \| | | '_ \ | |/ _` | |_ | | '__/ _ \ \ /\ / / _` | | | | |
| 20 | | | |\ | | | | || | (_| | _| | | | | __/\ V V / (_| | | | | |
| 21 | | |_| \_|_|_| |_|/ |\__,_|_| |_|_| \___| \_/\_/ \__,_|_|_| | |
| 22 | | |__/ | |
| 23 | | (c) NinTechNet Limited ~ https://nintechnet.com/ | |
| 24 | +=====================================================================+ |
| 25 | */ |
| 26 | |
| 27 | if (! defined('ABSPATH') ) { |
| 28 | die('Forbidden'); |
| 29 | } |
| 30 | |
| 31 | /* ------------------------------------------------------------------ */ |
| 32 | define('NFW_NULL_BYTE', 2); |
| 33 | define('NFW_SCAN_BOTS', 531); |
| 34 | define('NFW_ASCII_CTRL', 500); |
| 35 | define('NFW_DOC_ROOT', 510); |
| 36 | define('NFW_WRAPPERS', 520); |
| 37 | define('NFW_OBJECTS', 525); |
| 38 | define('NFW_LOOPBACK', 540); |
| 39 | define('NFW_DEFAULT_MSG', '<br /><br /><br /><br /><center>' . |
| 40 | sprintf('Sorry %s, your request cannot be processed.', '<b>%%REM_ADDRESS%%</b>') . |
| 41 | '<br />' . 'For security reasons, it was blocked and logged.' . |
| 42 | '<br /><br />%%NINJA_LOGO%%<br /><br />' . |
| 43 | 'If you believe this was an error please contact the<br />webmaster and enclose the '. |
| 44 | 'following incident ID:' .' <br /><br />[ <b>#%%NUM_INCIDENT%%</b> ]</center>' |
| 45 | ); |
| 46 | |
| 47 | /** |
| 48 | * Since WP 6.7, translation loading must not be triggered too early. |
| 49 | */ |
| 50 | require_once __DIR__ . '/lib/i18n.php'; |
| 51 | |
| 52 | if (! defined('NFW_LOG_DIR') ) { |
| 53 | define('NFW_LOG_DIR', WP_CONTENT_DIR ); |
| 54 | } |
| 55 | if (! empty( $_SERVER['DOCUMENT_ROOT'] ) && $_SERVER['DOCUMENT_ROOT'] != '/') { |
| 56 | $_SERVER['DOCUMENT_ROOT'] = rtrim( $_SERVER['DOCUMENT_ROOT'] , '/'); |
| 57 | } |
| 58 | |
| 59 | /* ------------------------------------------------------------------ */ |
| 60 | |
| 61 | /** |
| 62 | * Select whether we want to use PHP or NF (default since v4.8.1) sessions. |
| 63 | */ |
| 64 | if ( is_file( NFW_LOG_DIR .'/nfwlog/phpsession') ) { |
| 65 | require_once __DIR__ .'/lib/class-php-session.php'; |
| 66 | } else { |
| 67 | if (! defined('NFWSESSION_DIR') ) { |
| 68 | /** |
| 69 | * NFWSESSION_DIR can be defined in the .htninja. |
| 70 | */ |
| 71 | define('NFWSESSION_DIR', NFW_LOG_DIR .'/nfwlog/session'); |
| 72 | } |
| 73 | require_once __DIR__ .'/lib/class-nfw-session.php'; |
| 74 | } |
| 75 | |
| 76 | if (! defined( 'NFW_REMOTE_ADDR') ) { |
| 77 | /** |
| 78 | * Error: the firewall isn't loaded. |
| 79 | */ |
| 80 | require_once __DIR__ .'/lib/class-ip.php'; |
| 81 | NinjaFirewall_IP::check_ip( ['ac_ip' => 1 ] ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Those classes and constants could be already loaded/defined by the firewall (if enabled). |
| 86 | */ |
| 87 | if ( ! defined('NFWLOG_DEBUG') ) { |
| 88 | define('NFWLOG_MEDIUM', 1); |
| 89 | define('NFWLOG_HIGH', 2); |
| 90 | define('NFWLOG_CRITICAL', 3); |
| 91 | define('NFWLOG_POSTDETECT', 4); |
| 92 | define('NFWLOG_UPLOAD', 5); |
| 93 | define('NFWLOG_INFO', 6); |
| 94 | define('NFWLOG_DEBUG', 7); |
| 95 | } |
| 96 | require_once __DIR__ .'/lib/class-firewall-log.php'; |
| 97 | require_once __DIR__ . '/lib/class-helpers.php'; |
| 98 | require_once __DIR__ .'/lib/class_mail.php'; |
| 99 | |
| 100 | require __DIR__ . '/lib/scheduled_tasks.php'; |
| 101 | require __DIR__ . '/lib/helpers.php'; |
| 102 | require __DIR__ . '/lib/settings_events.php'; |
| 103 | |
| 104 | add_action( 'nfwgccron', 'nfw_garbage_collector' ); |
| 105 | |
| 106 | /* ------------------------------------------------------------------ */ |
| 107 | |
| 108 | function nfw_activate() { |
| 109 | |
| 110 | // Install/activate NinjaFirewall |
| 111 | |
| 112 | if ( defined('WP_CLI') && WP_CLI && PHP_SAPI === 'cli' ) { |
| 113 | $php_cli = true; |
| 114 | } |
| 115 | |
| 116 | if (! isset( $php_cli ) ) { |
| 117 | /** |
| 118 | * Warn if the user does not have the 'unfiltered_html' capability. |
| 119 | */ |
| 120 | if (! current_user_can('unfiltered_html') ) { |
| 121 | wp_die( |
| 122 | esc_html__('You do not have "unfiltered_html" capability. Please enable it in order to run NinjaFirewall (or make sure you do not have "DISALLOW_UNFILTERED_HTML" in your wp-config.php script).', 'ninjafirewall'), |
| 123 | esc_html__('Plugin Activation Error', 'ninjafirewall'), |
| 124 | ['back_link' => true ] |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * WordPress minimum version. |
| 131 | */ |
| 132 | global $wp_version; |
| 133 | if ( version_compare( $wp_version, '4.7.0', '<') ) { |
| 134 | wp_die( sprintf( |
| 135 | esc_html__('NinjaFirewall requires WordPress %s or greater but your current version is %s.', 'ninjafirewall'), '4.7.0', $wp_version |
| 136 | ), |
| 137 | esc_html__('Plugin Activation Error', 'ninjafirewall'), |
| 138 | ['back_link' => true ] |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * PHP minimum version. |
| 144 | */ |
| 145 | if ( version_compare( PHP_VERSION, '7.1.0', '<') ) { |
| 146 | wp_die( sprintf( |
| 147 | esc_html__('NinjaFirewall requires PHP 7.1 or greater but your current version is %s.', 'ninjafirewall'), PHP_VERSION |
| 148 | ), |
| 149 | esc_html__('Plugin Activation Error', 'ninjafirewall'), |
| 150 | ['back_link' => true ] |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * We need the mysqli extension loaded. |
| 156 | */ |
| 157 | if (! function_exists('mysqli_connect') ) { |
| 158 | wp_die( sprintf( |
| 159 | esc_html__('NinjaFirewall requires the PHP %s extension.', 'ninjafirewall'), '<code>mysqli</code>' |
| 160 | ), |
| 161 | esc_html__('Plugin Activation Error', 'ninjafirewall'), |
| 162 | ['back_link' => true ] |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * We don't do Windows. |
| 168 | */ |
| 169 | if ( PATH_SEPARATOR == ';') { |
| 170 | wp_die( |
| 171 | esc_html__('NinjaFirewall is not compatible with Microsoft Windows.', 'ninjafirewall'), |
| 172 | esc_html__('Plugin Activation Error', 'ninjafirewall'), |
| 173 | ['back_link' => true ] |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | if (! $nfw_options = nfw_get_option( 'nfw_options' ) ) { |
| 178 | // First time we're running: download the security rules |
| 179 | // and populate the options: |
| 180 | require_once __DIR__ .'/lib/install_default.php'; |
| 181 | nfw_load_default_conf(); |
| 182 | // Reload them |
| 183 | $nfw_options = nfw_get_option( 'nfw_options' ); |
| 184 | } else { |
| 185 | // (Re)create the loader |
| 186 | require_once __DIR__ .'/lib/install_default.php'; |
| 187 | nfw_create_loader(); |
| 188 | } |
| 189 | |
| 190 | $nfw_options['enabled'] = 1; |
| 191 | nfw_update_option( 'nfw_options', $nfw_options); |
| 192 | |
| 193 | $res = nfw_enable_wpwaf(); |
| 194 | if (! empty( $res ) ){ |
| 195 | /** |
| 196 | * Display WAF activation errors. |
| 197 | */ |
| 198 | wp_die( $res ); |
| 199 | } |
| 200 | |
| 201 | // Create scheduled tasks. |
| 202 | nfw_create_scheduled_tasks(); |
| 203 | |
| 204 | // Re-enable brute-force protection |
| 205 | if ( file_exists( NFW_LOG_DIR . '/nfwlog/cache/bf_conf_off.php' ) ) { |
| 206 | rename(NFW_LOG_DIR . '/nfwlog/cache/bf_conf_off.php', NFW_LOG_DIR . '/nfwlog/cache/bf_conf.php'); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | register_activation_hook( __FILE__, 'nfw_activate' ); |
| 211 | |
| 212 | /* ------------------------------------------------------------------ */ |
| 213 | |
| 214 | function nfw_deactivate() { |
| 215 | |
| 216 | if ( defined('WP_CLI') && WP_CLI && PHP_SAPI === 'cli') { |
| 217 | $php_cli = true; |
| 218 | } |
| 219 | |
| 220 | if (! isset( $php_cli ) ) { |
| 221 | /** |
| 222 | * Warn if the user does not have the 'unfiltered_html' capability unless it's CLI. |
| 223 | */ |
| 224 | if (! current_user_can('unfiltered_html') ) { |
| 225 | exit( esc_html__('You do not have "unfiltered_html" capability. Please enable it in order to run NinjaFirewall (or make sure you do not have "DISALLOW_UNFILTERED_HTML" in your wp-config.php script).', 'ninjafirewall') ); |
| 226 | } |
| 227 | nf_not_allowed('block', __LINE__ ); |
| 228 | |
| 229 | global $current_user; |
| 230 | $current_user = wp_get_current_user(); |
| 231 | $user_login = $current_user->user_login; |
| 232 | $user_roles = $current_user->roles[0]; |
| 233 | } else { |
| 234 | $user_login = 'WP CLI'; |
| 235 | $user_roles = '-'; |
| 236 | } |
| 237 | |
| 238 | $nfw_options = nfw_get_option('nfw_options'); |
| 239 | |
| 240 | /** |
| 241 | * Re-used code from Firewall Options. |
| 242 | */ |
| 243 | if ( empty( $_REQUEST['action'] ) || strpos( $_REQUEST['action'], 'deactivate') === false ) { |
| 244 | |
| 245 | if ( is_multisite() ) { |
| 246 | $url = network_home_url('/'); |
| 247 | } else { |
| 248 | $url = home_url('/'); |
| 249 | } |
| 250 | |
| 251 | $subject = [ ]; |
| 252 | $content = [ "$user_login ($user_roles)", NFW_REMOTE_ADDR, |
| 253 | ucfirst( date_i18n('F j, Y @ H:i:s O') ), $url ]; |
| 254 | |
| 255 | NinjaFirewall_mail::send('disabled', $subject, $content, '', [], 1 ); |
| 256 | } |
| 257 | |
| 258 | $nfw_options['enabled'] = 0; |
| 259 | nfw_disable_wpwaf(); |
| 260 | |
| 261 | /** |
| 262 | * Disable brute-force protection. |
| 263 | */ |
| 264 | if ( file_exists( NFW_LOG_DIR .'/nfwlog/cache/bf_conf.php') ) { |
| 265 | rename(NFW_LOG_DIR .'/nfwlog/cache/bf_conf.php', NFW_LOG_DIR .'/nfwlog/cache/bf_conf_off.php'); |
| 266 | } |
| 267 | |
| 268 | nfw_update_option('nfw_options', $nfw_options); |
| 269 | |
| 270 | /** |
| 271 | * Remove any existing cron. |
| 272 | */ |
| 273 | nfw_delete_scheduled_tasks(); |
| 274 | |
| 275 | } |
| 276 | |
| 277 | register_deactivation_hook( __FILE__, 'nfw_deactivate'); |
| 278 | |
| 279 | /* ------------------------------------------------------------------ */ |
| 280 | // Load script/style files |
| 281 | |
| 282 | function nfw_load_ext( $hook ) { |
| 283 | |
| 284 | // Load the external JS script and CSS: |
| 285 | // -Single site: to the admin only. |
| 286 | // -Multi-site: to the superadmin and from the main network admin screen only. |
| 287 | // -All: only if this is a NinjaFirewall menu page, or the Plugins page |
| 288 | if (! current_user_can('activate_plugins') || ! is_main_site() ) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | if ( stripos( $hook, 'ninjafirewall' ) === false && $hook != 'plugins.php') { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | if ( strpos ( $hook, 'nfsubwplus' ) !== false ) { |
| 297 | // Load thickbox JS and CSS (WP only for "WP+" menu page's screenshots) |
| 298 | $extra_js = ['jquery', 'thickbox']; |
| 299 | $extra_css = ['thickbox']; |
| 300 | } else { |
| 301 | $extra_js = ['jquery']; |
| 302 | $extra_css = null; |
| 303 | } |
| 304 | |
| 305 | // TipTip (WP Edition only) |
| 306 | wp_enqueue_script( |
| 307 | 'jquery-tiptip', |
| 308 | plugin_dir_url( __FILE__ ) .'static/jquery.tipTip.js', |
| 309 | ['jquery'], |
| 310 | NFW_ENGINE_VERSION |
| 311 | ); |
| 312 | |
| 313 | |
| 314 | wp_enqueue_script( |
| 315 | 'nfw_javascript', |
| 316 | plugin_dir_url( __FILE__ ) .'static/ninjafirewall.js', |
| 317 | $extra_js, |
| 318 | NFW_ENGINE_VERSION |
| 319 | ); |
| 320 | |
| 321 | // Load Chart.js if we are viewing the statistics page: |
| 322 | if ( strpos( $hook, 'NinjaFirewall' ) !== false ) { |
| 323 | wp_enqueue_script( |
| 324 | 'nfw_charts', |
| 325 | plugin_dir_url( __FILE__ ) . 'static/chart.min.js', |
| 326 | ['jquery'], |
| 327 | NFW_ENGINE_VERSION, |
| 328 | // We load it in the footer, because some plugins loads it too |
| 329 | // on every pages and that could mess with our pages |
| 330 | true |
| 331 | ); |
| 332 | } |
| 333 | |
| 334 | wp_enqueue_style( |
| 335 | 'nfw_style', |
| 336 | plugin_dir_url( __FILE__ ) .'static/ninjafirewall.css', |
| 337 | $extra_css, |
| 338 | NFW_ENGINE_VERSION, |
| 339 | false |
| 340 | ); |
| 341 | |
| 342 | // Javascript i18n: |
| 343 | $nfw_js_array = [ |
| 344 | |
| 345 | // Generic |
| 346 | 'restore_default' => |
| 347 | __('All fields will be restored to their default values and any changes you made will be lost. Continue?', 'ninjafirewall'), |
| 348 | |
| 349 | // Full WAF/WordPress WAF |
| 350 | 'missing_nonce' => |
| 351 | __('Missing security nonce, try to reload the page.', 'ninjafirewall'), |
| 352 | 'missing_httpserver' => |
| 353 | __('Please select the HTTP server in the list.', 'ninjafirewall'), |
| 354 | // Dashboard |
| 355 | 'del_errorlog' => |
| 356 | __('Delete the firewall\'s error log ?', 'ninjafirewall'), |
| 357 | |
| 358 | // Firewall Options |
| 359 | 'restore_warning' => |
| 360 | __('This action will restore the selected configuration file and will override all your current firewall options, policies and rules. Continue?', 'ninjafirewall'), |
| 361 | |
| 362 | // Firewall Policies |
| 363 | 'warn_sanitise' => |
| 364 | __('Any character that is not a letter [a-zA-Z], a digit [0-9], a dot [.], a hyphen [-] or an underscore [_] will be removed from the filename and replaced with the substitution character. Continue?', 'ninjafirewall'), |
| 365 | 'ssl_warning' => |
| 366 | __('Ensure that you can access your admin dashboard over HTTPS before enabling this option, otherwise you will lock yourself out of your site. Continue?', 'ninjafirewall'), |
| 367 | 'woo_warning' => |
| 368 | __("WooCommerce is running: if you block accounts creation, your customers won't be able to sign up. Continue?", 'ninjafirewall'), |
| 369 | 'reguser_warning' => |
| 370 | __("Your blog has user registration enabled: if you block accounts creation, your customers won't be able to sign up. Continue?", 'ninjafirewall'), |
| 371 | 'regsite_warning' => |
| 372 | __("Your multisite installation allows users to register new sites: if you enable this option, they will likely get blocked when creating their blog. Continue?", 'ninjafirewall'), |
| 373 | |
| 374 | // File Check |
| 375 | 'del_snapshot' => |
| 376 | __('Delete the current snapshot ?', 'ninjafirewall'), |
| 377 | |
| 378 | // Login Protection |
| 379 | 'invalid_char' => |
| 380 | __('Invalid character.', 'ninjafirewall'), |
| 381 | 'no_admin' => |
| 382 | __('"admin" is not acceptable, please choose another user name.', 'ninjafirewall'), |
| 383 | 'max_char' => |
| 384 | __('Please enter max 1024 character only.', 'ninjafirewall'), |
| 385 | 'select_when' => |
| 386 | __('Select when to enable the login protection.', 'ninjafirewall'), |
| 387 | 'missing_auth' => |
| 388 | __('Enter a name and a password for the HTTP authentication.', 'ninjafirewall'), |
| 389 | |
| 390 | // Firewall Log |
| 391 | 'invalid_key' => |
| 392 | __('Your public key is not valid.', 'ninjafirewall'), |
| 393 | |
| 394 | // Events notification |
| 395 | 'missing_parameters' => |
| 396 | __('Missing parameters.', 'ninjafirewall'), |
| 397 | 'unknown_error' => |
| 398 | __('Unknown error.', 'ninjafirewall'), |
| 399 | |
| 400 | // Live Log |
| 401 | 'live_log_desc' => |
| 402 | __('Live Log lets you watch your blog traffic in real time. To enable it, click on the button below.', 'ninjafirewall'), |
| 403 | 'no_traffic' => |
| 404 | __('No traffic yet, please wait', 'ninjafirewall'), |
| 405 | 'seconds' => |
| 406 | ' ' . __('seconds...', 'ninjafirewall'), |
| 407 | 'err_unexpected' => |
| 408 | __('Error: Live Log did not receive the expected response from your server:', 'ninjafirewall'), |
| 409 | 'error_404' => |
| 410 | __('Error: URL does not seem to exist (404 Not Found):', 'ninjafirewall'), |
| 411 | 'log_not_found' => |
| 412 | __('Error: Cannot find your log file. Try to reload this page.', 'ninjafirewall'), |
| 413 | 'http_error' => |
| 414 | __('Error: The HTTP server returned the following error code:', 'ninjafirewall') |
| 415 | ]; |
| 416 | |
| 417 | wp_localize_script( 'nfw_javascript', 'nfwi18n', $nfw_js_array ); |
| 418 | } |
| 419 | |
| 420 | add_action( 'admin_enqueue_scripts', 'nfw_load_ext' ); |
| 421 | |
| 422 | /* ------------------------------------------------------------------ */ |
| 423 | |
| 424 | function nfw_admin_init() { |
| 425 | |
| 426 | // We must make sure that the current PHP session is always |
| 427 | // updated even for whitelisted non-admin users (must be logged-in |
| 428 | // to prevent unauthenticated AJAX calls to trigger it): |
| 429 | if ( is_user_logged_in() ) { |
| 430 | NinjaFirewall_session::start(); |
| 431 | // Save user's capabilities |
| 432 | $nf_user = wp_get_current_user(); |
| 433 | if ( $nf_user instanceof WP_User ) { |
| 434 | NinjaFirewall_session::write( ['allcaps' => $nf_user->allcaps ] ); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | $nfw_options = nfw_get_option( 'nfw_options' ); |
| 439 | $nfw_rules = nfw_get_option( 'nfw_rules' ); |
| 440 | |
| 441 | // Post-update adjustment: |
| 442 | require plugin_dir_path(__FILE__) . 'lib/init_update.php'; |
| 443 | |
| 444 | // Make sure cronjobs are running as expected |
| 445 | nfw_verify_scheduled_tasks(); |
| 446 | |
| 447 | // -------------------------------------------- |
| 448 | // Anything below requires admin authentication |
| 449 | // -------------------------------------------- |
| 450 | |
| 451 | if ( nf_not_allowed(0, __LINE__) ) { return; } |
| 452 | |
| 453 | // Create our unique PID |
| 454 | $nfw_pid = NFW_LOG_DIR .'/nfwlog/cache/.pid'; |
| 455 | if (! file_exists( $nfw_pid ) ) { |
| 456 | file_put_contents( $nfw_pid, uniqid('', true) ); |
| 457 | } |
| 458 | |
| 459 | // Update fallback loader if needed |
| 460 | if ( wp_doing_ajax() == false ) { |
| 461 | nfw_enable_wpwaf(); |
| 462 | } |
| 463 | |
| 464 | // Security update in WP plugins: |
| 465 | global $pagenow; |
| 466 | if ( $pagenow == 'plugins.php' && current_user_can( 'update_plugins' ) ) { |
| 467 | nfw_verify_secupdates(); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Export NinjaFirewall's configuration. |
| 472 | */ |
| 473 | if ( isset( $_POST['ninjafirewall_export'] ) ) { |
| 474 | |
| 475 | require_once __DIR__ .'/lib/class-import-export.php'; |
| 476 | NinjaFirewall_ImpExp::export(); |
| 477 | } |
| 478 | |
| 479 | // Download File Check modified files list: |
| 480 | if ( isset($_POST['dlmods']) ) { |
| 481 | if ( empty($_POST['nfwnonce']) || ! wp_verify_nonce($_POST['nfwnonce'], 'filecheck_save') ) { |
| 482 | wp_nonce_ays('filecheck_save'); |
| 483 | } |
| 484 | if (file_exists(NFW_LOG_DIR . '/nfwlog/cache/nfilecheck_diff.php') ) { |
| 485 | $download_file = NFW_LOG_DIR . '/nfwlog/cache/nfilecheck_diff.php'; |
| 486 | } elseif (file_exists(NFW_LOG_DIR . '/nfwlog/cache/nfilecheck_diff.php.php') ) { |
| 487 | $download_file = NFW_LOG_DIR . '/nfwlog/cache/nfilecheck_diff.php.php'; |
| 488 | } else { |
| 489 | wp_nonce_ays('filecheck_save'); |
| 490 | } |
| 491 | $stat = stat($download_file); |
| 492 | $data = '== NinjaFirewall File Check (diff)'. "\n"; |
| 493 | $data.= '== ' . site_url() . "\n"; |
| 494 | $data.= '== ' . date_i18n('M d, Y @ H:i:s O', $stat['ctime']) . "\n\n"; |
| 495 | $data.= '[+] = ' . __('New file', 'ninjafirewall') . |
| 496 | ' [!] = ' . __('Modified file', 'ninjafirewall') . |
| 497 | ' [-] = ' . __('Deleted file', 'ninjafirewall') . |
| 498 | "\n\n"; |
| 499 | $fh = fopen($download_file, 'r'); |
| 500 | while (! feof($fh) ) { |
| 501 | $res = explode('::', fgets($fh) ); |
| 502 | if ( empty($res[1]) ) { continue; } |
| 503 | if ($res[1] == 'N') { |
| 504 | $data .= '[+] ' . $res[0] . "\n"; |
| 505 | } elseif ($res[1] == 'D') { |
| 506 | $data .= '[-] ' . $res[0] . "\n"; |
| 507 | } elseif ($res[1] == 'M') { |
| 508 | $data .= '[!] ' . $res[0] . "\n"; |
| 509 | } |
| 510 | } |
| 511 | fclose($fh); |
| 512 | $data .= "\n== EOF\n"; |
| 513 | /** |
| 514 | * Use the home_url instead of SERVER_NAME, as they could be different. |
| 515 | */ |
| 516 | $dl_name = sanitize_file_name( wp_parse_url( home_url(), PHP_URL_HOST ) ); |
| 517 | if ( empty( $dl_name ) ) { |
| 518 | $dl_name = sanitize_file_name( $_SERVER['SERVER_NAME'] ); |
| 519 | } |
| 520 | header('Content-Type: text/plain'); |
| 521 | header('Content-Length: '. strlen( $data ) ); |
| 522 | header('Content-Disposition: attachment; filename="'. $dl_name .'_diff.txt"'); |
| 523 | echo $data; |
| 524 | exit; |
| 525 | } |
| 526 | |
| 527 | // Download File Check snapshot: |
| 528 | if ( isset($_POST['dlsnap']) ) { |
| 529 | if ( empty($_POST['nfwnonce']) || ! wp_verify_nonce($_POST['nfwnonce'], 'filecheck_save') ) { |
| 530 | wp_nonce_ays('filecheck_save'); |
| 531 | } |
| 532 | if (file_exists(NFW_LOG_DIR . '/nfwlog/cache/nfilecheck_snapshot.php') ) { |
| 533 | $stat = stat(NFW_LOG_DIR . '/nfwlog/cache/nfilecheck_snapshot.php'); |
| 534 | $data = '== NinjaFirewall File Check (snapshot)'. "\n"; |
| 535 | $data.= '== ' . site_url() . "\n"; |
| 536 | $data.= '== ' . date_i18n('M d, Y @ H:i:s O', $stat['ctime']) . "\n\n"; |
| 537 | $fh = fopen(NFW_LOG_DIR . '/nfwlog/cache/nfilecheck_snapshot.php', 'r'); |
| 538 | while (! feof($fh) ) { |
| 539 | $res = explode('::', fgets($fh) ); |
| 540 | if (! empty($res[0][0]) && $res[0][0] == '/') { |
| 541 | $data .= $res[0] . "\n"; |
| 542 | } |
| 543 | } |
| 544 | fclose($fh); |
| 545 | $data .= "\n== EOF\n"; |
| 546 | /** |
| 547 | * Use the home_url instead of SERVER_NAME, as they could be different. |
| 548 | */ |
| 549 | $dl_name = sanitize_file_name( wp_parse_url( home_url(), PHP_URL_HOST ) ); |
| 550 | if ( empty( $dl_name ) ) { |
| 551 | $dl_name = sanitize_file_name( $_SERVER['SERVER_NAME'] ); |
| 552 | } |
| 553 | header('Content-Type: text/plain'); |
| 554 | header('Content-Length: '. strlen( $data ) ); |
| 555 | header('Content-Disposition: attachment; filename="'. $dl_name .'_snapshot.txt"'); |
| 556 | echo $data; |
| 557 | exit; |
| 558 | } else { |
| 559 | wp_nonce_ays('filecheck_save'); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | // Applies to admin only (unlike the WP+ Edition): |
| 564 | if (! empty( $nfw_options['wl_admin'] ) ) { |
| 565 | if (! empty( $nfw_options['bf_enable'] ) && ! empty( $nfw_options['bf_rand'] ) ) { |
| 566 | NinjaFirewall_session::write( ['nfw_goodguy' => true, 'nfw_bfd' => $nfw_options['bf_rand'] ] ); |
| 567 | } else { |
| 568 | NinjaFirewall_session::write( ['nfw_goodguy' => true ] ); |
| 569 | } |
| 570 | return; |
| 571 | } |
| 572 | NinjaFirewall_session::delete('nfw_goodguy'); |
| 573 | } |
| 574 | |
| 575 | add_action('admin_init', 'nfw_admin_init' ); |
| 576 | |
| 577 | // --------------------------------------------------------------------- |
| 578 | // Check if the user wants to remove her email from the notification list. |
| 579 | |
| 580 | function nfw_init_emailremoval() { |
| 581 | |
| 582 | if (! empty( $_GET['nfw_stop_notification'] ) ) { |
| 583 | require_once 'lib/class-email-sodium.php'; |
| 584 | NinjaFirewall_emailsodium::sodium_decrypt( $_GET['nfw_stop_notification'] ); |
| 585 | } |
| 586 | |
| 587 | } |
| 588 | add_action('init', 'nfw_init_emailremoval' ); |
| 589 | |
| 590 | // --------------------------------------------------------------------- |
| 591 | // Check if the user is an admin and if we must whitelist them. |
| 592 | |
| 593 | function nfw_login_hook( $user_login, $user ) { |
| 594 | |
| 595 | NinjaFirewall_session::start(); |
| 596 | |
| 597 | $nfw_options = nfw_get_option( 'nfw_options' ); |
| 598 | |
| 599 | // Don't do anything if NinjaFirewall is disabled: |
| 600 | if ( empty( $nfw_options['enabled'] ) ) { return; } |
| 601 | |
| 602 | // Fetch user roles: |
| 603 | $whoami = ''; |
| 604 | foreach( $user->roles as $k => $v ) { |
| 605 | if ( $v == 'administrator' ) { |
| 606 | $admin_flag = 1; |
| 607 | } |
| 608 | $whoami .= "$v "; |
| 609 | } |
| 610 | $whoami = trim( $whoami ); |
| 611 | |
| 612 | // Still nothing: Maybe an additional superadmin |
| 613 | if ( empty( $whoami ) && is_multisite() ) { |
| 614 | // $user->ID is required here |
| 615 | if ( is_super_admin( $user->ID ) ) { |
| 616 | $admin_flag = 1; |
| 617 | $whoami = 'administrator'; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // Are we supposed to send an alert? |
| 622 | if (! empty( $nfw_options['a_0'] ) ) { |
| 623 | if ( ( $nfw_options['a_0'] == 1 && isset( $admin_flag ) ) || $nfw_options['a_0'] == 2 ) { |
| 624 | |
| 625 | nfw_send_loginemail( $user_login, $whoami ); |
| 626 | /** |
| 627 | * Write event to log. |
| 628 | */ |
| 629 | if (! empty( $nfw_options['a_41'] ) ) { |
| 630 | NinjaFirewall_log::write( |
| 631 | 'Logged in user', |
| 632 | "{$user_login} ({$whoami})", |
| 633 | NFWLOG_INFO, 0, $nfw_options, NFW_LOG_DIR .'/nfwlog' |
| 634 | ); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | //Whitelist: |
| 640 | if (! empty( $nfw_options['wl_admin']) ) { |
| 641 | if ( ( $nfw_options['wl_admin'] == 1 && isset( $admin_flag ) ) || $nfw_options['wl_admin'] == 2 ) { |
| 642 | // Set the goodguy flag |
| 643 | NinjaFirewall_session::write( ['nfw_goodguy' => true ] ); |
| 644 | return; |
| 645 | } |
| 646 | } |
| 647 | NinjaFirewall_session::delete('nfw_goodguy'); |
| 648 | } |
| 649 | |
| 650 | // Hook priority can be defined in the wp-config.php or .htninja |
| 651 | if ( defined('NFW_LOGINHOOK') ) { |
| 652 | $NFW_LOGINHOOK = (int) NFW_LOGINHOOK; |
| 653 | } else { |
| 654 | $NFW_LOGINHOOK = -999999999; |
| 655 | } |
| 656 | add_action( 'wp_login', 'nfw_login_hook', $NFW_LOGINHOOK, 2 ); |
| 657 | |
| 658 | /* ------------------------------------------------------------------ */ |
| 659 | function nfw_logout_hook() { |
| 660 | |
| 661 | NinjaFirewall_session::start(); |
| 662 | |
| 663 | // Whoever it was, we clear the goodguy flag |
| 664 | NinjaFirewall_session::delete('nfw_goodguy'); |
| 665 | // And the Live Log flag as well |
| 666 | NinjaFirewall_session::delete('nfw_livelog'); |
| 667 | NinjaFirewall_session::delete('allcaps'); |
| 668 | } |
| 669 | |
| 670 | add_action( 'wp_logout', 'nfw_logout_hook' ); |
| 671 | |
| 672 | // ===================================================================== |
| 673 | // Plugin ugrade AJAX function. |
| 674 | |
| 675 | require __DIR__ .'/lib/class-plugin-upgrade.php'; |
| 676 | add_action('wp_ajax_nfw_pluginupgrade', ['NinjaFirewall_plugin', 'upgrade'] ); |
| 677 | |
| 678 | /* ------------------------------------------------------------------ */ |
| 679 | // FullWAF upgrade AJAX function. |
| 680 | |
| 681 | add_action( 'wp_ajax_nfw_fullwafsetup', 'nfw_fullwafsetup' ); |
| 682 | |
| 683 | function nfw_fullwafsetup() { |
| 684 | |
| 685 | nf_not_allowed( 'block', __LINE__ ); |
| 686 | |
| 687 | if (! check_ajax_referer( 'events_save', 'nonce', false ) ) { |
| 688 | esc_html_e('Error: Security nonces do not match. Reload the page and try again.', 'ninjafirewall'); |
| 689 | wp_die(); |
| 690 | } |
| 691 | |
| 692 | $nfw_options = nfw_get_option( 'nfw_options' ); |
| 693 | if ( empty( $nfw_options['enabled'] ) ) { |
| 694 | esc_html_e('Error: NinjaFirewall is disabled', 'ninjafirewall'); |
| 695 | wp_die(); |
| 696 | } |
| 697 | |
| 698 | if ( empty( $_POST['httpserver'] ) ) { |
| 699 | printf( esc_html__('Error: missing parameter (%s).', 'ninjafirewall'), 'httpserver' ); |
| 700 | wp_die(); |
| 701 | } |
| 702 | if ( preg_match('/^[^1-8]$/', $_POST['httpserver'] ) ) { |
| 703 | printf( esc_html__('Error: wrong parameter value (%s).', 'ninjafirewall'), 'httpserver' ); |
| 704 | wp_die(); |
| 705 | } |
| 706 | if ( empty( $_POST['diy'] ) || ! preg_match( '/^(nfw|usr)$/', $_POST['diy'] ) ) { |
| 707 | printf( esc_html__('Error: wrong parameter value (%s).', 'ninjafirewall'), 'diy' ); |
| 708 | wp_die(); |
| 709 | } |
| 710 | |
| 711 | // Retrieve the list of excluded folders, if any, and save it |
| 712 | nfw_save_waf_exclusionlist( $_POST['exclude_waf_list'] ); |
| 713 | |
| 714 | // Disable the sandbox? |
| 715 | if ( empty( $_POST['sandbox'] ) ) { |
| 716 | define('NFW_BYPASS_SANDBOX', true); |
| 717 | } |
| 718 | |
| 719 | $time = time() + 300; |
| 720 | |
| 721 | // 1: Apache mod_php |
| 722 | // 2: Apache + CGI/FastCGI or PHP-FPM |
| 723 | // 3: Apache + suPHP |
| 724 | // 4: Nginx + CGI/FastCGI or PHP-FPM |
| 725 | // 5: Litespeed |
| 726 | // 6: Openlitespeed |
| 727 | // 7: Other webserver + CGI/FastCGI or PHP-FPM |
| 728 | // 8: Apache + LSAPI |
| 729 | $httpserver = (int) $_POST['httpserver']; |
| 730 | |
| 731 | // [6] Openlitespeed: nothing to do. |
| 732 | if ( $httpserver == 6 ) { |
| 733 | set_transient( 'nfw_fullwaf', "{$httpserver}:{$time}", 60 * 5 ); |
| 734 | echo '200'; |
| 735 | wp_die(); |
| 736 | } |
| 737 | |
| 738 | require_once __DIR__ .'/lib/install.php'; |
| 739 | |
| 740 | // .htaccess mods only |
| 741 | if ( $httpserver == 1 || $httpserver == 5 || $httpserver == 8 ) { |
| 742 | // User wants to make the modification |
| 743 | if ( $_POST['diy'] == 'usr' ) { |
| 744 | // Nothing to do |
| 745 | set_transient( 'nfw_fullwaf', "{$httpserver}:{$time}", 60 * 5 ); |
| 746 | echo '200'; |
| 747 | wp_die(); |
| 748 | } |
| 749 | // Make changes |
| 750 | $ret = nfw_fullwaf_htaccess( $httpserver ); |
| 751 | if ( $ret !== true ) { |
| 752 | echo esc_html( $ret ); |
| 753 | } else { |
| 754 | set_transient( 'nfw_fullwaf', "{$httpserver}:{$time}", 60 * 5 ); |
| 755 | echo '200'; |
| 756 | } |
| 757 | wp_die(); |
| 758 | } |
| 759 | |
| 760 | if ( $_POST['diy'] == 'usr' ) { |
| 761 | // Nothing to do, but add 5-minute notice to the overview page |
| 762 | // because an INI file is being used |
| 763 | set_transient( 'nfw_fullwaf', "{$httpserver}:{$time}", 60 * 5 ); |
| 764 | echo '200'; |
| 765 | wp_die(); |
| 766 | } |
| 767 | |
| 768 | // [1] .user.ini |
| 769 | // [2] php.ini |
| 770 | if ( empty ( $_POST['initype'] ) || ! preg_match( '/^[12]$/', $_POST['initype'] ) ) { |
| 771 | $initype = 1; |
| 772 | } else { |
| 773 | $initype = (int) $_POST['initype']; |
| 774 | } |
| 775 | |
| 776 | if ( $httpserver == 3 ) { // Apache + suPHP |
| 777 | // Set up the htaccess file |
| 778 | $ret = nfw_fullwaf_htaccess( $httpserver ); |
| 779 | if ( $ret !== true ) { |
| 780 | echo esc_html( $ret ); |
| 781 | wp_die(); |
| 782 | } |
| 783 | } |
| 784 | // ini file |
| 785 | $ret = nfw_fullwaf_ini( $httpserver, $initype ); |
| 786 | if ( $ret !== true ) { |
| 787 | echo esc_html( $ret ); |
| 788 | wp_die(); |
| 789 | } else { |
| 790 | // Add 5-minute notice to the overview page |
| 791 | // because an INI file is being used |
| 792 | set_transient( 'nfw_fullwaf', "{$httpserver}:{$time}", 60 * 5 ); |
| 793 | echo 200; |
| 794 | } |
| 795 | wp_die(); |
| 796 | } |
| 797 | |
| 798 | /* ------------------------------------------------------------------ */ |
| 799 | // Configure Full WAF mode or fallback to WP WAF mode. AJAX action. |
| 800 | |
| 801 | add_action( 'wp_ajax_nfw_fullwafconfig', 'nfw_fullwafconfig' ); |
| 802 | |
| 803 | function nfw_fullwafconfig() { |
| 804 | |
| 805 | nf_not_allowed( 'block', __LINE__ ); |
| 806 | |
| 807 | if (! check_ajax_referer( 'events_save', 'nonce', false ) ) { |
| 808 | esc_html_e('Error: Security nonces do not match. Reload the page and try again.', 'ninjafirewall'); |
| 809 | wp_die(); |
| 810 | } |
| 811 | |
| 812 | if ( empty( $_POST['what'] ) || ! preg_match( '/^[12]$/', $_POST['what'] ) ) { |
| 813 | printf( esc_html__('Error: missing parameter (%s).', 'ninjafirewall'), 'what' ); |
| 814 | wp_die(); |
| 815 | } |
| 816 | |
| 817 | // Downgrade to WP WAF |
| 818 | if ( $_POST['what'] == 2 ) { |
| 819 | |
| 820 | require __DIR__ .'/lib/install.php'; |
| 821 | nfw_get_constants(); |
| 822 | nfw_remove_directives(); |
| 823 | |
| 824 | // Full WAF directories exclusion |
| 825 | } else { |
| 826 | // Retrieve the list of excluded folders, if any, and save it |
| 827 | nfw_save_waf_exclusionlist( $_POST['list'] ); |
| 828 | } |
| 829 | |
| 830 | wp_die(200); |
| 831 | } |
| 832 | |
| 833 | /* ------------------------------------------------------------------ */ |
| 834 | // Save new exclusion list. |
| 835 | |
| 836 | function nfw_save_waf_exclusionlist( $input ) { |
| 837 | |
| 838 | $nfw_options = nfw_get_option('nfw_options'); |
| 839 | |
| 840 | // Retrieve the list of excluded folders, if any, and save it |
| 841 | $tmp_exclude_waf_list = json_decode( stripslashes( $input ) ); |
| 842 | if ( $tmp_exclude_waf_list === false || $tmp_exclude_waf_list === null ) { |
| 843 | printf( esc_html__('Error: missing parameter (%s).', 'ninjafirewall'), 'list'); |
| 844 | wp_die(); |
| 845 | } |
| 846 | $exclude_waf_list = []; |
| 847 | if (! empty( $tmp_exclude_waf_list ) ) { |
| 848 | foreach( $tmp_exclude_waf_list as $folder ) { |
| 849 | if ( is_dir( realpath( ABSPATH . $folder ) ) ) { |
| 850 | $exclude_waf_list[] = $folder; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | // Update/clear the list |
| 855 | if (! empty( $exclude_waf_list ) ) { |
| 856 | $nfw_options['exclude_waf_list'] = json_encode( $exclude_waf_list ); |
| 857 | } else { |
| 858 | unset( $nfw_options['exclude_waf_list'] ); |
| 859 | } |
| 860 | nfw_update_option('nfw_options', $nfw_options); |
| 861 | // (Re)create the loader |
| 862 | require_once __DIR__ .'/lib/install_default.php'; |
| 863 | nfw_create_loader(); |
| 864 | |
| 865 | } |
| 866 | |
| 867 | /* ------------------------------------------------------------------ */ |
| 868 | |
| 869 | function is_nfw_enabled() { |
| 870 | |
| 871 | $nfw_options = nfw_get_option( 'nfw_options' ); |
| 872 | |
| 873 | if (! defined('NFW_STATUS') ) { |
| 874 | define('NF_DISABLED', 10); |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | if ( isset($nfw_options['enabled']) && $nfw_options['enabled'] == '0' ) { |
| 879 | define('NF_DISABLED', 9); |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | if (NFW_STATUS == 21 || NFW_STATUS == 22 || NFW_STATUS == 23) { |
| 884 | define('NF_DISABLED', 10); |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | // OK |
| 889 | if (NFW_STATUS == 20) { |
| 890 | define('NF_DISABLED', 0); |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | define('NF_DISABLED', NFW_STATUS); |
| 895 | return; |
| 896 | |
| 897 | } |
| 898 | |
| 899 | /* ------------------------------------------------------------------ */ |
| 900 | |
| 901 | function ninjafirewall_admin_menu() { |
| 902 | |
| 903 | if ( nf_not_allowed( 0, __LINE__ ) ) { return; } |
| 904 | |
| 905 | if (! empty($_REQUEST['nfw_act']) && $_REQUEST['nfw_act'] == 99) { |
| 906 | if ( empty($_GET['nfwnonce']) || ! wp_verify_nonce($_GET['nfwnonce'], 'show_phpinfo') ) { |
| 907 | wp_nonce_ays('show_phpinfo'); |
| 908 | } |
| 909 | phpinfo(33); |
| 910 | exit; |
| 911 | } |
| 912 | |
| 913 | add_menu_page( 'NinjaFirewall', 'NinjaFirewall', 'manage_options', |
| 914 | 'NinjaFirewall', 'nf_sub_main', plugins_url( '/images/nf_icon.png', __FILE__ ) |
| 915 | ); |
| 916 | |
| 917 | global $menu_hook; |
| 918 | |
| 919 | require_once plugin_dir_path(__FILE__) . 'lib/help.php'; |
| 920 | |
| 921 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Dashboard', 'ninjafirewall'), __('Dashboard', 'ninjafirewall'), 'manage_options', |
| 922 | 'NinjaFirewall', 'nf_sub_main' ); |
| 923 | add_action( 'load-' . $menu_hook, 'help_nfsubmain' ); |
| 924 | |
| 925 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Firewall Options', 'ninjafirewall'), __('Firewall Options', 'ninjafirewall'), 'manage_options', |
| 926 | 'nfsubopt', 'nf_sub_options' ); |
| 927 | add_action( 'load-' . $menu_hook, 'help_nfsubopt' ); |
| 928 | |
| 929 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Firewall Policies', 'ninjafirewall'), __('Firewall Policies', 'ninjafirewall'), 'manage_options', |
| 930 | 'nfsubpolicies', 'nf_sub_policies' ); |
| 931 | add_action( 'load-' . $menu_hook, 'help_nfsubpolicies' ); |
| 932 | |
| 933 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Monitoring', 'ninjafirewall'), __( 'Monitoring', 'ninjafirewall'), 'manage_options', |
| 934 | 'nfsubfileguard', 'nf_sub_monitoring' ); |
| 935 | add_action( 'load-' . $menu_hook, 'help_nfsubfileguard' ); |
| 936 | |
| 937 | $nscan_options = get_option( 'nscan_options' ); |
| 938 | if ( defined('NSCAN_NAME') && defined('NSCAN_SLUG') && ! empty( $nscan_options['scan_nfwpintegration'] ) ) { |
| 939 | $menu_hook = add_submenu_page( 'NinjaFirewall', NSCAN_NAME, NSCAN_NAME, 'manage_options', NSCAN_NAME, 'nscan_main_menu' ); |
| 940 | require_once dirname( __DIR__ ).'/'. NSCAN_SLUG .'/lib/help.php'; |
| 941 | add_action( 'load-' . $menu_hook, 'nscan_help' ); |
| 942 | } else { |
| 943 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Anti-Malware', 'ninjafirewall'), __('Anti-Malware', 'ninjafirewall'), 'manage_options', |
| 944 | 'nfsubmalwarescan', 'nf_sub_malwarescan' ); |
| 945 | } |
| 946 | |
| 947 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Network', 'ninjafirewall'), __('Network', 'ninjafirewall'), 'manage_network', |
| 948 | 'nfsubnetwork', 'nf_sub_network' ); |
| 949 | add_action( 'load-' . $menu_hook, 'help_nfsubnetwork' ); |
| 950 | |
| 951 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Event Notifications', 'ninjafirewall'), __('Event Notifications', 'ninjafirewall'), 'manage_options', |
| 952 | 'nfsubevent', 'nf_sub_event' ); |
| 953 | add_action( 'load-' . $menu_hook, 'help_nfsubevent' ); |
| 954 | |
| 955 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Log-in Protection', 'ninjafirewall'), __('Login Protection', 'ninjafirewall'), 'manage_options', |
| 956 | 'nfsubloginprot', 'nf_sub_loginprot' ); |
| 957 | add_action( 'load-' . $menu_hook, 'help_nfsublogin' ); |
| 958 | |
| 959 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Logs', 'ninjafirewall'), __('Logs', 'ninjafirewall'), 'manage_options', |
| 960 | 'nfsublog', 'nf_sub_log' ); |
| 961 | add_action( 'load-' . $menu_hook, 'help_nfsublog' ); |
| 962 | |
| 963 | $menu_hook = add_submenu_page( 'NinjaFirewall', __('NinjaFirewall: Security Rules', 'ninjafirewall'), __('Security Rules', 'ninjafirewall'), 'manage_options', |
| 964 | 'nfsubupdates', 'nf_sub_updates' ); |
| 965 | add_action( 'load-' . $menu_hook, 'help_nfsubupdates' ); |
| 966 | |
| 967 | $menu_hook = add_submenu_page( 'NinjaFirewall', 'NinjaFirewall: WP+ Edition', '<b style="color:#fcdc25">WP+ Edition</b>', 'manage_options', |
| 968 | 'nfsubwplus', 'nf_sub_wplus' ); |
| 969 | |
| 970 | } |
| 971 | // Must load before NinjaScanner (11): |
| 972 | if (! is_multisite() ) { |
| 973 | add_action( 'admin_menu', 'ninjafirewall_admin_menu', 10 ); |
| 974 | } else { |
| 975 | add_action( 'network_admin_menu', 'ninjafirewall_admin_menu', 10 ); |
| 976 | } |
| 977 | |
| 978 | /* ------------------------------------------------------------------ */ |
| 979 | |
| 980 | function nf_admin_bar_status() { |
| 981 | |
| 982 | if (! current_user_can( 'manage_options' ) ) { |
| 983 | return; |
| 984 | } |
| 985 | |
| 986 | $nfw_options = nfw_get_option( 'nfw_options' ); |
| 987 | if ( @$nfw_options['nt_show_status'] != 1 && ! current_user_can('manage_network') ) { |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | if (! defined('NF_DISABLED') ) { |
| 992 | is_nfw_enabled(); |
| 993 | } |
| 994 | if (NF_DISABLED) { return; } |
| 995 | |
| 996 | global $wp_admin_bar; |
| 997 | $wp_admin_bar->add_menu( [ |
| 998 | 'id' => 'nfw_ntw1', |
| 999 | 'title' => '<img src="' . plugins_url() . '/ninjafirewall/images/ninjafirewall_20.png" ' . |
| 1000 | 'style="vertical-align:middle;margin-right:5px" />' |
| 1001 | ] ); |
| 1002 | |
| 1003 | if ( current_user_can( 'manage_network' ) ) { |
| 1004 | $wp_admin_bar->add_menu( [ |
| 1005 | 'parent' => 'nfw_ntw1', |
| 1006 | 'id' => 'nfw_ntw2', |
| 1007 | 'title' => __( 'NinjaFirewall Settings', 'ninjafirewall'), |
| 1008 | 'href' => network_admin_url() . 'admin.php?page=NinjaFirewall' |
| 1009 | ] ); |
| 1010 | } else { |
| 1011 | if ( defined('NFW_STATUS') ) { |
| 1012 | $wp_admin_bar->add_menu( [ |
| 1013 | 'parent' => 'nfw_ntw1', |
| 1014 | 'id' => 'nfw_ntw2', |
| 1015 | 'title' => __( 'NinjaFirewall is enabled', 'ninjafirewall') |
| 1016 | ] ); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | if ( is_multisite() ) { |
| 1022 | add_action('admin_bar_menu', 'nf_admin_bar_status', 95); |
| 1023 | } |
| 1024 | |
| 1025 | /* ------------------------------------------------------------------ */ |
| 1026 | |
| 1027 | function nf_sub_main() { |
| 1028 | |
| 1029 | // Main menu (Overview) |
| 1030 | require plugin_dir_path(__FILE__) . 'lib/settings_dashboard.php'; |
| 1031 | |
| 1032 | } |
| 1033 | |
| 1034 | /* ------------------------------------------------------------------ */ |
| 1035 | |
| 1036 | function nf_sub_options() { // i18n |
| 1037 | |
| 1038 | require plugin_dir_path(__FILE__) . 'lib/settings_firewall_options.php'; |
| 1039 | |
| 1040 | } |
| 1041 | |
| 1042 | /* ------------------------------------------------------------------ */ |
| 1043 | |
| 1044 | function nf_sub_policies() { |
| 1045 | |
| 1046 | // Firewall Policies menu |
| 1047 | require plugin_dir_path(__FILE__) . 'lib/settings_firewall_policies.php'; |
| 1048 | |
| 1049 | } |
| 1050 | |
| 1051 | /* ------------------------------------------------------------------ */ |
| 1052 | |
| 1053 | function nf_sub_monitoring() { |
| 1054 | |
| 1055 | require plugin_dir_path(__FILE__) . 'lib/settings_monitoring.php'; |
| 1056 | |
| 1057 | } |
| 1058 | add_action('nfscanevent', 'nfscando'); |
| 1059 | |
| 1060 | function nfscando() { |
| 1061 | |
| 1062 | define('NFSCANDO', 1); |
| 1063 | nf_sub_monitoring(); |
| 1064 | } |
| 1065 | |
| 1066 | /* ------------------------------------------------------------------ */ |
| 1067 | |
| 1068 | function nf_sub_network() { |
| 1069 | |
| 1070 | // Network menu (multi-site only) |
| 1071 | require plugin_dir_path(__FILE__) . 'lib/settings_network.php'; |
| 1072 | |
| 1073 | } |
| 1074 | |
| 1075 | /* ------------------------------------------------------------------ */ |
| 1076 | |
| 1077 | function nf_sub_malwarescan() { |
| 1078 | |
| 1079 | require plugin_dir_path(__FILE__) . 'lib/anti_malware.php'; |
| 1080 | |
| 1081 | } |
| 1082 | |
| 1083 | /* ------------------------------------------------------------------ */ |
| 1084 | |
| 1085 | function nf_sub_event() { |
| 1086 | |
| 1087 | require plugin_dir_path(__FILE__) . 'lib/settings_event_notifications.php'; |
| 1088 | |
| 1089 | } |
| 1090 | |
| 1091 | add_action('shutdown', 'nf_check_dbdata', 1); |
| 1092 | |
| 1093 | add_action('nfdailyreport', 'nfdailyreportdo'); |
| 1094 | |
| 1095 | function nfdailyreportdo() { |
| 1096 | define('NFREPORTDO', 1); |
| 1097 | nf_sub_event(); |
| 1098 | } |
| 1099 | |
| 1100 | /* ------------------------------------------------------------------ */ |
| 1101 | |
| 1102 | function nf_sub_log() { |
| 1103 | |
| 1104 | require plugin_dir_path(__FILE__) . 'lib/settings_logs.php'; |
| 1105 | |
| 1106 | } |
| 1107 | |
| 1108 | /* ------------------------------------------------------------------ */ |
| 1109 | |
| 1110 | function nf_sub_loginprot() { |
| 1111 | |
| 1112 | require plugin_dir_path(__FILE__) . 'lib/settings_login_protection.php'; |
| 1113 | |
| 1114 | } |
| 1115 | |
| 1116 | /* ------------------------------------------------------------------ */ |
| 1117 | |
| 1118 | function nf_sub_updates() { |
| 1119 | |
| 1120 | require plugin_dir_path(__FILE__) . 'lib/settings_security_rules.php'; |
| 1121 | |
| 1122 | } |
| 1123 | |
| 1124 | add_action('nfsecupdates', 'nfupdatesdo'); |
| 1125 | |
| 1126 | function nfupdatesdo() { |
| 1127 | define('NFUPDATESDO', 1); |
| 1128 | nf_sub_updates(); |
| 1129 | } |
| 1130 | |
| 1131 | /* ------------------------------------------------------------------ */ |
| 1132 | |
| 1133 | function nf_sub_wplus() { |
| 1134 | |
| 1135 | require plugin_dir_path(__FILE__) . 'lib/wpplus.php'; |
| 1136 | } |
| 1137 | |
| 1138 | /* ------------------------------------------------------------------ */ |
| 1139 | |
| 1140 | function ninjafirewall_settings_link( $links ) { |
| 1141 | |
| 1142 | // Check if access is restricted to one or more specific admins |
| 1143 | // See: https://blog.nintechnet.com/restricting-access-to-ninjafirewall-wp-edition-settings/ |
| 1144 | if ( nf_not_allowed( 0, __LINE__ ) ) { |
| 1145 | unset( $links ); |
| 1146 | $links[] = __('Access Restricted', 'ninjafirewall'); |
| 1147 | return $links; |
| 1148 | } |
| 1149 | |
| 1150 | if ( is_multisite() ) { $net = 'network/'; } else { $net = ''; } |
| 1151 | |
| 1152 | $links[] = '<a href="'. get_admin_url(null, $net .'admin.php?page=NinjaFirewall') .'">'. __('Settings', 'ninjafirewall') .'</a>'; |
| 1153 | $links[] = '<a href="https://nintechnet.com/ninjafirewall/wp-edition/?pricing" target="_blank">'. __('Upgrade to Premium', 'ninjafirewall'). '</a>'; |
| 1154 | $links[] = '<a href="https://wordpress.org/support/view/plugin-reviews/ninjafirewall?rate=5#postform" target="_blank">'. __('Rate it!', 'ninjafirewall'). '</a>'; |
| 1155 | unset( $links['edit'] ); |
| 1156 | return $links; |
| 1157 | |
| 1158 | } |
| 1159 | |
| 1160 | if ( is_multisite() ) { |
| 1161 | add_filter( 'network_admin_plugin_action_links_' . plugin_basename(__FILE__), 'ninjafirewall_settings_link' ); |
| 1162 | } else { |
| 1163 | add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'ninjafirewall_settings_link' ); |
| 1164 | } |
| 1165 | |
| 1166 | /* ------------------------------------------------------------------ */ |
| 1167 | |
| 1168 | function nfw_dashboard_widgets() { |
| 1169 | |
| 1170 | require plugin_dir_path(__FILE__) . 'lib/widget.php'; |
| 1171 | |
| 1172 | } |
| 1173 | |
| 1174 | if ( is_multisite() ) { |
| 1175 | add_action( 'wp_network_dashboard_setup', 'nfw_dashboard_widgets' ); |
| 1176 | } else { |
| 1177 | add_action( 'wp_dashboard_setup', 'nfw_dashboard_widgets' ); |
| 1178 | } |
| 1179 | |
| 1180 | /* ------------------------------------------------------------------ */ |
| 1181 | |
| 1182 | function nf_not_allowed( $block, $line = 0, $ajax = 0 ) { |
| 1183 | |
| 1184 | if ( is_multisite() ) { |
| 1185 | if ( current_user_can('manage_network') && is_main_site() ) { |
| 1186 | return false; |
| 1187 | } |
| 1188 | } else { |
| 1189 | if ( current_user_can('manage_options') && |
| 1190 | current_user_can('unfiltered_html') ) { |
| 1191 | // Check if that admin is allowed to use NinjaFirewall |
| 1192 | // (see NFW_ALLOWED_ADMIN at http://nin.link/nfwaa ): |
| 1193 | if ( defined('NFW_ALLOWED_ADMIN') ) { |
| 1194 | $current_user = wp_get_current_user(); |
| 1195 | $admins = explode(',', NFW_ALLOWED_ADMIN ); |
| 1196 | foreach ( $admins as $admin ) { |
| 1197 | if ( trim( $admin ) == $current_user->user_login ) { |
| 1198 | return false; |
| 1199 | } |
| 1200 | } |
| 1201 | } else { |
| 1202 | return false; |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | if ( $block ) { |
| 1208 | if ( defined('WP_CLI') && WP_CLI ) { |
| 1209 | // Format text for WP-CLI: |
| 1210 | WP_CLI::error( |
| 1211 | sprintf( |
| 1212 | __('You are not allowed to perform this task (%s).', 'ninjafirewall'), |
| 1213 | "NinjaFirewall: $line" |
| 1214 | ) |
| 1215 | ); |
| 1216 | } elseif ( $ajax ) { |
| 1217 | $message = sprintf( |
| 1218 | esc_html__('You are not allowed to perform this task (%s).', 'ninjafirewall'), |
| 1219 | "NinjaFirewall: $line" |
| 1220 | ); |
| 1221 | wp_send_json( ['error' => $message ] ); |
| 1222 | |
| 1223 | } else { |
| 1224 | die( '<br /><br /><br /><div class="error notice is-dismissible"><p>' . |
| 1225 | sprintf( |
| 1226 | esc_html__('You are not allowed to perform this task (%s).', 'ninjafirewall'), |
| 1227 | "NinjaFirewall: $line" |
| 1228 | ) .'</p></div>' |
| 1229 | ); |
| 1230 | } |
| 1231 | } |
| 1232 | return true; |
| 1233 | } |
| 1234 | |
| 1235 | /* ------------------------------------------------------------------ */ |
| 1236 | // EOF // |
| 1237 |