package ONO::FW::Apps::Core::ToolBox;
################################################################################
# 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::DB;
use ONO::ToolBox::Auth;
use ONO::FW::Apps::Core;
use ONO::Lib::Lang::LangKitEdu;
use ONO::Ext::Morzino::ToolBox;
use ONO::FW::User::Init;
use ONO::Lib::Code::RandomID;
use ONO::Lib::PDF::ToolBox;
use ONO::Lib::Web::Cookie;
use ONO::Lib::Web::LocateIP;
use ONO::Lib::Web::Client;
###################################################################
#
###################################################################
#: This module offers ONO app core features.
sub init {
#: Initialize an ONO app - this makes sure the app is getting
#: all required input data to both start and run successfully.
my (
$self,
$vars_ref,
) = @_;
my %vars = %$vars_ref;
if ($vars{'output_paper_id'}) {
if (ONO::IO->exists("var/tmp/apps/worksheet-maker/input-cache/$vars{'output_paper_id'}.txt")) {
foreach my $line (ONO::IO->list("var/tmp/apps/worksheet-maker/input-cache/$vars{'output_paper_id'}.txt")) {
$line =~ s~(\n|\r|\t)~~g;
my @lp = split(/=/,$line);
$vars{$lp[0]} = $lp[1];
}
}
}
$vars{'app_lang'} !~ s~[^a-z]~~g;
if (!$vars{'app_lang'}) {
$vars{'app_lang'} = "en";
}
if ($vars{'output_paper_save'} && !$vars{'output_paper_save_pdfkey'}) {
$vars{'output_paper'} = 1;
}
if ($vars{'output_web_question_number'} < 2) {
$vars{'output_web_question_number'} = 1;
}
if ($vars{'output_web_report'} || $vars{'output_web_share'}) {
$vars{'output_web_finish'}++;
}
if ($vars{'app_input_comma_status'} eq "rand") {
$vars{'app_input_comma_status'} = 3;
$vars{'app_input_comma_rand'} = 1;
}
if ($vars{'app_input_range_mode'} eq "default") {
$vars{'app_input_range_list'} = "";
}
if ($vars{'app_input_range_mode'} eq "list") {
$vars{'app_input_range_from'} = "";
$vars{'app_input_range_to'} = "";
}
foreach my $key (keys %vars) {
if ($key =~ /^output_web_answer/) {
$vars{$key} =~ s~(’|‘)~'~g;
}
if ($key =~ /^app_input_(number_of_questions)$/) {
$vars{$key} =~ s~\%(.*)$~~;
$vars{$key} =~ s~[^0-9]~~g;
if ($key eq "app_input_number_of_questions" && $vars{$key} > 500) {
$vars{$key} = 500;
}
}
}
if (!$vars{'app_name'}) {
$ENV{'SCRIPT_NAME'} =~ m~/cgi-bin/local/perl/(.*?)/apps/(.*?).pl~;
$vars{'app_name'} = $2;
}
my ($BLK_ref,$UCBLK_ref) = ONO::Lib::Lang::LangKitEdu->get($vars{'app_lang'},$vars_ref);
my $community = ONO::IO->community();
my $db = ONO::DB->connect($community);
$vars_ref = ONO::ToolBox::Auth->auth($db,$community,\%vars);
my (
$vpath,
$lang,$domain,$community,
$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$timestamp,
$script_url,$action_url,$form
) = ONO::FW::User::Init->getvar($vars_ref);
%vars = %$vars_ref;
if (ONO::IO->devstation) {
$vars{'username'} = "jos";
}
if (!$vars{'username'}) {
my ($AUTH_ID) = ONO::Ext::Morzino::ToolBox->virclass_auth($db,$community,"I");
$vars{'virclass'} = $AUTH_ID;
if (ONO::IO->devstation) {
$vars{'virclass'} = "1:AbcdXYZ0";
}
}
if (!$vars{'output_web'} && !$vars{'output_paper'} && !$vars{'output_save'} && !$vars{'exercise'} && !$vars{'exercise_id'} && !$vars{'output_worksheet_maker'}) {
my $input_found;
foreach my $key (keys %vars) {
if ($key =~ /^app_input/) {
$input_found++;
}
}
if (!$input_found) {
if ($ENV{'SCRIPT_NAME'} =~ m~/cgi-bin/local/perl/(.*?)/apps/(.*?)\.pl~) {
my $community = $1;
my $app = $2;
my $ID = ONO::Lib::Web::Cookie->get("ono_app_${app}_settings");
foreach my $cdat (ONO::IO->list("var/tmp/cookies/apps/$ID.txt")) {
if ($cdat =~ m~^(.*?)=(.*?)$~) {
$vars{$1} = $2;
}
}
if ($vars{'username'} && !ONO::IO->devstation) {
foreach my $line (ONO::IO->list("var/community/$community/accounts/data/".ONO::IO->deepdir($vars{'username'})."/$vars{'username'}/apps_settings/$app.txt")) {
$line =~ s~(\n|\r|\t)~~g;
if ($line =~ /^(.*?)=(.*?)$/) {
$vars{$1} = $2;
}
}
}
}
}
}
if (ONO::IO->devstation) {
$vars{'virclass'} = "1:AbcdXYZ0";
}
if ($vars{'varstoreaction'}) {
($vars_ref,%vars) = ONO::FW::Apps::Core->varstoreaction(\%vars);
}
if (!$vars{'app_sid'} || $vars{'output_web_repeat'} || $vars{'output_web_newsid'}) {
$vars{'app_sid'} = "$year$mon$mday\_".ONO::Lib::Code::RandomID->make(32);
}
$vars{'virclass_user'} = lc substr($vars{'virclass_user'},0,16);
$vars{'virclass_user'} =~ s~[^a-z0-9]~~g;
if ($vars{'app_input_numsys_status'} eq "roman" || $vars{'app_input_numsys_status'} eq "hex") {
$vars{'app_input_comma_status'} = 0;
}
my ($action_url,$form) = &action_url_form("",$vars{'app_lang'},$vars{'app_name'},$vars{'app_sid'},$vars{'virclass_user'});
my $format = $vars{'app_input_paper_format'};
if ($vars{'app_input_paper_format_status'}) {
$format = $vars{'app_input_paper_format_status'};
}
if ($vars{'ono_app_print_format_status'}) {
$format = $vars{'ono_app_print_format_status'};
}
my $canvas = ONO::Lib::PDF::ToolBox->canvas_init($format,25,50,170,190);
my $pdfkey = "$year$mon$mday\_".ONO::Lib::Code::RandomID->make;
$vars{'output_canvas'} = $canvas;
$vars{'output_pdfkey'} = $pdfkey;
if ($vars{'output_paper_embed_q170'} || $vars{'app_input_paper_format_status'} eq "q170") {
$format = "q170";
$canvas = ONO::Lib::PDF::ToolBox->canvas_init($format,0,0,170,170);
}
if ($vars{'output_worksheet_maker'}) {
$format = "q170/48";
$canvas = ONO::Lib::PDF::ToolBox->canvas_init($format,0,0,170,48);
$pdfkey = "$year$mon$mday\_tmp_$vars{'output_paper_id'}_$vars{'output_paper_counter'}";
}
if ($format eq "q170/190") {
$canvas = ONO::Lib::PDF::ToolBox->canvas_init($format,0,0,170,190);
}
if ($format eq "q200") {
$canvas = ONO::Lib::PDF::ToolBox->canvas_init($format,20,5,200,200);
}
if (ONO::IO->devstation) {
# check if we are allowed to use this app...
my ($test1,$test2,$test3);
if (ONO::IO->devstation) {
($test1,$test2,$test3) = (0,0,0);
}
if ($ENV{'SERVER_NAME'} =~ /education\.lu/ || $test1) {
# access is allowed IF location is luxembourg OR user is logged in
if (!$vars{'username'} && !ONO::Lib::Web::LocateIP->locate_country_lu(ONO::Lib::Web::Client->ip(),1)) {
$vars{'output_paper'} = 0;
$vars{'output_web'} = 0;
$vars{'output_login_required'} = 1;
}
}
if ($ENV{'SERVER_NAME'} =~ /oli\.lu/ || $test2) {
# oli.lu no longer allows access to the e-learning apps
$vars{'output_paper'} = 0;
$vars{'output_web'} = 0;
}
if ($ENV{'SERVER_NAME'} =~ /morzino/ || $test3) {
# access is allowed, but SPECIMEN is printed if not logged in
# and online training is blocked...
# (credits checking is still missing here)
if (!$vars{'username'} || $test3) {
$vars{'output_print_specimen'} = 1;
$vars{'output_block_online_checking'} = 1;
if (ONO::FW::Apps::Core->running(\%vars)) {
$form = "form_disabled";
}
}
}
}
return (
$db,
$community,
$action_url,
$form,
$format,
$canvas,
$pdfkey,
$sec,$min,$hour,
$mday,$mon,$year,
$wday,$yday,$timestamp,
$lang,
$BLK_ref,
\%vars,
);
}
sub action_url_form {
#: Return both action URL and action HTML form.
return (
qq~$ENV{'SCRIPT_NAME'}?app_lang=$_[1]&app_name=$_[2]&app_sid=$_[3]&virclass_user=$_[4]&~,
qq~form action="$ENV{'SCRIPT_NAME'}" method="post">
<input type="hidden" name="app_lang" value="$_[1]">
<input type="hidden" name="app_name" value="$_[2]">
<input type="hidden" name="app_sid" value="$_[3]">
<input type="hidden" name="virclass_user" value="$_[4]"~,
);
}
sub action_url_vars {
#: Generate URL variable string.
my $vars_ref = $_[1];
my %vars = %$vars_ref;
my $VARS;
foreach my $key (keys %vars) {
if ($key =~ /^(app_input|output)_/) {
$VARS .= qq~$key=$vars{$key}&~;
}
}
return $VARS;
}
sub input_vars {
my (
$self,
$vars_ref,
$switches,
) = @_;
#: Generate input transmissionHTML code.
#:
#: Switches:
#:
#: -f first question is 1 as first step, not 2
#: -r reload app with same settings mode
#: -R include Report variables
#: -x incluce exercise code and ID (incompatible with -r)
my %vars = %$vars_ref;
if ($switches =~ /f/) {
if ($vars{'output_web_question_number'} < 2) {
$vars{'output_web_question_number'} = 1;
}
} else {
if ($vars{'output_web_question_number'} < 3) {
$vars{'output_web_question_number'} = 2;
}
}
if ($vars{'output_web_answer_status'} == 1) {
$vars{'output_web_question_number'}++;
}
my $INPUT = qq~<input type="hidden" name="output_selected" value="2"><input type="hidden" name="output_web" value="1">~;
if ($switches !~ /r/) {
if ($vars{'output_web_page'} < 2) {
$vars{'output_web_page'} = 1;
}
if ($vars{'output_web_next_page'}) {
$vars{'output_web_page'} = $vars{'output_web_next_page'};
}
$INPUT .= qq~<input type="hidden" name="output_web_page" value="$vars{'output_web_page'}">
<input type="hidden" name="output_web_question_number" value="$vars{'output_web_question_number'}">
<input type="hidden" name="output_web_mistakes" value="$vars{'output_web_mistakes'}">
<input type="hidden" name="exercise" value="$vars{'exercise'}">
<input type="hidden" name="exercise_id" value="$vars{'exercise_id'}">
~;
} else {
if ($switches =~ /x/) {
$INPUT .= qq~<input type="hidden" name="exercise" value="$vars{'exercise'}">
<input type="hidden" name="exercise_id" value="$vars{'exercise_id'}">
~;
}
}
foreach my $key (keys %vars) {
if ($key =~ m~^app_input~) {
$INPUT .= qq~<input type="hidden" name="$key" value="$vars{$key}">~;
}
}
if ($switches =~ /R/) {
foreach my $key (keys %vars) {
if ($key =~ m~^output_web_~) {
my $key2 = $key;
$key2 =~ s~^output_web_~output_report_~;
$INPUT .= qq~<input type="hidden" name="$key2" value="$vars{$key}">~;
}
}
}
return $INPUT;
}
###################################################################
# THAT'S IT :-D
###################################################################
1;
__END__