<?php
/**
 * VMPFence Scan Options Configuration Extension
 * Extends existing VMPFence_Config for scan-specific options
 */

if (!defined('ABSPATH')) {
    exit;
}

class VMPFence_Scan_Config {
    
    // Scan type constants
    const SCAN_TYPE_LIMITED = 'limited';
    const SCAN_TYPE_STANDARD = 'standard';
    const SCAN_TYPE_HIGH_SENSITIVITY = 'high_sensitivity';
    const SCAN_TYPE_CUSTOM = 'custom';
    
    /**
     * All scan-specific configuration options with defaults
     */
    private static $scan_options = array(
        // SCAN SCHEDULING & GENERAL
        'scheduledScansEnabled' => 1,
        'scanType' => self::SCAN_TYPE_STANDARD,
        'schedMode' => 'auto',
        'manualScanType' => 'weekly',
        
        // REPUTATION & SECURITY CHECKS - VERIFIED
        'scansEnabled_checkGSB' => 0,
        'scansEnabled_checkReadableConfig' => 0,
        'spamvertizeCheck' => 0,
        'checkSpamIP' => 0,
        'scansEnabled_checkHowGetIPs' => 0,
        'scansEnabled_suspectedFiles' => 0,
        
        // API KEYS & EXTERNAL SERVICES
        'google_safebrowsing_api_key' => 'AIzaSyBg2yUhFu5hjCW4BovmoiX9f0vcfV0n4go',
        
        // FILE INTEGRITY SCANNING - VERIFIED
        'scansEnabled_core' => 0,
        'scansEnabled_themes' => 0,
        'scansEnabled_plugins' => 0,
        'scansEnabled_coreUnknown' => 0,
        'scansEnabled_oldVersions' => 0,
        
        // MALWARE & CONTENT SCANNING - VERIFIED
        'scansEnabled_malware' => 0,
        'scansEnabled_fileContents' => 0,
        'scansEnabled_fileContentsGSB' => 0,
        'scansEnabled_scanImages' => 0,
        'scansEnabled_suspiciousAdminUsers' => 0,
        
        // USER SECURITY SCANNING - NEW OPTIONS
        'scansEnabled_risky_usernames' => 0,
        'scansEnabled_admin_users' => 0, 
        'scansEnabled_user_enumeration' => 0,
        
        // POSTS & COMMENTS SCANNING - VERIFIED
        'scansEnabled_posts' => 0,
        'scansEnabled_comments' => 0,
        'scansEnabled_passwds' => 0,
        'scansEnabled_diskSpace' => 0,
        
        // WORDPRESS OPTIONS & DATABASE - VERIFIED
        'scansEnabled_suspiciousOptions' => 0,
        'scansEnabled_wafStatus' => 0,
        
        // ADVANCED SCANNING OPTIONS - VERIFIED
        'other_scanOutside' => 0,
        
        // VMPFence-specific options
        'other_hideWP' => 0,
        'deleteTablesOnDeact' => 0,
        'debugOn' => 0,
        
        // PERFORMANCE & RESOURCE CONTROLS - Use defaults
        'lowResourceScansEnabled' => 0,
        'scan_maxDuration' => '', // Empty = use system default
        'scan_maxIssues' => '',   // Empty = unlimited
        'scan_maxExecTime' => 0,  // 0 = use default
        'maxMem' => '',           // Empty = use default
        'scan_max_resume_attempts' => 2,  // Default retry attempts
        'scan_force_ipv4_start' => 0,   // IPv4 only option
        
        // FILE EXCLUSIONS & INCLUSIONS - VERIFIED
        'scan_exclude' => '',
        'scan_include_extra' => '',
        
        // NOTIFICATION & ALERTING (VMPFence-specific)
        'alertOn_scanIssues' => 1,
        'alertOn_critical' => 1,
        'alertOn_update' => 1,
        'alert_maxHourly' => 0,
        'email_summary_enabled' => 1,
        'email_summary_excluded_directories' => '',
        
        // SCHEDULED SCAN TIMING
        'schedStartHour' => 2,
        'schedStartDayOfWeek' => 0,
        'schedStartDate' => '',
        
        // API & PREMIUM FEATURES
        'isPaid' => 0,
        'keyType' => 'free',
        'apiKey' => '',
        'howGetIPs' => '',
        'serverIPs' => '',
        
        // ALWAYS ENABLED
        'scansEnabled_geoipSupport' => 1,
        
        // DEPRECATED (kept for compatibility)
        'scansEnabled_highSense' => 0,
    );
    
