1 rizwank 1.1 #!/usr/bin/perl
2 #------------------------------------------------------------------------------
3 # Launch awstats with -staticlinks option to build all static pages.
4 # See COPYING.TXT file about AWStats GNU General Public License.
5 #------------------------------------------------------------------------------
6 # $Revision: 1.30 $ - $Author: eldy $ - $Date: 2004/01/25 15:19:47 $
7
8 #$|=1;
9 #use warnings; # Must be used in test mode only. This reduce a little process speed
10 #use diagnostics; # Must be used in test mode only. This reduce a lot of process speed
11 use strict;no strict "refs";
12 use Time::Local; # use Time::Local 'timelocal_nocheck' is faster but not supported by all Time::Local modules
13
14
15 #------------------------------------------------------------------------------
16 # Defines
17 #------------------------------------------------------------------------------
18 my $REVISION='$Revision: 1.30 $'; $REVISION =~ /\s(.*)\s/; $REVISION=$1;
19 my $VERSION="1.2 (build $REVISION)";
20
21 # ---------- Init variables --------
22 rizwank 1.1 my $Debug=0;
23 my $DIR;
24 my $PROG;
25 my $Extension;
26 my $SiteConfig;
27 my $Update=0;
28 my $BuildPDF=0;
29 my $BuildDate=0;
30 my $Lang;
31 my $YearRequired;
32 my $MonthRequired;
33 my $Awstats='awstats.pl';
34 my $AwstatsDir='';
35 my $HtmlDoc='htmldoc'; # ghtmldoc.exe
36 my $StaticExt='html';
37 my $DirIcons='';
38 my $DirConfig='';
39 my $OutputDir='';
40 my $OutputSuffix;
41 my $OutputFile;
42 my @pages=();
43 rizwank 1.1 my @OutputList=();
44 my $FileConfig;
45 my $FileSuffix;
46 my $SiteConfig;
47 use vars qw/
48 $ShowAuthenticatedUsers $ShowFileSizesStats $ShowScreenSizeStats $ShowSMTPErrorsStats
49 $ShowEMailSenders $ShowEMailReceivers $ShowWormsStats $ShowClusterStats
50 $ShowMenu $ShowMonthStats $ShowDaysOfMonthStats $ShowDaysOfWeekStats
51 $ShowHoursStats $ShowDomainsStats $ShowHostsStats
52 $ShowRobotsStats $ShowSessionsStats $ShowPagesStats $ShowFileTypesStats
53 $ShowOSStats $ShowBrowsersStats $ShowOriginStats
54 $ShowKeyphrasesStats $ShowKeywordsStats $ShowMiscStats $ShowHTTPErrorsStats
55 /;
56 # ----- Time vars -----
57 use vars qw/
58 $starttime
59 $nowtime $tomorrowtime
60 $nowweekofmonth $nowweekofyear $nowdaymod $nowsmallyear
61 $nowsec $nowmin $nowhour $nowday $nowmonth $nowyear $nowwday $nowyday $nowns
62 /;
63
64 rizwank 1.1
65 #------------------------------------------------------------------------------
66 # Functions
67 #------------------------------------------------------------------------------
68
69 #------------------------------------------------------------------------------
70 # Function: Write error message and exit
71 # Parameters: $message
72 # Input: None
73 # Output: None
74 # Return: None
75 #------------------------------------------------------------------------------
76 sub error {
77 print "Error: $_[0].\n";
78 exit 1;
79 }
80
81 #------------------------------------------------------------------------------
82 # Function: Write a warning message
83 # Parameters: $message
84 # Input: $WarningMessage %HTMLOutput
85 rizwank 1.1 # Output: None
86 # Return: None
87 #------------------------------------------------------------------------------
88 sub warning {
89 my $messagestring=shift;
90 debug("$messagestring",1);
91 # if ($WarningMessages) {
92 # if ($HTMLOutput) {
93 # $messagestring =~ s/\n/\<br\>/g;
94 # print "$messagestring<br>\n";
95 # }
96 # else {
97 print "$messagestring\n";
98 # }
99 # }
100 }
101
102 #------------------------------------------------------------------------------
103 # Function: Write debug message and exit
104 # Parameters: $string $level
105 # Input: %HTMLOutput $Debug=required level $DEBUGFORCED=required level forced
106 rizwank 1.1 # Output: None
107 # Return: None
108 #------------------------------------------------------------------------------
109 sub debug {
110 my $level = $_[1] || 1;
111 if ($Debug >= $level) {
112 my $debugstring = $_[0];
113 if ($ENV{"GATEWAY_INTERFACE"}) { $debugstring =~ s/^ /   /; $debugstring .= "<br>"; }
114 print localtime(time)." - DEBUG $level - $debugstring\n";
115 }
116 }
117
118 #------------------------------------------------------------------------------
119 # Function: Read config file
120 # Parameters: None or configdir to scan
121 # Input: $DIR $PROG $SiteConfig
122 # Output: Global variables
123 # Return: -
124 #------------------------------------------------------------------------------
125 sub Read_Config {
126 # Check config file in common possible directories :
127 rizwank 1.1 # Windows : "$DIR" (same dir than awstats.pl)
128 # Standard, Mandrake and Debian package : "/etc/awstats"
129 # Other possible directories : "/usr/local/etc/awstats", "/etc"
130 # FHS standard, Suse package : "/etc/opt/awstats"
131 my $configdir=shift;
132 my @PossibleConfigDir=();
133
134 if ($configdir) { @PossibleConfigDir=("$configdir"); }
135 else { @PossibleConfigDir=("$AwstatsDir","$DIR","/etc/awstats","/usr/local/etc/awstats","/etc","/etc/opt/awstats"); }
136
137 # Open config file
138 $FileConfig=$FileSuffix='';
139 foreach my $dir (@PossibleConfigDir) {
140 my $searchdir=$dir;
141 if ($searchdir && $searchdir !~ /[\\\/]$/) { $searchdir .= "/"; }
142 if (open(CONFIG,"${searchdir}awstats.$SiteConfig.conf")) { $FileConfig="${searchdir}awstats.$SiteConfig.conf"; $FileSuffix=".$SiteConfig"; last; }
143 if (open(CONFIG,"${searchdir}awstats.conf")) { $FileConfig="${searchdir}awstats.conf"; $FileSuffix=''; last; }
144 }
145 if (! $FileConfig) { error("Couldn't open config file \"awstats.$SiteConfig.conf\" nor \"awstats.conf\" after searching in path \"".join(',',@PossibleConfigDir)."\": $!"); }
146
147 # Analyze config file content and close it
148 rizwank 1.1 &Parse_Config( *CONFIG , 1 , $FileConfig);
149 close CONFIG;
150 }
151
152 #------------------------------------------------------------------------------
153 # Function: Parse content of a config file
154 # Parameters: opened file handle, depth level, file name
155 # Input: -
156 # Output: Global variables
157 # Return: -
158 #------------------------------------------------------------------------------
159 sub Parse_Config {
160 my ( $confighandle ) = $_[0];
161 my $level = $_[1];
162 my $configFile = $_[2];
163 my $versionnum=0;
164 my $conflinenb=0;
165
166 if ($level > 10) { error("$PROG can't read down more than 10 level of includes. Check that no 'included' config files include their parent config file (this cause infinite loop)."); }
167
168 while (<$confighandle>) {
169 rizwank 1.1 chomp $_; s/\r//;
170 $conflinenb++;
171
172 # Extract version from first line
173 if (! $versionnum && $_ =~ /^# AWSTATS CONFIGURE FILE (\d+).(\d+)/i) {
174 $versionnum=($1*1000)+$2;
175 #if ($Debug) { debug(" Configure file version is $versionnum",1); }
176 next;
177 }
178
179 if ($_ =~ /^\s*$/) { next; }
180
181 # Check includes
182 if ($_ =~ /^Include "([^\"]+)"/ || $_ =~ /^#include "([^\"]+)"/) { # #include kept for backward compatibility
183 my $includeFile = $1;
184 if ($Debug) { debug("Found an include : $includeFile",2); }
185 if ( $includeFile !~ /^[\\\/]/ ) {
186 # Correct relative include files
187 if ($FileConfig =~ /^(.*[\\\/])[^\\\/]*$/) { $includeFile = "$1$includeFile"; }
188 }
189 if ($level > 1) {
190 rizwank 1.1 warning("Warning: Perl versions before 5.6 cannot handle nested includes");
191 next;
192 }
193 if ( open( CONFIG_INCLUDE, $includeFile ) ) {
194 &Parse_Config( *CONFIG_INCLUDE , $level+1, $includeFile);
195 close( CONFIG_INCLUDE );
196 }
197 else {
198 error("Could not open include file: $includeFile" );
199 }
200 next;
201 }
202
203 # Remove comments
204 if ($_ =~ /^\s*#/) { next; }
205 $_ =~ s/\s#.*$//;
206
207 # Extract param and value
208 my ($param,$value)=split(/=/,$_,2);
209 $param =~ s/^\s+//; $param =~ s/\s+$//;
210
211 rizwank 1.1 # If not a param=value, try with next line
212 if (! $param) { warning("Warning: Syntax error line $conflinenb in file '$configFile'. Config line is ignored."); next; }
213 if (! defined $value) { warning("Warning: Syntax error line $conflinenb in file '$configFile'. Config line is ignored."); next; }
214
215 if ($value) {
216 $value =~ s/^\s+//; $value =~ s/\s+$//;
217 $value =~ s/^\"//; $value =~ s/\";?$//;
218 # Replace __MONENV__ with value of environnement variable MONENV
219 # Must be able to replace __VAR_1____VAR_2__
220 while ($value =~ /__([^\s_]+(?:_[^\s_]+)*)__/) { my $var=$1; $value =~ s/__${var}__/$ENV{$var}/g; }
221 }
222
223 # If parameters was not found previously, defined variable with name of param to value
224 $$param=$value;
225 }
226
227 if ($Debug) { debug("Config file read was \"$configFile\" (level $level)"); }
228 }
229
230
231
232 rizwank 1.1
233 #------------------------------------------------------------------------------
234 # MAIN
235 #------------------------------------------------------------------------------
236 ($DIR=$0) =~ s/([^\/\\]*)$//; ($PROG=$1) =~ s/\.([^\.]*)$//; $Extension=$1;
237
238 my $QueryString=''; for (0..@ARGV-1) { $QueryString .= "$ARGV[$_]&"; }
239
240 if ($QueryString =~ /(^|-|&)month=(year)/i) { error("month=year is a deprecated option. Use month=all instead."); }
241
242 if ($QueryString =~ /(^|-|&)debug=(\d+)/i) { $Debug=$2; }
243 if ($QueryString =~ /(^|-|&)configdir=([^&]+)/i) { $DirConfig="$2"; }
244 if ($QueryString =~ /(^|-|&)config=([^&]+)/i) { $SiteConfig="$2"; }
245 if ($QueryString =~ /(^|-|&)awstatsprog=([^&]+)/i) { $Awstats="$2"; }
246 if ($QueryString =~ /(^|-|&)buildpdf/i) { $BuildPDF=1; }
247 if ($QueryString =~ /(^|-|&)buildpdf=([^&]+)/i) { $HtmlDoc="$2"; }
248 if ($QueryString =~ /(^|-|&)staticlinksext=([^&]+)/i) { $StaticExt="$2"; }
249 if ($QueryString =~ /(^|-|&)dir=([^&]+)/i) { $OutputDir="$2"; }
250 if ($QueryString =~ /(^|-|&)diricons=([^&]+)/i) { $DirIcons="$2"; }
251 if ($QueryString =~ /(^|-|&)update/i) { $Update=1; }
252 if ($QueryString =~ /(^|-|&)date/i) { $BuildDate='%YY%MM%DD'; } # For backward compatibility
253 rizwank 1.1 if ($QueryString =~ /(^|-|&)builddate=?([^&]*)/i) { $BuildDate=$2||'%YY%MM%DD'; }
254 if ($QueryString =~ /(^|-|&)year=(\d\d\d\d)/i) { $YearRequired="$2"; }
255 if ($QueryString =~ /(^|-|&)month=(\d{1,2})/i || $QueryString =~ /(^|-|&)month=(all)/i) { $MonthRequired="$2"; }
256 if ($QueryString =~ /(^|-|&)lang=([^&]+)/i) { $Lang="$2"; }
257
258 if ($OutputDir) { if ($OutputDir !~ /[\\\/]$/) { $OutputDir.="/"; } }
259
260 if (! $SiteConfig) {
261 print "----- $PROG $VERSION (c) Laurent Destailleur -----\n";
262 print "$PROG allows you to launch AWStats with -staticlinks option\n";
263 print "to build all possible pages allowed by AWStats -output option.\n";
264 print "\n";
265 print "Usage:\n";
266 print "$PROG.$Extension (awstats_options) [awstatsbuildstaticpages_options]\n";
267 print "\n";
268 print " where awstats_options are any option known by AWStats\n";
269 print " -config=configvalue is value for -config parameter (REQUIRED)\n";
270 print " -update option used to update statistics before to generate pages\n";
271 print " -lang=LL to output a HTML report in language LL (en,de,es,fr,...)\n";
272 print " -month=MM to output a HTML report for an old month=MM\n";
273 print " -year=YYYY to output a HTML report for an old year=YYYY\n";
274 rizwank 1.1 print "\n";
275 print " and awstatsbuildstaticpages_options can be\n";
276 print " -awstatsprog=pathtoawstatspl AWStats software (awstats.pl) path\n";
277 print " -dir=outputdir Output directory for generated pages\n";
278 print " -diricons=icondir Relative path to use as icon dir in <img> links\n";
279 print " -builddate=%YY%MM%DD Used to add build date in built pages filenames\n";
280 print " -staticlinksext=xxx For pages with .xxx extension instead of .html\n";
281 print " -buildpdf[=pathtohtmldoc] Build a PDF file after building HTML pages.\n";
282 print " Output directory must contains icon directory\n";
283 print " when this option is used (need 'htmldoc')\n";
284 print "\n";
285 print "New versions and FAQ at http://awstats.sourceforge.net\n";
286 exit 0;
287 }
288
289
290 my $retour;
291
292 # Check if AWSTATS prog is found
293 my $AwstatsFound=0;
294 if (-s "$Awstats") { $AwstatsFound=1; }
295 rizwank 1.1 elsif (-s "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl") {
296 $Awstats="/usr/local/awstats/wwwroot/cgi-bin/awstats.pl";
297 $AwstatsFound=1;
298 }
299 if (! $AwstatsFound) {
300 error("Can't find AWStats program ('$Awstats').\nUse -awstatsprog option to solve this");
301 exit 1;
302 }
303 $AwstatsDir=$Awstats; $AwstatsDir =~ s/[\\\/][^\\\/]*$//;
304 debug("AwstatsDir=$AwstatsDir");
305
306 # Check if HTMLDOC prog is found
307 if ($BuildPDF) {
308 my $HtmlDocFound=0;
309 if (-x "$HtmlDoc") { $HtmlDocFound=1; }
310 elsif (-x "/usr/bin/htmldoc") {
311 $HtmlDoc='/usr/bin/htmldoc';
312 $HtmlDocFound=1;
313 }
314 if (! $HtmlDocFound) {
315 error("Can't find htmldoc program ('$HtmlDoc').\nUse -buildpdf=htmldocprog option to solve this");
316 rizwank 1.1 exit 1;
317 }
318 }
319
320 # Read config file (SiteConfig must be defined)
321 &Read_Config($DirConfig);
322
323 # Define list of output files
324 if ($ShowDomainsStats) { push @OutputList,'alldomains'; }
325 if ($ShowHostsStats) { push @OutputList,'allhosts'; push @OutputList,'lasthosts'; push @OutputList,'unknownip'; }
326 if ($ShowAuthenticatedUsers) { push @OutputList,'alllogins'; push @OutputList,'lastlogins'; }
327 if ($ShowRobotsStats) { push @OutputList,'allrobots'; push @OutputList,'lastrobots'; }
328 if ($ShowEMailSenders) { push @OutputList,'allemails'; push @OutputList,'lastemails'; }
329 if ($ShowEMailReceivers) { push @OutputList,'allemailr'; push @OutputList,'lastemailr'; }
330 if ($ShowSessionsStats) { push @OutputList,'session'; }
331 if ($ShowPagesStats) { push @OutputList,'urldetail'; push @OutputList,'urlentry'; push @OutputList,'urlexit'; }
332 #if ($ShowFileTypesStats) { push @OutputList,'filetypes'; } # There is dedicated page for filetypes
333 if ($ShowOSStats) { push @OutputList,'osdetail'; push @OutputList,'unknownos'; }
334 if ($ShowBrowsersStats) { push @OutputList,'browserdetail'; push @OutputList,'unknownbrowser'; }
335 if ($ShowScreenSizeStats) { push @OutputList,'screensize'; }
336 if ($ShowOriginStats) { push @OutputList,'refererse'; push @OutputList,'refererpages'; }
337 rizwank 1.1 if ($ShowKeyphrasesStats) { push @OutputList,'keyphrases'; }
338 if ($ShowKeywordsStats) { push @OutputList,'keywords'; }
339 #if ($ShowMiscStats) { push @OutputList,'misc'; } # There is no dedicated page for misc
340 if ($ShowHTTPErrorsStats) {
341 #push @OutputList,'errors'; # There is no dedicated page for errors
342 push @OutputList,'errors404';
343 }
344 #if ($ShowSMTPErrorsStats) { push @OutputList,'errors'; }
345
346 # Launch awstats update
347 if ($Update) {
348 my $command="\"$Awstats\" -config=$SiteConfig -update";
349 $command .= " -configdir=$DirConfig" if defined $DirConfig;
350 print "Launch update process : $command\n";
351 $retour=`$command 2>&1`;
352 }
353
354 # Built the OutputSuffix value (used later to build page name)
355 $OutputSuffix=$SiteConfig;
356 if ($BuildDate) {
357 ($nowsec,$nowmin,$nowhour,$nowday,$nowmonth,$nowyear,$nowwday,$nowyday) = localtime(time);
358 rizwank 1.1 $nowweekofmonth=int($nowday/7);
359 $nowweekofyear=int(($nowyday-1+6-($nowwday==0?6:$nowwday-1))/7)+1; if ($nowweekofyear > 52) { $nowweekofyear = 1; }
360 $nowdaymod=$nowday%7;
361 $nowwday++;
362 $nowns=Time::Local::timegm(0,0,0,$nowday,$nowmonth,$nowyear);
363 if ($nowdaymod <= $nowwday) { if (($nowwday != 7) || ($nowdaymod != 0)) { $nowweekofmonth=$nowweekofmonth+1; } }
364 if ($nowdaymod > $nowwday) { $nowweekofmonth=$nowweekofmonth+2; }
365 # Change format of time variables
366 $nowweekofmonth="0$nowweekofmonth";
367 if ($nowweekofyear < 10) { $nowweekofyear = "0$nowweekofyear"; }
368 if ($nowyear < 100) { $nowyear+=2000; } else { $nowyear+=1900; }
369 $nowsmallyear=$nowyear;$nowsmallyear =~ s/^..//;
370 if (++$nowmonth < 10) { $nowmonth = "0$nowmonth"; }
371 if ($nowday < 10) { $nowday = "0$nowday"; }
372 if ($nowhour < 10) { $nowhour = "0$nowhour"; }
373 if ($nowmin < 10) { $nowmin = "0$nowmin"; }
374 if ($nowsec < 10) { $nowsec = "0$nowsec"; }
375 # Replace tag with new value
376 $BuildDate =~ s/%YYYY/$nowyear/ig;
377 $BuildDate =~ s/%YY/$nowsmallyear/ig;
378 $BuildDate =~ s/%MM/$nowmonth/ig;
379 rizwank 1.1 #$BuildDate =~ s/%MO/$MonthNumLibEn{$nowmonth}/ig;
380 $BuildDate =~ s/%DD/$nowday/ig;
381 $BuildDate =~ s/%HH/$nowhour/ig;
382 $BuildDate =~ s/%NS/$nowns/ig;
383 $BuildDate =~ s/%WM/$nowweekofmonth/g;
384 my $nowweekofmonth0=$nowweekofmonth-1; $BuildDate =~ s/%Wm/$nowweekofmonth0/g;
385 $BuildDate =~ s/%WY/$nowweekofyear/g;
386 my $nowweekofyear0=sprintf("%02d",$nowweekofyear-1); $BuildDate =~ s/%Wy/$nowweekofyear0/g;
387 $BuildDate =~ s/%DW/$nowwday/g;
388 my $nowwday0=$nowwday-1; $BuildDate =~ s/%Dw/$nowwday0/g;
389 $OutputSuffix.=".$BuildDate";
390 }
391
392 my $cpt=0;
393 my $NoLoadPlugin="";
394 if ($BuildPDF) { $NoLoadPlugin.="tooltips,rawlog,hostinfo"; }
395 my $smallcommand="\"$Awstats\" -config=$SiteConfig".($BuildPDF?" -buildpdf":"").($NoLoadPlugin?" -noloadplugin=$NoLoadPlugin":"")." -staticlinks".($OutputSuffix ne $SiteConfig?"=$OutputSuffix":"");
396 if ($StaticExt && $StaticExt ne 'html') { $smallcommand.=" -staticlinksext=$StaticExt"; }
397 if ($DirIcons) { $smallcommand.=" -diricons=$DirIcons"; }
398 if ($DirConfig) { $smallcommand.=" -configdir=$DirConfig"; }
399 if ($Lang) { $smallcommand.=" -lang=$Lang"; }
400 rizwank 1.1 if ($MonthRequired) { $smallcommand.=" -month=$MonthRequired"; }
401 if ($YearRequired) { $smallcommand.=" -year=$YearRequired"; }
402
403 # Launch main awstats output
404 my $command="$smallcommand -output";
405 print "Build main page: $command\n";
406 $retour=`$command 2>&1`;
407 $OutputFile=($OutputDir?$OutputDir:"")."awstats.$OutputSuffix.$StaticExt";
408 open("OUTPUT",">$OutputFile") || error("Couldn't open log file \"$OutputFile\" for writing : $!");
409 print OUTPUT $retour;
410 close("OUTPUT");
411 $cpt++;
412 push @pages, $OutputFile; # Add page to @page for PDF build
413
414 # Launch all other awstats output
415 for my $output (@OutputList) {
416 my $command="$smallcommand -output=$output";
417 print "Build $output page: $command\n";
418 $retour=`$command 2>&1`;
419 $OutputFile=($OutputDir?$OutputDir:"")."awstats.$OutputSuffix.$output.$StaticExt";
420 open("OUTPUT",">$OutputFile") || error("Couldn't open log file \"$OutputFile\" for writing : $!");
421 rizwank 1.1 print OUTPUT $retour;
422 close("OUTPUT");
423 $cpt++;
424 push @pages, $OutputFile; # Add page to @page for PDF build
425 }
426
427 # Build pdf file
428 if ($QueryString =~ /(^|-|&)buildpdf/i) {
429 # my $pdffile=$pages[0]; $pdffile=~s/\.\w+$/\.pdf/;
430 my $command="\"$HtmlDoc\" -t pdf --webpage --quiet --no-title --textfont helvetica --left 16 --bottom 8 --top 8 --browserwidth 800 --headfootsize 8.0 --fontsize 7.0 --header xtx --footer xd/ --outfile awstats.$OutputSuffix.pdf @pages\n";
431 print "Build PDF file : $command\n";
432 $retour=`$command 2>&1`;
433 my $signal_num=$? & 127;
434 my $dumped_core=$? & 128;
435 my $exit_value=$? >> 8;
436 if ($? || $retour =~ /error/) {
437 if ($retour) { error("Failed to build PDF file with following error: $retour"); }
438 else { error("Failed to run successfuly htmldoc process: Return code=$exit_value, Killer signal num=$signal_num, Core dump=$dumped_core"); }
439 }
440 $cpt++;
441 }
442 rizwank 1.1
443
444 print "$cpt files built.\n";
445 print "Main HTML page is 'awstats.$OutputSuffix.$StaticExt'.\n";
446 if ($QueryString =~ /(^|-|&)buildpdf/i) { print "PDF file is 'awstats.$OutputSuffix.pdf'.\n"; }
447
448 0; # Do not remove this line
|