minecraft 5.94 KB
#!/usr/bin/env perl
use Mojolicious::Lite;
use utf8;
use Mojo::ByteStream 'b';
use DBIx::Custom;
use Mojo::Headers;

# Documentation browser under "/perldoc"
plugin 'PODRenderer';
my $config = plugin 'Config';

app->secret($config->{secret} || 'minecraft');
plugin charset => {charset => $config->{encoding}};

our $dbi;

# Connect to db
if ( $config->{db_driver} eq 'SQLite' ) {
	$dbi = DBIx::Custom->connect(
		dsn => "dbi:$config->{db_driver}:database=$config->{db}",
		option => {sqlite_unicode => 1},
	);
} elsif ( $config->{db_driver} eq 'mysql') {
	$dbi = DBIx::Custom->connect(
		dsn => "dbi:mysql:database=$config->{db};host=$config->{db_host};port=$config->{db_port}",
		user => $config->{db_username},
		password => $config->{db_password},
		option => { PrintError => 1, mysql_enable_utf8 => 1 }
	);
}

sub wikitree {
	my $tree_row = $dbi->select(
			['url', 'name'],
			table  => 'wiki',
		);
	my $tree = $tree_row->all;

	return $tree;
};


get '/' => sub {
	my $self = shift;

	my $news_row = $dbi->select(
		['*'],
		table => 'news',
		append => 'order by id desc limit 20'
	);
	my $news = $news_row->all;

	$self->render(
		template 	=> 'index',
		title 		=> 'Welcome',
		layout 		=> 'default',
		news 	=> $news,
	);
};


get '/download' => sub {
	my $self = shift;

	my @files = glob("public/data/*.7z");
	my @wikitree = wikitree();
	my @data;

	foreach my $x (@files) {
		my ( undef, $path, $file ) = split (/\//, $x);
		push @data, $path . "/" . $file;
	} 

	$self->render(
		template 	=> 'download',
		title 		=> 'Download',
		layout 		=> 'default',
		folders		=> \@data,
		wikitree	=> \@wikitree,
	);
};

get '/wiki/:url' => sub {
	my $self = shift;
	my $url = $self->param('url');

	my @wikitree = wikitree();
	
	my $page_row = $dbi->select(
			['page', 'id'],
			table  => 'wiki',
			where => { url => $url },
		);
	my $page = $page_row->one;


	$self->render(
		template 	=> 'wiki',
		title 		=> 'Wiki',
		layout 		=> 'default',
		wikitree	=> \@wikitree,
		page 		=> $page,
		url 		=> $url,
	);
};

get '/wiki' => sub {
	my $self = shift;

	my @wikitree = wikitree();

	my $page_row = $dbi->select(
			['page', 'id'],
			table  => 'wiki',
		);
	my $page = $page_row->one;

	$self->render(
		template 	=> 'wiki',
		title 		=> 'Wiki',
		layout 		=> 'default',
		wikitree	=> \@wikitree,
		page 		=> $page,
		url 		=> 1,
	);
};


post '/login' => sub {
	my $self 		= shift;
	my $login 		= $self->param('username') || '';
	my $password 	= $self->param('password') || '';
	my $redirect 	= $self->param('redirect') || '/';

	if ( $login eq $config->{username} ) {
		if ( $password eq $config->{userpass} ) {
			$self->session( 'login' => $login );
		} else {
			$redirect = '/';
		}
	} else {
		$redirect = '/';
	}

	$self->redirect_to( $redirect );
};

get '/logout' => sub {
	my $self = shift;
	$self->session( 'expires' => 1 );
	$self->redirect_to( '/' );
}; 

# Authentication
under sub {
    my $self = shift;

    # Authenticated
    return 1
        if $self->session( 'login' );

    # Not authenticated
    $self->flash( 'message' => 'You are not logged in' );
    $self->render(
    	title => 'login',
        'template' => 'login',
        'status' => 401,
    );
    return;
};

any '/news/new' => sub {
	my $self = shift;
	my $name = $self->param('name');
	my $article = $self->param('article');

	if ( !$name ) {
		$self->render(
			template 	=> 'news/new',
			title 		=> 'News',
			layout 		=> 'default',
		);
	} else {
		$dbi->insert(
			{ 
				article => $article,
				name => $name,
			}, 
			table  => 'news',
		);
		$self->redirect_to('/');
	}
};

get '/news/:id' => sub {
	my $self = shift;
	my $action = $self->param('a');
	my $id = $self->param('id');

	if ( $action eq 'edit' ) {		
		my $article_row = $dbi->select(
			['*'],
			table  => 'news',
		);
		my $article = $article_row->one;

		$self->render(
			template 	=> 'news/edit',
			title 		=> 'News',
			layout 		=> 'default',
			article 	=> $article,
		);
	} elsif ( $action eq 'delete' ) {
		my $status = $dbi->delete(
			table  => 'news', 
			where => { id => $id } 
		);
		$self->redirect_to('/');
	}

};

post '/news/:id' => sub {
	my $self = shift;
	my $action = $self->param('a');
	my $name = $self->param('name');
	my $article = $self->param('article');
	my $id = $self->param('id');

	if ( $action eq 'edit' ) {
		$dbi->update(
			{ 
				article => $article,
				name => $name,
			}, 
			table  => 'news',
			where  => { id => $id }
		);
		$self->redirect_to('/');	
	} else {

	}

};

get '/wiki/:url/edit' => sub {
	my $self = shift;
	my $url = $self->param('url');

	my @wikitree = wikitree();
	
	my $page_row = $dbi->select(
			['id', 'page', 'url', 'name'],
			table  => 'wiki',
			where => { url => $url },
		);
	my $page = $page_row->one;


	$self->render(
		template 	=> 'wiki/edit',
		title 		=> 'Wiki',
		layout 		=> 'default',
		wikitree	=> \@wikitree,
		page 		=> $page,
		url 		=> $url,
	);
};

get '/wiki/add/new' => sub {
	my $self = shift;
	my $url = $self->param('url');

	my @wikitree = wikitree();

	$self->render(
		template 	=> 'wiki/new',
		title 		=> 'Wiki',
		layout 		=> 'default',
		wikitree	=> \@wikitree,
	);
};

get '/wiki/:id/delete' => sub {
	my $self 	= shift;
	my $id 		= $self->param('id');

	my $status = $dbi->delete(
		table  => 'wiki', 
		where => { id => $id } 
	);

	$self->redirect_to('/wiki');
};

post '/wiki/:id/edit' => sub {
	my $self 	= shift;
	my $id 		= $self->param('page_id');
	my $url 	= $self->param('page_url');
	my $page 	= $self->param('page_text');
	my $name 	= $self->param('page_name');

	$dbi->update(
		{ 
			url => $url,
			page => $page,
			name => $name,
		}, 
		table  => 'wiki',
		where  => { id => $id }
	);
	$self->redirect_to("/wiki/$url");
};

post '/wiki/add/new' => sub {
	my $self 	= shift;
	my $id 		= $self->param('page_id');
	my $url 	= $self->param('page_url');
	my $page 	= $self->param('page_text');
	my $name 	= $self->param('page_name');

	$dbi->insert(
		{ 
			url => $url,
			page => $page,
			name => $name,
		}, 
		table  => 'wiki',
	);
	$self->redirect_to("/wiki/$url");
};

app->start;
__DATA__