ONO::Core::Kernel

package ONO::Core::Kernel;
################################################################################
# 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;

#: ONO::Core::Kernel is responsible for low level communication with the host
#: operating system.
#:
#: Using this module in production projects should be avoided whenever
#: possible, try to use ONO::IO or other library / toolbox features instead.

###############################################################################
# SENDMAIL
###############################################################################

my $sendmail;
if (-e "/usr/sbin/sendmail") {
$sendmail = "/usr/sbin/sendmail";
} else {
$sendmail = &exec("","which sendmail");
$sendmail =~ s~[^A-Za-z0-9\/]~~gi;
}

sub sendmail {

#: Returns the path to the UNIX sendmail script.
#:
#: The path is detected outside of the function to speed up mod_perl
#: systems.

return $sendmail;
}

###############################################################################
# DETECT VPATH
###############################################################################

# This uses mod_perl caching, as the output usually doesn't change quickly

my $custom_vpath;

if (&sys("","exists","/etc/apache2/ono_kernel_docroot.conf")) {

foreach my $dat (&sys("","list","/etc/apache2/ono_kernel_docroot.conf")) {
$custom_vpath .= $dat;
}
$custom_vpath =~ s/(\n|\r|\t)//gi;

}

sub vpath {

#: Return the path to the ONO installation's document root, this is usually
#: called the "vpath" or "virtual path" on ONO systems.
#:
#: This works for both CGI scripts and command line tools, so that higher
#: level features can be used in both web scripts and cron scripts for
#: example.
#:
#: The vpath can also be set manually, using the following file inside the
#: UNIX host /etc/apache2 directory (usually not necessary, and not
#: working on systems with multiple virtual hosts):
#:
#: /etc/apache2/ono_kernel_docroot.conf
#:
#: Note that you should NOT use this function on production systems,
#: use ONO::IO's vpath function instead.

my $vpath;

unless ($ENV{'MOD_PERL'}) {
eval "use FindBin";
if (!$@) { $vpath = $FindBin::Bin }
$vpath =~ s~/ono/sys(.*?)$~~;
$vpath =~ s~/local/perl/(.*?)$~~;
$vpath =~ s~/cgi-bin/ono/(.*?)$~~;
$vpath =~ s~/cgi-bin/ono$~~;
$vpath =~ s~/cgi-bin$~~;
$vpath =~ s~/ono/cron$~~;
}

if (!$vpath || $ENV{'MOD_PERL'}) {
$vpath = $ENV{'DOCUMENT_ROOT'};
}
if ($custom_vpath) {
$vpath = $custom_vpath;
}

return $vpath;
}

###############################################################################
# unix exec
###############################################################################

sub exec {

#: Execute a UNIX command. This will pass to input to a UNIX shell, but
#: no special characters will be allowed, which means that only very simple
#: commands can be executed.
#:
#: Note that you should NOT use this function on production systems,
#: use ONO::IO's exec functions (there are several ones) instead.

my ($self,$exec) = @_;

$exec =~ s~[^A-Za-z0-9\ \-\_\.\/]~~g;

return `$exec`;

}

###############################################################################
# host filesystem access
###############################################################################

sub sys {

#: The sys function allows direct access to specific UNIX system data.
#: It allows to read directories and certain logfiles, as well as to read
#: and write files. Note that file and dir I/O is also available via
#: ONO::Core::HostIO.
#:
#: Note that you should NOT use this function on production systems,
#: use ONO::IO's I/O tools or appropriate library / toolbox modules
#: instead.

my ($self,$mode,$opts,$data) = @_;

$mode =~ s~[^A-Za-z0-9\_\-]~~g;

# access_log, error_log

if ($mode =~ /^(access_log|error_log)$/) {

my $FILE = "/var/log/apache2/$mode";
unless (-e $FILE) {
$mode =~ s~\_~\.~;
$FILE = "/var/log/apache2/$mode";
}

open (FILE,$FILE);
my @data = <FILE>;
close FILE;

return @data;

}

# exists

if ($mode eq "exists") {

if (-e $opts) {
return 1;
} else {
return 0;
}
}

# list / load

if ($mode eq "list" || $mode eq "load") {

open (FILE,$opts);
my @data = <FILE>;
close FILE;

return @data;

}

# ls

if ($mode eq "ls") {

opendir(DIR, $opts);
my @objects = readdir(DIR);
closedir DIR;

return @objects;

}

# store

if ($mode eq "store") {

open (FILE, "> $opts");
print FILE $data;
close FILE;

}

}

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

1;

__END__