1#!/usr/local/bin/perl -w
2#------------------------------------------------------------------------------
3# Free addition to AWStats Web Log Analyzer. Used to export the contents of
4# sections of the Apache server log database to CSV for use in other tools.
5# Works from command line or as a CGI.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#------------------------------------------------------------------------------
20use CGI qw(:standard);
21
22my $ALLOWDOWNLOAD=0;
23
24# Disabled by default for security reason
25if (! $ALLOWDOWNLOAD)
26{
27	print("Error: You must first edit script to change ALLOWDOWNLOAD to 1 to allow usage of this script.\n");
28	print("Reason is that enabling this script may be a security hole as it allows someone to download/view details of your awstats data files.\n");
29	exit;
30}
31
32my $q               = new CGI;
33my $outputFile      = "";   # used to write the output to a file
34my $inputFile       = "";   # the fully qualified path to the input log database file
35my $sectionToReport = "";   # contains the tag to search for in the database file
36my $startSearchStr  = "BEGIN_";
37my $endSearchStr    = "END_";
38my $startPrinting   = 0;    # flag to indicate that the start tag has been found
39my $attachFileName  = "";
40
41# These parameters are used to build the input file name of the awstats log database
42my $baseName        = "";
43my $month           = "";
44my $year            = "";
45my $day             = "";
46my $siteConfig      = "";
47
48if ($q->param("outputFile")) {
49  if ($outputFile eq '') { $outputFile = $q->param("outputFile"); }
50}
51
52if ($q->param("inputFile")) {
53  if ($inputFile eq '') { $inputFile = $q->param("inputFile"); }
54}
55
56if ($q->param("section")) {
57  if ($sectionToReport eq '' ) { $sectionToReport = $q->param("section"); }
58}
59
60if ($q->param("baseName")) {
61  if ($baseName eq '' ) { $baseName = $q->param("baseName"); }
62}
63
64if ($q->param("month")) {
65  if ($month eq '' ) { $month = $q->param("month"); }
66}
67
68if ($q->param("year")) {
69  if ($year eq '' ) { $year = $q->param("year"); }
70}
71
72if ($q->param("day")) { $day = $q->param("day"); }
73
74if ($q->param("siteConfig")) {
75  if ($siteConfig eq '' ) { $siteConfig = $q->param("siteConfig"); }
76}
77
78# set the attachment file name to the report section
79if ($sectionToReport ne '' ) {
80  $attachFileName = $sectionToReport . ".csv";
81} else {
82  $attachFileName = "exportCSV.csv";
83}
84print $q->header(-type=> "application/force-download", -attachment=>$attachFileName);
85
86# Build the start/end search tags
87$startSearchStr = $startSearchStr . $sectionToReport;
88$endSearchStr   = $endSearchStr . $sectionToReport;
89
90if ( !$inputFile ) { $inputFile ="$baseName$month$year$day.$siteConfig.txt" };
91
92open (IN, $inputFile) || die "cannot open $inputFile\n";
93
94# If there's a parameter for the output, open it here
95if ($outputFile ne '') {
96  open (OUT,">$outputFile") || die "cannot create $outputFile\n";
97  flock (OUT, 2);
98}
99# Loop through the input file searching for the start string. When
100# found, start displaying the input lines (with spaces changed
101# to commas) until the end tag is found.
102
103# Array to store comments for printing once we hit the desired section
104my $commentCount = -1;
105my %commentArray;
106
107while (<IN>) {
108  chomp;
109
110  if (/^#\s(.*-)\s/){    # search for comment lines
111    s/ - /,/g;   # replace dashes with commas
112    s/#//;       # get rid of the comment sign
113    $commentArray[++$commentCount] = $_;
114  }
115
116  # put the test to end printing here to eliminate printing
117  # the line with the END tag
118  if (/^$endSearchStr\b/) {
119    $startPrinting = 0;
120  }
121
122  if ($startPrinting) {
123    s/ /,/g;
124    print "$_\n";
125    if ($outputFile ne '') {
126      print OUT "$_\n";
127    }
128  }
129  # if we find an END tag and we haven't started printing, reset the
130  # comment array to start re-capturing comments for next section
131  if ((/^END_/) && ($startPrinting == 0)) {
132    $commentCount = -1;
133  }
134
135  # put the start printing test after the first input line
136  # to eliminate printing the line with the BEGIN tag...find it
137  # here, then start printing on the next input line
138  if (/^$startSearchStr\b/) {
139    $startPrinting = 1;
140    # print the comment array - it provides labels for the columns
141    for ($i = 0; $i <= $commentCount; $i++ ) {
142    print "$commentArray[$i]\n";
143    }
144  }
145}
146
147close(IN);
148
149# Close the output file if there was one used
150if ($outputFile ne '') {
151  close(OUT);
152}
153