#!/usr/bin/perl -w
# ---------------------------------------------------------------------------
#
# Script to record statistics information from POPFile users
#
# Copyright (c) 2001-2008 John Graham-Cumming
#
#   This file is part of POPFile
#
#   POPFile 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.
#
#   POPFile 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 POPFile; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ---------------------------------------------------------------------------

use strict;
use CGI qw/:standard/;
use DBI;


# Where POPFile lives, where the POPFile home directory can be found

my $site = 'http://getpopfile.org';
my $home = '/var/www/cgi-bin/data';

my ( $mc, $ec, $bc ) = ( param('mc'), param('ec'), param('bc') );

$ec =~ s/^=//;
$mc =~ s/^=//;

# Verify that the passed in values are numeric, if they are not then
# don't do further processing.

if ( ( $mc =~ /^\d+$/ ) &&
     ( $ec =~ /^\d+$/ ) &&
     ( $bc =~ /^\d+$/ ) ) {
    if ( my $db = DBI->connect(  "dbi:SQLite2:dbname=$home/stats.db", '', '' ) ) {
        my $lt = time;
        my $sql = 'INSERT INTO stats ( time, buckets, messages, errors, accuracy ) VALUES ( ?, ?, ?, ?, ? )';
        my @vals = ( $lt, $bc, $mc, $ec );
        push @vals, $mc ? ( 100 - $ec/$mc*100 ) : 0;
        $db->do( $sql, undef, @vals ) or warn "Could not insert stuff (" . $db->errstr . ')';
    }
} 

print redirect( "$site/images/ok_popfile.gif" );
