#!/usr/bin/perl
#########################################################################
####                                                                #####
####                  USER CONFIGURATION FILE                       #####
####                                                                #####
####                     v 3.18 (26/Aug/2013)                      #####
####                                                                #####
#########################################################################


#########################################################################
###                           Platform used                           ###
#########################################################################

# Just change $type_serveur according to your OS

$type_serveur = 0;  # (0 = Unix/MacOS ; 1 = Windows; 2 = Domino)
$htmlext = ".html"; # html file extension
$gifext = ".gif";   # gif file extension
$plext = ".pl";     # perl file extension
$zipext = ".gz";    # compression suffix
$carext = ".";      # character extension separator

#########################################################################
###                           Logfile format                          ###
#########################################################################


### Logfile format (http://www.w3perl.com/docs/logformat.html)
# CLF           : Common Log File
# ECLF          : Extended Common Log File (combined)
# ECLFIO        : Extended Common Log File with mod_logio (combinedio)
# NECLF         : New Extended Common Log File
# IIS           : IIS format (IIS) - Logfiles starts with in
# W3C           : W3C format (IIS) - Logfiles starts with ex
# Webstar       : Webstar format
# FTP           : FTP (unix)
# SFTP          : SFTP (unix)
# FTP-IIS       : FTP (IIS server with W3C format)
# Squid-Native  : Squid default format
# Squid-Common  : Squid common format
# Squid-ECLF    : Squid extended common format
# Sendmail      : Sendmail/Mail format
# Postfix       : Postfix/Mail format
# Exim          : Exim/Mail format
# Mdaemon       : Mdaemon format
# RealServer    : RealServer
# Cups          : Cups
# Dhcp          : Dhcp
# Dhcp-ISC      : Dhcp-ISC
# own           : User's own logfile format
$logfile_format = "Squid-ECLF";

### Default logfile format string
# Modify this string if you have changed your server configuration
# W3C/Webstar format is automatically extracted from logfile.
# You can also define your own logfile format here
# If you leave this field blank, default value will be used
# Fields allowed :
# %date           : Date as Day:Hour or just Day
# %time           : Hour
# %hourshift      : Shift from GMT
# %host           : Remote host IP or hostname
# %method         : Method used to send the requested file
# %page           : File requested
# %protocol       : Protocole used
# %status         : Status code
# %requetesize    : Byte transfered for the requested file
# %agent          : Browser and OS of the remote host
# %referer        : Page the request come from
# %virtualhost    : Name of the server the request was from
# %query          : Arguments from the request
# %direction      : Outgoing or incoming transfer
# %I              : Input bandwith
# %O              : Output bandwith
# %transfert_time : Time to transfer
# %elapsed        : Time to answer the request
# %codestatus     : Status code
# %peerstatus     : Peer status
# %mimetype       : Mimetype
# %id             : Log ID
# %module         : Log module
# %message        : Text string
# %null           : Anything not in this list !
$struct_logfile = "%host %null %login %date %hourshift %method %page %protocol %status %requetesize %referer %agent %codestatus";

### Logfile filename (http://www.w3perl.com/docs/logfiles.html)
# Fields allowed : 
# %year         [xxxx]
# %smallyear    [xx]
# %month        [01-12]
# %smallmonth   [1-12]
# %lettermonth  [Jan-Dec]
# %day          [01-31]
# %smallday     [1-31]
# %hour         [0-23]
# %min          [0-59]
# %sec          [0-59]
# %rotate       [1-]
# %prefixlog should be used for access.log
# If you are using compressed logfiles, don't add suffix here
$struct_compressed_logfile = "%prefixlog.%rotate";

### Virtual server within CLF or ECLF (one logfile for several web server)
$virtualfilter = "";  # select which virtual server you want to parse
$virtualCLF = 0;        # set to 1 if %virtualhost is hidden in %page (%page value begin with http://)

### Date format
# Fields allowed : 
# %year [YY] or [YYYY]
# %month [00-12]
# %day [00-31]
$date_format = "";


### Remote log files
# If you want to retrieve external log
$retrieve_external_log = 0; # set to 1 to activate
$wget_bin = ""; # Wget binary path
$wget_user = ""; # Login if needed
$wget_pass = ''; # Password if needed - simple quote is needed here to avoid perl to evaluate expr
$remote_host_log = ""; # Remote host with protocol (eg: http://www.mydomain.com)
$remote_host_port = "80"; # Remote port host (usually 80 for web)
$remote_host_dir = ""; # Remote logfiles directory from your server root (eg: /logs/)

#########################################################################
###                 W3Perl Path Configuration                         ###
#########################################################################

## Output path (best inside your web server directory)
$path = "/var/w3perl/htdocs/demo-squid/";

## Path where the package have been installed
$pathinit = "/home/domisse/public_html/w3perl/";

## Path where your HTML documents are stored (leave blank if no server have been installed)
$pathserver = "/var/w3perl/htdocs/";

## URL where your HTML documents are located (should start with / - usually /)
$linkpathserver = "/";

## URL where your stats will be output - Don't use a path but an URL !!!
## If your w3perl's output is at http://127.0.0.1/stats/, URL will be /stats/
## An empty value means no web server is running (Ajax not available).
$linkpath_url = "/demo-squid/";

## Cgi-bin path (leave blank if cgi are not available - no server running)
$cgipath = "/var/w3perl/cgi-bin/";

## Path where log files are located on your server
@fileroot = ('/var/w3perl/logs/futurequest/squid/');

## $prefixlog is the CONSTANT part of your log filename
## (usually the name of your current logfile located in $fileroot)
## $referlog, $agentlog and $errorlog are the name of your referer,
## your agent log and error file if you have one.
$prefixlog = "access.log";
$referlog = "";
$agentlog = "";
$errorlog = "";

