#!/usr/bin/perl
#
#
# PIX Logging Architecture
# [Kristof Philipsen]
#
# This file is part of PIX Logging Architecture
#
# PIX Logging Architecture is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PIX Logging Architecture is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PIX Logging Architecture; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                                                                                                                                                                                                  
use DBI;
use CGI;

use Date::Manip; 
                                                                                                                                                                                                  
#
# include configuration
#
require "conf.pl";      # General Configuration
require "subs.pl";      # Subroutines
                                                                                                                                                                                                  
#
# Make Database Connection
#
db_connect();
                                                                                                                                                                                                  
#
# Get CGI Parameters
#
$purge = new CGI;
$purge_id = clean_input($purge->param("purge_id"));
$purge_mode = clean_input($purge->param("purge_mode"));
$confirm_purge_policy = clean_input($purge->param("confirm_purge_policy"));
$confirm_purge_accept = clean_input($purge->param("confirm_purge_accept"));

#
# HTML SECTION
#
printtitle();
                                                                                                                                                                                                  
print <<EOF;
<span class='titlehead'><b>PLA Configuration > Logs Purging</b></span>
<br><br>
EOF

$getAllPurgePolicy = "SELECT purge_id,policy_name,policy_target FROM purge_config order by purge_id";
$getAllPurgePolicies = $db_handle->prepare($getAllPurgePolicy);
$getAllPurgePolicies->execute();

if ($purge_mode eq "commit") {
if (!$confirm_purge_policy || !$confirm_purge_accept) {
print <<EOF;
<b>Purge has not been confirmed.</b> <i><font color="red">Please select the check boxes on the previous page to confirm the purging of the log entries.</font></i>
<br><br>
<a href="javascript:history.go(-1)" class="bodylink">Click here to go back to the previous page</a>.
EOF
exit();
}
}



if ($purge_mode eq "select") {
print <<EOF;
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<td width="200" bgcolor="#5479d8"><span class="button">Purge Policy Name</td>
<td width="100" bgcolor="#5479d9"><span class="button">Table Name</td>
<td width="100" bgcolor="#5479d8"><span class="button">Options</td>
EOF

while (($purge_id,$policy_name,$policy_target)= $getAllPurgePolicies->fetchrow) {
print <<EOF;
<tr OnMouseOver="this.bgColor='#d9d9d9'" OnMouseOut="this.bgColor='#ffffff'">
<td width="200"><a href="pix_config_log_purge?purge_id=$purge_id&purge_mode=confirm" class="bodylink">$policy_name</a></td>
<td width="100"><span class="main">$policy_target</span></td>
<td width="100"><a href="pix_config_log_purge?purge_id=$purge_id&purge_mode=confirm" class="bodylink">[ View / Run Purge Policy ]</span></a></td>
<tr>
EOF
}

print <<EOF;
</table>
EOF
}

if ($purge_mode eq "confirm" || $purge_mode eq "commit") {
$getAllPurgeDetail = "SELECT * FROM purge_config where purge_id=\"$purge_id\"";
$getAllPurgeDetails = $db_handle->prepare($getAllPurgeDetail);
$getAllPurgeDetails->execute();
($purge_id,$purge_entry_time,$purge_update_time,$policy_name,$policy_details,$policy_target,$policy_start_date,$policy_start_match_regex,$policy_start_criteria,$policy_start_dateformat,$policy_end_date,$policy_end_match_regex,$policy_end_criteria,$policy_end_dateformat) = $getAllPurgeDetails->fetchrow;
}


if ($policy_start_date ne "") {
$start_date = DateCalc($policy_start_date,$policy_start_criteria,\$err);
$start_date = UnixDate($start_date,$policy_start_dateformat);
}


if ($policy_end_date ne "") {
$end_date = DateCalc($policy_end_date,$policy_end_criteria,\$err);
$end_date = UnixDate($end_date,$policy_end_dateformat);
}


if ($policy_start_date ne "") {
$log_count = "SELECT count(*) FROM $policy_target where log_time $policy_start_match_regex '$start_date\%' and log_time $policy_end_match_regex '$end_date\%'";
} else {
$log_count = "SELECT count(*) FROM $policy_target where log_time $policy_end_match_regex '$end_date\%'";
$start_date = "not applicable (N/A)"
}

$logpurge_count = $db_handle->prepare($log_count)
    or die "Couldn't prepare query '$log_count': $DBI::errstr\n";
$logpurge_count->execute()
    or die "Couldn't execute query '$logpurge_count': $DBI::errstr\n";
$log_purge_count = $logpurge_count->fetchrow;