    /**
     * Initialize all scan options on plugin activation
     * This method is called from the activation hook
     */
    public static function initializeScanOptions() {
        // Get all scan options that should be initialized
        $scan_options = self::getAllScanOptions();
        
        foreach ($scan_options as $option_name => $default_value) {
            // Only set if option doesn't already exist
            $existing_value = VMPFence_Config::get($option_name, null);
            // if ($existing_value === null) {
            //     VMPFence_Config::set($option_name, $default_value);
            // }
            VMPFence_Config::set($option_name, $default_value);
        }
        
        // error_log('VMPFence: Initialized ' . count($scan_options) . ' scan options on activation');

        $scan_options = self::standardScanOptions();
        
        foreach ($scan_options as $option_name => $default_value) {
            // Only set if option doesn't already exist
            $existing_value = VMPFence_Config::get($option_name, null);
            VMPFence_Config::set($option_name, $default_value);
        }
    }
    
    /**
     * Get all scan options with their default values
     */
    private static function getAllScanOptions() {
        return array(
            // SCAN SCHEDULING & GENERAL
            'scheduledScansEnabled' => 1,
            'scanType' => self::SCAN_TYPE_STANDARD,
            'schedMode' => 'auto',
            'manualScanType' => 'weekly',
            
            // REPUTATION & SECURITY CHECKS - VERIFIED
            'scansEnabled_checkGSB' => 0,
            'scansEnabled_checkReadableConfig' => 0,
            'spamvertizeCheck' => 0,
            'checkSpamIP' => 0,
            'scansEnabled_checkHowGetIPs' => 0,
            'scansEnabled_suspectedFiles' => 0,
            
            // FILE INTEGRITY SCANNING - VERIFIED
            'scansEnabled_core' => 0,
            'scansEnabled_themes' => 0,
            'scansEnabled_plugins' => 0,
            'scansEnabled_coreUnknown' => 0,
            'scansEnabled_oldVersions' => 0,
            
            // MALWARE & CONTENT SCANNING - VERIFIED
            'scansEnabled_malware' => 0,
            'scansEnabled_fileContents' => 0,
            'scansEnabled_fileContentsGSB' => 0,
            'scansEnabled_scanImages' => 0,
            'scansEnabled_suspiciousAdminUsers' => 0,
            
            // USER SECURITY SCANNING - NEW OPTIONS
            'scansEnabled_risky_usernames' => 0,
            'scansEnabled_admin_users' => 0, 
            'scansEnabled_user_enumeration' => 0,
            
            // POSTS & COMMENTS SCANNING - VERIFIED
            'scansEnabled_posts' => 0,
            'scansEnabled_comments' => 0,
            'scansEnabled_passwds' => 0,
            'scansEnabled_diskSpace' => 0,
            
            // WORDPRESS OPTIONS & DATABASE - VERIFIED
            'scansEnabled_suspiciousOptions' => 0,
            'scansEnabled_wafStatus' => 0,
            
            // ADVANCED SCANNING OPTIONS - VERIFIED
            'other_scanOutside' => 0,
            
            // VMPFence-specific options
            'other_hideWP' => 0,
            'deleteTablesOnDeact' => 0,
            'debugOn' => 0,
            
            // PERFORMANCE & RESOURCE CONTROLS
            'lowResourceScansEnabled' => 0,
            'scan_maxDuration' => '',
            'scan_maxIssues' => '',
            'scan_maxExecTime' => 0,
            'maxMem' => '',
            'scan_max_resume_attempts' => 2,
            'scan_force_ipv4_start' => 0,
            
            // FILE EXCLUSIONS & INCLUSIONS - VERIFIED
            'scan_exclude' => '',
            'scan_include_extra' => '',
            
            // NOTIFICATION & ALERTING (VMPFence-specific)
            'alertOn_scanIssues' => 1,
            'alertOn_critical' => 1,
            'alertOn_update' => 1,
            'alert_maxHourly' => 0,
            'email_summary_enabled' => 1,
            'email_summary_excluded_directories' => '',
            
            // SCHEDULED SCAN TIMING
            'schedStartHour' => 2,
            'schedStartDayOfWeek' => 0,
            'schedStartDate' => '',
            
            // API 
            'isPaid' => 0,
            'keyType' => 'free',
            'apiKey' => '',
            'howGetIPs' => '',
            'serverIPs' => '',
            
            // ALWAYS ENABLED
            'scansEnabled_geoipSupport' => 1,
            
            // DEPRECATED (kept for compatibility)
            'scansEnabled_highSense' => 0,
        );
    }
    