## Select graphic tool (0 for jqplot or 1 for fly)
$fly_set = 0;

## Fly/Flydraw path
$FLY = "/var/w3perl/cgi-bin/fly";

## Gunzip or unzip path (decompression tool)
$GZIP = "/bin/gzip -dc";

#########################################################################
###                    W3Perl Exportation                             ###
#########################################################################

### PDF
$enable_pdf = 0;
$HTMLDOC = "";   # Convert html to pdf

### Email
$enable_email = 0;   # email report enabled ?
$emailadr = '';               # email who will receive report 
$emailadr_pass = "";               # Password required if authenticated smtp server is used
$emailadr_cc = '';               # CC: email
$email_smtp = "localhost";   # smtp server to send email
$email_smtp_port = 25;  # smtp server port
$email_html = 0;       # send html report
$email_pdf = 0;         # send pdf report
$email_daily = 0;     # last day report
$email_weekly = 0;   # last week report
$email_monthly = 0; # current month report
$email_yearly = 0;   # current year report


#########################################################################
###                    W3Perl Configuration                           ###
#########################################################################

## Server address (just the name, don't add http://)
## Avoid IP as domainname is extracted from this value
## Use www.google.com if you have google's logfiles
$localserver = "www.w3perl.com";
$localserver_port = 80;
$localserver_protocol = "http";

## Local host address (just the name, don't add http://)
## Same as $localserver if the scripts are running on the server
$localhost = "127.0.0.1";
$localhost_port = 80;
$localhost_protocol = "http";

## URL filtering
## Parse only lines which matches this string
## You can use regular expression
## uk|fr will select lines with uk OR fr
$tri = "/";

## URL exclude
## Skip only lines which matches this string
## You can use regular expression
## uk|fr will reject lines with uk OR fr
$tri_exclude = "";

## Filtering on country - use country extension
$country_filtering = "";

## Hosts to filter
## ('.ens.fr') reject the ens.fr domain
## ('.fr') reject all french hosts
## ('.ens.fr','.iap.com') reject ens.fr and iap.com domain
@nolog = ();

## full detailled stats for these html pages (URL from your Document root)
@selection = ();

## Subdirectories with graphical stats output
## (URL from your Document root)
@selecrepert = ();

## Excluding subdirectories / emails from your report
## (URL from your Document root)
@excluderepert = ();

## Virtual server to exclude (available for NECLF)
@excludevirtual = ();

## Status code to exclude (status 400 and beyond are filtered by default)
@excludestatuscode = ();

## Reject redirection status code (30X status code) : 0 = yes (default)
$redirect_flag = 0; # set to 1 to allow redirect status code 301/302 and 304

## RSS file
@rss_file = ();

## Global variables
$precision = 3;     # level of accuracy (from 1 (basic) to 4 (huge))
$locallog = 0;      # local access log (0 = no = exclude local log)
$localonly = 0;     # only local access log (1 = yes = only local log)
$zip = 1;           # compressed log files (1 = yes)
$zipcut = 3;        # split log files : 0 => no ; 1 => monthly ; 2 => daily ; 3 => apache rotation

## Threshold
$show_all = 1;      # will also display all items
$topten = 10;       # Show only  best items
$max_item_page = 5000;       # Maximum number of items to display when show_all is enabled
$display_subitem = 10;       # Maximum number of subitems to display in weekly/monthly/yeraly reports
$seuilpage = 10;    # Show pages with more than  requests
$seuilsite = 10;    # Show hosts with more than  requests
$seuilscript = 10;  # Show scripts with more than  requests
$seuilrepert = 100; # displaying directories with more than  requests
$optdirsize = 4;    # directories traffic graphs [html pages ( 1 : total, 2 : external, 3 : local), all files ( 4 : total, 5 : external, 6 : local)]
$nbdays = 50;       # number of days to display for graphs

@extension = ('html','php');    # html pages extensions to parse, ('') mean html files have no extension
@extensionimage = ('gif','jpg','png'); # images extensions to parse


## IP/Hostname resolution/mapping
$reverse_dns = 0; # Reverse IP to hostname using DNS (slow down computing by a large amount)

# Maxmind module to extract countries from IP
$reverse_geo = 0; # Map IP to country code (need to get ONE of the following perl module installed as they do the same job)
$netgeo = 0; # Map IP to country code (Geo::IP perl module)
$geoip_free = 0; # Map IP to country code (Geo::IPfree the perl module)

# Maxmind module to locate cities from hostname
$geoipcity = 0; # Maxmind perl module to map hostname/cities from a local database (install Geo::IP perl module)
$geoipcity_file = ""; # Uncompressed cities database

# Ipv4/Ipv6 filtering
$ipv4_skip = 0;
$ipv6_skip = 0;

# CSV file to map local hostname with users
$resolv_users_file = ""; # (see /w3perl/resources/resolv_users.csv as an example)


$titlename = 0;   # convert URL to title (0 =  no)
$defaulthomepage = "index.html"; # request on '/' means request on 'index.html'

## $localdomainename is your domain name
## Take care as it will be an IP address if $localserver is set to an IP address (no way to extract domain from an IP)
$localdomainename = "w3perl.com";

## $localdomaine should match every host within your domain
## add IP adresses if your hosts don't have DNS lookup
## or/and add the constant domain string if several websites share your domain
## ^ mean 'begin with', $ mean 'end with', put \ before any dot (using regex)
## Ex : ^145.238.44.* (all hosts inside 145.238.44. are domain hosts)
## Ex : obspm.fr|^145.238.44.* (match obspm.fr domain and all hosts from class 145.238.44.)
## Ex : .obspm. (match all domain which match obspm)
$localdomaine = "w3perl.com";