if ($purge_mode eq "confirm") {

print <<EOF;
<table width="500" cellpadding="0" cellspacing="0">
<td bgcolor="#5479d8"><span class="button">General Purge Policy Details</span></td>
<tr>
<td><span class="main">
Purging Policy: <b>$policy_name</b><br>
Purging Policy Details: <b>$policy_details</b><br>
Policy Target: <b>$policy_target</b><br>
<br>
</td>
<tr>
<td bgcolor="#5479d8"><span class="button">Purge Policy Criteria</span></td>
<tr>
<td><span class="main">
Purging all logs newer than: <b>$start_date</b><br>
Purging all logs older than: <b>$end_date</b><br>
<br>
</td>
<tr>
<td bgcolor="#5479d8"><span class="button">Purge Policy Matches</span></td>
<tr>
<td><span class="main">
Number of log entries matching the purging criteria: <b>$log_purge_count</b><br>
<br>
</td>
<tr>
</table>
<br><br>
<font color="red"><b>NOTE:</b> Please note that some of the purging jobs may be quite big and could take a considerable amount of time to execute. During this time your HTTP session to the PLA web interface may time out, however your jobs will keep running.</font>
<br><br>
If you are sure you wish to purge the logs according to the specified purging policy, check the following boxes and click on the "Purge Logs" button:<br><br>
<form name="validate_purge" action="pix_config_log_purge" method="get">
<input type="hidden" name="purge_id" value="$purge_id">
<input type="hidden" name="purge_mode" value="commit">
<input type="checkbox" name="confirm_purge_policy" value="1"> I wish to purge the PLA Database according to the Purge Policy "$policy_name". <br>
<input type="checkbox" name="confirm_purge_accept" value="1"> I accept the responsibility for the permanent deletion of certain records from within the PLA Database.<br><br>
<input type="submit" name="confirm" value="Purge Logs">
</form>
</span>
EOF
}

if ($purge_mode eq "commit") {
 
   if (!$confirm_purge_policy || !$confirm_purge_accept) {
print <<EOF;
Purge has not been confirmed.
EOF
   } else {   
   $total_count = "SELECT count(*) FROM $policy_target";
   $total_log_count = $db_handle->prepare($total_count)
    or die "Couldn't prepare query '$total_log_count': $DBI::errstr\n";
   $total_log_count->execute();
   $log_all_count = $total_log_count->fetchrow;

   if ($policy_start_date ne "") {
print <<EOF;
Number of Log Entries <u>Before Purge</u>: <b>$log_all_count</b><br>
Number of Log Entries <u>Being Deleted</u>: <b>$log_purge_count</b><br>
<br>
<b><blink>Purge has been confirmed and is being executed...</blink></b><br><br>
<ul>
>> <u>Executing:</u> "<i>DELETE from $policy_target where log_time $policy_start_match_regex '$start_date\%' and log_time $policy_end_match_regex '$end_date\%';</i>"<br>
EOF
   $delete_logs = "DELETE from $policy_target where log_time $policy_start_match_regex '$start_date\%' and log_time $policy_end_match_regex '$end_date\%'";
   $delete_action_logs = $db_handle->prepare($delete_logs);
   $delete_action_logs->execute();


   $total_count = "SELECT count(*) FROM $policy_target";
   $total_log_count = $db_handle->prepare($total_count)
    or die "Couldn't prepare query '$total_log_count': $DBI::errstr\n";
   $total_log_count->execute();
   $log_all_count = $total_log_count->fetchrow;

   print ">> <u>Result:</u> SUCCESS ($log_purge_count log entries deleted from '$policy_target') ($log_all_count entries remaining in '$policy_target')<br>";
   print "</ul>";

    
   } else {

print <<EOF;
Number of Log Entries <u>Before Purge</u>: <b>$log_all_count</b><br>
Number of Log Entries <u>Being Deleted</u>: <b>$log_purge_count</b><br>
<br>
<b><blink>Purge has been confirmed and is being executed...</blink></b><br><br>
<ul>
>> <u>Executing:</u> <i>DELETE from $policy_target where log_time $policy_end_match_regex '$end_date\%';</i><br>
EOF

   $delete_logs = "DELETE from $policy_target where log_time $policy_end_match_regex '$end_date\%'";
   $delete_action_logs = $db_handle->prepare($delete_logs);
   $delete_action_logs->execute();

   $total_count = "SELECT count(*) FROM $policy_target";
   $total_log_count = $db_handle->prepare($total_count)
    or die "Couldn't prepare query '$total_log_count': $DBI::errstr\n";
   $total_log_count->execute();
   $log_all_count = $total_log_count->fetchrow;

   print ">> <u>Result:</u> SUCCESS ($log_purge_count log entries deleted from '$policy_target') ($log_all_count entries remaining in '$policy_target')<br>";
   print "</ul>";
   }
   }

}
