smeserver-awstats/root/usr/share/smanager/lib/SrvMngr/Controller/Awstats.pm

356 lines
12 KiB
Perl

package SrvMngr::Controller::Awstats;
#----------------------------------------------------------------------
# heading : Investigation
# description : Web statistics
# navigation : 6000 350
#
# menu : A
#
# name : awstats, method : get, url : /awstats, ctlact : awstats#main
# name : awstats2, method : get, url : /awstats2, ctlact : awstats#do_display
# name : awstatsc, method : post, url : /awstats, ctlact : awstats#do_display
# name : awstatsu, method : post, url : /awstats2, ctlact : awstats#do_update
# name : awstats3, method : get, url : /awstats3, ctlact : awstats#do_update
#
# routes : end
#----------------------------------------------------------------------
use strict;
use warnings;
use Mojo::Base 'Mojolicious::Controller';
use Locale::gettext;
use SrvMngr::I18N;
use SrvMngr qw(theme_list init_session);
use POSIX qw(strftime);
use esmith::DomainsDB;
use esmith::ConfigDB;
our $ddb = esmith::DomainsDB->open || die "Couldn't open domains db";
our $cdb = esmith::ConfigDB->open || die "Couldn't open configuration db";
our $awsdirdata="/home/e-smith/files/users/admin/home/awstats";
our $REGEXP_DOMAIN = qq([a-zA-Z0-9\-\.]+);
sub main {
my $c = shift;
$c->app->log->info($c->log_req);
my %aws_datas = ();
my $title = $c->l('aws_FORM_TITLE');
$aws_datas{trt} = 'LST';
my $rec = $cdb->get('AWStats');
$aws_datas{status} = $rec->prop('status')|| 'disabled';
$aws_datas{mailstatus} = ( $rec->prop('mailstatus') || 'disabled' );
$aws_datas{ftpstatus} = ( $rec->prop('ftpstatus') || 'disabled' );
$aws_datas{username} = $c->session->{username} || 'unknown';
my @domains;
# if ( $aws_datas->{status} ne 'enabled' ) {
for ($ddb->domains()) {
my $domain = $_->key;
next unless ( -d "$awsdirdata/$domain" );
my $ns = $_->prop('Nameservers') || 'internet';
$domain = $1 if ($domain =~ /^($REGEXP_DOMAIN)$/);
my $awstats = 'Empty';
if (system("/bin/ls $awsdirdata/$domain/ | /bin/grep '.txt' > /dev/null 2>&1") == 0 ) {
$awstats = 'Show';
}
my $status = 'active';
#if ( ????system == 0 ) { // T O D O inactive domains//
# $status = 'inactive';
#}
push @domains,
{ Domain => $_->key, $_->props,
Nameservers => $ns,
AWStats => $awstats,
Status => $status
}
}
if (open (ST, "/var/log/httpd/awstats_lr")) {
$aws_datas{logres} = <ST>;
close (ST);
}
# }
$c->stash( title => $title, aws_datas => \%aws_datas, domains => \@domains );
$c->render(template => 'awstats');
}
sub do_display {
my $c = shift;
$c->app->log->info($c->log_req);
my %aws_datas = ();
my $title = $c->l('aws_FORM_TITLE');
my $result;
my $trt = $c->param('trt') || 'LST';
my $domain = $c->param('Domain');
if ( $trt eq 'SHW' ) {
$aws_datas{periodrep} = 'month';
$aws_datas{monthrep} = strftime( "%m", localtime );
$aws_datas{yearrep} = strftime( "%Y", localtime );
$aws_datas{domain} = $domain;
$result = _get_awst( $c, \%aws_datas );
}
if ( $trt eq 'CNF' ) {
my $rec = $cdb->get('AWStats');
if ($rec) {
$aws_datas{status} = $rec->prop('status')|| 'disabled';
$aws_datas{mailstatus} = ( $rec->prop('mailstatus') || 'disabled' );
$aws_datas{ftpstatus} = ( $rec->prop('ftpstatus') || 'disabled' );
$aws_datas{freq} = $rec->prop('Freq')|| '5m';
$aws_datas{barSize} = $rec->prop('BarSize')|| 'medium';
$aws_datas{language} = $rec->prop('Language')|| 'auto';
$aws_datas{maxNbOfDomain} = $rec->prop('MaxNbOfDomain')|| '50';
$aws_datas{maxNbOfHosts} = $rec->prop('MaxNbOfHostsShown')|| '100';
$aws_datas{maxNbOfKeywords} = $rec->prop('MaxNbOfKeywordsShown')|| '50';
$aws_datas{maxNbOfPage} = $rec->prop('MaxNbOfPageShown')|| '200';
$aws_datas{maxNbOfReferer} = $rec->prop('MaxNbOfRefererShown')|| '50';
$aws_datas{maxNbOfRobot} = $rec->prop('MaxNbOfRobotShown')|| '25';
$aws_datas{minHitPage} = $rec->prop('MinHitFile')|| '1';
$aws_datas{minHitHost} = $rec->prop('MinHitHost')|| '1';
$aws_datas{minHitKeyword} = $rec->prop('MinHitKeyword')|| '1';
$aws_datas{minHitRefer} = $rec->prop('MinHitRefer')|| '1';
$aws_datas{minHitRobot} = $rec->prop('MinHitRobot')|| '1';
$aws_datas{rawlog} = $rec->prop('Rawlog')|| 'no';
$aws_datas{skipExternalIP} = $rec->prop('SkipExternalIP')|| 'yes';
$aws_datas{skipLocalIP} = $rec->prop('SkipLocalIP')|| 'yes';
$aws_datas{skipLocalNetworks} = $rec->prop('SkipLocalNetworks')|| 'no';
$aws_datas{userFramesWhenCGI} = $rec->prop('UserFramesWhenCGI')|| 'no';
$aws_datas{username} = $c->session->{username} || 'unknown';
}
}
$aws_datas{trt} = $trt;
$aws_datas{domain} = $domain;
$c->stash( title => $title .' '. $domain, aws_datas => \%aws_datas, modul => $result );
$c->render(template => 'awstats');
}
sub do_update {
my $c = shift;
$c->app->log->info($c->log_req);
my $rt = $c->current_route;
my $trt = $c->param('trt') || 'SHWP';
my $title = $c->l('aws_FORM_TITLE');
my %aws_datas = ();
$aws_datas{trt} = $trt;
my ($res, $result) = '';
if ( $trt eq 'UPD' ) {
my $status = $c->param('Status');
my $mailstatus = $c->param('Mailstatus');
my $ftpstatus = $c->param('Ftpstatus');
my $freq = $c->param('Freq');
my $skipLocalIP = $c->param('SkipLocalIP');
my $skipExternalIP = $c->param('SkipExternalIP');
my $skipLocalNetworks = $c->param('SkipLocalNetworks');
my $userFramesWhenCGI = $c->param('UserFramesWhenCGI');
my $language = $c->param('Language');
my $rawlog = $c->param('Rawlog');
my $barSize = $c->param('BarSize');
my $maxNbOfDomain = $c->param('MaxNbOfDomain');
my $maxNbOfHosts = $c->param('MaxNbOfHosts');
my $minHitHost = $c->param('MinHitHost');
my $maxNbOfPages = $c->param('MaxNbOfPages');
my $minHitPage = $c->param('MinHitPage');
my $maxNbOfRobot = $c->param('MaxNbOfRobot');
my $minHitRobot = $c->param('MinHitRobot');
my $maxNbOfReferer = $c->param('MaxNbOfReferer');
my $minHitRefer = $c->param('MinHitRefer');
my $maxNbOfKeywords = $c->param('MaxNbOfKeywords');
my $minHitKeyword = $c->param('MinHitKeyword');
#$result = 'blocked';
if ($maxNbOfDomain =~ /^([0-9]+)$/) {
$maxNbOfDomain = $1;
} else {
$result .= $c->l('FIELD_INVALID_CHARS') .' : '. $maxNbOfDomain .'<br>';
}
if ($maxNbOfHosts =~ /^([0-9]+)$/) {
$maxNbOfHosts = $1;
} else {
$result .= $c->l('FIELD_INVALID_CHARS') .' : '. $maxNbOfHosts .'<br>';
}
if ($minHitHost =~ /^([0-9]+)$/) {
$minHitHost = $1;
} else {
$result .= $c->l('FIELD_INVALID_CHARS') .' : '. $minHitHost .'<br>';
}
$maxNbOfDomain = 1 if $maxNbOfDomain < 1;
$maxNbOfHosts = 1 if $maxNbOfHosts < 1;
$minHitHost = 1 if $minHitHost < 1;
$maxNbOfPages = 1 if $maxNbOfPages < 1;
$minHitPage = 1 if $minHitPage < 1;
$maxNbOfRobot = 1 if $maxNbOfRobot < 1;
$minHitRobot = 1 if $minHitRobot < 1;
$maxNbOfReferer = 1 if $maxNbOfReferer < 1;
$minHitRefer = 1 if $minHitRefer < 1;
$maxNbOfKeywords = 1 if $maxNbOfKeywords < 1;
$minHitKeyword = 1 if $minHitKeyword < 1;
# controls
$res ='';
if ( ! $result ) {
my $rec = $cdb->get('AWStats');
if ($rec) {
$rec->set_prop('status', $status); ;
$rec->set_prop('mailstatus', $mailstatus); ;
$rec->set_prop('ftpstatus', $ftpstatus); ;
$rec->set_prop('Freq', $freq);
$rec->set_prop('BarSize',$barSize );
$rec->set_prop('Language', $language);
$rec->set_prop('MaxNbOfDomain', $maxNbOfDomain);
$rec->set_prop('MaxNbOfHostsShown', $maxNbOfHosts);
$rec->set_prop('MaxNbOfKeywordsShown', $maxNbOfKeywords);
$rec->set_prop('MaxNbOfPageShown', $maxNbOfPages);
$rec->set_prop('MaxNbOfRefererShown', $maxNbOfReferer);
$rec->set_prop('MaxNbOfRobotShown', $maxNbOfRobot);
$rec->set_prop('MinHitFile', $minHitPage);
$rec->set_prop('MinHitHost', $minHitHost);
$rec->set_prop('MinHitKeyword', $minHitKeyword);
$rec->set_prop('MinHitRefer', $minHitRefer);
$rec->set_prop('MinHitRobot', $minHitRobot);
$rec->set_prop('Rawlog', $rawlog);
$rec->set_prop('SkipExternalIP', $skipExternalIP);
$rec->set_prop('SkipLocalIP', $skipLocalIP);
$rec->set_prop('SkipLocalNetworks', $skipLocalNetworks);
$rec->set_prop('UserFramesWhenCGI', $userFramesWhenCGI);
}
if ( system( '/sbin/e-smith/signal-event awstats-update &' ) != 0 ) {
$result = ($c->l('aws_ERROR_CONFIG_UPDATE') . " $trt");
} else {
# sleep (2);
$res = 'OK';
$result = $c->l('aws_CONFIG_SUCCESSFULLY_MODIFIED');
}
}
}
if ( $trt eq 'SHWP' ) {
$aws_datas{periodrep} = $c->param('databasebreak');
$aws_datas{monthrep} = $c->param('month');
$aws_datas{yearrep} = $c->param('year');
$aws_datas{dayrep} = $c->param('day') || '';
$aws_datas{hourrep} = $c->param('hour') || ''; # does not exist (bug in awstats 7.8-3)
$aws_datas{domain} = $c->param('config') || 'None';
$result = _get_awst( $c, \%aws_datas );
$c->stash( title => $title .' '. $aws_datas{domain}, aws_datas => \%aws_datas, modul => $result );
return $c->render( 'awstats' );
}
# common parts
if ($res ne 'OK') {
$c->stash( error => $result );
$c->stash( title => $title, aws_datas => \%aws_datas );
return $c->render( 'awstats' );
}
# $cdb = esmith::ConfigDB->open();
my $message = "'AWStats configuration' update ($trt) DONE";
$c->app->log->info($message);
$c->flash( success => $result );
$c->redirect_to('/awstats');
}
sub _get_awst {
my ($c, $aws_datas) = @_;
my $periodrep = $aws_datas->{periodrep};
my $yearrep = $aws_datas->{yearrep};
my $monthrep = $aws_datas->{monthrep};
my $dayrep = $aws_datas->{dayrep};
my $hourrep = $aws_datas->{hourrep};
my $domain = $aws_datas->{domain};
my $res;
$domain = $1 if ($domain =~ /^($REGEXP_DOMAIN)$/);
$periodrep = $1 if ($periodrep =~ /^([A-Za-z]+)$/);
$monthrep = $1 if ($monthrep =~ /^([A-Za-z\-]+)$/);
$yearrep = $1 if ($yearrep =~ /^([0-9]+)$/);
$dayrep = $1 if ($dayrep and $dayrep =~ /^([0-9]+)$/);
$hourrep = $1 if ($hourrep and $hourrep =~ /^([0-9]+)$/);
# cli awstats.pl call
$res = `/etc/e-smith/web/panels/manager/cgi-bin/.awstats/awstats.pl -databasebreak=$periodrep -month=$monthrep -year=$yearrep -day=$dayrep -hour=$hourrep -config="${domain}" -lang=auto -output`;
# change awstats link for this controller
$res =~ s|action="/cgi-bin/awstats.pl?|action="/smanager/awstats3?trt=SHWP&|;
# change DirIcons for sm2 public one
$res =~ s|\.\./\.\./awstats|\./images/awstats/icon|g;
return $res;
}
sub freq_options_list {
my $c = shift;
return [[ '5 ' . $c->l('MINUTES') => '5m'],
[ '10 ' . $c->l('MINUTES') => '10m'],
[ '15 ' . $c->l('MINUTES') => '15m'],
[ '30 ' . $c->l('MINUTES') => '30m'],
[ '1 ' . $c->l('HOUR') => '1h'],
[ '2 ' . $c->l('HOURS') => '2h'],
[ '4 ' . $c->l('HOURS') => '4h'],
[ '8 ' . $c->l('HOURS') => '8h'],
[ '12 ' . $c->l('HOURS') => '12h']];
}
sub language_options_list {
my $c = shift;
return [[ 'Auto Detect' => 'auto' ],
['English' => 'en' ],
['French', => 'fr' ],
['Dutch' => 'nl' ],
['Spanish' => 'es' ],
['Italian' => 'it' ],
['German', => 'de' ],
['Polish', => 'pl' ],
['Greek' => 'gr' ],
['Czech' => 'cz' ],
['Portuguese' => 'pt' ]];
}
1