$tildealias = "";   # your aliases for users HTML directory
$exclude_frame = 0; # set to 1 to exclude framed page
$exclude_robot = 0; # set to 1 to exclude robot 
$exclude_refererspam = 0; # set to 1 to exclude referer spammer 

# Spip engine used ?
$spip = 0; # set to 1 if you are using a spip engine

#########################################################################
###                         w3perl output                             ###
#########################################################################

# Choose languages output (@lang and @homepages must have the same number of elements)
# @homepages filename should be different from each other (else it will be overwritten !)
@homepages = ('index-fr','index');
@lang = ('fr','uk');

### Graphical output

$bargraph = 0;  # bar chart
$tridim = 1;    # 3d bar chart
$linegraph = 0; # line
$fillgraph = 0; # line filled

# custom colours for links and background, graphs size (now deprecated with w3perl.css)
$custom_text = "#000099";  # text color
$custom_link = "#000080";  # link color
$custom_vlink = "#800000"; # visited link color

# choose either a BGCOLOR or/and a BACKGROUND images
$bgcolor = "#FFFFFF";
$background = "";

# hide/display in menu (show = 0 / hide = 1)
$hide_other_report = 0; # Others stats reports
$hide_resources_menu = 0; # Resources menu

#########################################################################
###    Choose which scripts should be working  (1 = on, 0 = off)      ###
#########################################################################

# Scripts running
$actif_inc = 1;
$actif_real = 1;
$actif_hour = 1;
$actif_day = 1;
$actif_week = 1;
$actif_month = 1;
$actif_year = 1;
$actif_agent = 1;
$actif_refer = 1;
$actif_session = 1;
$actif_error = 0;
$actif_history = 0;
$actif_url = 0;

# Scripts option
$option_pages = "";
$option_inc = "";
$option_real = "";
$option_hour = "";
$option_day = "";
$option_week = "-g line";
$option_month = "";
$option_year = "";
$option_agent = "";
$option_refer = "";
$option_session = "-d 10";
$option_error = "";
$option_history = "";
$option_url = "";

# Scripts running every n day
$inc_launch = 1;
$hour_launch = 1;
$day_launch = 1;
$week_launch = 1;
$month_launch = 1;
$year_launch = 1;
$refer_launch = 1;
$agent_launch = 1;
$error_launch = 1;
$session_launch = 1;
$url_launch = 1;
$history_launch = 1;

# Hour when scripts are running 
$inc_launch_time = 0;
$inc_launch_time_min = 0;
$hour_launch_time = 0;
$day_launch_time = 0;
$week_launch_time = 0;
$month_launch_time = 0;
$year_launch_time = 0;
$agent_launch_time = 0;
$refer_launch_time = 0;
$error_launch_time = 0;
$session_launch_time = 0;
$url_launch_time = 0;
$history_launch_time = 0;

$week_launch_day = 1;
$month_launch_day = 1;


# cron-url.pl
$url_frame = 0;         # Framed HTML pages
$url_doublon = 0;       # HTML pages with same TITLE tag or without
$url_absolute_link = 0; # List of absolute links to avoid
$url_symbolic_link = 0; # List of symbolics link (Unix)
$url_bad_link = 1;      # List of wrong links
$url_useless = 0;       # List of HTML pages never link from
$url_directory = 1;     # Stats about directories
$url_weight = 1;        # Stats about the most heavy pages
$url_link = 1;          # Stats about number of links in HTML pages
$url_image = 1;         # Stats about number of images in HTML pages
$url_new_doc = 1;       # New HTML pages on your server
$url_cdrom = 0;         # Show HTML pages without index.html link
$url_img_tag = 0;       # Images without ALT or having bad WIDTH and HEIGHT tags
$url_tree = 1;          # Show server tree
$url_users = 0;          # Show users account


# cron-session.pl
$session_visit = 1;       # Show the number of visits by day
$session_average = 1;     # Show average load versus hour and day of the week
$session_description = 1; # List of session by hosts
$session_engine = 0;      # List of robots scanning your web
$session_reading = 0;      # Average reading time per page


# cron-pages.pl and cron-inc.pl
$inc_host = 1;      # not yet implemented
$inc_page = 1;      # not yet implemented
$inc_directory = 1; # not yet implemented
$inc_country = 1;   # not yet implemented
$inc_script = 1;    # Stats about the scripts used and the parameters used
$inc_virus = 0;    # Stats about virus
$inc_user = 1;    # Stats about user login
$inc_download_aborted = 0;    # Stats about incomplete download

#-
#----------------------------------------------------------------------------#
#-                         Stop here for configuration                      -#
#-                  You should not modify anything after this line          -#
#----------------------------------------------------------------------------#
#-

#########################################################################
###                      w3perl global variables                      ###
#########################################################################
###                                                                   ###
###           Ne pas modifier !!! - Don't modify !!!                  ###

$fileroot = $fileroot[0];
$txtext = ".txt";
$fileurl = "listeurl".$txtext;
$dirdata = "data";
$dirress = "resources";
chop($localserver) if ($localserver =~ /\/$/);
chop($localdomainename) if ($localdomainename =~ /\/$/);
chop($localdomaine) if ($localdomaine =~ /\/$/);