    /**
     * Get all scan options defaults
     */
    public static function getScanDefaults() {
        return self::$scan_options;
    }
    
    /**
     * All disabled options baseline
     */
    private static function _inactiveScanOptions() {
        return array(
            'spamvertizeCheck' => 0,
            'checkSpamIP' => 0,
            'scansEnabled_checkGSB' => 0,
            'scansEnabled_checkHowGetIPs' => 0,
            'scansEnabled_checkReadableConfig' => 0,
            'scansEnabled_suspectedFiles' => 0,
            'scansEnabled_core' => 0,
            'scansEnabled_themes' => 0,
            'scansEnabled_plugins' => 0,
            'scansEnabled_coreUnknown' => 0,
            'scansEnabled_malware' => 0,
            'scansEnabled_fileContents' => 0,
            'scansEnabled_fileContentsGSB' => 0,
            'scansEnabled_posts' => 0,
            'scansEnabled_comments' => 0,
            'scansEnabled_suspiciousOptions' => 0,
            'scansEnabled_oldVersions' => 0,
            'scansEnabled_suspiciousAdminUsers' => 0,
            'scansEnabled_risky_usernames' => 0,
            'scansEnabled_admin_users' => 0, 
            'scansEnabled_user_enumeration' => 0,
            'scansEnabled_passwds' => 0,
            'scansEnabled_diskSpace' => 0,
            'scansEnabled_wafStatus' => 0,
            'other_scanOutside' => 0,
            'scansEnabled_scanImages' => 0,
            'lowResourceScansEnabled' => 0,
            'scan_exclude' => '',
            'scan_include_extra' => '',
        );
    }
    
    /**
     * Limited Scan
     */
    public static function limitedScanOptions() {
        return array_merge(self::_inactiveScanOptions(), array(
            'scansEnabled_checkHowGetIPs' => 1,
            'scansEnabled_malware' => 1,
            'scansEnabled_fileContents' => 1,
            'scansEnabled_fileContentsGSB' => 1,
            'scansEnabled_suspiciousOptions' => 1,
            'scansEnabled_oldVersions' => 1,
            'scansEnabled_wafStatus' => 1,
            'lowResourceScansEnabled' => 1,
            'scan_exclude' => '',
            'scan_include_extra' => '',
            'scansEnabled_geoipSupport' => 1,
            // Performance options
            'scan_maxIssues' => 1000,
            'scan_maxDuration' => '',
            'maxMem' => 256,
            'scan_maxExecTime' => 0,
            'scan_max_resume_attempts' => 2,
        ));
    }
    
    /**
     * Standard Scan
     */
    public static function standardScanOptions() {
        return array_merge(self::_inactiveScanOptions(), array(
            'spamvertizeCheck' => 1,
            'checkSpamIP' => 1,
            'scansEnabled_checkHowGetIPs' => 1,
            'scansEnabled_checkReadableConfig' => 1,
            'scansEnabled_suspectedFiles' => 1,
            'scansEnabled_core' => 1,
            'scansEnabled_themes' => 1,
            'scansEnabled_plugins' => 1,
            'scansEnabled_coreUnknown' => 1,
            'scansEnabled_malware' => 1,
            'scansEnabled_fileContents' => 1,
            'scansEnabled_fileContentsGSB' => 1,
            'scansEnabled_posts' => 1,
            'scansEnabled_comments' => 1,
            'scansEnabled_suspiciousOptions' => 1,
            'scansEnabled_oldVersions' => 1,
            'scansEnabled_suspiciousAdminUsers' => 1,
            'scansEnabled_passwds' => 1,
            'scansEnabled_diskSpace' => 1,
            'scansEnabled_wafStatus' => 1,
            'scan_exclude' => '',
            'scan_include_extra' => '',
            'scansEnabled_geoipSupport' => 1,
            // Performance options
            'lowResourceScansEnabled' => 0,
            'scan_maxIssues' => 1000,
            'scan_maxDuration' => '',
            'maxMem' => 256,
            'scan_maxExecTime' => 0,
            'scan_max_resume_attempts' => 2,
        ));
    }
    
