ONO::Apps::Admin::Init

package ONO::Apps::Admin::Init;
################################################################################
# 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::Lib::Lang::BLK;

use ONO::Apps::Admin::Screen;
use ONO::Apps::Admin::Login;
use ONO::ToolBox::Auth::ToolBox;

use ONO::ToolBox::Auth;
use ONO::ToolBox::ONO;

use ONO::IO;
use ONO::DB;

use ONO::Lib::SQL::Quote;

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

#: This module will kick off the front end application, it handles login,
#: logout, as well as application launch.

use lib "../../ono/sys";

sub init {

my (
$self,
$db,
$community,
$vars_ref,
) = @_;

my %vars = %$vars_ref;

if ($ENV{'REQUEST_URI'} =~ m~/ono/admin/\?/(.*?)/~) {
$vars{'mode'} = $1;
}

$vars{'form'} = qq~form action="/ono/admin/" method="post"~;
if ($vars{'mode'}) {
$vars{'form'} .= qq~><input type="hidden" name="mode" value="$vars{'mode'}"~;
}

my $lang;
my $ono_ref = ONO::ToolBox::ONO->get("h");

my ($BLK_ref,$UCBLK_ref) = ONO::Lib::Lang::BLK->get($lang,$vars_ref);

my $WEB;
my $valid_admin;

if (!$vars{'sid'}) {
($vars_ref,%vars) = ONO::ToolBox::Auth->ono(\%vars,"admin");
}

if ($ENV{'REQUEST_URI'} =~ m~/ono/admin/\?/logout/~) {

$vars_ref = ONO::ToolBox::Auth::ToolBox->logout($db,$community,\%vars);
%vars = %$vars_ref;

}

if ($vars{'sid'} =~ /^root-/) {
$valid_admin++;
} else {
if ($vars{'sid'} =~ /^$vars{'username'}-/) {
$valid_admin = ONO::DB->count($db,"${community}_community_users","username = '$vars{'username'}' AND (type = 'admin' OR type = 'super')");
}
}

if ($valid_admin && $vars{'username'} && $vars{'sid'}) {

$WEB = ONO::Apps::Admin::Screen->screen($db,$community,$lang,$BLK_ref,\%vars,ONO::Lib::SQL::Quote->vars(\%vars),$ono_ref);

} else {

$WEB = ONO::Apps::Admin::Login->screen($lang,$BLK_ref,\%vars,$ono_ref);

}

return $WEB;

}

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

1;

__END__