$mail_format = 0 if ($mail_format eq '');
$mail_format = 1 if ($struct_logfile =~ /\%module/ && $logfile_format !~ /^SFTP/i);
$ftp_format = 0 if ($ftp_format eq '');
$ftp_format = 1 if ($struct_logfile =~ /\%direction/);
$ftp_format = 1 if ($logfile_format =~ /^FTP-IIS/i || $logfile_format =~ /^SFTP/i);
$dhcp_format = 0 if ($dhcp_format eq '');
$dhcp_format = 1 if ($logfile_format =~ /^dhcp/i);
if ($ftp_format == 1 && $ftp_direction eq '') {
    $ftp_direction = "o"; 
    @extension = ('');
    @extensionimage = ('');
#    @extensionimage = ();
}
$squid_format = 0 if ($squid_format eq '');
$squid_format = 1 if ($struct_logfile =~ /\%codestatus/);
$cups_format = 0 if ($cups_format eq '');
$cups_format = 1 if ($logfile_format =~ /^Cups/i);

for ($i=0;$i<=$#extension;$i++) {
#    $extension .= "\\$carext".$extension[$i]."\$|";
    $extension .= "$carext".$extension[$i]."\$|";
}
chop($extension);

if ($ftp_format == 0 && $mail_format == 0 && $cups_format == 0 && $dhcp_format == 0) {
    if (scalar(@extension) == 0) {
    $error_msg = "You should give at least one extension to map html files\n";
    $error_msg .= "If your files have no extension, use the \'\' for empty value";
    &error_config($error_msg);
    }
    if ($extension =~ /\\\.\$/) {
       $extension =~ s/\\\.\$//;
#       $extension =~ s/^\|//;
       $nohtmlext = 1;
       }
}

### checking few things

if ($zip == 1) {
    $tmp = substr($GZIP,0,rindex($GZIP,' '));
    if (!(-x $tmp)) {
        $error_msg = "Unable to find your compression tool in $tmp\n";
        &error_config($error_msg);
    }
}

if ($FLY ne '' && $fly_set == 1) {
    if (!(-x $FLY)) {
	$error_msg = "Unable to find the graphic tool Fly/Flydraw in $FLY\n";
	&error_config($error_msg);
    }
}

if ($enable_pdf == 1) {
    $error_msg = "\$HTMLDOC should be set to your htmldoc binary path\n" if ($HTMLDOC eq '');
    $error_msg = "Unable to find the pdf tool in $HTMLDOC\n" if (!(-x $HTMLDOC));
    &error_config($error_msg) if ($error_msg ne '');
}

# Check $defaulthomepage file extension is part of @extension
#if ($ftp_format == 0 && $mail_format == 0 && $cups_format == 0) {
#    if ($defaulthomepage eq '') {
#	$error_msg = "You should give a value for \$defaulthomepage\n";
#	&error_config($error_msg);
#   }
#}

# Check Windows path
if ($type_serveur == 1) {
    $error_msg = "\$path is not a windows path\n";
    &error_config($error_msg) if (substr($path,1,1) ne ':');
    $error_msg = "\$pathinit is not a windows path\n";
    &error_config($error_msg) if (substr($pathinit,1,1) ne ':');
    $error_msg = "\$pathserver is not a windows path\n";
    &error_config($error_msg) if ($pathserver ne '' && substr($pathserver,1,1) ne ':');
    $error_msg = "\$cgipath is not a windows path\n";
    &error_config($error_msg) if ($cgipath ne '' && substr($cgipath,1,1) ne ':');
}

if ($titlename == 1 && $actif_url == 0 && $cups_format == 0 && (!(-f "$path$dirdata$dirsep$fileurl"))) {
    $error_msg = "- You should run the cron-url script in order to use URL mapping\n";
    $error_msg .= " => Set \$actif_url = 1 in your configuration file\n";
    $error_msg .= "or\n- Disable URL mapping\n => Set \$titlename = 0 in your configuration file\n";
    &error_config($error_msg);    
}

#if ($defaulthomepage eq '') {
#    $error_msg = "\$defaulthomepage must not be empty. Use the filename the server use when a request is made on a directory\n";
#    &error_config($error_msg);
#}

if ($locallog == 0 && $localonly == 1) {
    $error_msg = "You have to enable \$locallog = 1 if you choose to get only domain stats\n";
    &error_config($error_msg);
}

if ($#homepages == -1) {
    $error_msg = "You have to choose at least one language to get stats output !\n";
    &error_config($error_msg);
}

if ($#homepages != $#lang) {
    $error_msg = "\@homepages and \@lang should have the same number of items. Each language should have an output file\n";
    &error_config($error_msg);
}

if ($defaulthomepage ne '') {
    $error_msg = "One of your language should use the \$defaulthomepage\n";
    $found = 0;
    $tmp = substr($defaulthomepage,0,rindex($defaulthomepage,'.'));
    for ($nblang=0;$nblang<=$#homepages;$nblang++) {
#    $found = 1 if ("$homepages[$nblang]$htmlext" eq $defaulthomepage);
	$found = 1 if ("$homepages[$nblang]" eq $tmp);
    }
    &error_config($error_msg) if ($found == 0);
}

if ($topten < 1) {
    $error_msg = "\$topten should be different than 0\n";
    &error_config($error_msg);
}

if ($logfile_format eq '') {
    $error_msg = "No logfile format have been defined ! Choose one !\n";
    &error_config($error_msg);
}

if ($resolv_users_file ne '') {
    $resolv_users_file = $pathinit.$dirress.$dirsep.$resolv_users_file if ($resolv_users_file !~ /$dirsep/);
    $error_msg = "Unable to find $resolv_users_file\n";
    &error_config($error_msg) if (!(-f $resolv_users_file));
}

#if (index($path,$linkpath_url) == -1) {
#    $error_msg = "\$linkpath_url should be extracted from the last part of \$path\n";
#    &error_config($error_msg);
#}

if ($server eq $localdomainename && $server !~ /[0-9]+$/) {
    $error_msg = "\$localdomainename should be different from \$server\n\$server should be a hostname, \$localdomainename is the domain which belongs to \$server\n";
    &error_config($error_msg);
}

