<commit_before><?php
/**
 * @package BC_Security
 */

namespace BlueChip\Security\Modules\Notifications;

use BlueChip\Security\Helpers\Is;
use BlueChip\Security\Modules;
use BlueChip\Security\Modules\Log\Logger;
use BlueChip\Security\Modules\Checksums;
use BlueChip\Security\Modules\Login;

class Watchman implements Modules\Loadable, Modules\Initializable, Modules\Activable
{
    /**
     * @var string Remote IP address
     */
    private $remote_address;

    /**
     * @var \BlueChip\Security\Modules\Notifications\Settings
     */
    private $settings;

    /**
     * @var \BlueChip\Security\Modules\Log\Logger
     */
    private $logger;

    /**
     * @var array List of notifications recipients
     */
    private $recipients;


    /**
     * @param \BlueChip\Security\Modules\Notifications\Settings $settings
     * @param string $remote_address Remote IP address.
     * @param \BlueChip\Security\Modules\Log\Logger $logger
     */
    public function __construct(Settings $settings, $remote_address, Logger $logger)
    {
        $this->remote_address = $remote_address;
        $this->settings = $settings;
        $this->logger = $logger;

        // Get recipients.
        $this->recipients = $settings[Settings::NOTIFICATION_RECIPIENTS];
        // If site admin should be notified to, include him as well.
        if ($settings[Settings::NOTIFY_SITE_ADMIN]) {
            array_unshift($this->recipients, get_option('admin_email'));
        }
    }


    public function load()
    {
        // Bail early, if no recipients are set.
        if (empty($this->recipients)) {
            return;
        }

        if ($this->settings[Settings::CORE_UPDATE_AVAILABLE]) {
            add_action('set_site_transient_update_core', [$this, 'watchCoreUpdateAvailable'], 10, 1);
        }
        if ($this->settings[Settings::PLUGIN_UPDATE_AVAILABLE]) {
            add_action('set_site_transient_update_plugins', [$this, 'watchPluginUpdatesAvailable'], 10, 1);
        }
        if ($this->settings[Settings::THEME_UPDATE_AVAILABLE]) {
            add_action('set_site_transient_update_themes', [$this, 'watchThemeUpdatesAvailable'], 10, 1);
        }
    }


    /**
     * Initialize notification according to settings.
     */
    public function init()
    {
        // Bail early, if no recipients are set.
        if (empty($this->recipients)) {
            return;
        }

        if ($this->settings[Settings::ADMIN_USER_LOGIN]) {
            add_action('wp_login', [$this, 'watchWpLogin'], 10, 2);
        }
        if ($this->settings[Settings::KNOWN_IP_LOCKOUT]) {
            add_action(Login\Hooks::LOCKOUT_EVENT, [$this, 'watchLockoutEvents'], 10, 3);
        }
        if ($this->settings[Settings::CHECKSUMS_VERIFICATION_ERROR]) {
            add_action(Checksums\Hooks::CHECKSUMS_RETRIEVAL_FAILED, [$this, 'watchChecksumsRetrievalFailed'], 10, 1);
            add_action(Checksums\Hooks::CHECKSUMS_VERIFICATION_ALERT, [$this, 'watchChecksumsVerificationAlert'], 10, 2);
        }
    }


    public function activate()
    {
        // Do nothing.
    }


