package MList_Manager;
use base 'CGI::Application';
use base 'MList_Manager::settings';
use base 'MList_Manager::mailing_lists';
use base 'MList_Manager::subscribers';
use base 'MList_Manager::admin_subscribers';
use base 'MList_Manager::admin_messages';
########
########
######## When I add new section I need to add it here
########
########
use CGI::Session;
use Config::Trivial;
use DBI;
sub setup {
my $self = shift;
$self->mode_param('action');
$self->start_mode('main');
$self->run_modes([qw/
not_loged
login
logout
main
settings
mailing_lists
subscribers
admin_subscribers
admin_messages
/]);
########
########
######## And here also ...
########
########
}
sub cgiapp_prerun() {
my $self = shift;
my $q = $self->query();
my $f = $q->Vars();
my $run_mode = $self->get_current_runmode();
unless($run_mode eq 'subscribers'){
my $session = CGI::Session->new('driver::File',$q,{Directory => 'sessions'});
my $cookie = $q->cookie(CGISESSID => $session->id);
$self->header_add(-cookie => [$cookie]);
$self->param('session' => $session);
# Configs
my $c = $self->param('settings');
unless($run_mode eq 'login'){
unless(defined($session->param('admin_user')) && ($session->param('admin_user') eq $c->{'admin_user'}) &&
($session->param('admin_pass') eq $c->{'admin_pass'})){
$self->prerun_mode('not_loged');
}
}
}
}
sub not_loged {
my $self = shift;
my $comments = shift;
my $t = $self->load_tmpl(filename => 'login.html',die_on_bad_param=>0);
$t->param(%{$comments}) if defined $comments;
return $t->output();
}
sub login {
my $self = shift;
my $q = $self->query();
my $f = $q->Vars();
# Configs
my $c = $self->param('settings');
if($f->{admin_pass} eq $c->{admin_pass} && $f->{admin_user} eq $c->{admin_user}) {
# Login OK
my $session = $self->param('session');
$session->param('admin_user' => $c->{admin_user});
$session->param('admin_pass' => $c->{admin_pass});
$session->expire('+5h');
my $cookie = $q->cookie(CGISESSID => $session->id);
$self->header_add(-cookie => [$cookie]);
my $settings = {};
my $config;
if(-e 'config.dat'){
$config = Config::Trivial->new(config_file => 'config.dat');
$settings = $config->read();
}
# Fix !! Strip off everything after ? in the $f->{index}
$f->{index} = split(/\?/,$f->{index}) if defined $f->{index};
$settings->{'index_url'} = $f->{'index'} if ! defined $settings->{'index_url'};
Config::Trivial->write(
config_file => 'config.dat',
configuration => $settings
);
return $self->main();
} else {
# Login ERROR
return $self->not_loged({'message' => 'Wrong username or password'});
}
}
sub logout {
# He is allready loged in ...
my $self = shift;
my $session = $self->param('session');
$session->param('admin_user' => '');
$session->param('admin_pass' => '');
my $t = $self->load_tmpl(filename => 'logout.html', die_on_bad_params => 0);
return $t->output();
}
sub main {
my $self = shift;
my $t = $self->load_tmpl(filename => 'main.html', die_on_bad_params => 0);
my $c = $self->param('settings');
if(defined $c->{total_ok} && $c->{total_ok} == 1){
$t->param({'mailing_lists' => $self->DB_execute({'sql' => 'SELECT COUNT(*) FROM mailing_lists',
'method' => 'fetchrow_array'}),
'subscribers' => $self->DB_execute({'sql' => 'SELECT COUNT(*) FROM subscribers WHERE status = ?',
'data' => ['subscribed'],
'method' => 'fetchrow_array'})
});
} else {
$t->param({'mailing_lists'=> " N/A ",
'subscribers' => " N/A "});
}
$t->param('index_url' => $c->{index_url});
return $t->output();
}
1;
####
package MList_Manager::mailing_lists;
....
sub mailing_lists {
my $self = shift;
my $q = $self->query();
my $f = $q->Vars();
# Code that calls each section from the file that it's
# part of. In case some section doesnt exists, AUTOLOADER
# is called automaticly.
# If nothig is to be called, it calls mailing_lists_main
if (defined($f->{section}) && (length($f->{section})>=1)){
my $section = $_file . $f->{section};
return &$section($self);
########
######## faking it here since this is actualy
######## executed as main module wich inherited this
######## methods ...
########
########
}
return $self->mailing_lists_main();
}
....