if ($linkpath_url eq $path) {
    $error_msg = "\$linkpath_url should be different from \$path\n\$path is your output path, \$linkpath_url is the output URL\n";
    &error_config($error_msg);
}

if ($zipcut == 2 && $struct_compressed_logfile !~ /\%day/) {
    $error_msg .= "Your log filename should contains at least the day if you select daily logfiles\n";
    &error_config($error_msg);    
}

# Slash

if ($linkpath_url !~ /\/$/) {
    $error_msg = "Missing trailing slash for \$linkpath_url\n";
    &error_config($error_msg);
}

if ($linkpath_url !~ /^\//) {
    $error_msg = "Missing leading slash for \$linkpath_url\n";
    &error_config($error_msg);
}

if ($path !~ /\/$/) {
    $error_msg = "Missing trailing slash for \$path\n";
    &error_config($error_msg);
}

if ($pathinit !~ /\/$/) {
    $error_msg = "Missing trailing slash for \$pathinit\n";
    &error_config($error_msg);
}

if ($pathserver ne '' && $pathserver !~ /\/$/) {
    $error_msg = "Missing trailing slash for \$pathserver\n";
    &error_config($error_msg);
}

if ($cgipath !~ /\/$/ && $cgipath ne '') {
    $error_msg = "Missing trailing slash for \$cgipath\n";
    &error_config($error_msg);
}

if ($fileroot !~ /\/$/) {
    $error_msg = "Missing trailing slash for \$fileroot\n";
    &error_config($error_msg);
}

#

if ($geoipcity == 1) {
    if ($geoipcity_file eq '') {
	$error_msg = "\$geoipcity_file should be filled with the location of the geoipcity uncompressed database file\n";
	&error_config($error_msg);
    }
    if ($netgeo == 0) {
	$error_msg = "Geo::IP perl module should be installed in order to get cities reports (set \$netgeo to 1)\n";
	&error_config($error_msg);
    }
}

if ($reverse_geo == 1 && $netgeo == 1 && $geoip_free == 1) {
	$error_msg = "You can\'t use both Geo::IP and GeoIPfree as they do the same job. Choose one of them\n";
	&error_config($error_msg);
}

if ($enable_email == 1) {
    $error_msg = "No mail server defined !\n";
    &error_config($error_msg) if ($email_smtp eq '');
    $error_msg = "Email address not defined !\n";
    &error_config($error_msg) if ($emailadr eq '');
    $error_msg = "Need a literal domain name, not an IP\n";
    &error_config($error_msg) if ($localserver =~ /\d$/ || $localdomainename =~ /\d$/);

    if ($email_html == 0 && $email_pdf == 0) {
	$error_msg = "Choose to send html or/and PDF report\n";
	&error_config($error_msg);
    }

    $err = &check_email_adr($emailadr);

    if ($emailadr_cc ne '') {
	@tmp = split(/,\s+/,$emailadr_cc);
	# check email

	for ($i=0;$i<$#tmp;$i++) {
	    $email = $tmp[$i];
	    $err = &check_email_adr($email);
	    last if ($err != 0);
	}
    }

    $error_msg = "Invalid character found in email address : $email\n" if ($err == 1);
    $error_msg = "Invalid email address ; $email\n" if ($err == 2);
    &error_config($error_msg) if ($err != 0);	

}

if ($localdomaine eq '') {
    if ($localserver =~ /\d$/) {
	$localdomaine = $localserver;
    } else {
	if (split(/[.]/,$localserver) <= 2) {
	    $localdomaine = $localserver;
	} else {
	    $localdomaine = substr($localserver,(index($localserver,'.')+1));
	}
    }
    print "WARNING : \$localdomaine is not set. Assuming value $localserver\n";
}