    /**
     * Send notification that plugin has been deactivated.
     */
    public function deactivate()
    {
        // Bail early, if no recipients are set.
        if (empty($this->recipients)) {
            return;
        }

        if ($this->settings[Settings::PLUGIN_DEACTIVATED]) {
            // Get the bastard that turned us off!
            $user = wp_get_current_user();

            $subject = __('BC Security deactivated', 'bc-security');
            $message = sprintf(
                __('User "%s" had just deactivated BC Security plugin on your website!', 'bc-security'),
                $user->user_login
            );

            $this->notify($subject, $message);
        }<commit_msg>Fix compatibility with WP-CLI. Once again fixes #16.<commit_after><?php
/**
 * @package BC_Security
 */

namespace BlueChip\Security\Modules\Notifications;

use BlueChip\Security\Helpers\Is;
use BlueChip\Security\Modules;
use BlueChip\Security\Modules\Log\Logger;
use BlueChip\Security\Modules\Checksums;
use BlueChip\Security\Modules\Login;

class Watchman implements Modules\Loadable, Modules\Initializable, Modules\Activable
{
    /**
     * @var string Remote IP address
     */
    private $remote_address;

    /**
     * @var \BlueChip\Security\Modules\Notifications\Settings
     */
    private $settings;

    /**
     * @var \BlueChip\Security\Modules\Log\Logger
     */
    private $logger;

    /**
     * @var array List of notifications recipients
     */
    private $recipients;


    /**
     * @param \BlueChip\Security\Modules\Notifications\Settings $settings
     * @param string $remote_address Remote IP address.
     * @param \BlueChip\Security\Modules\Log\Logger $logger
     */
    public function __construct(Settings $settings, $remote_address, Logger $logger)
    {
        $this->remote_address = $remote_address;
        $this->settings = $settings;
        $this->logger = $logger;

        // Get recipients.
        $this->recipients = $settings[Settings::NOTIFICATION_RECIPIENTS];
        // If site admin should be notified to, include him as well.
        if ($settings[Settings::NOTIFY_SITE_ADMIN]) {
            array_unshift($this->recipients, get_option('admin_email'));
        }
    }


    public function load()
    {
        // Bail early, if no recipients are set.
        if (empty($this->recipients)) {
            return;
        }

        if ($this->settings[Settings::CORE_UPDATE_AVAILABLE]) {
            add_action('set_site_transient_update_core', [$this, 'watchCoreUpdateAvailable'], 10, 1);
        }
        if ($this->settings[Settings::PLUGIN_UPDATE_AVAILABLE]) {
            add_action('set_site_transient_update_plugins', [$this, 'watchPluginUpdatesAvailable'], 10, 1);
        }
        if ($this->settings[Settings::THEME_UPDATE_AVAILABLE]) {
            add_action('set_site_transient_update_themes', [$this, 'watchThemeUpdatesAvailable'], 10, 1);
        }
    }


    /**
     * Initialize notification according to settings.
     */
    public function init()
    {
        // Bail early, if no recipients are set.
        if (empty($this->recipients)) {
            return;
        }

        if ($this->settings[Settings::ADMIN_USER_LOGIN]) {
            add_action('wp_login', [$this, 'watchWpLogin'], 10, 2);
        }
        if ($this->settings[Settings::KNOWN_IP_LOCKOUT]) {
            add_action(Login\Hooks::LOCKOUT_EVENT, [$this, 'watchLockoutEvents'], 10, 3);
        }
        if ($this->settings[Settings::CHECKSUMS_VERIFICATION_ERROR]) {
            add_action(Checksums\Hooks::CHECKSUMS_RETRIEVAL_FAILED, [$this, 'watchChecksumsRetrievalFailed'], 10, 1);
            add_action(Checksums\Hooks::CHECKSUMS_VERIFICATION_ALERT, [$this, 'watchChecksumsVerificationAlert'], 10, 2);
        }
    }


    public function activate()
    {
        // Do nothing.
    }


    /**
     * Send notification that plugin has been deactivated.
     */
    public function deactivate()
    {
        // Bail early, if no recipients are set.
        if (empty($this->recipients)) {
            return;
        }

        if ($this->settings[Settings::PLUGIN_DEACTIVATED]) {
            $subject = __('BC Security deactivated', 'bc-security');

            $user = wp_get_current_user();
            if ($user->ID) {
                // Name the bastard that turned us off!
                $message = sprintf(
                    __('User "%s" had just deactivated BC Security plugin on your website!', 'bc-security'),
                    $user->user_login
                );
            } else {
                // No user means plugin has been probably deactivated via WP-CLI.
                // See: https://github.com/chesio/bc-security/issues/16#issuecomment-321541102
                $message = __('BC Security plugin on your website has been deactivated!', 'bc-security');
            }

            $this->notify($subject, $message);
        }
{'fraction_non_alphanumeric': 0.12445496449482994, 'fraction_numerical': 0.006851874922137785, 'mean_word_length': 2.411814704632384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1691466', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2291, 'n_words': 481}