#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI qw(:standart);
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;
use DBI;
use POSIX;
require('config.cgi');
our %config;
my $cgi = new CGI;
print $cgi->header();
# ---------- Ip auf Blackliste überprüfen ---------- #
my $dbh = DBI->connect("dbi:mysql:$config{'mysql_db'}:$config{'mysql_host'}","$config{'mysql_user'}","$config{'mysql_pass'}") || die("$DBI::errstr\n");
my $sth = $dbh->prepare("SELECT * FROM blacklist WHERE ip=?") || die("$DBI::errstr\n");
$sth->execute($ENV{'REMOTE_ADDR'}) || die("$DBI::errstr\n");
my $request= $sth->fetchrow_array();
$sth->finish();
$dbh->disconnect;
# ---------- Bei auftretender Ip beenden ---------- #
exit if ($request ne "");
# ---------- Settings lesen ---------- #
my $dbh = DBI->connect("dbi:mysql:$config{'mysql_db'}:$config{'mysql_host'}","$config{'mysql_user'}","$config{'mysql_pass'}") || die("$DBI::errstr\n");
my $sth = $dbh->prepare("SELECT blogname,messages_per_page FROM settings") || die("$DBI::errstr\n");
$sth->execute();
my @settings = $sth->fetchrow_array();
$sth->finish();
my $blogname = $settings[0];
my $proseite = $settings[1];
###########################################################################################################################
# ---------- Navigation lesen ---------- #
my $sth_box_name = $dbh->prepare("SELECT box_name FROM navibox") or die $dbh->errstr;
my $sth_links = $dbh->prepare("SELECT id,link_titel,link,categorie,box_name,option,content FROM navigation WHERE box_name=?") or die $dbh->errstr;
$sth_box_name->execute or die $dbh->errstr;
my @boxes;
while( my $box_name = $sth_box_name->fetchrow_array ) {
$sth_links->execute($box_name);
my @links;
while( my $hashref = $sth_links->fetchrow_hashref ) {
push @links, { link => $hashref->{link} };
}
push @boxes, { box_name => $box_name, box_links => \@links };
}
$sth_links->finish();
$sth_box_name->finish();
###########################################################################################################################
my $page = CGI::param('page');
if ($page !~ /^\d*$/) { $page = 0; }
my $perm = CGI::param('perm');
if ($perm eq "") {
&index();
}
else {
if ($perm !~ /^\d*$/) { $perm = 0; }
&perm();
}
# ---------- Hauptseite ---------- #
sub index() {
my $template = HTML::Template->new(filename => "$config{'path'}/index.tmpl");
my $start = $page * $proseite;
# ---------- Anzahl der Blogeinträge zählen ---------- #
$sth = $dbh->prepare("SELECT COUNT(*) as anzahl FROM posts") || die("$DBI::errstr\n");
$sth->execute() || die("$DBI::errstr\n");
my $anzahl = $sth->fetchrow_array();
$sth->finish();
# ---------- Seiten berechnen ---------- #
my $seiten = ceil($anzahl / $proseite);
if ($page > $seiten - 1){$page = 0; $start = $page * $proseite;}
# ---------- Einträge für jeweilige Seite auslesen ---------- #
$sth = $dbh->prepare("SELECT * FROM posts order by date DESC Limit $start,$proseite") || die("$DBI::errstr\n");
$sth->execute() || die("$DBI::errstr\n");
my @posts;
my $comment_str;
while(my @post_infos = $sth->fetchrow_array())
{
# ---------- Anzahl der Kommentare zählen ---------- #
$anzahl = "";
my $sth2 = $dbh->prepare("SELECT COUNT(*) as anzahl FROM comments WHERE post_perm=? AND unlocked=?") || die("$DBI::errstr\n");
$sth2->execute($post_infos[0],1) || die("$DBI::errstr\n");
$anzahl = $sth2->fetchrow_array();
$sth2->finish();
if($anzahl == 0) {$comment_str = "Keine Kommentare";}
elsif($anzahl == 1) {$comment_str = "1 Kommentar";}
else {$comment_str = "$anzahl Kommentare";}
my %posts = (
perm => $post_infos[0],
head => $post_infos[2],
date => $post_infos[3],
user => $post_infos[4],
comment_count => $comment_str,
post => $post_infos[5]
);
push(@posts,\%posts);
}
$sth->finish();
$dbh->disconnect;
# ---------- Seitennavigation vorbereiten ---------- #
my $newer = $page - 1;
my $older = $page + 1;
my $html_newer = " « zurück ";
my $html_older = " nächste » ";
if ($newer < 0) { $html_newer = ""; }
if ( ($older > $seiten - 1) || ($seiten == 1) ) { $html_older = ""; }
###########################################################################################################################
# ----- Template Parameter ------ #
$template->param(
path => $config{'htmlpath'},
blogname => $blogname,
posts => \@posts,
zurueck => $html_newer,
naechste => $html_older,
box_list => \@boxes
);
print $template->output();
}