if ($retrieve_external_log == 1) {
    if (!(-x $wget_bin)) {
	$error_msg = "Unable to find the wget tool $wget_bin\n";
	&error_config($error_msg);
    }
    $wget_pass =~ s/(\W)/\\$1/g;
#    $wget_login =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg if ($wget_login ne '');
#    $wget_pass =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg if ($wget_pass ne '');

    if ($remote_host_log eq '') {
        $error_msg = "No remote host specified to retrieve remote logfiles\n";
	&error_config($error_msg);
    }
    $error_msg = "Specify protocol http:// or ftp:// for remote host\n";
    &error_exit($error_msg) if ($remote_host_log !~ /\:\/\//);
    $error_msg = "Remote host directory should start from your remote server root\n";
    &error_exit($error_msg) if ($remote_host_dir !~ /^\//);
    $error_msg = "Remote host directory should end with a trailing slash\n";
    &error_exit($error_msg) if ($remote_host_dir !~ /\/$/);
}


if ($#nolog != -1) {
    for ($i=0;$i<=$#nolog;$i++) {
	if ($nolog[$i] eq $localdomainename && $locallog == 0) {
	    print "WARNING : Duplicate option to exclude local domain. Removing entry in \@nolog\n";
	    splice(@nolog,$i,1);
	}
	if ($nolog[$i] eq $localdomainename && $locallog == 1) {
	    $error_msg = "You cannot exclude local domain hosts in \@nolog and select local domain report (\$locallog)\n";
	    &error_config($error_msg);
	}
    }
}

if ($struct_logfile !~ /\%/ && $struct_logfile ne '') {
    $error_msg = "No valid fields found in \$struct_logfile.\nIt should be used to describe your logfile format.\n";
    &error_config($error_msg);
}

if ($pathserver =~ /^https?:\/\//) {
    $error_msg = "\$pathserver is a path, not an URL\n";
    &error_config($error_msg);
}

if ($pathserver eq '') {
    $option_url = "";
    $actif_url = 0;
}

if ($logfile_format =~ /^Cups/i) {
    $error_msg = "You should specify a printer name in \$printer_default\n";
    &error_config($error_msg) if ($printer_server ne '' && $printer_default eq '');
    $error_msg = "You should specify a CUPS servername in \$printer_server\n";
    &error_config($error_msg) if ($printer_server eq '' && $printer_default ne '');
}

#if ($prefixlog eq '') {
#    $error_msg = "\$prefixlog should not be empty. The is is the constant part of your log filename\n";
#    &error_config($error_msg);
#}

###

sub error_config {
    local($msg) = @_;

    if ($ENV{'REQUEST_METHOD'} eq "GET" || $ENV{'REQUEST_METHOD'} eq "POST") {
	print "Content-type: text/html\n\n";
       # Adding CSS style
        print "<style type=\"text/css\">\n";
        print "body {\n";
        print "background-color: #FCFCFC;\n";
        print "color: #202020;\n";
        print "margin: 10px;\n";
        print "font-family: Verdana, Geneva, Arial, sans-serif;\n";
        print "font-size: small;\n";
        print "}\n";
        print "ul {\n";
        print "list-style-type: square;\n";
        print "padding:0px 5px;\n";
        print "}\n";
        print ".error_exit {\n";
        print "color: #FF0000;\n";
        print "font-weight: bold;\n";
        print "}\n";
        print ".launch_title {\n";
        print "color: #30A030;\n";
        print "font-weight: bold;\n";
        print "}\n";
        print "</style>\n";
    }
    print "ERROR in configuration file\n";
    print "<DIV CLASS=\"error_exit\">" if ($ENV{'REQUEST_METHOD'} eq "GET");
    $msg =~ s/\n/\<BR\>/g if ($ENV{'REQUEST_METHOD'} eq "GET");
    print "$msg\n\n";
    print "</DIV>" if ($ENV{'REQUEST_METHOD'} eq "GET");
    print "<P>" if ($ENV{'REQUEST_METHOD'} eq "GET");
    exit(1);
}

$reverse_geo = 1 if ($netgeo == 1 || $geoip_free == 1);
$reverse_geo = 1 if ($geoipcity == 1);

### resources and documentation directories (don't modify)


$dirdocs = "docs".$dirsepurl."uk";
$dirdocs_fr = "docs".$dirsepurl."fr";
$htmldoc_option = "-t pdf --continuous --quiet --no-links --title --textfont helvetica --left 16 --bottom 8 --top 8 --browserwidth 800 --headfootsize 8.0 --fontsize 7.0 --header xtx --footer xd/";

# fly graphic size

if ($fly_set == 1) {
    $xmax = 450;               # x graph size
    $ymax = 250;               # y graph size
    $grad = 10;                # number of graduation
    $xstep = int($xmax/$grad); # x step for graph
    $ystep = int($ymax/$grad); # y step for graph
    $xdecal = 75;              # x scale width
    $xdecalm = $xdecal - 5;    # length of x scale
    $ydecal = 40;              # y scale width
    $ydecalm = $ydecal - 15;   # length of y scale
}

# bubble help size

$help_width = 400;
$popup_graph_width = 120;

### Using default value if $struct_compressed_logfile empty

if ($struct_logfile eq '') {
    if ($logfile_format eq '') {
	$error_msg =  "Can't guess logfile format if you don't specify which kind of logfile you are using !\n";
	&error_config($error_msg);
    }
    $struct_logfile = "%host %null %login %date %hourshift %method %page %protocol %status %requetesize" if ($logfile_format =~ /^CLF/i);
    $struct_logfile = "%host %null %login %date %hourshift %method %page %protocol %status %requetesize %referer %agent" if ($logfile_format =~ /^ECLF/i);
    $struct_logfile = "%host %null %login %date %hourshift %virtualhost %method %page %protocol %status %requetesize %referer %agent" if ($logfile_format =~ /^NECLF/i);
    $struct_logfile = "%host %login %date %time %null %null %null %elapsed %null %requetesize %status %null %method %page %query" if ($logfile_format =~ /^IIS/i);
    $struct_logfile = "%time %host %method %page %status" if ($logfile_format =~ /^W3C/i);
    $struct_logfile = "%host %null %login %date %hourshift %method %page %protocol %status %requetesize %agent" if ($logfile_format =~ /^RealServer/i);
    $struct_logfile = "%date %transfert_time %host %requetesize %page %null %null %direction %null %login %method %null %null %status" if ($logfile_format =~ /^FTP/i);
    $struct_logfile = "%date %elapsed %host %codestatus %requetesize %method %page %login %peerstatus %mimetype" if ($logfile_format =~ /^Squid-Native/i);
    $struct_logfile = "%host %null %login %date %hourshift %method %page %protocol %status %requetesize %codestatus" if ($FORM{'logfile'} =~ /^Squid-Common/i);
    $struct_logfile = "%host %null %login %date %hourshift %method %page %protocol %status %requetesize %referer %agent %codestatus" if ($FORM{'logfile'} =~ /^Squid-ECLF/i);
    $struct_logfile = "%date %null %module %id %message" if ($logfile_format =~ /^Mail/i);
    $struct_logfile = "%time %host %login %method %page %query %status %null" if ($logfile_format =~ /^FTP-IIS/i);
    $struct_logfile = "%virtualhost %login %page %date %hourshift %null %occurence %null %host %title %null %null" if ($logfile_format =~ /^Cups/i);
    $struct_logfile = "%date %time %id %module %message" if ($logfile_format =~  /^Exim/i);
    $struct_logfile = "%date %null %module %id %message" if ($logfile_format =~ /^Syslog/i);
    $struct_logfile = "%date %null %module %message" if ($logfile_format =~ /^sftp/i);
    $struct_logfile = "%date %virtualhost %null %method %null %host %null %page" if ($logfile_format =~ /^Dhcp/i);
}

### Load balancing

$mode_lb = 0;
if ($#fileroot > 0) {
    $mode_lb = 1;
}


if ($mode_lb == 1) {
    $zipcut = 0;
    $zip = 0;
    $struct_compressed_logfile = "%prefixlog";
}

### Compressed logfiles
$struct_compressed_logfile =~ s/\s+$//;
@compressed_logfile_fields = split(/[^a-zA-Z0-9]+/,$struct_compressed_logfile);
@compressed_sep_fields = split(/\%[a-zA-Z0-9]+/,$struct_compressed_logfile); 

$prefixlog_found = 0;
for ($i=1;$i<=$#compressed_logfile_fields;$i++) {
    $prefixlog_found = 1 if ($compressed_logfile_fields[$i] eq "prefixlog");
    $compressed_logfile_fields[$i] = "%".$compressed_logfile_fields[$i];
    $fields_compressed_logfile{$compressed_logfile_fields[$i]} = $i;
}

if ($prefixlog_found == 0 && $prefixlog ne '') {
    $error_msg = "\$struct_compressed_logfile does not contains \%prefixlog which is the constant core of your logfilename\n";
    &error_config($error_msg);
}

### Windows or Unix logfile

if ($tri !~ /\// && $mail_format == 0) {
    $error_msg .= "\$tri should be at least '/'\n";
    &error_config($error_msg);
}

# load the logfile format for parsing

@logfile_fields = split(/\s+/,$struct_logfile);
$login_seen = 0;
$agent_seen = 0;
for ($i=0;$i<=$#logfile_fields;$i++) {
    $fields_logfile{$logfile_fields[$i]} = $i;
    $fields_logfile{$logfile_fields[$i]} += 4 if ($ftp_format == 1 && $logfile_format !~ /FTP-IIS/i && $logfile_format !~ /SFTP/i);
    $fields_logfile{$logfile_fields[$i]} += 2 if ($ftp_format == 1 && $logfile_format =~ /SFTP/i);
    $fields_logfile{$logfile_fields[$i]} += 2 if ($logfile_format =~ /^Dhcp-ISC/i);
    $fields_logfile{$logfile_fields[$i]} += 2 if ($mail_format == 1 && $logfile_format !~ /exim/i && $logfile_format !~ /mdaemon/i);
#    $fields_logfile{$logfile_fields[$i]} += 2 if ($mail_format == 1);
    $login_seen = 1 if ($logfile_fields[$i] eq '%login');
    $agent_seen = 1 if ($logfile_fields[$i] eq '%agent');
    $fields_after_login{$logfile_fields[$i]} = 0;
    $fields_after_login{$logfile_fields[$i]} = 1 if ($login_seen == 1);
    $fields_after_agent{$logfile_fields[$i]} = 0;
    $fields_after_agent{$logfile_fields[$i]} = 1 if ($agent_seen == 1);
}

# Check if requetesize is there

if ($squid_format == 1 || $mail_format == 1 || $ftp_format == 1 || $mail_format == 1 || $cups_format == 1) {   
    $inc_download_aborted = 0;
}

$notrafic = 0;
if ($struct_logfile !~ /\%requetesize/ && $mail_format == 0) {
    $notrafic = 1;
}

# checking if IIS logfile format have been selected

# Common (Unix)
$iis_format = 0;
$logfile_sep = "[ ]+";
#$logfile_sep = "[ ]+" if ($squid_format == 1);

if ($squid_format == 0 && $ftp_format == 0 && $mail_format == 0 && $cups_format == 0) { 
    if ($prefixlog eq "ex" || $prefixlog eq "in") {
#    if ($prefixlog eq "ex" || $prefixlog eq "in" || $logfile_fields[0] eq "%date" || ($logfile_fields[4] eq "%null" && $logfile_fields[5] eq "%null")) {

	# IIS (Windows)
	$iis_format = 1;
	$type_serveur = 1;
#	@date_string = split(/%/,$date_format);
	
	$logfile_sep = ", " if ($logfile_fields[1] eq "%login"); # Standard
	$logfile_sep = " " if ($logfile_fields[1] ne "%login");	
    }
}


if ($mail_format == 1 && $logfile_format eq "Mdaemon") {
#    @date_string = split(/%/,$date_format);
    $logfile_sep = "\\s+";
}

if ($logfile_format eq "Webstar") {
    $logfile_sep = "\\t";
}


$logfile_sep = $logfile_sep_own if ($logfile_sep_own ne '');

if ($struct_logfile =~ /\%time/) {
    if ($date_format eq '') { 
	$error_msg = "You should specify the date format used in your logfile\n";
	&error_config($error_msg);
    }
    $date_format =~ s/[\-\/ ]//g; # remove extra chracters if found 
    @date_string = split(/%/,$date_format); 
}


if ($struct_logfile =~ /\%hour[^s]/) {
    $error_msg = "\%hour field is deprecated. Use \%time instead\n";
    &error_config($error_msg);
}


if ($type_serveur == 1) {
    for ($i=0;$i<=($#selecrepert);$i++) {
	$selecrepert[$i] =~ tr/[A-Z]/[a-z]/;
    }
}


# checking extended or common logfile format

if ($struct_logfile =~ /\%referer/) {
    $referlog = $prefixlog; # for extented common logfile (NCSA > 1.4)
}

if ($struct_logfile =~ /\%agent/) {
    $agentlog = $prefixlog; # for extented common logfile (NCSA > 1.4)
}

### yellow pages

#if ($yellowfile ne '') {
#open(NIS,"$yellowfile") || die "Error, unable to open $yellowfile\n";
#while (<NIS>) {
#($a,$b) = split;
#$nis{$a} = $b;
#}
#close(NIS);
#}

### init

$exclude_refererspam = 0 if ($struct_logfile !~ /\%refer/);

#$maxlengthos = 10; # Maximum length filename (Mac = 31) 

# find the name of the package

$tmp = $path;
chop($tmp);
$pos = rindex($tmp,$dirsep);
$packagename = substr($tmp,$pos+1,length($tmp));

# global variables

$localdomaine =~ tr/A-Z/a-z/;
$stop = 0;
$nbdays = 1 if ($nbdays < 0);
#$nbdays = 366 if ($nbdays == 0 || $nbdays > 366);

$pi = atan2(1,1)*4;
$piradiant = $pi/180;
$halfxmax = $xmax/2;
$halfymax = $ymax/2;
$diam = $ymax;
$rayon = $diam/2;

# homepages filename

#@homepagesframed = ('');
for ($nblang=0;$nblang<=$#homepages;$nblang++) {
#        $homepagesframed[$nblang] = $homepages[$nblang]."2".$htmlext;
        $homepages[$nblang] = $homepages[$nblang].$htmlext;
}

# graphic flags

for ($nblang=0;$nblang<=$#homepages;$nblang++) {
$flag[$nblang] = "flag-".$lang[$nblang].$gifext;
}

# graphic option

@graphic = ('bar','3d','line','fill'); 
$bargraph = 1 if ($tridim == 1);
$linegraph = 1 if ($fillgraph == 1);
$perspec = 5; # perpective view for 3D graph

### output directories
### Sous NT, on calcule $linkpathinit 
### Sous Unix, $linkpathinit = '' car lien symbolique

$linkpathinit = '';

if ($type_serveur == 1) {
    if ($pathinit =~ /\/(\w+)\/$tildealias/ && $tildealias ne '') {
         $tmp2 = $1;
         $tmp3 = $';
    } else {
    $pos = rindex(lc($path),$dirsep);
    $pos2 = index(lc($pathinit),lc($path));
    $tmp = lc($path);
    while ($pos2 == -1) {
	$pos = rindex($tmp,$dirsep,$pos-1);
	$tmp = substr($tmp,$dirsep,$pos+1);
	$linkpathinit .= "..$dirsepurl";
	$pos2 = index(lc($pathinit),$tmp);
    }
    $b = substr(lc($pathinit),$pos+1);
    if ("$dirsep$b" eq $pathinit) {
     $linkpathinit = '';
    } else {
       chop($b);
       $linkpathinit .= "$b$dirsepurl" if ($b ne '');
    }
    }

    if ($linkpathinit =~ /$tildealias/ && $tildealias ne '') {

    $pos = rindex($path,$dirsep);
    $pos2 = index($pathserver,$path);
    $tmp = $path;
    while ($pos2 == -1) {
	$pos = rindex($tmp,$dirsep,$pos-1);
	$tmp = substr($tmp,$dirsep,$pos+1);
	$linkpathinit .= "..$dirsepurl";
	$pos2 = index($pathserver,$tmp);
    }

    $linkpathinit .= "~$tmp2$tmp3";
    }
}

$homeicons = $linkpathinit;
$homeicons .= $dirress.$dirsepurl."homepage";

$backgrd = "BGCOLOR=\"$bgcolor\"";         # color background
$backgrd .= " BACKGROUND=\"$background\"" if ($background ne '');


## building string match for speed

$nolog = '';
for ($i=0;$i<=$#nolog;$i++) {
    $nolog .= $nolog[$i]."|";
    }
chop($nolog);

$selection = '';
for ($i=0;$i<=$#selection;$i++) {

    $fileext = &Extract_Filetype($selection[$i]);
     if ($fileext =~ /$extension/ && $precision > 3) {
        print "WARNING : $selection[$i] have been removed from \@selection as it already match $extension\n";
     } else {
         $selection .= $selection[$i]."|";
     }
    }
chop($selection);



if ($precision > 3) {
    if ($squid_format == 1 || $mail_format == 1 || $ftp_format == 1 || $mail_format == 1 || $cups_format == 1) {
        if ($selection ne '') {
           print "WARNING : \@selection is disabled as you choose the highest level of precision\n";
           @selection = ();
        }
    }
}



$excluderepert = '';
for ($i=0;$i<=$#excluderepert;$i++) {
    $excluderepert[$i] =~ s/\/\//\//g;
    if ($excluderepert[$i] !~ /^\//) {
       $excluderepert .= '/';
    } 
    $excluderepert .= $excluderepert[$i];
    if ($excluderepert[$i] !~ /\/$/) {
       $excluderepert .= '/';
       $excluderepert[$i] = $excluderepert[$i]."/";
    }
    $excluderepert .= "|";
    }
chop($excluderepert);

$excludestatuscode = '';
for ($i=0;$i<=$#excludestatuscode;$i++) {
    if ($excludestatuscode[$i] < 400 && $excludestatuscode[$i] != 301 && $excludestatuscode[$i] != 302 && $excludestatuscode[$i] != 304) {
    $excludestatuscode .= $excludestatuscode[$i]."|";
}
    }
chop($excludestatuscode);

$rss_file = '';
for ($i=0;$i<=$#rss_file;$i++) {
    $rss_file .= "^".$rss_file[$i]."|";
    }
chop($rss_file);


$excludevirtual = '';
for ($i=0;$i<=$#excludevirtual;$i++) {
    $excludevirtual .= $excludevirtual[$i]."|";
    }
chop($excludevirtual);

1;
