#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI::Cookie; use CGI qw(:standard); use DBI; use DBD::mysql; use HTML::Template; $tmpl = HTML::Template->new(filename => 'index.html'); @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime; $xronos = "$mday $months[$mon], $hour:$min"; $ip = $ENV{'REMOTE_ADDR'}; @numbers = split (/\./,$ip); $address = pack ("C4", @numbers); $host = gethostbyaddr ($address, 2) || $ip; $host = "Νίκος" if ( ($host eq "localhost") or ($host eq "dslcustomer-221-228.vivodi.gr" or $host eq "dell") ); print header( -charset=>'iso-8859-7' ); $db = ($ENV{'SERVER_NAME'} ne 'nikolas.50free.net') ? DBI->connect('DBI:mysql:nikos_db', 'root', '') : DBI->connect('DBI:mysql:nikos_db:50free.net', 'nikos_db', '********') or print font({-size=>5, -color=>'Lime'}, $DBI::errstr) and exit 0; #******************************************************************************* @files = <../data/text/*.txt>; @display_files = map( /([^\/]+)\.txt/, @files ); $keimeno = param('select') or "Αρχική Σελίδα"; $st = $db->prepare( "SELECT host FROM logs" ); $st->execute(); while( $row = $st->fetchrow_hashref ) { if( $host eq $row->{host} ) { $hostmatch = 1; } } if( param('select') and param('select') !~ /\.\./ ) { open(FILE, "<../data/text/$keimeno.txt") or die $!; @data = ; close(FILE); $data = join('', @data); $db->do( "UPDATE logs SET keimeno='$keimeno' WHERE host='$host'" ) or die $db->errstr; } elsif( $hostmatch == 1 ) { $db->do( "UPDATE logs SET visits = visits + 1 WHERE host='$host'" ) or die $db->errstr; $st = $db->prepare( "SELECT * FROM logs WHERE host='$host'" ); $st->execute(); $row = $st->fetchrow_hashref; $data = "Καλώς ήλθες " .$host. "! Χαίρομαι που βρίσκες την σελίδα ενδιαφέρουσα!\n" . "Τελευταία φορά ήρθες εδώ ως " .$row->{host}. " στις " .$row->{xronos}. " !!\n" . "Σύνολικές ήρθες εδώ " .$row->{visits}. " φορές!!!\n" . "Τελευταία είδες το κείμενο { " .$row->{keimeno}. " }\n" . "Ποιό κείμενο θα μελετήσεςι αυτήν τη φορά !?"; } elsif( $hostmatch != 1 ) { if ( $host ne "Νίκος" ) { $data = "Γεια σου " .$host. "!\n" . "Έρχεσαι για 1η φορά εδώ !!\n" . "Ελπίζω να βρεις τα κείμενα ενδιαφεροντα !!!"; $db->do( "INSERT INTO logs VALUES (null, '$host', '$xronos', '$text', 1)" ) or die $db->errstr; } else { $data = "Γεια σου Νικόλα, τι χαμπάρια !?! :-)"; } } $data =~ s/\n/\\n/g; $data =~ s/"/\\"/g; $data =~ tr/\cM//d; $tmpl->param( data => $data ); # To fill TMPL_LOOPs you have to pass a reference to an array with # references to hashes inside. And the inner hashes contain the data # for the TMPL_VARs inside the TMPL_LOOP. Well, it's a bit tricky. @display_files = map ( /([^\/]+)\.txt/, glob '../data/text/*.txt' ); @array_of_hashes = map { option => $_ }, @display_files; $tmpl->param( options => \@array_of_hashes ); open(FILE, "<../data/text/tips") or die $!; @tips = ; close(FILE); @tips = grep { !/^\s*\z/s } @tips; $tip = $tips[int(rand(@tips))]; $db->do( "UPDATE counter SET counter = counter + 1" ) if ($host ne "Νίκος"); $st = $db->prepare( "SELECT counter FROM counter" ); $st->execute(); $row = $st->fetchrow_hashref; $tip = $tips[ int rand @tips ]; $tmpl->param( tip => $tip ); $tmpl->param(host => $host); $tmpl->param(xronos => $xronos); $tmpl->param(counter => $counter); # Appropriate headers go here print $tmpl->output;