ONO::Cron::Service::Daily

package ONO::Cron::Service::Daily;
################################################################################
# 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::ToolBox::SendMail;
use ONO::ToolBox::ONO;

use ONO::Cron::Service::ToolBox;

###############################################################################
# ONO
###############################################################################

#: ONO Cron Service daily tasks.

sub run {

#: Execution logic.

my (
$self,
$db,
$sec,$min,$hour,
$mday,$mon,$year,
$wday,$yday,$timestamp,
$switches,
) = @_;

my $DATA = ONO::Cron::Service::ToolBox->print("Service running: Daily",$switches);

my %vars;

my %ono = ONO::ToolBox::ONO->get();

############################################################################
# make required dirs
############################################################################

$DATA .= ONO::Cron::Service::ToolBox->print("- make required dirs",$switches);

foreach my $dir (
'goto',
'goto/-',
'tmp',
'tmp/audio',
'tmp/images',
'tmp/var',
'tmp/video',
'var',
'var/tmp',
) {
ONO::IO->mkdir($dir);
ONO::IO->chmod($dir,"777");
ONO::IO->dirprotect($dir);
}

############################################################################
# update robots.txt
############################################################################

$DATA .= ONO::Cron::Service::ToolBox->print("- update robots.txt",$switches);

ONO::IO->store(
"robots.txt",
qq~User-agent: *
Disallow: /account/
Disallow: /admin/
Disallow: /cgi-bin/
Disallow: /data/
Disallow: /etc/
Disallow: /home/
Disallow: /hosts/
Disallow: /http/
Disallow: /images/users/
Disallow: /legal/
Disallow: /login/
Disallow: /ono/
Disallow: /pdf/
Disallow: /root/
Disallow: /sql/
~,
);

############################################################################
# set robot traps
############################################################################

$DATA .= ONO::Cron::Service::ToolBox->print("- set robot traps",$switches);

foreach my $dir (
'admin',
'data',
'files',
'files/audio',
'files/images',
'files/media',
'files/music',
'files/pdf',
'files/photos',
'files/videos',
'hidden',
'legal',
'legal-information',
) {
ONO::IO->mkpath($dir);
ONO::IO->chmod($dir,"777");
ONO::IO->store("$dir/index.shtml",qq~<!--#include virtual="/cgi-bin/ono/trap.pl" -->~);
}

############################################################################
# htaccess rewrites
############################################################################

$DATA .= ONO::Cron::Service::ToolBox->print("- create rewrites",$switches);

ONO::IO->htaccess_rewrite("goto/-","../../cgi-bin/ono/redir.pl");

############################################################################
# write some info...
############################################################################

$DATA .= ONO::Cron::Service::ToolBox->print("- write readme files",$switches);

ONO::IO->store(
"tmp/var/_README.txt",
"Only store files in subdirs here.\nEach subdir will be emptied once a week, and we'll attempt to delete/remove it.\nFiles older than 48 hours will be removed on a daily basis.",
);

############################################################################
# protect user image dirs
############################################################################

ONO::IO->dirprotect("images/users");
foreach my $d1 (ONO::IO->dir("images/users")) {
if ($d1 =~ /[a-z0-9]/) {
ONO::IO->dirprotect("images/users/$d1");
foreach my $d2 (ONO::IO->dir("images/users/$d1")) {
if ($d2 =~ /[a-z0-9]/) {
ONO::IO->dirprotect("images/users/$d1/$d2");
foreach my $d3 (ONO::IO->dir("images/users/$d1/$d2")) {
if ($d3 =~ /[a-z0-9]/) {
ONO::IO->dirprotect("images/users/$d1/$d2/$d3");
}
}
}
}
}
}

############################################################################
# cache cleanup - delete all cache files older than 1 month
############################################################################

$DATA .= ONO::Cron::Service::ToolBox->print("- cache cleanup in /var/tmp/cache/",$switches);

foreach my $file (ONO::IO->dir("var/tmp/cache")) {
if ($file =~ /\.txt$/) {
if (ONO::IO->timestamp("var/tmp/cache/$file") < ($timestamp - (60 * 60 * 24 * 31))) {

$DATA .= ONO::Cron::Service::ToolBox->print(" + removing /var/tmp/cache/$file",$switches);
ONO::IO->rm("var/tmp/cache/$file");

}
}
}

############################################################################
# clean up /tmp/var/ - delete files older than 48 hours
############################################################################

$DATA .= ONO::Cron::Service::ToolBox->print("- clean up /tmp/var/",$switches);

foreach my $subdir (ONO::IO->dir("tmp/var")) {
if ($subdir !~ /\./ && $subdir =~ /[a-z]/) {
foreach my $file (ONO::IO->dir("tmp/var/$subdir")) {
if ($file !~ /^\./ && $file =~ /[a-z]/) {
if ($file =~ /\./) {

# clean up /tmp/var/subdir

if (ONO::IO->timestamp("tmp/var/$subdir/$file") < ($timestamp - (60 * 60 * 48))) {
ONO::IO->rm("tmp/var/$subdir/$file");
}
} else {
foreach my $file2 (ONO::IO->dir("tmp/var/$subdir/$file")) {
if ($file2 !~ /^\./ && $file2 =~ /\./ && $file2 =~ /[a-z]/) {

# clean up /tmp/var/subdir/subdir2

if (ONO::IO->timestamp("tmp/var/$subdir/$file/$file2") < ($timestamp - (60 * 60 * 48))) {
ONO::IO->rm("tmp/var/$subdir/$file/$file2");
}
}
}
}
}
}
}
}

############################################################################
# website security
############################################################################

my $PD = "<html><body>Permisson Denied</body></html>";

foreach my $opt ('web','schoolweb') {
if (ONO::IO->exists($opt)) {
foreach my $d1 (ONO::IO->ls("$opt")) {
if ($d1 !~ /\./ && $d1 =~ /[A-Za-z0-9]/ && $d1 ne "-" && length $d1 == 1) {
ONO::IO->dirprotect("$opt/$d1",$PD);
foreach my $d2 (ONO::IO->ls("$opt/$d1")) {
if ($d2 !~ /\./ && $d2 =~ /[A-Za-z0-9]/) {
ONO::IO->dirprotect("$opt/$d1/$d2",$PD);
foreach my $d3 (ONO::IO->ls("$opt/$d1/$d2")) {
if ($d3 !~ /\./ && $d3 =~ /[A-Za-z0-9]/) {
ONO::IO->dirprotect("$opt/$d1/$d2/$d3",$PD);
foreach my $dir (ONO::IO->ls("$opt/$d1/$d2/$d3")) {
if ($dir !~ /\./ && $dir =~ /[A-Za-z0-9]/) {
ONO::IO->dirprotect("$opt/$d1/$d2/$d3/$dir",$PD);
foreach my $opt2('data','media/protected','shopping_cart') {
if (ONO::IO->exists("$opt/$d1/$d2/$d3/$dir/$opt2")) {
ONO::IO->htaccess("$opt/$d1/$d2/$d3/$dir/$opt2",":");
}
}
}
}
}
}
}
}
}
}
}
}

if (ONO::IO->exists("community/portal")) {

# this may become PARTLY obsolete as soon as we will move data to /var...

ONO::IO->dirprotect("community/portal",$PD);

foreach my $opt ('data','school') {

ONO::IO->dirprotect("community/portal/$opt",$PD);

foreach my $dir (ONO::IO->dir("community/portal/$opt")) {
if ($dir =~ /[0-9]/) {
ONO::IO->dirprotect("community/portal/$opt/$dir",$PD);

}
}

}

}

if (!ONO::IO->devstation && ONO::IO->exists("ono-data")) {
ONO::IO->dirprotect("ono-data",$PD);
ONO::IO->htaccess("ono-data",":");
}

############################################################################
# done
############################################################################

return $DATA;

}

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

1;

__END__