<?php
defined('_SITEGUARDING_WAP') or die;

class FUNC_WAP2_settings
{
	public static $settings_list = array(
		'access_key',
		'email_for_notifications',
		'show_protectedby',
		'send_notifications',
        
		'protect_login_page',
		'captcha_secret_key',
		'captcha_site_key',
        
		'rules_blocked_ip',
		'rules_allowed_ip',
		'rules_blocked_files',
		'rules_blocked_urls',
        
		'enable_access_notification',
        
		'enable_2fa',
		
		'send_notification_success',
		'send_notification_failed',
		'notification_email',
		'send_by_telegram',
		'telegram_bot_api_token',
		'chat_id',
		'reg_code',

	);
    
    public static function PageHTML()  
    {
        
        $params = FUNC_WAP2_general::Get_SQL_Params(self::$settings_list);
        if (trim($params['email_for_notifications']) == '') 
        {
			$config = new JConfig();
			$params['email_for_notifications'] = $config->mailfrom;
            FUNC_WAP2_general::Set_SQL_Params( array('email_for_notifications' => $params['email_for_notifications']) );
        }
        if ($_SESSION['session_plgwap2_license_info']['membership'] != 'pro')
        {
            $params['show_protectedby'] = 1;
            FUNC_WAP2_general::Set_SQL_Params( array('show_protectedby' => 1) );
        }
        ?>

        <?php
            FUNC_WAP2_general::Wait_CSS_Loader();
        ?>
        
        <div id="main" class="ui main container" style="float: left; display:none;">
        
            <?php
            if (self::CheckActions()) $params = FUNC_WAP2_general::Get_SQL_Params(self::$settings_list);	
            ?>
        
            <h2 class="ui dividing header">Settings & Tools</h2>
            
            
            <?php if (!FUNC_WAP2_general::IsPRO()) FUNC_WAP2_general::BannerArea(); ?>




            <form method="post" id="plgwpagp_decision_page" action="<?php echo JRoute::_( 'index.php?option=com_jmwascanner&view=settings' ); ?>">

            <div class="ui styled accordion full_w">
              <div class="active title">
                <i class="dropdown icon"></i>
                Antivirus settings
              </div>
              <div class="active content">
                <div class="ui form full_h">
                  <div class="field">
                    <label>Access Key</label>
                    <input type="text" name="access_key" id="access_key" value="<?php echo $params['access_key']; ?>">
                    <p class="ui tiny c_red">This key is necessary to access to <a target="_blank" href="http://www.siteguarding.com">SiteGuarding API</a> features. Every website has uniq access key. Don't change it if you don't know what is it.</p>
                  </div>
                </div>
              </div>




              <div class="title">
                <i class="dropdown icon"></i>
                Notifications
              </div>
              <div class="content">
                <div class="ui form full_h">
                    <div class="inline field">
                        <div class="ui toggle checkbox">
                            <input type="checkbox" name="send_notifications" type="checkbox" id="send_notifications" class="hidden" value="1" <?php if ($params['send_notifications'] == 1) echo 'checked="checked"'; ?>>
                            <label>Send Notifications <span class="c_red">We will send important notifications by email.</span></label>
                        </div>
                    </div>
                    
                  <div class="field">
                    <label>Email for Notifications</label>
                    <input type="text" name="email_for_notifications" id="email_for_notifications" value="<?php echo $params['email_for_notifications']; ?>">
                  </div>
                </div>
              </div>




              <div class="title">
                <i class="dropdown icon"></i>
                Tool: Bruteforce protection
              </div>
              <div class="content">
                <?php
                   $data = array(
                        'header' => 'Bruteforce protection for login page',
                        'content' => '<p><img style="float:left; padding:5px 25px 10px 0" src="'. JURI::root() . 'administrator/components/com_jmwascanner/images/bruteforce-attack.png" /><b>Activates special captcha page against bruteforce attack.</b><br><br>Brute-force attack is the most common attack, used against Web applications. The purpose of this attack is to gain access to user’s accounts by repeated attempts to guess the password of the user or group of users. If the Web application does not have any protective measures against this type of attack, it is quite simple to hack the system. This method of password guessing is good because in the end the password is cracked, but it may take a very, very long time.<br><br><a class="mini ui green button" href="'.FUNC_WAP2_general::$LINKS['learn_bruteforce'].'" target="_blank">Learn more</a></p>',
                   );
                   FUNC_WAP2_general::Print_MessageBox($data);
                ?>
                <h3 class="ui dividing header">Settings</h3>
                <div class="ui form full_h">
                    <div class="inline field">
                        <div class="ui toggle checkbox">
                            <input type="checkbox" name="protect_login_page" type="checkbox" id="protect_login_page" class="hidden" value="1" <?php if ($params['protect_login_page'] == 1) echo 'checked="checked"'; ?>>
                            <label>Enable Bruteforce protection (protection for administrator login page)</label>
                        </div>
                    </div>
                    <div class="field">
                      <label>reCAPTCHA Site Key</label>
                      <input type="text" name="captcha_site_key" id="captcha_site_key" value="<?php echo $params['captcha_site_key']; ?>">
                    </div>
                    <div class="field">
                      <label>reCAPTCHA Secret key</label>
                      <input type="text" name="captcha_secret_key" id="captcha_secret_key" value="<?php echo $params['captcha_secret_key']; ?>">
                    </div>
                </div>
                    
                <p>Get reCAPTCHA keys for your site here <a target="_blank" href="https://www.google.com/recaptcha/intro/index.html">https://www.google.com/recaptcha/intro/index.html</a></p>
                
                <h3 class="ui dividing header">Help with reCAPTCHA keys</h3>
                <p></p><b>Step 1. Go to <a target="_blank" href="https://www.google.com/recaptcha/intro/index.html">https://www.google.com/recaptcha/</a> and fill the form</b><br><br>
                <img src="<?php echo JURI::root() . 'administrator/components/com_jmwascanner/images/help1.jpg'; ?>"/><br><br>
                
                <br><b>Step 2. Copy and Insert the keys</b><br><br>
                <img src="<?php echo JURI::root() . 'administrator/components/com_jmwascanner/images/help2.jpg'; ?>"/>
                </p>
              </div>




              <div class="title">
                <i class="dropdown icon"></i>
                Additional Firewal Rules
              </div>
              <div class="content">
                <div class="ui form full_h">

                    <h4 class="ui header">Block by IP address</h4>
                    
                    <div class="ui ignored message">
                          <i class="help circle icon"></i>e.g. 200.150.160.1 or 200.150.160.* or or 200.150.*.*
                    </div>
                    
                    <div class="ui input" style="width: 100%;margin-bottom:10px">
                        <textarea name="rules_blocked_ip" style="width: 100%;height:200px" placeholder="Insert IP addresses or range you want to block, one by line"><?php echo $params['rules_blocked_ip']; ?></textarea>
                    </div>
                    
                    
                    <h4 class="ui header">Allowed IP addresses</h4>
                    
                    <div class="ui ignored message">
                          <i class="help circle icon"></i>e.g. 200.150.160.1 or 200.150.160.* or or 200.150.*.*
                    </div>
                    
                    <div class="ui input" style="width: 100%;margin-bottom:10px">
                        <textarea name="rules_allowed_ip" style="width: 100%;height:200px" placeholder="Insert IP addresses or range you want to allow for any action, one by line"><?php echo $params['rules_allowed_ip']; ?></textarea>
                    </div>
                    
                    
                    <h4 class="ui header">Block access to the files</h4>
                    
                    <div class="ui ignored message">
                          <i class="help circle icon"></i>e.g. /configuration.php
                    </div>
                    
                    <div class="ui input" style="width: 100%;margin-bottom:10px">
                        <textarea name="rules_blocked_files" style="width: 100%;height:200px" placeholder="Insert the files you want to block for direct access, one by line"><?php echo $params['rules_blocked_files']; ?></textarea>
                    </div>
                    
                    
                    <h4 class="ui header">Block access to the URLs</h4>
                    
                    <div class="ui ignored message">
                          <i class="help circle icon"></i>e.g. /administrator/ (nobody will be able to login to /administrator/, don't forget to allow your IP)
                    </div>
                    
                    <div class="ui input" style="width: 100%;margin-bottom:10px">
                        <textarea name="rules_blocked_urls" style="width: 100%;height:200px" placeholder="Insert the URLs you want to block for direct access, one by line"><?php echo $params['rules_blocked_urls']; ?></textarea>
                    </div>


                </div>
              </div>
              
              
              
              
              <div class="title">
                <i class="dropdown icon"></i>
                General
              </div>
              <div class="content">
                <div class="ui form full_h">
                    <div class="inline field">
                        <div class="ui toggle checkbox">
                            <input <?php if ($_SESSION['session_plgwap2_license_info']['membership'] != 'pro') echo 'readonly'; ?> type="checkbox" name="show_protectedby" type="checkbox" id="show_protectedby" class="hidden" value="1" <?php if ($params['show_protectedby'] == 1) echo 'checked="checked"'; ?>>
                            <label>Show 'Protected by'</label>
                        </div>
                    </div>
                    <div class="inline field">
                        <b>Server Time: </b><?php echo date("Y-m-d H:i:s"); ?>. This time stamp we will use in our logs by default. You can change timezone in <a href="index.php?option=com_admin&view=profile&layout=edit&id=<?php echo $user=JFactory::getUser()->id; ?>" target="_blank">Joomla user settings</a>
                    </div>
                </div>
              </div>
              
            </div>
            
            <br />
            <button type="submit" class="medium positive ui button">Save Settings   </button>

            <input type="hidden" name="action" value="save_settings"/>

            </form>
            
            <script>
            jQuery(document).ready(function(){
                jQuery('.ui.accordion').accordion();
                jQuery('.ui.checkbox').checkbox();
                jQuery('#main').css('opacity','0');
                jQuery('#main').css('display','block');
                jQuery('#loader').css('display','none');
				fromBlur();
            });
			
			var i = 0;
			
			function fromBlur() {
				running = true;
					if (running){
					
						jQuery('#main').css("opacity", i);
						
						i = i + 0.02;

					if(i > 1) {
						running = false;
						i = 0;
					}
					if(running) setTimeout("fromBlur()",5);

				}
			}

            </script>
        </div>
        <?php
    } 
    
    
    public static function CheckActions()
    {
        if (!isset($_REQUEST['action'])) return;
        
        $action = trim($_REQUEST['action']);
        
        if ($action == 'save_settings')
        {
            $data = array();
            
            foreach (self::$settings_list as $row)
            {
                $data[$row] = trim($_POST[$row]);
            }
			
			$config = new JConfig();
			if ($data['notification_email'] == '') $data['notification_email'] = $config->mailfrom;
            
			
            if ($data['protect_login_page'] == 1 && ($data['captcha_secret_key'] == '' || $data['captcha_site_key'] == '')) $data['protect_login_page'] = 0;
            
            $folder_siteguarding_logs = dirname(dirname(dirname(dirname(__FILE__)))).'/siteguarding_logs';
            if (!file_exists($folder_siteguarding_logs)) mkdir($folder_siteguarding_logs);
            $fp = fopen($folder_siteguarding_logs.'/sgantivirus.login.keys.php', 'w');
            fwrite($fp, '<?php $captcha_key_site = "'.$data['captcha_site_key'].'"; $captcha_key_secret = "'.$data['captcha_secret_key'].'"; ?>');
            fclose($fp);
            
            // Patch administrator\index.php
            if ($data['protect_login_page'] == 1) 
            {
                if (FUNC_WAP2_general::PatchJMIndex_file(true) === false)
                {
                    $data['protect_login_page'] = 0;
                    $_SESSION['session_plgwap2_alert_message'] = ' Protect Login page is disabled. Can\'t modify administrator\index.php';
                }
            }
            if ($data['protect_login_page'] == 0) 
            {
                FUNC_WAP2_general::PatchJMIndex_file(false);
            }
            
            
            
                // Firewall rules
                $folder = JPATH_ADMINISTRATOR.'/siteguarding_firewall/';
                
                $files = array(
                    'rules_allowed_ip',
                    'rules_blocked_ip',
                    'rules_blocked_files',
                    'rules_blocked_urls'
                );
                
                $full_rules_txt = '';
                foreach ($files as $file)
                {
                    $filename = $folder.$file.".txt";
                    $txt = '';
                    if (isset($_POST[$file]))
                    {
                        $txt = trim($_POST[$file]);
                    }
                    else $txt = '';
                    
                    $fp = fopen($filename, 'w');
                    fwrite($fp, $txt);
                    fclose($fp);
                }
                
                self::CombineFirewallRules();
                        

            FUNC_WAP2_general::Set_SQL_Params($data);
                
            $msg_data = array(
                'type' => 'ok',
                'size' => 'small',
                'content' => 'Settings saved.',
           );
           FUNC_WAP2_general::Print_MessageBox($msg_data);
           
           
           unset($_SESSION['session_plgwap2_license_info']);
		   $file = $plugin_dir.'tmp'.DIRSEP.'antivirus_license.json';
           $license_info = FUNC_WAP2_general::GetLicenseInfo(FUNC_WAP2_general::GetDomain(), $_SESSION['session_plgwap2_params']['access_key']);
           FUNC_WAP2_general::CreateFile($file, json_encode($license_info));
            
           $_SESSION['session_plgwap2_license_info'] = $license_info;
           unset($_SESSION['session_plgwap2_params']);
           return true;
        }
    }
    
    
	public static function CombineFirewallRules()
	{
	    $a = array();
        $folder = JPATH_ADMINISTRATOR.'/siteguarding_firewall/';
        if (!file_exists($folder)) self::InstallFirewallFolder();
        
        $files = array(
            'rules_allowed_ip.txt' => '::ALLOW_ALL_IP::',
            'rules_blocked_ip.txt' => '::BLOCK_ALL_IP::',
            'rules_blocked_files.txt' => '::RULES::',
            'rules_blocked_urls.txt' => '::BLOCK_URLS::',
            'rules_requests.txt' => '::BLOCK_REQUESTS::'
        );
        
        $full_rules_txt = '';
        foreach ($files as $file => $firewall_section)
        {
            if (file_exists($folder.$file))
            {
                $filename = $folder.$file;
                $size = filesize($filename);
                $handle = fopen($filename, "r");
                $txt = ($size) ? fread($handle, filesize($filename)) : '';
                fclose($handle);
                
                $full_rules_txt .= $firewall_section."\n";
                
                if ($file == 'rules_blocked_files.txt' && $txt != '')
                {
                    $txt = explode("\n", $txt);
                    if (count($txt))
                    {
                        foreach ($txt as $k => $v)
                        {
                            $txt[$k] = 'allow|file|'.$v;
                        }
                        
                        $txt = implode("\n", $txt);
                    }
                }
                $full_rules_txt .= $txt."\n\n";
            }
        }
        
        $filename = $folder."rules.txt";
        $fp = fopen($filename, 'w');
        fwrite($fp, $full_rules_txt);
        fclose($fp);
	}
    
    
    
    
	public static function InstallFirewallFolder()
	{
        $folder = JPATH_ADMINISTRATOR.'/siteguarding_firewall/';
        if (!file_exists($folder)) mkdir($folder);
        
        $file = $folder.'.htaccess';
        if (!file_exists($file))
        {
            $fp = fopen($file, 'w');
            $t = '<Limit GET POST>
order deny,allow
deny from all
</Limit>';
            fwrite($fp, $t);
            fclose($fp);
        }
        
        $file = $folder.'rules_requests.txt';
        if (!file_exists($file))
        {
            $fp = fopen($file, 'w');
            $t = 'cDF8Kg0KKnxiYXNlNjRfZGVjb2RlDQoqfHN0cl9yb3QxMw0KKnw8P3BocA0KKnxldmFsKA0KKnxGaWxlc01hbg0KKnxlZG9jZWRfNDZlc2FiDQoqfG1vdmVfdXBsb2FkZWRfZmlsZQ0KKnxleHRyYWN0KCRfQ09PS0lFKQ0KbG9nfHdwdXBkYXRlc3RyZWFtDQpleGVjdXRlfHdwX2luc2VydF91c2VyDQpsb2d8d3AuDQp1c2VybmFtZXxqb29tbGEu';
            $t = base64_decode($t);
            fwrite($fp, $t);
            fclose($fp);
        }
        
        $files = array(
            'rules_allowed_ip.txt',
            'rules_blocked_ip.txt',
            'rules_blocked_files.txt',
            'rules_blocked_urls.txt'
        );
        foreach ($files as $file)
        {
            $file = $folder.$file;
            if (!file_exists($file))
            {
                $fp = fopen($file, 'w');
                fwrite($fp, '');
                fclose($fp);
            }
        }
        
        $folder = $folder.'/logs/';
        if (!file_exists($folder)) mkdir($folder);
        
        $file = $folder.'.htaccess';
        if (!file_exists($file))
        {
            $fp = fopen($file, 'w');
            $t = '<Limit GET POST>
order deny,allow
deny from all
</Limit>';
            fwrite($fp, $t);
            fclose($fp);
        }
	}


    
    

}

?>