<?php
/**
 * Plugin Name: CIDRAM
 * Version: 4.1.0
 * Description: CIDRAM (Classless Inter-Domain Routing Access Manager) is a PHP script designed to protect websites by blocking requests originating from IP addresses regarded as being sources of undesirable traffic, including (but not limited to) traffic from non-human access endpoints, cloud services, spambots, scrapers, etc. It does this by calculating the possible CIDRs of the IP addresses supplied from inbound requests and then attempting to match these possible CIDRs against its signature files (these signature files contain lists of CIDRs of IP addresses regarded as being sources of undesirable traffic); If matches are found, the requests are blocked.
 * Author: Caleb Mazalevskis
 * Author URI: https://github.com/Maikuolan
 * Plugin URI: https://cidram.github.io/
 * Text Domain: cidram
 * Contributors: https://github.com/CIDRAM/CIDRAM/graphs/contributors
 * Tags: anti-spam, cidr, firewall, security, waf
 * License: GPLv2
 * License URI: https://github.com/CIDRAM/CIDRAM/blob/master/LICENSE.txt
 */

require_once __DIR__ . DIRECTORY_SEPARATOR . 'vault' . DIRECTORY_SEPARATOR . 'loader.php';

if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'cidram-configuration.yml') && file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'vault' . DIRECTORY_SEPARATOR . 'config.yml')) {
    copy(__DIR__ . DIRECTORY_SEPARATOR . 'vault' . DIRECTORY_SEPARATOR . 'config.yml', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'cidram-configuration.yml');
    $NewCIDRAMInstall = true;
}

if (
    !file_exists(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.htaccess') &&
    is_writable(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR)
) {
    $Handle = fopen(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.htaccess', 'wb');
    if (is_resource($Handle)) {
        fwrite($Handle, "<Files \"cidram-configuration.yml\">  \n  Order Allow,Deny\n  Deny from all\n</Files>\n");
        fclose($Handle);
    }
    unset($Handle);
}

$CIDRAM = new \CIDRAM\CIDRAM\FrontEnd(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'cidram-configuration.yml');

if (isset($NewCIDRAMInstall)) {
    $ConfigPath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'cidram-configuration.yml';
    $NewConfigData = $OldConfigData = file_get_contents($ConfigPath);
    $Try = get_bloginfo('language');
    if (isset($CIDRAM->CIDRAM['Config Defaults']['general']['lang']['choices'][$Try])) {
        $CIDRAM->Configuration['general']['lang'] = $Try;
        $NewConfigData = str_replace("\n lang: \"en\"\n", "\n lang: \"" . $Try . "\"\n", $NewConfigData);
    } elseif (strpos($Try, '-') !== false) {
        $Try = preg_replace('~-.*$~', '', $Try);
        if (isset($CIDRAM->CIDRAM['Config Defaults']['general']['lang']['choices'][$Try])) {
            $CIDRAM->Configuration['general']['lang'] = $Try;
            $NewConfigData = str_replace("\n lang: \"en\"\n", "\n lang: \"" . $Try . "\"\n", $NewConfigData);
        }
    }
    if ($NewConfigData !== $OldConfigData && is_writable($ConfigPath)) {
        $Handle = fopen($ConfigPath, 'wb');
        if (is_resource($Handle)) {
            fwrite($Handle, $NewConfigData);
            fclose($Handle);
        }
    }
    unset($Handle, $Try, $NewConfigData, $OldConfigData, $ConfigPath, $NewCIDRAMInstall);
}

if (!wp_doing_cron()) {
    $CIDRAM->protect();
}

if (
    isset($CIDRAM->CIDRAM['QueryVars']['cidram-page']) ||
    isset($CIDRAM->CIDRAM['QueryVars']['cidram-asset']) ||
    isset($_POST['cidram-form-target'])
) {
    $CIDRAM->CIDRAM['IntegrationFooter'] = '<a href="' . esc_url(admin_url()) . '">⬅' . __('Return to the WordPress Dashboard', 'cidram') . '</a>';
    add_action('init', function() use (&$CIDRAM) {
        if (current_user_can('edit_plugins')) {
            $CIDRAM->view();
        }
    });
}

add_action('admin_menu', function(){
    add_menu_page('CIDRAM', 'CIDRAM', 'manage_options', 'cidram-admin&cidram-page', function(){}, 'dashicons-shield');
});

unset($CIDRAM);