    /**
     * High Sensitivity Scan
     */
    public static function highSensitivityScanOptions() {
        return array_merge(self::_inactiveScanOptions(), array(
            'spamvertizeCheck' => 1,
            'checkSpamIP' => 1,
            'scansEnabled_checkGSB' => 1,
            'scansEnabled_checkHowGetIPs' => 1,
            'scansEnabled_checkReadableConfig' => 1,
            'scansEnabled_suspectedFiles' => 1,
            'scansEnabled_core' => 1,
            'scansEnabled_coreUnknown' => 1,
            'scansEnabled_malware' => 1,
            'scansEnabled_fileContents' => 1,
            'scansEnabled_fileContentsGSB' => 1,
            'scansEnabled_posts' => 1,
            'scansEnabled_comments' => 1,
            'scansEnabled_suspiciousOptions' => 1,
            'scansEnabled_oldVersions' => 1,
            'scansEnabled_suspiciousAdminUsers' => 1,
            'scansEnabled_risky_usernames' => 1,
            'scansEnabled_admin_users' => 1,
            'scansEnabled_user_enumeration' => 1,
            'scansEnabled_passwds' => 1,
            'scansEnabled_diskSpace' => 1,
            'scansEnabled_wafStatus' => 1,
            'scan_exclude' => '',
            'scan_include_extra' => '',
            'scansEnabled_geoipSupport' => 1,
            'scansEnabled_themes' => 1,
            'scansEnabled_plugins' => 1,
            'other_scanOutside' => 1,
            'scansEnabled_scanImages' => 1,
            // Performance options
            'lowResourceScansEnabled' => 0,
            'scan_maxIssues' => 1000,
            'scan_maxDuration' => '',
            'maxMem' => 256,
            'scan_maxExecTime' => 0,
            'scan_max_resume_attempts' => 2,
        ));
    }
    
    /**
     * Get scan options based on scan type
     */
    public static function getScanOptions($scan_type = null) {
        if (!$scan_type) {
            $scan_type = VMPFence_Config::get('scanType', self::SCAN_TYPE_STANDARD);
        }
        
        switch ($scan_type) {
            case self::SCAN_TYPE_LIMITED:
                return self::limitedScanOptions();
            case self::SCAN_TYPE_STANDARD:
                return self::standardScanOptions();
            case self::SCAN_TYPE_HIGH_SENSITIVITY:
                return self::highSensitivityScanOptions();
            case self::SCAN_TYPE_CUSTOM:
                return self::getCustomScanOptions();
            default:
                return self::standardScanOptions();
        }
    }
    
    /**
     * Custom scan options - get current user configuration
     */
    public static function getCustomScanOptions() {
        $custom_options = array();
        
        foreach (array_keys(self::$scan_options) as $option) {
            if (strpos($option, 'scansEnabled_') === 0 || 
                strpos($option, 'scan_') === 0 || 
                strpos($option, 'other_') === 0 ||
                in_array($option, array('spamvertizeCheck', 'checkSpamIP', 'lowResourceScansEnabled'))) {
                $custom_options[$option] = VMPFence_Config::get($option);
            }
        }
        
        $custom_options['scansEnabled_geoipSupport'] = 1;
        return $custom_options;
    }
    
    /**
     * Apply scan type preset to scan options
     * 
     * @param array $scan_options Current scan options
     * @param string $scan_type Scan type (limited, standard, high_sensitivity)
     * @return array Modified scan options with preset applied
     */
    public static function applyScanTypePreset($scan_options, $scan_type) {
        switch ($scan_type) {
            case self::SCAN_TYPE_LIMITED:
                $preset = self::limitedScanOptions();
                break;
            case self::SCAN_TYPE_STANDARD:
                $preset = self::standardScanOptions();
                break;
            case self::SCAN_TYPE_HIGH_SENSITIVITY:
                $preset = self::highSensitivityScanOptions();
                break;
            case self::SCAN_TYPE_CUSTOM:
                // For custom, return options as-is
                return $scan_options;
            default:
                $preset = self::standardScanOptions();
                break;
        }
        
        // Apply preset values to scan options
        return array_merge($scan_options, $preset);
    }
}
