ONO::Scripts::File

package ONO::Scripts::File;
################################################################################
# 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 CGI;

use ONO::ToolBox::Downloads;
use ONO::IO;

###############################################################################
# load the input variables via cgi
###############################################################################

sub run {

#: This module will either display or download a file.

my $cgi = new CGI;
my $file = $cgi->param('file');

$file =~ s~\?(.*)$~~;
if ($file !~ m~^/~) {
$file = "/$file";
}

if ($ENV{'REQUEST_URI'} =~ m~/download/-/(.*)\.(.*?)$~) {
$file = "$1.$2";
}

###############################################################################
# THE DISPLAY ENGINE
###############################################################################

if ($file =~ m~^/(images|media|servers|ono/osr|home|sql|sys/images|web|schoolweb)/~ || ONO::IO->image($file) || !$file) {

if ($file && (ONO::IO->exists($file) || $file =~ m~^/(ono|servers|sql|sys/images)/~ || $file =~ m~^/media/mediaservers/~)) {

ONO::ToolBox::Downloads->download("","",$cgi,ONO::IO->path,$file);

} else {

print "Content-Type: text/plain; charset=UTF-8\n\n";
print "[error: no such file: $file]";

}

} else {

print "Content-Type: text/plain; charset=UTF-8\n\n";
print "[error: permission denied: $file]";

}

}

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

1;

__END__