#!/usr/bin/perl
use CGI::Carp "fatalsToBrowser";
use CGI qw(:standard);
use DBI;
use POSIX;
# Required Librariers
# --------------------------------------------------------
eval {
($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1"); # Get the script location: UNIX /
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1"); # Get the script location: Windows \
require "conf.cfg";
require "sub.pl";
};
if ($@) {
print "Content-type:text/html\n";
print "Pragma: no-cache\n";
print "Cache-Control: no-cache\n";
print "Expires: Thu Jan 1 00:00:00 1970\n\n";
print "Error including libraries: $@\n";
print "Make sure they exist, permissions are set properly, and paths are set correctly.";
exit;
}
# ========================================================
eval {
(%in)=&parse_form;
$dbh = DBI->connect ("DBI:mysql:host=$host_name;database=$db_name", $user_name, $password)|| die &cgierr("Can't connect to mysql");
if($in{category}){&viewcategory;}
elsif($in{detailed}){&viewdetailed;}
elsif($in{go}){&go;}
else {&viewindex;}
};
if ($@) { &cgierr("fatal error: $@"); } # never produces that nasty 500 server error page.
$dbh->disconnect ();
exit;
########################################################
sub viewcategory{
($category=$in{category})=~tr/_/ /;
$exist=(($template,$related_cat,$nonenglish)=$dbh->selectrow_array("SELECT template,related,nonenglish FROM `categories` WHERE name='$category'"));
if(!$exist){
if (!(($nonenglish)=$dbh->selectrow_array("SELECT nonenglish FROM `categories` WHERE name LIKE '$category/%' LIMIT 1"))){print "Location:/\n\n";exit;}
}else{$addable=1}
#print $namenext,$nonenglishnext,$exist;
#=======Get global settings
my $sth = $dbh->prepare ("SELECT * FROM `settings` WHERE id='1'");
$sth->execute ();
my $settings=$sth -> fetchrow_hashref ();
foreach("defaultlisting","structure"){$settings->{$_}=unescapeHTML($settings->{$_})}
#======= Build linked =================
$linked=qq($basename);
my @subcats=split(/\//,$category);
my @subcatsnonenglish=split(/\//,$nonenglish);
$i=0;
foreach $item (@subcats){
push(@cat,$item);
$subcat=join("/",@cat);
$subcat=~ tr/ /_/;
if($subcatsnonenglish[$i]){push(@catnonenglish,$subcatsnonenglish[$i]);$linked.=qq( > $subcatsnonenglish[$i])}
else{$linked.=qq( - $item)}
$i++;
}
($subcatsnonenglish[$i-1])?($cat_title="$subcatsnonenglish[$i-1]"):($cat_title=pop(@subcats));
$nonenglishname=join("/",@catnonenglish);
#======= Next categories ==============
#my ($ref_cat_names) = $dbh->selectcol_arrayref("SELECT name FROM `categories` WHERE name LIKE '$category/%' ");
my $sth = $dbh->prepare ("SELECT name,nonenglish FROM `categories` WHERE name LIKE '$category/%' ");
$sth->execute ();
while (my $ref=$sth -> fetchrow_hashref ()){
my @subcatsnonenglish=split(/\//,$ref->{nonenglish});
if($ref->{name}=~m/^$category\/([\w\s-]+)(\/?)/ and $ref->{nonenglish} and !exists($nexturls{$1})){
$url=$1;
$ref->{nonenglish}=~s/^$nonenglishname\///;
$ref->{nonenglish}=~s/\/.*$//;
$nexturls{$url}=$ref->{nonenglish};
}
elsif($ref->{name}=~m/^$category\/([\w\s-]+)/ and !exists($nexturls{$1})){
$nexturls{$1}=$1;
}
if($ref->{name}=~m/^$category\/([\w\s-]+)\// and !exists($prefix{$1})){
$prefix{$1}++;
}
}
if(%nexturls){
$nextlinks.=qq(
\n\n);
$i=0;
foreach (@nexturls=sort keys %nexturls){
$nexturl="$category/$_";
$count_entries=$dbh->selectrow_array("SELECT COUNT(*) FROM `submissions` WHERE category LIKE '$nexturl%' AND status='approved'");
$nexturl=~tr/ /_/;
($prefix{$_})?($prefix='@'):($prefix='');
if($i == ceil(scalar(@nexturls)*2/3) or $i == ceil(scalar(@nexturls)/3)){$nextlinks.=qq( | \n |
);
}
#======= Related ===================
@related=split(/\0/,$related_cat);
foreach $rel (@related){
if ($nonenglish=$dbh->selectrow_array("SELECT nonenglish FROM `categories` WHERE name='$rel'")){
$rel=~ tr/ /_/;
$related.=qq(
$basename\/$nonenglish);
}else{
$rel=~ tr/ /_/;
$related.=qq(
$basename\/$rel);
}
}
#=========Links =========
my $sth = $dbh->prepare ("SELECT * FROM `submissions` WHERE category='$category' AND status='approved' ORDER BY priority DESC");
$col=$sth->execute ();
$col+=0;
while (my $reflink=$sth -> fetchrow_hashref ()){
$average=sprintf("%.2f",86400*$reflink->{counter}/(time()-$reflink->{time}));
$links.= &template ("$settings->{defaultlisting}",{%$reflink,average=>$average,cgiurl=>$cgiurl},"$settings->{defaultlisting}");
}
#=========================
$content=&template ('', {
},"$settings->{structure}");
if(!$template){$template="template.htm";}
&header;
print &template ("$template", {title =>"$basename - $cat_title",cgiurl=>$cgiurl,
linked=>$linked,nextlinks=>$nextlinks,related=>$related,
links=>$links,add_to=>$in{category},addable=>$addable
});
}
#----------------------------------------------------------------------------------------------------------------------------
sub viewdetailed{
my $sth = $dbh->prepare ("SELECT * FROM `submissions` WHERE id='$in{detailed}' AND status='approved'");
$sth->execute ();
my $ref=$sth -> fetchrow_hashref ();
if (!$ref){print "Location:/\n\n";}
else{
#======= Build linked =================
($nonenglish)=$dbh->selectrow_array("SELECT nonenglish FROM `categories` WHERE name='$ref->{category}' LIMIT 1");
$linked=qq($basename);
my @subcats=split(/\//,$ref->{category});
my @subcatsnonenglish=split(/\//,$nonenglish);
$i=0;
foreach $item (@subcats){
push(@cat,$item);
$subcat=join("/",@cat);
$subcat=~ tr/ /_/;
if($subcatsnonenglish[$i]){push(@catnonenglish,$subcatsnonenglish[$i]);$linked.=qq( - $subcatsnonenglish[$i])}
else{$linked.=qq( - $item)}
$i++;
}
($subcatsnonenglish[$i-1])?($cat_title="$subcatsnonenglish[$i-1]"):($cat_title=pop(@subcats));
$ref->{description}=~s/\n/\
/g;
$ref->{additionalinfo}=~s/\n/\
/g;
&header;
print &template ("detailed.htm", {%$ref,linked=>$linked,cat_title=>$cat_title});
}
}
#----------------------------------------------------------------------------------------------------------------------------
sub viewindex{
my ($ref_cat_names,$nonenglish);
my $sth = $dbh->prepare("SELECT name,nonenglish FROM `categories`");
$sth->execute ();
while (my $ref=$sth -> fetchrow_hashref ()){
if($ref->{name}=~m/^([\w\s-]+)(\/?)/ and $ref->{nonenglish} and !exists($nexturls{$1})){
$url=$1;
$ref->{nonenglish}=~s/\/.*//g;
$nexturls{$url}=$ref->{nonenglish};
}
elsif($ref->{name}=~m/^([\w\s-]+)(\/?)/ and !exists($nexturls{$1})){
$nexturls{$1}=$1;
}
if($ref->{name}=~m/^([\w\s-]+)\// and !exists($prefix{$1})){
$prefix{$1}++;
}
}
if(%nexturls){
$nextlinks.=qq(\n\n);
foreach (@nexturls=sort keys %nexturls){
$nexturl="$_";
#$count_entries=$dbh->selectrow_array("SELECT COUNT(*) FROM `submissions` WHERE category LIKE '$nexturl%' AND status='approved'");
$sth = $dbh->prepare("SELECT name,nonenglish FROM `categories` WHERE name LIKE '$nexturl%' AND inindex='checked'");
$inindexcount=$sth->execute ()+0;
if($inindexcount){$inindex=" \n";
while(my $ref=$sth -> fetchrow_hashref ()){
if($ref->{nonenglish}){($inindexname=$ref->{nonenglish})=~s/.*\///g;}else{($inindexname=$ref->{name})=~s/.*\///g;}
($inindexurl=$ref->{name})=~tr/ /_/;
$inindex.=qq( • $inindexname);
}
$inindex.=" "
}else{$inindex="\n";}
$nexturl=~tr/ /_/;
if($prefix{$_}){$prefix='';$postfix=''}else{$prefix='';$postfix=''}#
if($i == ceil(scalar(@nexturls)*2/3) or $i == ceil(scalar(@nexturls)/3)){$nextlinks.=qq( | \n |
);
}
&header;
print &template ('index_template.htm', {title =>"$basename",
categories=>$nextlinks
});
}
sub go{
if (!(($lastip,$url) =$dbh->selectrow_array("SELECT lastip,url FROM `submissions` WHERE id='$in{go}' AND status='approved'"))){print "Location:/\n\n";}
else{
if($lastip ne $ENV{REMOTE_ADDR}){$dbh->do ("UPDATE `submissions` SET counter=counter+1,lastip='$ENV{REMOTE_ADDR}' WHERE id='$in{go}'");}
print "Location:$url\n\n";
}
}