<?php


    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    class WPH_security_scan_vulnerabilities    extends WPH_security_scan_item
        {
            var $wph;
                     
            function __construct()
                {
                    $this->id       =   $this->get_id();
                   
                    global $wph;
                    
                    $this->wph  =   $wph;
                }   
            
            public function get_id()
                {
                    return 'vulnerabilities';
                }
                
                
            public function get_settings()
                {
                    
                    return array(
                                    'title'         =>  'Vulnerabilities',
                                    'icon'          =>  'dashicons-wordpress-alt',
                                    
                                    'help'          =>  __("Built on insights from our proprietary AI engine, trained with advanced models and enriched by CVE records and other trusted vulnerability sources, this feature checks your site’s plugins against a comprehensive database of known security risks. It continuously analyzes installed components for outdated versions, exposed weaknesses, and potential exploit vectors. By identifying vulnerabilities early, it enables you to take timely action, reduce attack surfaces, and maintain a secure, stable, and trustworthy WordPress environment for your users and data.",    'wp-hide-security-enhancer'),
                                    
                                    'score_points'  =>  200,
                                    
                                    'callback'      =>  array ( $this, 'scan' ),
                                    'use_transient' =>  TRUE
                                    );
                }
                
            
            function scan()
                {
                    $_JSON_response     =   array();
                    
                    $security_vulnerabilities_instance    =   new WPH_component_security_vulnerabilities( FALSE );
                    $site_plugins   =   $security_vulnerabilities_instance->get_site_plugins();
                    $json_site_plugins =   json_encode ( $site_plugins );
                    $api_response_data  =   $security_vulnerabilities_instance->_call_API( $json_site_plugins );
                    
                    $_JSON_response['info']  =   __( 'Using', 'wp-hide-security-enhancer' ) . ' : ' . count ( $site_plugins ) . ' ' . __( 'Plugins', 'wp-hide-security-enhancer' );
                    
                    if ( $api_response_data['status']   !== 'success' )
                        {
                            $_JSON_response['status']       =   FALSE;
                            $_JSON_response['description']  =   '<div class="vulnerability-report">

                                                                    <div class="description"><span class="dashicons dashicons-no"></span> ' . __( 'Unable to connect with API. Try again later with a new Scan.', 'wp-hide-security-enhancer' ) .
                                                                    '</div>
                                                                    </div>';
                            $_JSON_response['actions']      =   array (
                                                                        'fix'       =>  '<a class="button-primary" target="_blank" href="'. network_admin_url ( 'admin.php?page=wp-hide-security-vulnerabilities' ) .'">Fix</a>',
                                                                        'ignore'    =>  '//--post-generated--',
                                                                        'restore'           =>  '//--post-generated--',
                                                                        );
                            
                            return $this->return_json_response( $_JSON_response );  
                        }
                    
                    $get_overview_data  =   $security_vulnerabilities_instance->get_overview_data();
                    if ( $get_overview_data['vulnerable']   >   0   )
                        {
                    
                            $_JSON_response['status']       =   FALSE;
                            $_JSON_response['description']  =   '<div class="vulnerability-report">

                                                                    <div class="description">
                                                                        <p>' . __( 'One or more security vulnerabilities have been detected on your website, including issues affecting ', 'wp-hide-security-enhancer' ) . $get_overview_data['vulnerable'] . __(' installed plugins. These weaknesses could expose your site to attacks, data breaches, or unauthorized access if not addressed promptly.', 'wp-hide-security-enhancer' ) . '</p>
                                                                    </div>
                                                                    
                                                                    <div class="error_log">
                                                                        <ul>
                                                                    ';
                            
                            $site_vulnerabilities_scan  =   $security_vulnerabilities_instance->get_site_vulnerabilities_scan();
                            foreach ( $site_plugins as  $site_plugin )
                                    {
                                        $found_vulnerable   =   FALSE;
                                        foreach ( $site_vulnerabilities_scan['results'] as  $vulnerability )
                                            {
                                                if ( isset ( $vulnerability['cve'] )    &&  count ( $vulnerability['cve'] ) >   0   &&  $site_plugin['slug']   === $vulnerability['plugin'] )
                                                    {
                                                        $found_vulnerable   =   TRUE;
                                                        break;   
                                                    }
                                            }
                                        
                                        if ( $found_vulnerable )
                                            {
                                                $_JSON_response['description']  .=   "<li><span class='info'>[" . __( 'Vulnerable', 'wp-hide-security-enhancer' ) . "]</span> " . $site_plugin['title'] . ' <b>' . __( 'version ', 'wp-hide-security-enhancer' ) . $site_plugin['version'] . '</b></li>';
                                            }
                                    }
                                    
                            $_JSON_response['description']  .=   '</ul></div></div>';
                            
                            $_JSON_response['actions']      =   array (
                                                                'fix'       =>  '<a class="button-primary tips" original-title="Go to a Fix" target="_blank" href="'. network_admin_url ( 'admin.php?page=wp-hide-security-vulnerabilities' ) .'">' . __( 'More Details', 'wp-hide-security-enhancer' ) .'</a>',
                                                                'ignore'            =>  '//--post-generated--',
                                                                'restore'           =>  '//--post-generated--',
                                                                'help'              =>  '<a class="button tips" original-title="Get Help from AI" target="_blank" href="https://chat.openai.com/?q=Help me understand the &quot; One or more security vulnerabilities have been detected on your website, including issues affecting multiple installed plugins. These weaknesses could expose your site to attacks, data breaches, or unauthorized access if not addressed promptly. &quot;. This is a Scan Item in WP Hide plugin">AI Help</a>',
                                                                );
                        }
                        else
                        {
                            $_JSON_response['status']       =   TRUE;
                            $_JSON_response['description']  =   __( '<span class="dashicons dashicons-yes"></span>No known vulnerabilities have been identified on your site.', 'wp-hide-security-enhancer' );
                        }
                        
                    return $this->return_json_response( $_JSON_response );
                
                }    
            
        }
        
        
?>