#!/usr/bin/perl
#
#Script to send daily web filter report
#should be executed by cron
#created by Robert Haines - June 2015
#
#
# Configuration Parameters

$servername = "DNSFILTER1";
$serverip = "192.168.7.218"; #ip address of the master
$clientnamefile = "/scripts/CLIENTNAMEIS.txt";
$sendtoemail = "rhaines\@proactiveits.com.au";
#$alsosendto = "info\@domain.com.au";
#$fromemail = "backup\@domain.com.au";

#Get information to send.

# Get information from DF
open(USERLIST, "wget -q -O- http://$serverip/emailreport/userlist_text.jsp|");
@userlist = <USERLIST>;
close(USERLIST);

# Get the client name for this system
open(CLIENTNAME, $clientnamefile);
@clientname = <CLIENTNAME>;
close(CLIENTNAME);
chop($clientname[0]);


#Send the email
        $from=$fromemail;
        $to=$sendtoemail;
        $cc=$alsosendto;
        $subject="Web Filter Report for $clientname[0]";
        $sendmailpath="/usr/sbin/sendmail";

        open (SENDMAIL, "| $sendmailpath -t");
print SENDMAIL "To: $to\n";
print SENDMAIL "Subject: $subject\n";
print SENDMAIL "Mime-Version: 1.0\n";
print SENDMAIL 'Content-Type: multipart/alternative; boundary="FE4B56ACB234342884748397";';
print SENDMAIL "\n";
print SENDMAIL "Content-Disposition: inline\n";
print SENDMAIL "--FE4B56ACB234342884748397\n";
print SENDMAIL "Content-Type: text/html; charset=ISO-8859-1\n";
print SENDMAIL "Content-Transfer-Encoding: 7bit";
print SENDMAIL "\n\n";
print SENDMAIL "
<html><head>
<h3>Daily Web Filter Report</h3>This report summaries internet access requests over the previous day. <br>To access the report, please click on the links below<br>

<br><a href='http://$serverip/emailreport/emailreport.jsp'>Daily Report All users</a><br><br>Alternativly, click on the individual user for an individualised report<br>

";

foreach $user(@userlist)
{
	chop($user);
	print SENDMAIL "<br><a href='http://$serverip/emailreport/emailreport.jsp?user=$user'>Daily Report for $user</a>";

}

print SENDMAIL "<br><br> For more detailed reporting, please log into the <a href='http://$serverip/admin'>Webfilter Console";

print SENDMAIL "\n\n--FE4B56ACB234342884748397\n";

close(SENDMAIL);
