Compare commits

..

No commits in common. "c8c1b7aeb1dd8df3ec1b9dce85795fcb260603bc" and "cf91c02ba076854e3294bf531acc980c5af7a24f" have entirely different histories.

17 changed files with 140 additions and 153 deletions

View File

@ -12,39 +12,39 @@ use CharmBoard::Util::Crypt::Seasoning;
# this method will run once at server start
sub startup {
my $app = shift;
my $self = shift;
# load plugins that require no additional conf
$app->plugin('TagHelpers');
$app->plugin('Model', {namespaces => ['CharmBoard::Model']});
$self->plugin('TagHelpers');
$self->plugin('Model', {namespaces => ['CharmBoard::Model']});
# load configuration from config file
my $config =
$app->plugin('Config' => { file => 'charmboard.conf' });
$self->plugin('Config' => { file => 'charmboard.conf' });
# set this specific forum's name
$app->helper(board_name => sub { $config->{board_name} });
$self->helper(board_name => sub { $config->{board_name} });
# load dev env only stuff, if applicable
if (lc($config->{environment}) eq 'dev') {
$app->renderer->cache->max_keys(0)
$self->renderer->cache->max_keys(0)
}
# import Mojolicious secrets
$app->secrets($config->{secrets});
$self->secrets($config->{secrets});
# import password pepper value
$app->helper(pepper => sub { $config->{pass_crypt}->{pepper} });
$self->helper(pepper => sub { $config->{pass_crypt}->{pepper} });
## database setup
# ? this could maybe be a given/when
{
my ($_dsn, $_unicode);
if (lc($app->config->{database}->{type}) eq 'sqlite') {
if (lc($self->config->{database}->{type}) eq 'sqlite') {
$_dsn = "dbi:SQLite:" . $config->{database}->{name};
$_unicode = "sqlite_unicode"
} elsif (lc($app->config->{database}->{type}) eq 'mariadb') {
} elsif (lc($self->config->{database}->{type}) eq 'mariadb') {
$_dsn = "dbi:mysql:" . $config->{database}->{name};
$_unicode = "mysql_enable_utf"
@ -63,18 +63,18 @@ sub startup {
{ $_unicode => 1 }
);
$app->helper(schema => sub { $schema })
$self->helper(schema => sub { $schema })
}
# session helpers
## create session
$app->helper(session_create => sub {
my $app = shift;
$self->helper(session_create => sub {
my $self = shift;
my $_session_key = seasoning(16);
# create session entry in db
$app->schema->resultset('Session')->create({
$self->schema->resultset('Session')->create({
session_key => $_session_key,
user_id => $_[0],
session_expiry => time + 604800,
@ -83,69 +83,64 @@ sub startup {
});
# now create session cookie
$app->session(is_auth => 1 );
$app->session(user_id => $_[0] );
$app->session(session_key => $_session_key);
$app->session(expiration => 604800 );
$self->session(is_auth => 1 );
$self->session(user_id => $_[0] );
$self->session(session_key => $_session_key);
$self->session(expiration => 604800 );
});
## destroy session
$app->helper(session_destroy => sub {
my $app = shift;
$self->helper(session_destroy => sub {
my $self = shift;
my $_session_key = $app->session('session_key');
my $_session_key = $self->session('session_key');
# destroy entry for this session in the database
$app->schema->resultset('Session')
$self->schema->resultset('Session')
->search({ session_key => $_session_key })
->delete;
# now nuke the actual session cookie
$app->session(expires => 1);
$self->session(expires => 1);
});
## verify session
$app->helper(session_verify => sub {
my $app = shift;
$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;
# get info from user's session cookie and store it in vars
my $_user_id = $app->session('user_id');
my $_session_key = $app->session('session_key');
my $_is_auth = $app->session('is_auth');
if ($_is_auth) {
try {
# check to see if session with this id is present in db
($app->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 == ($app->schema->resultset('Session')->
session_uid($_session_key))
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 if session is still within valid time as recorded in
# the db
time < ($app->schema->resultset('Session')->
session_expiry($_session_key))
or die;
} catch ($_catch_error) {
$_validity = undef;
$app->session_destroy;
}
} else {
$_validity = 0;
# 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;
});
# router
my $r = $app->routes;
my $r = $self->routes;
# controller routes
## index page

View File

@ -10,15 +10,11 @@ use Mojo::Base 'Mojolicious::Controller', -signatures;
use Tree::Simple;
sub index {
my $c = shift;
my $self = shift;
if ($c->session_verify eq undef) {
$c->redirect_to('/')
}
$c->render(
$self->render(
template => 'index',
category_tree => $c->model('forums')->list_full
category_tree => $self->model('forums')->list_full
)
}

View File

@ -11,19 +11,19 @@ use CharmBoard::Util::Crypt::Password;
use CharmBoard::Util::Crypt::Seasoning;
sub login {
my $c = shift;
my $self = shift;
$c->render(
$self->render(
template => 'login',
error => $c->flash('error'),
message => $c->flash('message')
error => $self->flash('error'),
message => $self->flash('message')
)
}
sub login_do {
my $c = shift;
my $username = $c->param('username');
my $password = $c->pepper . ':' . $c->param('password');
my $self = shift;
my $username = $self->param('username');
my $password = $self->pepper . ':' . $self->param('password');
my $catch_error;
@ -34,7 +34,7 @@ sub login_do {
# check user credentials first
try {
# check to see if user by entered username exists
$user_info = $c->schema->resultset('Users')
$user_info = $self->schema->resultset('Users')
->search({ username => $username });
$user_info or die;
@ -45,27 +45,27 @@ sub login_do {
} catch ($catch_error) { # redirect to login page on fail
print $catch_error;
$c->flash(error => 'Username or password incorrect.');
$c->redirect_to('login');
$self->flash(error => 'Username or password incorrect.');
$self->redirect_to('login');
}
try { # now attempt to create session
# get user ID for session creation
$user_id = $user_info->get_column('user_id')->first;
$c->session_create($user_id);
$self->session_create($user_id);
# redirect to index upon success
$c->redirect_to('/')
$self->redirect_to('/')
} catch ($catch_error) { # redirect to login page on fail
print $catch_error;
$c->flash(
$self->flash(
error => 'Your username and password were correct, but a server
error prevented you from logging in. This has been logged
so the administrator can fix it.'
);
$c->redirect_to('login')
$self->redirect_to('login')
}
}

View File

@ -9,12 +9,12 @@ use feature ':5.20';
use Mojo::Base 'Mojolicious::Controller', -signatures;
sub logout_do {
my $c = shift;
my $self = shift;
$c->session_destroy;
$self->session_destroy;
# redirect to index
$c->redirect_to('/')
$self->redirect_to('/')
}
1;

View File

@ -9,33 +9,32 @@ use feature ':5.20';
use Mojo::Base 'Mojolicious::Controller', -signatures;
sub thread_compose {
my $c = shift;
my $self = shift;
my $subf_id = $c->param('id');
my $subf_id = $self->param('id');
my $subf_cat =
$c->schema->resultset('Subforums')->cat_from_id($subf_id);
$self->schema->resultset('Subforums')->cat_from_id($subf_id);
my $cat_title =
$c->schema->resultset('Categories')
$self->schema->resultset('Categories')
->title_from_id($subf_cat);
$c->render(
$self->render(
template => 'thread_compose',
subf_id => $subf_id,
cat_title => $cat_title,
subf_title => $c->schema->resultset('Subforums')
subf_title => $self->schema->resultset('Subforums')
->title_from_id($subf_id),
error => $c->flash('error'),
message => $c->flash('message')
error => $self->flash('error'),
message => $self->flash('message')
)
}
sub thread_submit {
my $c = shift;
my $self = shift;
my $thread_title = $c->param('thread-title');
my $post_content = $c->param('post-content');
my $thread_title = $self->param('thread-title');
my $post_content = $self->param('post-content');
my $post_time = time;
my $subf_id = $c->param('id');
my $catch_error;
@ -44,12 +43,9 @@ sub thread_submit {
($thread_title, $post_content)
or die "Please fill both the title and post content fields"
} catch ($catch_error) {
$c->flash(error => $catch_error);
$c->redirect_to('board/:id/new')
$self->flash(error => $catch_error);
$self->redirect_to('/:id/new')
}
# now send it
}
1;

View File

@ -11,22 +11,22 @@ use CharmBoard::Util::Crypt::Password;
# initial registration page
sub register {
my $c = shift;
$c->render(
my $self = shift;
$self->render(
template => 'register',
error => $c->flash('error'),
message => $c->flash('message')
error => $self->flash('error'),
message => $self->flash('message')
)
}
# process submitted registration form
sub register_do {
my $c = shift;
my $self = shift;
my $username = $c->param('username');
my $email = $c->param('email');
my $password = $c->param('password');
my $confirm_password = $c->param('confirm-password');
my $username = $self->param('username');
my $email = $self->param('email');
my $password = $self->param('password');
my $confirm_password = $self->param('confirm-password');
my $catch_error;
@ -50,9 +50,9 @@ sub register_do {
# check to make sure username and/or email isn't already in use;
# if not, continue with registration
## search for input username and email in database
$user_check = $c->schema->resultset('Users')
$user_check = $self->schema->resultset('Users')
->search({ username => $username })->single;
$email_check = $c->schema->resultset('Users')
$email_check = $self->schema->resultset('Users')
->search({ email => $email })->single;
# TODO: compress this into something less redundant
@ -63,18 +63,18 @@ sub register_do {
($email_check) eq undef
or die "email already in use."
} catch ($catch_error) {
$c->flash(error => $catch_error);
$c->redirect_to('register')
$self->flash(error => $catch_error);
$self->redirect_to('register')
}
try {
$password = $c->pepper . ':' . $password;
$password = $self->pepper . ':' . $password;
# return hashed result + salt
($salt, $hash) = passgen($password) or die;
# add user info and pw/salt to DB
$c->schema->resultset('Users')->create({
$self->schema->resultset('Users')->create({
username => $username,
email => $email,
password => $hash,
@ -83,17 +83,17 @@ sub register_do {
})
or die;
$c->flash(message => 'User registered successfully!');
$c->redirect_to('register')
$self->flash(message => 'User registered successfully!');
$self->redirect_to('register')
} catch ($catch_error) {
print $catch_error;
$c->flash(
$self->flash(
error =>
'Your registration info was correct, but a server error
prevented you from registering. This has been logged so the
administrator can fix it.'
);
$c->redirect_to('register')
$self->redirect_to('register')
}
}

View File

@ -9,23 +9,23 @@ use feature ':5.20';
use Mojo::Base 'Mojolicious::Controller', -signatures;
sub subf_view {
my $c = shift;
my $self = shift;
my $subf_id = $c->param('id');
my $subf_id = $self->param('id');
my $subf_cat =
$c->schema->resultset('Subforums')->cat_from_id($subf_id);
$self->schema->resultset('Subforums')->cat_from_id($subf_id);
my $cat_title =
$c->schema->resultset('Categories')
$self->schema->resultset('Categories')
->title_from_id($subf_cat);
my @thread_list =
$c->schema->resultset('Threads')->fetch_by_subf($subf_id);
$self->schema->resultset('Threads')->fetch_by_subf($subf_id);
$c->render(
$self->render(
template => 'subf',
subf_id => $subf_id,
cat_title => $cat_title,
subf_title => $c->schema->resultset('Subforums')
subf_title => $self->schema->resultset('Subforums')
->title_from_id($subf_id),
thread_list => \@thread_list
)

View File

@ -10,9 +10,9 @@ use Mojo::Base 'MojoX::Model';
use Tree::Simple;
sub list_full {
my $c = shift;
my $self = shift;
# fetch a list of all categories
my @_all_cat = $c->{app}->schema->resultset('Categories')->fetch_all;
my @_all_cat = $self->{app}->schema->resultset('Categories')->fetch_all;
# create a Tree::Simple object that will contain the list
# of categories and the subforums that belong to them
@ -26,7 +26,7 @@ sub list_full {
# fetch all subforums that belong to this category
@_fetch_subf =
$c->{app}->schema->resultset('Subforums')
$self->{app}->schema->resultset('Subforums')
->fetch_by_cat($_iter_cat);
# add each fetched subforum as children of the branch

View File

@ -9,18 +9,18 @@ use feature ':5.20';
use base 'DBIx::Class::ResultSet';
sub fetch_all {
my $set = shift;
my $_set = shift;
my $_fetch = $set->search({}, { order_by => 'cat_rank' });
my $_fetch = $_set->search({}, { order_by => 'cat_rank' });
return ($_fetch->get_column('cat_id')->all)
}
sub title_from_id {
my $set = shift;
my $_set = shift;
return (
$set->search({ 'cat_id' => $_[0] })->get_column('cat_name')
$_set->search({ 'cat_id' => $_[0] })->get_column('cat_name')
->first)
}

View File

@ -9,26 +9,26 @@ use feature ':5.20';
use base 'DBIx::Class::ResultSet';
sub fetch_by_cat {
my $set = shift;
my $_set = shift;
my $_fetch = $set->search({ 'subf_cat' => $_[0] },
my $_fetch = $_set->search({ 'subf_cat' => $_[0] },
{ order_by => 'subf_rank' });
return ($_fetch->get_column('subf_id')->all)
}
sub cat_from_id {
my $set = shift;
my $_set = shift;
return (
$set->search({ 'subf_id' => $_[0] })->get_column('subf_cat')
$_set->search({ 'subf_id' => $_[0] })->get_column('subf_cat')
->first)
}
sub title_from_id {
my $set = shift;
my $_set = shift;
return ($set->search({ 'subf_id' => $_[0] })
return ($_set->search({ 'subf_id' => $_[0] })
->get_column('subf_name')->first)
}

View File

@ -9,18 +9,18 @@ use feature ':5.20';
use base 'DBIx::Class::ResultSet';
sub fetch_by_subf {
my $set = shift;
my $_set = shift;
my $_fetch =
$set->search({ 'thread_subf' => $_[0] });
$_set->search({ 'thread_subf' => $_[0] });
return ($_fetch->get_column('thread_id')->all)
}
sub title_from_id {
my $set = shift;
my $_set = shift;
return ($set->search({ 'thread_id' => $_[0] })
return ($_set->search({ 'thread_id' => $_[0] })
->get_column('thread_title')->first)
}

View File

@ -1,5 +1,5 @@
% layout 'default',
% title => $c->board_name;
% title => $self->board_name;
<% my $cat_header = begin %>
% my $_cat_id = shift; my $_name = shift;
@ -21,13 +21,13 @@
foreach my $category ($category_tree->getAllChildren) { %>
<%= $cat_header->(
$category->getNodeValue,
$c->schema->resultset('Categories')->
$self->schema->resultset('Categories')->
title_from_id($category->getNodeValue)) %>
<%
foreach my $subforum ($category->getAllChildren) { %>
<%= $subf_item->(
$subforum->getNodeValue,
$category->getNodeValue,
$c->schema->resultset('Subforums')->
$self->schema->resultset('Subforums')->
title_from_id($subforum->getNodeValue)) %>
<% }} %>

View File

@ -2,13 +2,13 @@
my $userControls;
# TODO: once implemented, put username + profile link first
if ($c->session('is_auth')) {
my $username = '';
if ($self->session('is_auth') == 1) {
my $username =
$userControls = "<a href=\"/logout\">logout</a>"}
else {
$userControls =
"<a href=\"/login\">login</a> |
<a href=\"/register\">register</a>"};
%>
<a href="/"><h2><%== $c->board_name %></h2></a>
<a href="/"><h2><%== $self->board_name %></h2></a>
<%== $userControls %><br /><br />

View File

@ -1,5 +1,5 @@
% layout 'default',
% title => $c->board_name . ' - Login';
% title => $self->board_name . ' - Login';
% if ($error) {
<p style="color: red"><%= $error %></p>
%};

View File

@ -1,5 +1,5 @@
% layout 'default',
% title => $c->board_name . ' - Registration';
% title => $self->board_name . ' - Registration';
% if ($error) {
<p style="color: red"><%= $error %></p>
%};

View File

@ -1,5 +1,5 @@
% layout 'default',
% title => $subf_title . ' - ' . $c->board_name;
% title => $subf_title . ' - ' . $self->board_name;
% my @thread_list = @{stash('thread_list')};
<% my $thread_item = begin %>
@ -9,7 +9,7 @@
</div>
<% end %>
<a href="/"><%= $c->board_name %></a> » <%= $cat_title %> »
<a href="/"><%= $self->board_name %></a> » <%= $cat_title %> »
<%= $subf_title %>
<br /><br />
@ -20,6 +20,6 @@ like to <a href="<%= $subf_id %>/new/">make one?</a>
foreach my $thread_id (@thread_list) { %>
<%= $thread_item->(
$thread_id,
$c->schema->resultset('Threads')->
$self->schema->resultset('Threads')->
title_from_id($thread_id)) %>
<% }} %>

View File

@ -1,6 +1,6 @@
% layout 'default',
% title => 'New thread - ' . $c->board_name;
<a href="/"><%= $c->board_name %></a> » <%= $cat_title %> »
% title => 'New thread - ' . $self->board_name;
<a href="/"><%= $self->board_name %></a> » <%= $cat_title %> »
<%= $subf_title %> » new thread
<br /><br />
<form method="post" action="/board/<%= $subf_id %>/new/">