2023-05-06 04:24:49 +00:00
|
|
|
package CharmBoard;
|
2023-05-15 23:50:44 +00:00
|
|
|
|
2023-05-09 06:37:07 +00:00
|
|
|
use utf8;
|
2023-05-15 23:50:44 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2024-08-19 08:13:01 +00:00
|
|
|
use experimental qw(try);
|
|
|
|
use feature ':5.20';
|
2023-05-15 23:50:44 +00:00
|
|
|
|
2023-05-06 04:24:49 +00:00
|
|
|
use Mojo::Base 'Mojolicious', -signatures;
|
2024-08-08 08:05:26 +00:00
|
|
|
use CharmBoard::Model::Schema;
|
2024-08-22 09:07:50 +00:00
|
|
|
use CharmBoard::Util::Crypt::Seasoning;
|
2023-05-06 04:24:49 +00:00
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
# this method will run once at server start
|
2023-05-15 23:50:44 +00:00
|
|
|
sub startup {
|
|
|
|
my $self = shift;
|
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
# load plugins that require no additional conf
|
2023-05-09 06:37:07 +00:00
|
|
|
$self->plugin('TagHelpers');
|
2024-08-20 03:28:05 +00:00
|
|
|
$self->plugin('Model', {namespaces => ['CharmBoard::Model']});
|
2023-05-15 23:50:44 +00:00
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
# load configuration from config file
|
2023-05-17 16:12:48 +00:00
|
|
|
my $config =
|
|
|
|
$self->plugin('Config' => { file => 'charmboard.conf' });
|
2023-05-09 06:37:07 +00:00
|
|
|
|
|
|
|
# set this specific forum's name
|
2023-05-17 16:12:48 +00:00
|
|
|
$self->helper(board_name => sub { $config->{board_name} });
|
2023-05-06 04:24:49 +00:00
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
# load dev env only stuff, if applicable
|
2024-08-19 08:28:14 +00:00
|
|
|
if (lc($config->{environment}) eq 'dev') {
|
2023-05-17 16:12:48 +00:00
|
|
|
$self->renderer->cache->max_keys(0)
|
|
|
|
}
|
2023-05-06 04:24:49 +00:00
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
# import Mojolicious secrets
|
2023-05-09 06:37:07 +00:00
|
|
|
$self->secrets($config->{secrets});
|
2023-05-06 04:24:49 +00:00
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
# import password pepper value
|
2023-05-17 16:12:48 +00:00
|
|
|
$self->helper(pepper => sub { $config->{pass_crypt}->{pepper} });
|
2023-05-07 06:04:15 +00:00
|
|
|
|
|
|
|
## database setup
|
2023-05-09 06:37:07 +00:00
|
|
|
# ? this could maybe be a given/when
|
2023-05-17 16:12:48 +00:00
|
|
|
{
|
|
|
|
my ($_dsn, $_unicode);
|
2024-08-19 08:13:01 +00:00
|
|
|
if (lc($self->config->{database}->{type}) eq 'sqlite') {
|
2023-05-17 16:12:48 +00:00
|
|
|
$_dsn = "dbi:SQLite:" . $config->{database}->{name};
|
|
|
|
$_unicode = "sqlite_unicode"
|
2023-05-17 04:18:25 +00:00
|
|
|
|
2024-08-19 08:13:01 +00:00
|
|
|
} elsif (lc($self->config->{database}->{type}) eq 'mariadb') {
|
2023-05-17 16:12:48 +00:00
|
|
|
$_dsn = "dbi:mysql:" . $config->{database}->{name};
|
|
|
|
$_unicode = "mysql_enable_utf"
|
2023-05-17 04:18:25 +00:00
|
|
|
|
2023-05-17 16:12:48 +00:00
|
|
|
} else {
|
|
|
|
die "\nUnknown, unsupported, or empty database type
|
2023-05-17 04:18:25 +00:00
|
|
|
in charmboard.conf. If you're sure you've set it to
|
|
|
|
something supported, maybe double check your spelling?
|
|
|
|
\n\n\t
|
2023-05-17 16:12:48 +00:00
|
|
|
Valid options: 'sqlite', 'mariadb'"
|
|
|
|
}
|
2023-05-17 04:18:25 +00:00
|
|
|
|
2024-08-08 08:05:26 +00:00
|
|
|
our $schema = CharmBoard::Model::Schema->connect(
|
2023-05-17 04:18:25 +00:00
|
|
|
$_dsn,
|
|
|
|
$config->{database}->{user},
|
|
|
|
$config->{database}->{pass},
|
2023-05-17 16:12:48 +00:00
|
|
|
{ $_unicode => 1 }
|
|
|
|
);
|
2023-05-17 04:18:25 +00:00
|
|
|
|
2023-05-17 16:12:48 +00:00
|
|
|
$self->helper(schema => sub { $schema })
|
|
|
|
}
|
2023-05-06 04:24:49 +00:00
|
|
|
|
2024-08-22 09:07:50 +00:00
|
|
|
# session helpers
|
|
|
|
## create session
|
|
|
|
$self->helper(session_create => sub {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $_session_key = seasoning(16);
|
|
|
|
|
|
|
|
# create session entry in db
|
|
|
|
$self->schema->resultset('Session')->create({
|
|
|
|
session_key => $_session_key,
|
|
|
|
user_id => $_[0],
|
|
|
|
session_expiry => time + 604800,
|
|
|
|
is_ip_bound => 0,
|
|
|
|
bound_ip => undef
|
|
|
|
});
|
|
|
|
|
|
|
|
# now create session cookie
|
|
|
|
$self->session(is_auth => 1 );
|
|
|
|
$self->session(user_id => $_[0] );
|
|
|
|
$self->session(session_key => $_session_key);
|
|
|
|
$self->session(expiration => 604800 );
|
|
|
|
});
|
|
|
|
## destroy session
|
|
|
|
$self->helper(session_destroy => sub {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $_session_key = $self->session('session_key');
|
|
|
|
|
|
|
|
# destroy entry for this session in the database
|
|
|
|
$self->schema->resultset('Session')
|
|
|
|
->search({ session_key => $_session_key })
|
|
|
|
->delete;
|
|
|
|
|
|
|
|
# now nuke the actual session cookie
|
|
|
|
$self->session(expires => 1);
|
|
|
|
});
|
2024-08-23 03:44:18 +00:00
|
|
|
## verify session
|
|
|
|
$self->helper(session_verify => sub {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# get info from user's session cookie and store it in vars
|
|
|
|
my $_user_id = $self->session('user_id');
|
|
|
|
my $_session_key = $self->session('session_key');
|
|
|
|
|
|
|
|
my $_validity = 1;
|
|
|
|
my $_catch_error;
|
|
|
|
|
|
|
|
try {
|
|
|
|
# check to see if session with this id is present in db
|
|
|
|
($self->schema->resultset('Session')->search
|
|
|
|
({ 'session_key' => $_session_key })
|
|
|
|
->get_column('session_key')->first)
|
|
|
|
or die;
|
|
|
|
|
|
|
|
# check to see if the current session key's user id matches
|
|
|
|
# that of the user id in the database
|
|
|
|
$_user_id == ($self->schema->resultset('Session')->
|
|
|
|
session_uid($_session_key))
|
|
|
|
or die;
|
|
|
|
|
|
|
|
# check if session is still within valid time as recorded in
|
|
|
|
# the db
|
|
|
|
time < ($self->schema->resultset('Session')->
|
|
|
|
session_expiry($_session_key))
|
|
|
|
or die;
|
|
|
|
} catch ($_catch_error) {
|
|
|
|
$_validity = undef;
|
|
|
|
$self->session_destroy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $_validity;
|
|
|
|
});
|
2024-08-22 09:07:50 +00:00
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
# router
|
2023-05-09 06:37:07 +00:00
|
|
|
my $r = $self->routes;
|
2023-05-06 04:24:49 +00:00
|
|
|
|
2024-08-08 07:22:31 +00:00
|
|
|
# controller routes
|
|
|
|
## index page
|
|
|
|
$r->get('/')->to(
|
|
|
|
controller => 'Controller::Index',
|
|
|
|
action => 'index'
|
|
|
|
);
|
2023-05-09 06:37:07 +00:00
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
## registration page
|
2023-05-06 04:24:49 +00:00
|
|
|
$r->get('/register')->to(
|
2023-05-09 06:37:07 +00:00
|
|
|
controller => 'Controller::Register',
|
2023-05-17 16:12:48 +00:00
|
|
|
action => 'register'
|
|
|
|
);
|
2023-05-06 04:24:49 +00:00
|
|
|
$r->post('/register')->to(
|
2023-05-09 06:37:07 +00:00
|
|
|
controller => 'Controller::Register',
|
2023-05-17 16:12:48 +00:00
|
|
|
action => 'register_do'
|
|
|
|
);
|
2023-05-09 06:37:07 +00:00
|
|
|
|
2023-05-07 06:04:15 +00:00
|
|
|
## login page
|
2023-05-06 04:24:49 +00:00
|
|
|
$r->get('/login')->to(
|
2023-05-09 06:37:07 +00:00
|
|
|
controller => 'Controller::Login',
|
2023-05-17 16:12:48 +00:00
|
|
|
action => 'login'
|
|
|
|
);
|
2023-05-06 04:24:49 +00:00
|
|
|
$r->post('/login')->to(
|
2023-05-09 06:37:07 +00:00
|
|
|
controller => 'Controller::Login',
|
2023-05-17 16:12:48 +00:00
|
|
|
action => 'login_do'
|
|
|
|
);
|
2023-05-15 23:50:44 +00:00
|
|
|
|
2023-05-09 06:37:07 +00:00
|
|
|
## logout
|
|
|
|
$r->get('/logout')->to(
|
|
|
|
controller => 'Controller::Logout',
|
2023-05-17 16:12:48 +00:00
|
|
|
action => 'logout_do'
|
2024-08-20 03:41:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# view subforum
|
2024-08-21 04:06:06 +00:00
|
|
|
$r->get('/board/:id')->to(
|
2024-08-20 03:41:58 +00:00
|
|
|
controller => 'Controller::ViewSubf',
|
|
|
|
action => 'subf_view'
|
|
|
|
);
|
|
|
|
|
|
|
|
# create thread
|
2024-08-21 04:06:06 +00:00
|
|
|
$r->get('/board/:id/new')->to(
|
2024-08-20 03:41:58 +00:00
|
|
|
controller => 'Controller::NewThread',
|
|
|
|
action => 'thread_compose'
|
|
|
|
);
|
2024-08-21 04:06:06 +00:00
|
|
|
$r->post('/board/:id/new')->to(
|
2024-08-20 03:41:58 +00:00
|
|
|
controller => 'Controller::NewThread',
|
|
|
|
action => 'thread_submit'
|
|
|
|
);
|
2023-05-06 04:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
2023-05-17 04:18:25 +00:00
|
|
|
__END__
|
|
|
|
|
|
|
|
=pod
|
|
|
|
=head1 NAME
|
|
|
|
CharmBoard - revive the fun posting experience!
|
|
|
|
|
|
|
|
=head1 NOTES
|
2023-05-17 16:12:48 +00:00
|
|
|
This documentation is intended for prospective code contributors. If
|
|
|
|
you're looking to set CharmBoard up, look for the Markdown format
|
|
|
|
(.md) documentation instead.
|
2023-05-17 04:18:25 +00:00
|
|
|
|
2023-05-17 16:12:48 +00:00
|
|
|
CharmBoard uses a max line length of 70 chars and a tab size of two
|
|
|
|
spaces.
|
2023-05-17 04:18:25 +00:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
2023-05-17 16:12:48 +00:00
|
|
|
CharmBoard is forum software written in Perl with Mojolicious,
|
|
|
|
intended to be a more fun alternative to the bigger forum suites
|
2024-08-21 04:34:54 +00:00
|
|
|
available today, inspired by older forum software like AcmlmBoard.
|
2023-05-17 04:18:25 +00:00
|
|
|
=cut
|