ONO::ToolBox::Analytics

package ONO::ToolBox::Analytics;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

use strict;

use ONO::IO;
use ONO::Lib::Web::BotDetect;

###############################################################################
# display
###############################################################################

#: This module offers some functions that help to build local ONO website data,
#: accessible via the Community Admin Screen within your platform project.
#:
#: See ONO_FW_User_Screen_Admin for the front end UI.

sub run {

#: This should be called by your platform main script, each time a web page
#: is being accessed. Only 1% of all accesses will be logged, in order to
#: keep the log sizes small and the server load low. Note that we try
#: to only log real users, not bots.


my (
$self,
$id,
$year,
$mon,
$mday,
$time,
$vars_ref,
) = @_;

if (int(rand(100)) == 50 || ONO::IO->devstation) {

if (!&exclude("",$ENV{'REQUEST_URI'},$ENV{'HTTP_USER_AGENT'})) {

my %vars = %$vars_ref;

my $REQUEST_URI = $ENV{'REQUEST_URI'};
$REQUEST_URI =~ s~\?(.*)$~~;

my $LOG = "$year$mon$mday:$time:$ENV{'REMOTE_ADDR'}:$ENV{'SERVER_NAME'}:$vars{'username'}:$REQUEST_URI:\n";

ONO::IO->mkpath("var/log/analytics/$id/$year");
ONO::IO->append("var/log/analytics/$id/$year/$year.log",$LOG);
ONO::IO->append("var/log/analytics/$id/$year/$year$mon.log",$LOG);
ONO::IO->append("var/log/analytics/$id/$year/$year$mon$mday.log",$LOG);

}

}

return "";

}

sub exclude {

#: This function helps to exclude all kinds of bots. This will, of course,
#: not work with bots that fake their identity.
#:
#: It also prevents logging unwanted URLs.

if ($_[2] !~ /(bot|crawler|spider|curl|fetch|slurp|archiver|mediapartners|libwwwperl|alyzer)/i && $_[2] !~ m~(http|https)://~i) {

# url check

if ($_[1] !~ /\.(txt|xml|css|js|php|ico|jpg|jpeg|gif|png|pdf|mp3|mp4)/ && $_[1] !~ m~/perl/directory/~ && $_[1] !~ m~/perl/(.*?)/(ajax|apps|cron)/~ && $_[1] !~ /community_screen=admin/) {

return 0;

} else {

if (ONO::Lib::Web::BotDetect->evil_bot() || ONO::Lib::Web::BotDetect->crawler_bot() || ONO::Lib::Web::BotDetect->archive_bot()) {

return 0;

} else {

return 1;

}

}

} else {

return 1;

}

}

###############################################################################
# end of script
###############################################################################

1;

__END__